Mybatis官方提供了逆向工程,用于解决这种重复的工作。而且生成模板有两种,一种是MyBatis3Simple简单版仅生成CRUD,另一种MyBatis3豪华版,豪华版带条件的增删改查。
Mybatis逆向工程不足之处
因为封装了一些方法,所以在.java文件和.xml文件中会有一些不需要开发人员修改的内容,从程序设计的角度来说应该对这一部分关闭修改。而且Example.java文件内容基本没有改动,但是会因为表结构的修改而重新覆盖该文件,这一部分工作对应程序开发是重复的,所以应该想办法尽量避免。
正因为Mybatis本身提供逆向工具存在的问题,所以通用Mapper在Mybatis的基础上完善,来解决这些重复的问题。而且这是一位中国人封装的,所以学习成本极低。
通用Mapper逆向工具
作用:替我们生成常用增删改查操作的SQL语句
通用Mapper(tk.mybatis)官方发布地址: github:https://github.com/abel533/Mapper
码云:https://gitee.com/free https://gitee.com/free/Mapper/wikis/1.1-java?parent=1.integration
mybatis-generator有很多种用法:命令行、eclipse/IDEA、Maven插件,其使用原理完全一样。 这里介绍的是Maven插件方式,开发工具为IDEA
一、通用Mapper(tk.mybatis)逆向工程配置使用
1、在pom文件中进行如下配置
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.10.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--通用Mapper--> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.1.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--jdbc起步依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <finalName>order-center</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.7</version> <configuration> <configurationFile> ${basedir}/src/main/resources/generator/generatorConfig.xml </configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>4.1.5</version> </dependency> </dependencies> </plugin> </plugins> </build>2、在src/main/resource/generator目录下新建配置文件generatorConfig.xml
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <properties resource="generator/config.properties"/> <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <plugin type="tk.mybatis.mapper.generator.MapperPlugin"> <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/> <property name="caseSensitive" value="true"/> <!--集成lombok--> <property name="lombok" value="Getter,Setter,ToString"/> </plugin> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/store_center?characterEncoding=UTF-8&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC" userId="root" password="yibo"> </jdbcConnection> <!--实体--> <javaModelGenerator targetPackage="com.yibo.storecenter.domain.entity" targetProject="src/main/java"/> <!--mapper.xml--> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/> <!--mapper接口--> <javaClientGenerator targetPackage="com.yibo.storecenter.mapper" targetProject="src/main/java" type="XMLMAPPER"/> <!--为哪张表生成代码--> <table tableName="store"></table> </context> </generatorConfiguration>3、点击IDEA右边的Maven Projects,找到Plugins,点开mybatis-generator,找到mybatis-generator:generate,双击即可
二、Mybatis官方逆向工程配置使用
1、在pom文件中进行如下配置
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!-- mybatis逆向工程核心依赖为下面这两个,上面是我其他Demo的依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.6</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> <!-- MyBatis 逆向工程 插件 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.6</version> <configuration> <!--允许移动生成的文件 --> <verbose>true</verbose> <!-- 是否覆盖 --> <overwrite>true</overwrite> <!-- 自动生成的配置 --> <configurationFile> ${basedir}src/main/resources/generator/generatorConfig.xml</configurationFile> </configuration> <!--下面这两个可以不配置--> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.6</version> </dependency> </dependencies> </plugin> </plugins> </build>2、在src/main/resource/generator目录下新建配置文件generatorConfig.xml
(src/main/resource/generator这个目录自己随意定)
<?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数据库驱动包路径--> <classPathEntry location="E:/apache/microservice_repository/mysql/mysql-connector-java/5.1.41/mysql-connector-java-5.1.41.jar" /> <!--context:代码生成规则配置的上下文 id:标识 targetRuntime: MyBatis3Simple 只会生成基本的CRUD操作--> <context id="MysqlContext" targetRuntime="MyBatis3" defaultModelType="flat"> <!-- 生成的Java文件的编码 --> <property name="javaFileEncoding" value="UTF-8"/> <!-- 格式化java代码 --> <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/> <!-- 格式化XML代码 --> <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/> <!-- beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,比如ORACLE就是双引号,MYSQL默认是`反引号; --> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <!--生成toString--> <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/> <!-- 生成的实体Bean,将实现Serializable --> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> <!--commentGenerator:注释生成策略--> <commentGenerator> <!--suppressAllComments:是否阻止注释生成--> <property name="suppressAllComments" value="true"/> <!--suppressDate:是否阻止时间戳生成--> <property name="suppressDate" value="true"/> </commentGenerator> <!--jdbcConnection:数据库的链接属性--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/yibo-dev" userId="root" password="yibo"> </jdbcConnection> <!--javaTypeResolver:java类型转换策略--> <javaTypeResolver > <!-- forceBigDecimals false:如果数据库中的字段类型为numeric或者decimal,在代码生成的时候根据数据库中设定的长度自动选择java类型进行转换 true:直接使用java.math.BigDecimal类型--> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!--domain生成策略;targetPackage:生成到哪个包下面,targetProject:生成到哪个项目目录下面--> <javaModelGenerator targetPackage="com.yibo.domain.entity" targetProject="src/main/java"> <!--<property name="enableSubPackages" value="true" />--> <!--表示是否修剪字符串(去掉空格--> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--sqlMapGenerator:映射文件生成策略 targetPackage:生成到哪个包下面,targetProject:生成到哪个项目目录下面 --> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!--mapper接口生成策略 type:ANNOTATEDMAPPER:注解的形式 XMLMAPPER:xml映射的形式--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.yibo.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!--指定要生成代码的表 domainObjectName:设置表对应的domain实体类生成的名称 --> <table tableName="users" domainObjectName="Users"></table> <table tableName="" domainObjectName=""></table> </context> </generatorConfiguration>3、点击IDEA右边的Maven Projects,找到Plugins,点开mybatis-generator,找到mybatis-generator:generate,双击即可
##4、如果遇到因为环境问题等种种原因导致双击mybatis-generator:generate启动报错的话,可以在工程中新建一个Generator类专门用于生成逆向工程文件,这种方式一定可以
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 Generator { public static void main(String[] args) throws Exception { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; File configFile = new File("./src/main/resources/generator/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); } }注意:generatorConfig.xml文件的路径一定要写正确!