mybatis-generator只有insert方法,你问题在这可以解决

需求

项目想使用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&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=CONVERT_TO_NULL&amp;serverTimezone=Asia/Shanghai&amp;useSSL=false&amp;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&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=CONVERT_TO_NULL&amp;serverTimezone=Asia/Shanghai&amp;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&amp;characterEncoding=UTF-8&amp;useSSL=false"
   			 userId="root"
   			 password="123456">
</jdbcConnection>
2、设置nullCatalogMeansCurrent=true

以第一种情况中的版本不匹配情况为前提,如果不想按照第一种情况修改,还有更简单的方法。即直接在配置数据库的connectionURL参数的最后面加上&amp;nullCatalogMeansCurrent=true即可。generatorConfig.xml的相关部分配置如下:

<!--数据库连接的信息的连接地址最后面加上&amp;nullCatalogMeansCurrent=true -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/ins_xxx?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=CONVERT_TO_NULL&amp;serverTimezone=Asia/Shanghai&amp;useSSL=false&amp;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需要加上时区信息&amp;zeroDateTimeBehavior=CONVERT_TO_NULL&amp;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的路上修行】

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
mybatis-plus-generatormybatis-plus是用于简化MyBatis开发的两个工具。mybatis-plus是一个MyBatis的增强工具包,提供了一些便捷的操作,节约了编写简单SQL的时间。而mybatis-plus-generator是一个代码生成器,可以自动生成一些基本的Controller、Service、Mapper和Mapper.xml文件。 通过整合mybatis-plus和mybatis-plus-generator,我们可以更高效地开发项目中的单表增删改查功能。使用mybatis-plus-generator可以自动生成一些基本的文件,例如Controller、Service、Mapper和Mapper.xml,极大地减少了手动创建这些文件的时间和工作量。而mybatis-plus提供的便捷操作可以节约编写简单SQL的时间。 然而,对于一些逻辑复杂、多表操作或动态SQL等情况,建议使用原生SQL来处理。mybatis-plus支持原生SQL的使用,通过写原生SQL可以更灵活地满足这些复杂需求。 综上所述,通过整合mybatis-plus和mybatis-plus-generator,我们可以在开发中更高效地处理单表的增删改查功能,并且对于复杂的需求可以使用原生SQL来满足。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Spring cloud整合MyBatis-plus和mybatis-plus-generator](https://blog.csdn.net/cssweb_sh/article/details/123767029)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [mybatis-plus-generatormybatisplus代码生成器篇)](https://blog.csdn.net/b13001216978/article/details/121690960)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值