Mybatis Generator具体使用小技巧

来自:网络
时间:2021-12-25
阅读:
目录

1、问题描述

mybatis generator 简单说就是生成一些mybatis的公共方法,用了好多年了,一直也没记录过,最近使用稍微有了点变话,简单记录下,方便下次使用,也希望可以帮到其他人。

2、解决方案

最近使用主要有两点变化:

(1)以前使用,指定了本地数据库驱动jar包,本次直接用maven,dependency指定下就好了,配置文件配置好后,团队人员都可以执行;

(2)发现朋友使用的方式挺好的,就是初期开发过程中,不可避免的会增加或修改一些字段,xml中新加或者重新生成方法,都有点麻烦,通过新增一个新的类,继承属性,可以尽可能减少改动;

2.1 pom中指定mybatis generator 插件

  <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.4.0</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>8.0.25</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>

说明:

在插件中指定(dependency)数据库的驱动, 示例中使用的是:mysql,oracel数据库更换成oracle驱动就好了;

2.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">
<generatorConfiguration>
    <context id="DB2Tables" targetRuntime="MyBatis3">
          <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/ruanjianlaowang"
                        userId="laowang"
                        password="laowang">
        </jdbcConnection>
        
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <javaModelGenerator targetPackage="com.laowang.lwcrud.db.entity" targetProject="./src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="com.laowang.lwcrud.db.mapper" targetProject="./src/main/java">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.laowang.lwcrud.db.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <table tableName="user" domainObjectName="User" enableInsert="true"
               enableDeleteByPrimaryKey="true"
               enableSelectByPrimaryKey="true"
               enableUpdateByPrimaryKey="true"
               enableCountByExample="false"
               enableDeleteByExample="true"
               enableSelectByExample="true"
               enableUpdateByExample="true">
            <property name="useActualColumnNames" value="false"/>
<!--            <generatedKey column="id" sqlStatement="MYSQL" identity="true"/>-->
        </table>

    </context>
</generatorConfiguration>

说明:

(1)<jdbcConnection><jdbcConnection>,用于指定数据库连接;
(2)<javaModelGenerator></javaModelGenerator>指定entity目录;
(3)<javaClientGenerator type="XMLMAPPER" ></javaClientGenerator >指定Mapper接口目录;
(4)<sqlMapGenerator></sqlMapGenerator>指定xml文件类目录;
(5)<table></table>指定具体数据表,其中的几个参数自己可以试试,带ByExample是生成示例,有些朋友喜欢用这个方式,个人还是比较喜欢直接用原始sql,加参数的方式;
(6)这里有个点,<table>中的generatedKey,假如数据库用自增id的话,可以选择这个,使用uuid的话,注释掉就好了。

2.3 执行

2.3.1 双击插件

Mybatis Generator具体使用小技巧

文件生成完成:

Mybatis Generator具体使用小技巧

2.3.2 生成实体类

Mybatis Generator具体使用小技巧

生成两个实体类;对应数据库中字段;

2.3.3 生成mapper类

Mybatis Generator具体使用小技巧

生成mapper方法,包含常用的保存、更新、删除等;

2.3.4 生成xml类

Mybatis Generator具体使用小技巧

生成xml方法,对应Mapper接口方法;

Mybatis Generator具体使用小技巧

2.3.5 生成扩展文件 (mapper)

新增扩展文件,在原有mapper后面增加Ext;

Mybatis Generator具体使用小技巧

2.3.6 生成扩展文件(extmapper.xml)

Mybatis Generator具体使用小技巧

说明:

扩展类继承,自动生成的属性,这个新增或者修改的属性同时会生效;

<mapper namespace="com.laowang.lwcrud.db.mapper.UserExtMapper">
    <resultMap id="BaseResultMap" type="com.laowang.lwcrud.db.entity.User"
               extends="com.laowang.lwcrud.db.mapper.UserMapper.BaseResultMap">
    </resultMap>

2.4 总结

整体来说,就是个工具的使用,通过插件生成公共的数据库操作方法,然后通过新增扩展ext文件记录自定义方法,避免每次修改数据库字段,对自定义的方法造成影响(假如有指定的属性字段修改了,也要一并修改),可以方便的使用;

返回顶部
顶部