maven mybatis generator的使用
一、 背景
在使用mybatis时我们需要重复的去创建pojo类、mapper文件以及dao类并且需要配置它们之间的依赖关系,比较麻烦且做了大量的重复工作,mybatis官方也发现了这个问题,因此给我们提供了mybatis generator工具来帮我们自动创建pojo类、mapper文件以及dao类并且会帮我们配置好它们的依赖关系。
二、 配置
1、引入mybatis generator插件
<build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.7</version> <configuration> <configurationFile>generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <executions> <execution> <id>Generate MyBatis Artifacts</id> <phase>deploy</phase> <goals> <goal>generate</goal> </goals> <configuration> <overwrite>true</overwrite> </configuration> </execution> </executions> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency> <dependency> <groupId>com.xxx</groupId> <artifactId>my-generator</artifactId> <version>0.1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> </dependencies> </plugin> </plugins> </build>
说明:my-generator 是自己定义的模板配置包
2. generatorConfig.xml 配置
<?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" > <!--使用方法:mvn mybatis-generator:generate--> <generatorConfiguration> <context id="context" targetRuntime="MyBatis3"> <property name="beginningDelimiter" value=""/> <property name="endingDelimiter" value=""/> <property name="javaFileEncoding" value="utf-8"/> <property name="useActualColumnNames" value="true"/> <commentGenerator type="com.xxx.common.generate.MyBatisCommentGenerator"> <!--<commentGenerator>--> <property name="suppressDate" value="false"/> <!-- 是否去除自动生成的注释true:是: false:否 --> <property name = "suppressAllComments" value="false"/> <property name="addRemarkComments" value="true"/> </commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://xxx.com:3306/xxx?useUnicode=true" userId="xxx" password="xxx"/> <javaTypeResolver type="com.xxx.common.generate.MyJavaTypeResolverImpl"> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成entity对象的包名和位置--> <javaModelGenerator targetPackage="com.xxx.demo.dal.model" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成mapper xml文件的包名和位置--> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成DAO的包名和位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.xxx.demo.dal.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 要生成的表tableName是数据库中的表名或视图名 domainObjectName是实体类名--> <table tableName="ssp_bill_dispatch" domainObjectName="SspBillDispatch" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true"> <property name="useActualColumnNames" value="false"/> <generatedKey column="id" sqlStatement="Mysql" identity="true"/> </table> </context> </generatorConfiguration>
MyBatisCommentGenerator
View Code
MyJavaTypeResolverImpl
package com.xxx.common.generate; import org.mybatis.generator.api.IntrospectedColumn; import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; import org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl; import java.sql.Types; /** * @author: xi * @Date: 2019-02-15 16:20 * @Description: MyJavaTypeResolver */ public class MyJavaTypeResolverImpl extends JavaTypeResolverDefaultImpl { @Override public FullyQualifiedJavaType calculateJavaType(IntrospectedColumn introspectedColumn) { FullyQualifiedJavaType answer = null; JdbcTypeInformation jdbcTypeInformation = this.typeMap.get(introspectedColumn.getJdbcType()); if (jdbcTypeInformation == null) { return answer; } else { switch (introspectedColumn.getJdbcType()) { case Types.INTEGER: // if (introspectedColumn.getLength() > 9) { // answer = new FullyQualifiedJavaType(Long.class.getName()); // } else { answer = new FullyQualifiedJavaType(Integer.class.getName()); // } break; case Types.BIT: answer = new FullyQualifiedJavaType(Byte.class.getName()); break; default: answer = jdbcTypeInformation.getFullyQualifiedJavaType(); answer = this.overrideDefaultType(introspectedColumn, answer); } return answer; } } }
3. 执行命令:
mvn mybatis-generator:generate