- MyBatis Generator (MBG): Mybatis官方提供的代码生成器,可以根据数据库中表结构自动生成简单的CRUD操作。但联合查询和存储过程,仍需手动手写SQL和对象。MBG可以通过使用Maven、Java编码、命令行等方式来运行。
- 本文主要介绍在Intellij IDEA中使用Maven插件mybatis-generator-maven-plugin来自动生成MyBatis代码的一些步骤。
引入mybatis-generator插件
<!-- mybatis自动生成插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
类路径下放置generatorConfig.XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 指定数据库的jdbc驱动jar包的位置 -->
<classPathEntry location="C:/Develop/apache-maven-3.5.3/repository/mysql/mysql-connector-java/5.1.32/mysql-connector-java-5.1.32.jar" />
<!--
context:是逆向工程的主要配置信息
id:起个名字
targetRuntime:
MyBatis3Simple:生成基本的增删改查
MyBatis3:可以生成带条件的增删改查
-->
<context id="default" targetRuntime="MyBatis3">
<!-- 配置注释生成规则,不生成注释 -->
<commentGenerator>
<property name="suppressDate" value="true" />
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--配置数据库连接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/db_test?characterEncoding=utf-8"
userId="root"
password="123">
</jdbcConnection>
<!--
非必须:
类型处理器,在数据库类型和java类型之间的转换控制
默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,
为true时把JDBC DECIMAL和NUMERIC类型解析为java.math.BigDecimal
-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--
设置生成的实体类所在的包和位置
enableSubPackages:是否允许子包
constructorBased:是否对modal添加构造函数
trimStrings:是否清理从数据库中查询出的字符串左右两边的空白字符
immutable:建立modal对象是否不可改变 即生成的modal对象不会有setter方法,只有构造方法
-->
<javaModelGenerator targetPackage="com.tt.pojo" targetProject=".\src\main\java">
<property name="enableSubPackages" value="false" />
<property name="constructorBased" value="true" />
<property name="trimStrings" value="true" />
<property name="immutable" value="false" />
</javaModelGenerator>
<!--
设置生成mapper文件的包和位置
-->
<sqlMapGenerator targetPackage="mybatis.mappers" targetProject=".\src\main\resources">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!--
设置生成mapper接口的包和位置
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.tt.mapper" targetProject=".\src\main\java">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 配置表生成实体类的规则 -->
<table tableName="tb_user" domainObjectName="User" />
</context>
</generatorConfiguration>
运行Maven插件
生成后的目录结构
测试
@Test
public void testMyBatis3() throws IOException{
//xxxExample就是封装查询条件的
//Criteria用来拼装查询条件
//1、查询所有
List<User> users = userMapper.selectByExample(null);
//2、查询名字中有“a”字母的,和年龄为18的
EmployeeExample example1 = new EmployeeExample();
Criteria criteria = example1.createCriteria();
criteria.andNameLike("%a%");
criteria.andAgeEqualTo("1");
List<User> users= userMapper.selectByExample(example1);
//3、查询名字中有“a”字母的,和年龄为18的或者名字有"b"字母的
EmployeeExample example2 = new EmployeeExample();
Criteria criteria = example2.createCriteria();
criteria.andNameLike("%a%");
criteria.andAgeEqualTo("1");
Criteria criteria2 = example2.createCriteria();
criteria2.andEmailLike("%e%");
example2.or(criteria2);
List<User> users= userMapper.selectByExample(example2);
}