1.概念
mybatis官方提供逆向工程,可以针对单表自动生成mybatis执行所需要的代码如创建实体类,映射文件等,可以让程序员将更多的精力放在繁杂的业务逻辑上,而不是关注在sql语句的编写。企业实际开发中,常用的逆向工程方式:由数据库的表生成java代码。
2.逆向工程
2.1 依赖包下载:逆向工程依赖.
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
</dependency>
jar包:
2.2 简单实用流程:
1、创建简单的java项目
2、导入jar包,创建generator配置文件;
3、使用java类来执行逆向工程;
4、把生成的代码拷贝到项目中。
5、在正式项目中使用逆向工程生成的代码
2.3 详解:
2.3.1 导入jar包:
2.3.2 创建generator配置文件:
配置文件需要修改的内容:
1. 数据库驱动、地址、用户名、密码
2. POJO类、mapper接口、mapper映射文件生成的位置
3. 指定数据表
在classpath下,创建generator.xml配置文件:(文件内容可以从逆向工程的jar包中docs目录下的index.html中找到相关代码)
<?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="mysqlTables" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql:///edu?serverTimezone=GMT%2B8"
userId="root"
password="root">
</jdbcConnection>
<!-- java类型解析-->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 模型生产包名-->
<javaModelGenerator targetPackage="com.luckySheep.edu.model" targetProject=".\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- mybatis映射xml-->
<sqlMapGenerator targetPackage="com.luckySheep.edu.mapper" targetProject=".\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- mybatis 的mapper接口生成的包路径-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.luckySheep.edu.mapper" targetProject=".\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 配置生成表的模型-->
<table tableName="t_class" domainObjectName="courseVideo"></table>
<table tableName="t_client" domainObjectName="Client"></table>
<table tableName="t_commission" domainObjectName="Commission" ></table>
<table tableName="t_course" domainObjectName="Course"></table>
<table tableName="t_department" domainObjectName="Department"></table>
<table tableName="t_employee" domainObjectName="Employee"></table>
<table tableName="t_order" domainObjectName="Order"></table>
<table tableName="t_salary_detail" domainObjectName="SalaryDetail"></table>
<table tableName="t_stu_course" domainObjectName="StuCourse"></table>
<table tableName="t_user" domainObjectName="User"></table>
<table tableName="t_student" domainObjectName="Student"></table>
</context>
</generatorConfiguration>
2.3.3 java类来执行逆向工程:
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class main {
public static void main(String[] args) throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("src/generator.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);
}
}
2.3.4 生成的代码:
3.学习总结
3.1 xxxExample类:
- distinct字段用于指定DISTINCT查询。
- orderByClause字段用于指定ORDER BY条件,这个条件没有构造方法,直接通过传递字符串值指定。
- oredCriteria字段用于自定义查询条件。
这个类是专门用来对这个单表进行查询的类,对该单表的CURD操作是脱离sql性质的(已经通过逆向工程生成相应的sql),直接在service层就可以完成相应操作。逆向工程生成的文件xxxExample.java中包含一个static 的内部类 Criteria ,在Criteria中有很多方法,主要是定义SQL 语句where后的查询条件。
资料: