Mybatis逆向工程
Mybatis逆向工程(Mybatis Generator)用于根据配置文件自动生成数据表对应的JavaBean,以及dao接口和映射文件。
该示例以Maven的插件形式运行Mybatis逆向工程。(Maven项目)
pom.xml中添加插件
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
数据表示例
以dept表(部门)和employees表(员工)为例。员工的d_id指向dept的主键。
dept:

employees:

创建配置文件
创建连接数据库相关信息的jdbc.properties文件以及逆向工程的配置文件generatorConfig.xml。放在resource目录下。并将MySQL jdbc驱动包放在resource下(这里使用的是mysql-connector-java-5.1.47.jar)。
PS:mybatis-generator插件默认在resource目录下查找
generatorConfig.xml配置文件,所以这里的generatorConfig.xml配置文件要注意位置与命名。
jdbc.properties:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/ssm?serverTimezone=UTC
user=root
password=admin
generatorConfig.xml:
<!DOCTYPE generatorConfiguration PUBLIC
"-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--导入properties配置文件-->
<properties resource="jdbc.properties"></properties>
<!--指定特定数据库的jdbc驱动jar包的位置-->
<classPathEntry location="驱动包的绝对路径"/>
<context id="default" targetRuntime="MyBatis3">
<!-- 配置生成的类不带注释(建议,因为生成的注释没啥用) -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--jdbc的数据库连接 -->
<jdbcConnection
driverClass="${driver}"
connectionURL="${url}"
userId="${user}"
password="${password}" />
<!-- 指定生成的JavaBean的包位置,
用来生成含有主键key的类,记录类 以及查询Example类
不用事先创建好包结构,mybatis-generator会根据配置自动创建,sqlMapGenerator同
targetPackage 指定生成的JavaBean生成所在的包名
targetProject 指定在该项目下所在的路径
-->
<javaModelGenerator targetPackage="com.generator.bean"
targetProject="src/main/java">
<!-- 是否允许子包,即targetPackage.schemaName.tableName -->
<property name="enableSubPackages" value="false"/>
<!-- 是否对model添加 构造函数 -->
<property name="constructorBased" value="true"/>
<!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 -->
<property name="immutable" value="false"/>
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources" />
<!-- 指定dao接口(mapper)的位置
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
-->
<javaClientGenerator targetPackage="com.generator.dao"
targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 指定每个表的生成策略
配置数据表与JavaBean的名称映射,以及是否创建根据条件查询的sql语句等 -->
<!-- tableName指定表名,domainObjectName指定表对应的JavaBean名 -->
<table tableName="dept" domainObjectName="Department"
enableCountByExample="false" enableUpdateByExample="true"
enableDeleteByExample="false" enableSelectByExample="true"
selectByExampleQueryId="false">
</table>
<table tableName="employees" domainObjectName="Employee"
enableCountByExample="false" enableUpdateByExample="true"
enableDeleteByExample="false" enableSelectByExample="true"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
插件相关配置
在maven窗口配置一个插件运行。
-
分别填入项目的目录以及命令
mybatis-generator:generate -e:
-
OK。
运行Generator
打开maven窗口可看到:

双击即可运行generator。
运行后的项目目录,可看到创建了JavaBean、dao以及映射文件:
XxxExample的使用
通过逆向工程生成的JavaBean中可以看到有XxxExample的类,这些类的作用是用于设置查询条件。
简单的示例:
@Test
public void testEmployeeExample() throws IOException {
SqlSession session = getSqlSession();//获取SqlSession(方法具体实现省略)
//创建一个example类
EmployeeExample example = new EmployeeExample();
//查询employees表中did为1的记录
example.or().andDIdEqualTo(1);
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
List<Employee> employees = mapper.selectByExample(example);
for(Employee e : employees){
System.out.println(e);
}
}
又如:通过模糊查询,查询EmpName包含“三”的记录。
EmployeeExample example = new EmployeeExample();
example.or().andEmpNameLike("%三%");
其他的添加查询条件的方法:(来自https://blog.csdn.net/biandous/article/details/65630783)
| 方法 | 说明 |
|---|---|
| example.setOrderByClause(“字段名 ASC”); | 添加升序排列条件,DESC为降序 |
| example.setDistinct(false) | 去除重复,boolean型,true为选择不重复的记录。 |
| criteria.andXxxIsNull | 添加字段xxx为null的条件 |
| criteria.andXxxIsNotNull | 添加字段xxx不为null的条件 |
| criteria.andXxxEqualTo(value) | 添加xxx字段等于value条件 |
| criteria.andXxxNotEqualTo(value) | 添加xxx字段不等于value条件 |
| criteria.andXxxGreaterThan(value) | 添加xxx字段大于value条件 |
| criteria.andXxxGreaterThanOrEqualTo(value) | 添加xxx字段大于等于value条件 |
| criteria.andXxxLessThan(value) | 添加xxx字段小于value条件 |
| criteria.andXxxLessThanOrEqualTo(value) | 添加xxx字段小于等于value条件 |
| criteria.andXxxIn(List<?>) | 添加xxx字段值在List<?>条件 |
| criteria.andXxxNotIn(List<?>) | 添加xxx字段值不在List<?>条件 |
| criteria.andXxxLike(“%”+value+”%”) | 添加xxx字段值为value的模糊查询条件 |
| criteria.andXxxNotLike(“%”+value+”%”) | 添加xxx字段值不为value的模糊查询条件 |
| criteria.andXxxBetween(value1,value2) | 添加xxx字段值在value1和value2之间条件 |
| criteria.andXxxNotBetween(value1,value2) | 添加xxx字段值不在value1和value2之间条件 |
以上方法都可以通过example类的or()方法调用(如示例),推荐使用这种方式。
使用or()方法添加多个条件时要注意:
-
若是要添加多个AND条件,应是这种方式:
example.or().andXxx1(xx).andXxx2(xx).addXxx3(xx);。 -
若是使用以下方式,添加的多个条件将是OR条件:
example.or().andXxx1(xx); example.or().andXxx2(xx); example.or().andXxx3(xx);
参考

浙公网安备 33010602011771号