7. MyBatis-代码生成器
MyBatis Generator:
简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件,接口,以及bean类。支持基本的增删改查,以及QBC风格的条件查询。但是表连接、存储过程等这些复杂sql的定义需要我们手工编写
官方文档地址
官方工程地址
7. 添加依赖
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
7. 使用说明
- 使用步骤:
- 编写MBG的配置文件(重要几处配置)
- jdbcConnection配置数据库连接信息
- javaModelGenerator配置javaBean的生成策略
- sqlMapGenerator 配置sql映射文件生成策略
- javaClientGenerator配置Mapper接口的生成策略
- table 配置要逆向解析的数据表
- tableName:表名
- domainObjectName:对应的javaBean名
- 运行代码生成器生成代码
- 编写MBG的配置文件(重要几处配置)
- 注意:
- Context标签
- targetRuntime=“MyBatis3“可以生成带条件的增删改查
- targetRuntime=“MyBatis3Simple“可以生成基本的增删改查
- 如果再次生成,建议将之前生成的数据删除,避免xml向后追加内容出现的问题。
- Context标签
7. 配置文件
mbg.xml
<generatorConfiguration>
<!--
targetRuntime="MyBatis3Simple":生成简单版的CRUD
"MyBatis3":豪华版
-->
<properties resource="c04/dbconfig.properties"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- jdbcConnection:指定如何连接到目标数据库 -->
<jdbcConnection driverClass="${jdbc.driver}"
connectionURL="${jdbc.url}"
userId="${jdbc.username}"
password="${jdbc.password}">
</jdbcConnection>
<!-- -->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- javaModelGenerator:指定javaBean的生成策略
targetPackage="test.model":目标包名
targetProject="\MBGTestProject\src":目标工程
-->
<javaModelGenerator targetPackage="club.coderhome.bean"
targetProject="C:\\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- sqlMapGenerator:sql映射生成策略: -->
<sqlMapGenerator targetPackage="club.coderhome.dao"
targetProject="C:\\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- javaClientGenerator:指定mapper接口所在的位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="club.coderhome.dao"
targetProject="C:\\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 指定要逆向分析哪些表:根据表要创建javaBean -->
<table tableName="department" domainObjectName="Department"></table>
<table tableName="employee" domainObjectName="Employee"></table>
</context>
</generatorConfiguration>
7. 生成器代码
GenerateCode.java
public class GenerateCode {
public static void main(String[] args) {
try {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File(GenerateCode.class.getClassLoader().getResource("c07/mbg.xml").getFile());
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
7. QBC风格的带条件查询
生成器会QBC风格代码
QBC风格代码使用实例:
@Test
public void test01(){
SqlSession openSession = build.openSession();
DeptMapper mapper = openSession.getMapper(DeptMapper.class);
DeptExample example = new DeptExample();
//所有的条件都在example中封装
Criteria criteria = example.createCriteria();
//select id, deptName, locAdd from tbl_dept WHERE
//( deptName like ? and id > ? )
criteria.andDeptnameLike("%部%");
criteria.andIdGreaterThan(2);
List<Dept> list = mapper.selectByExample(example);
for (Dept dept : list) {
System.out.println(dept);
}
}