什么是逆向工程
MyBatis的一个主要的特点就是需要程序员自己编写sql,那么如果表太多的话,难免会很麻烦,所以mybatis官方提供了一个逆向工程,可以针对单表自动生成mybatis执行所需要的代码(包括mapper.xml、mapper.java、pojo)。一般在开发中,常用的逆向工程方式是通过数据库的表生成代码
使用逆向工程生成代码
1. 新建一个工程
使用Maven新建一个Java工程引入相应的包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> </dependencies>
|
2. 建立逆向工程配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| <?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> <property name="suppressAllComments" value="true" /> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mydb" userId="cyp" password="cyp"> </jdbcConnection>
<javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <javaModelGenerator targetProject="./src/main/java" targetPackage="pojo"> <property name="enableSubPackages" value="false" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetProject="./src/main/resources" targetPackage="mapper"> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetProject="./src/main/java" targetPackage="dao"> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <table tableName="user" domainObjectName="User" /> <table tableName="student" domainObjectName="Student" /> <table tableName="account" domainObjectName="Account" />
</context> </generatorConfiguration>
|
配置文件中各个标签的作用已经在配置文件中注释
配置文件中POJO,mapper接口,mapper文件的包创建好
3. 新建工具类,用来加载配置文件和生成代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| package util; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.exception.XMLParserException; import org.mybatis.generator.internal.DefaultShellCallback;
public class MybatisGeneratorUtil { public static void generator() throws Exception, XMLParserException{ List<String> warnings = new ArrayList<String>(); File configurationFile = new File("generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration configuration = cp.parseConfiguration(configurationFile); DefaultShellCallback callback = new DefaultShellCallback(true); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(configuration, callback, warnings); myBatisGenerator.generate(null); } }
|
4. 新建类包含mian方法调用工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| package util; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.exception.XMLParserException; import org.mybatis.generator.internal.DefaultShellCallback;
public class MybatisGeneratorUtil { public static void generator() throws Exception, XMLParserException{ List<String> warnings = new ArrayList<String>(); File configurationFile = new File("generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration configuration = cp.parseConfiguration(configurationFile); DefaultShellCallback callback = new DefaultShellCallback(true); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(configuration, callback, warnings); myBatisGenerator.generate(null); } }
|
5. 运行后项目的结构
逆向工具生成的Example类
自定义查询条件的java类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| public class StudentExample {
protected String orderByClause;
protected boolean distinct;
protected List<Criteria> oredCriteria;
public StudentExample() { oredCriteria = new ArrayList<Criteria>(); }
public void setOrderByClause(String orderByClause) { this.orderByClause = orderByClause; }
public String getOrderByClause() { return orderByClause; }
public void setDistinct(boolean distinct) { this.distinct = distinct; }
public boolean isDistinct() { return distinct; }
public List<Criteria> getOredCriteria() { return oredCriteria; }
public void or(Criteria criteria) { oredCriteria.add(criteria); }
|