MyBatis Generator简介
MyBatis Generator简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件,接口,以及bean类。支持基本的增删改查,以及QBC风格的条件查询。但是表连接、存储过程等这些复杂sql的定义需要我们手工编写。
官方文档地址
http://www.mybatis.org/generator/
MyBatis Generator使用步骤
- 编写MyBatis Generator的配置文件(重要几处配置)
1)jdbcConnection配置数据库连接信息
2)javaModelGenerator配置javaBean的生成策略
3)sqlMapGenerator 配置sql映射文件生成策略
4)javaClientGenerator配置Mapper接口的生成策略
5)table 配置要逆向解析的数据表
tableName:表名
domainObjectName:对应的javaBean名
MBG配置文件:
<?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>
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- jdbcConnection:指定如何连接到目标数据库 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true"
userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--
javaModelGenerator:指定javaBean生成策略
targetPackage:指定目标包名;
targetProject:指定目标工程
-->
<javaModelGenerator targetPackage="com.fzl.mybatis.bean" targetProject=".\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--
sqlMapGenerator:sql映射生成策略
-->
<sqlMapGenerator targetPackage="com.fzl.mybatis.dao" targetProject=".\conf">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--
javaClientGenerator:指定mapper接口的位置
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.fzl.mybatis.dao" targetProject=".\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- table:要逆向生成哪些表 -->
<table tableName="tbl_dept" domainObjectName="Department"></table>
<table tableName="tbl_employee" domainObjectName="Employee"></table>
</context>
</generatorConfiguration>
- 运行代码生成器生成代码
生成器代码:
@Test
public void testMbg() throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("mbg.xml");
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);
}
从官方文档给出的示例中,只需要将File configFile = new File(“mbg.xml”);你面的xml文件改成你项目下的MyBatis Generator的配置文件即可。
注意:
Context
标签
targetRuntime=“MyBatis3“
可以生成带条件的增删改查。
targetRuntime=“MyBatis3Simple“
可以生成基本的增删改查。
如果再次生成,建议将之前生成的数据删除,避免xml向后追加内容出现的问题。
- 测试查询
@Test
public void testMbg3() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try {
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
//xxxExample就是封装查询条件的
//1.查询所有员工信息
List<Employee> emps = mapper.selectByExample(null);
//2.查询姓名中有字母a的,gender为0的员工信息
EmployeeExample example = new EmployeeExample();
//创建一个Criteria,这个Criteria就是拼装查询条件
com.fzl.mybatis.bean.EmployeeExample.Criteria criteria = example.createCriteria();
criteria.andLastNameLike("%a%");
criteria.andGenderEqualTo("2");
com.fzl.mybatis.bean.EmployeeExample.Criteria criteria2 = example.createCriteria();
criteria2.andEmailLike("%2%");
example.or(criteria2);
List<Employee> employees = mapper.selectByExample(example);
for(Employee e:employees) {
System.out.println(e.getLastName());
}
openSession.commit();
} finally {
openSession.close();
}
}