文章目录
需求
项目想使用mybatis-generator插件来生成一些基础类,包括entity,dao和mapper,但是在使用过程中需要了各种问题,现在记录下来:
1、正常版本
先直接给出可以正常使用的2处配置信息。可以根据注释修改相应的值,然后就可以直接使用。
pom文件中配置:
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
<!--地址是generatorConfig.xml的地址-->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
</configuration>
</plugin>
</plugins>
</build>
generatorConfig.xm可以直接放在resource目录下
<?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 location="D:\\mysql-connector-java-8.0.12.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="false"/>
<property name="suppressDate" value="false"/>
<property name="addRemarkComments" value="true"/>
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ins_xxxx?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=Asia/Shanghai&useSSL=false&nullCatalogMeansCurrent=true"
userId="root"
password="qsm">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置 -->
<javaModelGenerator targetPackage="com.qsm.xxx.domain.entity" targetProject="src/main/java">
<!-- 是否允许子包 -->
<property name="enableSubPackages" value="false"/>
<!-- 是否清理从数据库中查询出的字符串左右两边的空白字符 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成的映射文件包名和位置 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.qsm.xxx.dao"
targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
<table tableName="test" domainObjectName="Test" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<!-- 下面2个根据实际情况选填 -->
<!--
<ignoreColumn column="update_time"/>
<columnOverride column="is_deleted" property="deleted" javaType="java.lang.Boolean"/>
-->
</table>
</context>
</generatorConfiguration>
2、只有insert方法的解决办法
可能报错的信息为:
Cannot obtain primary key information from the database, generated objects may be incomplete
1、版本不匹配问题
版本不匹配是最有可能的情况。原始pom文件中配置的mybatis-generator-maven-plugin插件的版本是1.3.6,而在文件中的mysql数据库驱动mysql-connector-java是8.0.12版本。
所以原始版本不匹配时generatorConfig.xml的部分配置如下:
<!-- 数据库驱动包位置 -->
<classPathEntry location="D:\\mysql-connector-java-8.0.12.jar"/>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection
driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ins_xxx?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=Asia/Shanghai&useSSL=false"
userId="root"
password="123456">
</jdbcConnection>
而使用mysql数据库驱动mysql-connector-java为5.1.35版本时,即可修复只有insert的方法。generatorConfig.xml的部分配置如下:
<!-- 更改为5.1.35 -->
<classPathEntry location="D:\\mysql-connector-java-5.1.35.jar"/>
<!--去掉了.cj,去掉了zeroDateTimeBehavior和serverTimezone-->
<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ins_xxx?useUnicode=true&characterEncoding=UTF-8&useSSL=false"
userId="root"
password="123456">
</jdbcConnection>
2、设置nullCatalogMeansCurrent=true
以第一种情况中的版本不匹配情况为前提,如果不想按照第一种情况修改,还有更简单的方法。即直接在配置数据库的connectionURL参数的最后面加上&nullCatalogMeansCurrent=true
即可。generatorConfig.xml的相关部分配置如下:
<!--数据库连接的信息的连接地址最后面加上&nullCatalogMeansCurrent=true -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ins_xxx?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=Asia/Shanghai&useSSL=false&nullCatalogMeansCurrent=true"
userId="root"
password="123456">
</jdbcConnection>
3、设置catalog
再以第一种情况中的版本不匹配情况为前提,如果不想按照第一种情况修改,也不想按照第二种情况添加,还有一个的方法。那就是在generatorConfig.xml配置文件中设置数据表table标签的时候添加catalog
属性,其值就是数据库名。generatorConfig.xml的相关部分配置如下:
<!-- 要生成那些表(更改tableName和domainObjectName就可以),catalog的值为数据库名 -->
<table catalog="order" tableName="order_applicant" domainObjectName="OrderApplicant" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"/>
4、没有设置主键
MySQL数据库中没有设置主键,一般都是设置ID为主键就可以了。
CREATE TABLE `order_pay_record` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增主键',
`order_no` bigint(20) DEFAULT NULL COMMENT '订单号',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='支付记录表';
5、设置了不生成的参数
在generatorConfig.xml配置table的时候添加了相关参数enableSelectByPrimaryKey,enableUpdateByPrimaryKey和enableDeleteByPrimaryKey。(一般都是网上的模板,所以此处情况很少见)。把这三个参数去掉就好了,或者设置为true。
<table tableName="pro_pfp_ration"
domainObjectName="ProPfpRation" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"
enableSelectByPrimaryKey="false"
enableUpdateByPrimaryKey="false"
enableDeleteByPrimaryKey="false"/>
3、com.mysql.jdbc.Driver. This is deprecated问题
可能遇到报错信息为
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
这个问题主要是mysql驱动的版本高于6.0版本,则generatorConfig.xml连接数据库的信息中driverClass应该为com.mysql.cj.jdbc.Driver
,若是低于6.0的版本,则填写com.mysql.jdbc.Driver
。同时,若使用了6.0版本以上的驱动,则连接信息connectionURL需要加上时区信息&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=Asia/Shanghai
4、忽略某个字段或者重命名某个字段
根据阿里巴巴的开发手册规定,数据库中是否删除字段使用is_deleted,但是java中对应的实体类中却规定为deleled。或者数据表中update_time是根据时间戳自动填充的,因此不需要在实体类中显示。那么可以使用一下2个属性避免上诉的2个问题。在generatorConfig.xml的table标签时使用ignoreColumn和columnOverride属性:
<table tableName="test" domainObjectName="Test" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<ignoreColumn column="update_time"/>
<columnOverride column="is_deleted" property="deleted" javaType="java.lang.Boolean"/>
</table>
5、实体类生成数据库中注释
在generatorConfig.xml文件中commentGenerator里面添加addRemarkComments标签
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="false"/>
<property name="suppressDate" value="false"/>
<property name="addRemarkComments" value="true"/>
</commentGenerator>
生成出来的entity实体类为
public class OrderApplicant {
/**
* Database Column Remarks:
* 自增主键
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column order_applicant.id
*
* @mbg.generated Tue May 26 10:53:30 CST 2020
*/
private Long id;
/**
* Database Column Remarks:
* 订单号
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column order_applicant.order_no
*
* @mbg.generated Tue May 26 10:53:30 CST 2020
*/
private Long orderNo;
}
6、更简单的注释
可以查看下一篇博文
===============================
【正在去BAT的路上修行】