
在上一篇中,我们引入了Lombok,用一个workaround解决了pojo中编写代码过多的问题。接下来,结合TestLinkJ的开发背景,在已有数据库表的情况下,通过逆向工程,引入Mybatis-Generator这个工具来实现pojo/mapper/dao这些代码的自动生成。
http://www.mybatis.org/generator/ 在官网上给出了如下几种:
MyBatis Generator (MBG) can be run in the following ways:
我们以TestLink的testprojects表为例,用maven插件的方式来演示整个过程。 在src/main/resouces目录下,提供如下的MBG配置文件
<?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>
<!--classPathEntry:数据库的JDBC驱动,换成你自己的驱动位置 可选 -->
<classPathEntry location="D:\repo\testlink4j\mysql-connector-java-5.1.46.jar"/>
<!-- 一个数据库一个context,defaultModelType="flat" 大数据字段,不分表 -->
<context id="MysqlTables" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<!-- 自动识别数据库关键字,默认false,如果设置为true,根据SqlReservedWords中定义的关键字列表;一般保留默认值,遇到数据库关键字(Java关键字),使用columnOverride覆盖 -->
<property name="autoDelimitKeywords" value="true"/>
<!-- 生成的Java文件的编码 -->
<property name="javaFileEncoding" value="utf-8"/>
<!-- beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,比如ORACLE就是双引号,MYSQL默认是`反引号;-->
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!-- 格式化java代码 -->
<property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
<!-- 格式化XML代码 -->
<property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
<!-- include the plugin -->
<plugin type="com.softwareloop.mybatis.generator.plugins.LombokPlugin">
<!-- enable annotations -->
<property name="builder" value="true"/>
<!-- annotation's option(boolean) -->
<property name="builder.fluent" value="true"/>
<!-- annotation's option(String) -->
<property name="builder.builderMethodName" value="myBuilder"/>
<property name="accessors" value="true"/>
<!-- annotation's option(array of String) -->
<property name="accessors.prefix" value="m_, _"/>
<!-- disable annotations -->
<property name="allArgsConstructor" value="false"/>
</plugin>
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<!-- 注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/><!-- 是否取消注释 -->
<property name="suppressDate" value="false"/> <!-- 是否生成注释代时间戳-->
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/testlink?useUnicode=true&characterEncoding=utf8" userId="root" password=""/>
<javaTypeResolver>
<!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成实体类地址 -->
<javaModelGenerator targetPackage="com.testlink4j.domain" targetProject="src/main/java">
<!-- 是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
<!-- 从数据库返回的值去掉前后空格 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成map.xml文件存放地址 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 生成接口dao -->
<javaClientGenerator targetPackage="com.testlink4j.dao" targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- table可以有多个,每个数据库中的表都可以写一个table,tableName表示要匹配的数据库表,也可以在tableName属性中通过使用%通配符来匹配所有数据库表,只有匹配的表才会自动生成文件 enableSelectByPrimaryKey相应的配置表示是否生成相应的接口 -->
<table schema="testlink" tableName="testprojects" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"
enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"
enableDeleteByPrimaryKey="true">
<property name="useActualColumnNames" value="true"/>
</table>
<table schema="testlink" tableName="keywords" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"
enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"
enableDeleteByPrimaryKey="true">
<property name="useActualColumnNames" value="true"/>
<generatedKey column="id" sqlStatement="Mysql"/>
</table>
</context>
</generatorConfiguration>
由于是以maven插件的方式运行,需要在<build>中引入mbg的插件。
<build>
<plugins>
<!-- 其余plugin-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>com.softwareloop</groupId>
<artifactId>mybatis-generator-lombok-plugin</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
根据前面的介绍,我们在mbg的插件中还指定配套使用mybatis-generator-lombok-plugin插件来生成pojo。
执行以下命令即可:
mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate
运行结果如下:
[INFO] ------------------------------------------------------------------------
[INFO] Building testlink4j 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- mybatis-generator-maven-plugin:1.3.7:generate (default-cli) @ testlink4j ---
[INFO] Connecting to the Database
[INFO] Introspecting table testlink.testprojects
[INFO] Introspecting table testlink.keywords
[INFO] Generating Record class for table testprojects
[INFO] Generating Mapper Interface for table testprojects
[INFO] Generating SQL Map for table testprojects
[INFO] Generating Record class for table keywords
[INFO] Generating Mapper Interface for table keywords
[INFO] Generating SQL Map for table keywords
[INFO] Saving file TestprojectsMapper.xml
[INFO] Saving file KeywordsMapper.xml
[INFO] Saving file Testprojects.java
[INFO] Saving file TestprojectsMapper.java
[INFO] Saving file Keywords.java
[INFO] Saving file KeywordsMapper.java
[WARNING] Existing file D:\repo\testlink4j\src\main\java\com\testlink4j\domain\Testprojects.java was overwritten
[WARNING] Existing file D:\repo\testlink4j\src\main\java\com\testlink4j\dao\TestprojectsMapper.java was overwritten
[WARNING] Existing file D:\repo\testlink4j\src\main\java\com\testlink4j\domain\Keywords.java was overwritten
[WARNING] Existing file D:\repo\testlink4j\src\main\java\com\testlink4j\dao\KeywordsMapper.java was overwritten
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
刷新IDE,可以在以下目录看到新生成的代码 src/main/java/dao src/main/java/domain src/main/resouces/mapper

image.png
限于篇幅,就不一一展示结果了。感兴趣的读者可以自行尝试。 ———————————————— 未完待续。。。。。