mybatis-generator自动生成代码插件使用

mybatis-generator是一款在使用mybatis框架时,自动生成dao,mapper和mapper.xml的工具。之前公司使用的是自己开发的自动生成代码的框架,但是经常出现问题,所以最新使用的项目便更换为mybatis-generator,今天研究并记录一下

首先在pom文件增加依赖包和插件

<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.3.2</version>
</dependency>

在build的plugins中增加

<plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <configurationFile>./src/main/resources/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.46</version>
                    </dependency>
                </dependencies>
            </plugin>

新增配置文件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">
<generatorConfiguration>
    <context id="generator" targetRuntime="Mybatis3">

        <property name="javaFileEncoding" value="UTF-8"/>

        <!-- 生成model时使用lombok注解、并且不生成非Selective的Insert和Update -->
        <!--此lombok插件存在bug-->
        <!--<plugin type="org.mybatis.generator.plugins.LombokPlugin">
            <property name="hasLombok" value="true"/>
        </plugin>-->

        <!-- 关闭注释-->
        <commentGenerator>
            <property name="suppressDate" value="false"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!-- mysql数据库连接 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://rm-aaaaaaaaaa.mysql.rds.aliyuncs.com:3306/huihua_saas_demo?characterEncoding=utf8&amp;useSSL=true&amp;tinyInt1isBit=false"
                        userId="demo"
                        password="123456789987456"/>

        <!-- 生成model实体类文件位置 -->
        <javaModelGenerator targetPackage="com.miya.hh.order.data"
                            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>

        <!-- 生成mapper接口文件位置 -->
        <javaClientGenerator targetPackage="com.miya.hh.order.mapper"
                             targetProject="src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!--对应数据库表名,多个表,请复制指定,不需要的方法请关闭开关 -->
        <!--
          tableName:必须配置  指定表的名称
          domainObjectName:生成javabean对象的基本名称。如果未指定,MBG将自动基于表名生成。
          这个名字(无论是在这里指定,或自动生成) 。
          enableInsert:是否生成插入语句。默认是true
          enableSelectByPrimaryKey:是否通过主键生成选择语句。不管是否有这种设置,如果该表没有一个主键将不会生成。
          enableSelectByExample:是否应该生成通过example的选择语句。这个声明使得许多不同的动态查询是在运行时生成。
          enableUpdateByPrimaryKey:是否通过主键生成更新语句。如果该表没有主键,不管是否设置该属性,语句将不会生成。
          enableUpdateByExample:是否通过example对象生成更新语句。该语句将更新一个表中相匹配的记录。
          enableDeleteByPrimaryKey:是否通过主键生成删除语句。如果该表没有主键,不管这种设置该属性,语句将不会生成。
          enableDeleteByExample:是否通过example对象生成删除语句。这个声明使得许多不同的动态删除在运行时生成。
          enableCountByExample:是否通过example对象生成计算行数语句。该语句将返回一个表中的行数相匹配的example。
        -->
        <table tableName="order" domainObjectName="OrderDO" enableInsert="true"
               enableSelectByPrimaryKey="true" enableSelectByExample="false"
               enableCountByExample="false" enableUpdateByExample="false"
               enableUpdateByPrimaryKey="true"
               enableDeleteByExample="false" enableDeleteByPrimaryKey="true">
            <!-- 使用分布式主键生成工具(如:snowflake等)生成主键时,注释下面这行 -->
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
    </context>

</generatorConfiguration>

接着在Idea中双击下图中的指令即可生成代码
在这里插入图片描述
但是仍然存在一个问题,我想使用lombok插件,生成dao文件时不再需要生成getter和setter代码,但是搜索了各种解决方案,从github中https://github.com/softwareloop/mybatis-generator-lombok-plugin下载plugin,本地生成jar包,但是本地运行还是会报错

[ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.5:generate (default-cli) on project test-dao: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.5:generate failed: Cannot instantiate object of type com.example.test.common.utils.MyJavaTypeResolverDefaultImpl -> [Help 1]

网上的解决方案更换打包插件maven-jar-plugin也没有解决问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值