MyBatis Generator,简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的根据数据表表生成对应的JavaBean、Mapper接口和SQL映射文件。且支持基本的增删改查,以及QBC风格的条件查询。但是表连接、存储过程等复杂SQL的定义则需要手工编写。
官方文档地址为http://www.mybatis.org/generator/,官方工程地址为https://github.com/mybatis/generator/releases。
其中,MyBatis中逆向工程的具体测试代码下载地址:https://download.csdn.net/download/bingbeichen/10538138。
使用步骤
第一步,导入所需要的jar包,包括log4j-1.2.17.jar、mybatis-3.4.1.jar、mybatis-generator-core-1.3.2.jar、mysql-connector-java-5.1.7-bin.jar。
第二步,编写MBG的配置文件。
<!-- 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>
<!-- 配置MBG运行环境 -->
<!-- targetRuntime="MyBatis3" : 可以生成带条件的增删改查
targetRuntime="MyBatis3Simple" : 可以生成基本的增删改查
注意:如果再次生成,建议将之前生成的数据删除,避免xml向后追加内容出现的问题。
-->
<context id="DB2Tables" targetRuntime="MyBatis3Simple">
<!-- 配置数据库连接 -->
<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis"
userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 配置JavaBean生成策略 -->
<javaModelGenerator targetPackage="com.qiaobc.mybatis.bean"
targetProject=".\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 配置SQL映射文件生成策略 -->
<sqlMapGenerator targetPackage="mapper.xml"
targetProject=".\conf">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 配置Mapper接口生成策略 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.qiaobc.mybatis.mapper" targetProject=".\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 指定逆向分析的数据表,根据数据表生成JavaBean -->
<table tableName="tbl_dept" domainObjectName="Department"></table>
<table tableName="tbl_employee" domainObjectName="Employee"></table>
</context>
</generatorConfiguration>
第三步:运行生成器代码,以根据数据表自动生成对应的JavaBean、Mapper接口和SQL映射文件。
public class MBGTest {
@Test
public void test() throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("generatorConfig.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);
}
}
第四步,测试。
public class MBGTest {
/**
* 测试targetRuntime="MyBatis3Simple" : 可以生成基本的增删改查
* @throws IOException
*/
@Test
public void testMyBatis3Simple() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
List<Employee> emps = mapper.selectAll();
for (Employee emp : emps) {
System.out.println(emp.getLastName());
}
} finally {
sqlSession.close();
}
}
/**
* 测试targetRuntime="MyBatis3" : 可以生成带条件的增删改查
* @throws IOException
*/
@Test
public void testMyBatis3() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
// XxxExample用于封装查询条件
// 1.查询所有员工姓名
List<Employee> emps = mapper.selectByExample(null);
// 2.1 使用Example的Criteria封装查询条件
EmployeeExample example = new EmployeeExample();
Criteria criteria = example.createCriteria();
criteria.andLastNameLike("%e%");
criteria.andGenderEqualTo("1");
Criteria criteria2 = example.createCriteria();
criteria2.andEmailLike("%e%");
example.or(criteria);
// 2.查询员工中名字含有e且性别为1的员工,或者email中含有e
emps = mapper.selectByExample(example);
for (Employee emp : emps) {
System.out.println(emp.getLastName());
}
} finally {
sqlSession.close();
}
}
public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
}
}