Mybatis逆向工程
我们实际开发中,动辄上百张表,如果一一地去编写对应的 pojo 和 mapper 文件,简直是噩梦。Mybatis 为我们提供了一个逆向工程,可以根据单表自动生成 mybatis 执行所需要的(mapper.Java,mapper.xml、po..)等,免去我们不少麻烦,我们来学习一下怎么使用。
官方逆向工程开发文档
http://www.mybatis.org/generator/configreference/xmlconfig.html
1、创建新工程,导入jar包,需要mybatis.jar、mysql驱动和mybatis-generator-core.jar
2、创建配置文件 genreatorConfig.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>
<context id="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis" 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.pngyul.mybatis.pojo"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.pngyul.mybatis.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.pngyul.mybatis.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<table schema="" tableName="user"></table>
<table schema="" tableName="orders"></table>
<!-- 有些表的字段需要指定java类型
<table schema="" tableName="">
<columnOverride column="" javaType="" />
</table> -->
</context>
</generatorConfiguration>
3.使用java程序加载配置文件生成我们需要的代码 ,这段java程序在官方文档里面有,直接复制过来即可
public class GeneratorSqlmap {
public void generator() 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 static void main(String[] args) throws Exception {
try {
GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}
4.生成的Mapper接口中有很多常用的方法,并且我们发现除了生成POJO类,还生成了Example类,这个类是用来加条件的,如查询条件等。
我们来演示一下。
public class TestDemo {
@Test
public void test() {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
UserMapper mapper=(UserMapper)applicationContext.getBean("userMapper");
UserExample example=new UserExample();
//添加性别为1的条件
example.createCriteria().andSexEqualTo("1");
int count=(int) mapper.countByExample(example);
System.out.println(count);
}
}