逆向工程定义:
MyBatis Generator: • 简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件,接口,以及bean类。支持基本的增删改查,以及QBC风格的条件查询。但是表连接、存储过程等这些复杂sql的定义需要我们手工编写
使用步骤:
一:编写generatorConfig.xml的配置文件(重要几处配置)
1)jdbcConnection配置数据库连接信息
2)javaModelGenerator配置javaBean的生成策略
3)sqlMapGenerator 配置sql映射文件生成策略
4)javaClientGenerator配置Mapper接口的生成策略
5)table 配置要逆向解析的数据表
tableName:表名
domainObjectName:对应的javaBean名
(6)• 注意:
Context标签
targetRuntime=“MyBatis3“可以生成带条件QBC风格的增删改查
targetRuntime=“MyBatis3Simple“可以生成基本的增删改查
示例():
<?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">
<commentGenerator>
<!-- 是否去除自动生成的注释 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- Mysql数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ssm?allowMultiQueries=true"
userId="root"
password="root">
</jdbcConnection>
<!-- 默认为false,把JDBC DECIMAL 和NUMERIC类型解析为Integer,为true时
把JDBC DECIMAL 和NUMERIC类型解析为java.math.BigDecimal -->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成POJO类的位置 -->
<javaModelGenerator targetPackage="com.it.bean" targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com" targetProject=".\conf">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetProject:mapper接口生成的的位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.it.mapper" targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据表 -->
<table tableName="emp" domainObjectName="Emp"></table>
<table tableName="dept" domainObjectName="Dept"></table>
</context>
</generatorConfiguration>
二:运行代码生成器生成代码,自动生成bean以及映射文件和接口mapper
@Test
public void test1() 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);
}
三:关于QBC风格的带条件查询示例:
@Test
public void test2() throws Exception {
//获取SqlSession会话对象
SqlSession sqlSession = getSqlSessionFactory().openSession(true);
//获取mapper
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
//根据主键进行查询
//Emp emp = mapper.selectByPrimaryKey("8");
//执行条件查询需传入example实例,创建example
EmpExample empExample = new EmpExample();
//通过example创建条件对象
EmpExample.Criteria c1 = empExample.createCriteria();
//添加条件
c1.andEnameLike("%a%");
c1.andAgeBetween(20,25);
EmpExample.Criteria c2 = empExample.createCriteria();
c2.andDidEqualTo(2);
//如果有两个条件之间用or进行连接, 使用example的or方法进行拼接
empExample.or(c2);
List<Emp> emps = mapper.selectByExample(empExample);
for (Emp emp : emps) {
System.out.println(emp);
}
}