1.mybatis代码生成器的介绍
代码生成器的目标就是简化单表的增删改查操作,这些标准化的流程工作,交给机器来实现,不需要程序员自己去完成。一般对一张表的操作有,根据主键查询,根据map集合查询,单条数据插入,批量插入,根据主键删除等等一系列操作,下面就让我们来配置一下代码生成器的环境,如何让mybatis自动实现代码的生成。
2.mybatis代码生成器的配置
代码生成器配置并不复杂,主要就配置有generator.xml ,生成代码的测试类 GeneratorTest,还要配置相关的依赖文件
3.数据库文件
数据库文件在配置generator.xml的时候会用到,根据数据库表生成java文件
4.pom.xml配置文件
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
</dependencies>
5.generator.xml配置文件
注意:
1.generator.xml注意这个位置是放在和pom.xml文件的同一路径下
2.需要自己准备一个mysql驱动包,版本和数据库的版本对应
<?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>
<!--mysql驱动jar的位置 直到找到 mysql-connector-java-8.0.16.jar 版本不一致 但是为.jar结尾-->
<classPathEntry location="D:\developtools\mysqlconnection\mysql-connector-java-8.0.15.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<!--数据库的配置信息-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/travel?serverTimezone=Asia/Shanghai"
userId="root"
password="12345678">
</jdbcConnection>
<!--Java实体类的配置信息
targetPackage:目标包 实体类的位置是在java下
targetProject:目标工程 src下 java之前
-->
<javaModelGenerator targetPackage="com.tz.entity" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--映射文件的配置信息-->
<sqlMapGenerator targetPackage="mappers" targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--dao层的配置信息-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.tz.mapper" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--
schema:数据库名
tableName:表名
domainObjectName:实体类名
这四个一般为false 不然回生成复杂的增删改查操作
enableSelectByExample="false" 是否生成复杂的查询操作
enableCountByExample="false"
enableDeleteByExample="false"
enableUpdateByExample="false">
-->
<table schema="mybatis" tableName="tab_category" domainObjectName="Category"
enableSelectByExample="false"
enableCountByExample="false"
enableDeleteByExample="false"
enableUpdateByExample="false">
<property name="useActualColumnNames" value="true"/>
<generatedKey column="ID" sqlStatement="DB2" identity="true" />
<columnOverride column="DATE_FIELD" property="startDate" />
<ignoreColumn column="FRED" />
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
</table>
<table schema="mybatis" tableName="tab_favorite" domainObjectName="Favorite"
enableSelectByExample="false"
enableCountByExample="false"
enableDeleteByExample="false"
enableUpdateByExample="false">
<property name="useActualColumnNames" value="true"/>
<generatedKey column="ID" sqlStatement="DB2" identity="true" />
<columnOverride column="DATE_FIELD" property="startDate" />
<ignoreColumn column="FRED" />
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
</table>
<table schema="mybatis" tableName="tab_route" domainObjectName="Route"
enableSelectByExample="false"
enableCountByExample="false"
enableDeleteByExample="false"
enableUpdateByExample="false">
<property name="useActualColumnNames" value="true"/>
<generatedKey column="ID" sqlStatement="DB2" identity="true" />
<columnOverride column="DATE_FIELD" property="startDate" />
<ignoreColumn column="FRED" />
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
</table>
<table schema="mybatis" tableName="tab_route_img" domainObjectName="RouteImg"
enableSelectByExample="false"
enableCountByExample="false"
enableDeleteByExample="false"
enableUpdateByExample="false">
<property name="useActualColumnNames" value="true"/>
<generatedKey column="ID" sqlStatement="DB2" identity="true" />
<columnOverride column="DATE_FIELD" property="startDate" />
<ignoreColumn column="FRED" />
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
</table>
<table schema="mybatis" tableName="tab_seller" domainObjectName="Seller"
enableSelectByExample="false"
enableCountByExample="false"
enableDeleteByExample="false"
enableUpdateByExample="false">
<property name="useActualColumnNames" value="true"/>
<generatedKey column="ID" sqlStatement="DB2" identity="true" />
<columnOverride column="DATE_FIELD" property="startDate" />
<ignoreColumn column="FRED" />
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
</table>
<table schema="mybatis" tableName="tab_user" domainObjectName="User"
enableSelectByExample="false"
enableCountByExample="false"
enableDeleteByExample="false"
enableUpdateByExample="false">
<property name="useActualColumnNames" value="true"/>
<generatedKey column="ID" sqlStatement="DB2" identity="true" />
<columnOverride column="DATE_FIELD" property="startDate" />
<ignoreColumn column="FRED" />
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
</table>
</context>
</generatorConfiguration>
6.GeneratorTest生成代码
import org.junit.Test;
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 GeneratorTest {
@Test
public void test01() throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("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);
}
}