Automatic generation of Mybatis Mapper file

Work we need to create a data table based on the use of mybatis field pojo class, mapper file and dao class, and you need to configure the dependencies between them so that the work is very trivial and repetitive, mybatis official also found this problem, so give us mybatis generator provides a tool to help us automatically create pojo class, mapper file and dao class and will help us configure their dependencies.

Plug-in dependencies

<build>
    <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.3.5</version>
            <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>6.0.6</version>
                </dependency>
                <dependency>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-core</artifactId>
                    <version>1.3.5</version>
                </dependency>
            </dependencies>
            <configuration>
                <!--允许移动生成的文件 -->
                <verbose>true</verbose>
                <!-- 是否覆盖 -->
                <overwrite>true</overwrite>
                <!-- 自动生成的配置 -->
                <configurationFile>
                    src/main/resources/generatorConfig.xml
                </configurationFile>
            </configuration>
        </plugin>
    </plugins>
</build>

BUG reminder: Version problems mysql-connector-java if your drive is com.mysql.cj.jdbc.Driver, you need version 6.0.x of. If it is version 5.1.x of com.mysql.jdbc.Driver.

note

If you work, you have to use reverse engineering Mybatis generated files, you should not rely on a reference map in the project pom.xml, then you can create a special project sringboot tool to generate the file, and then drag it to your work project. In fact, what we need most is Mapper file restMap, if your database fields as many as 30 40, Manual Mapper file definitely makes you wonder in life.

Configuration 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:name -->
    <!-- targetRuntime:设置生成的文件适用于那个 mybatis 版本 -->
    <context id="default" targetRuntime="MyBatis3">

        <!-- 生成的 Java 文件的编码 -->
        <property name="javaFileEncoding" value="UTF-8"/>

        <!-- optional,旨在创建class时,对注释进行控制 -->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--jdbc的数据库连接 -->
        <jdbcConnection
                driverClass="com.mysql.cj.jdbc.Driver"
                connectionURL= 你数据库url
                userId= 账号
                password= 你的密码 >
        </jdbcConnection>

        <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
            targetPackage     指定生成的model生成所在的包名
            targetProject     指定在该项目下所在的路径
        -->
        <!--<javaModelGenerator targetPackage="com.mmall.pojo" targetProject=".\src\main\java">-->
        <javaModelGenerator targetPackage="com.ke.likehouse.model" targetProject="./src/main/java">
            <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
            <property name="enableSubPackages" value="false"/>
            <!-- 是否对model添加 构造函数,如果为true则不会生成ResultMap -->
            <property name="constructorBased" value="false"/>
            <!-- 是否对类CHAR类型的列的数据进行trim操作 -->
            <property name="trimStrings" value="true"/>
            <!-- 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 -->
            <property name="immutable" value="false"/>
        </javaModelGenerator>

        <!--mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
        <!--<sqlMapGenerator targetPackage="mappers" targetProject=".\src\main\resources">-->
        <sqlMapGenerator targetPackage="mybatis/mappers" targetProject="./src/main/resources">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
                type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
                type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
                type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
        -->
        <!-- targetPackage:mapper接口dao生成的位置 -->
        <!--<javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject=".\src\main\java">-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.ke.likehouse.dao" targetProject="./src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <!--生成的表-->
        <!--domainObjectName:生成的domain类的名字,如果不设置,直接使用表名作为domain类的名字;可以设置为somepck.domainName,那么会自动把domainName类再放到somepck包里面;-->
        <!--enableInsert(默认true):指定是否生成insert语句;-->
        <!--enableSelectByPrimaryKey(默认true):指定是否生成按照主键查询对象的语句(就是getById或get);-->
        <!--enableSelectByExample(默认true):MyBatis3Simple为false,指定是否生成动态查询语句;-->
        <!--enableUpdateByPrimaryKey(默认true):指定是否生成按照主键修改对象的语句(即update);-->
        <!--enableDeleteByPrimaryKey(默认true):指定是否生成按照主键删除对象的语句(即delete);-->
        <!--enableDeleteByExample(默认true):MyBatis3Simple为false,指定是否生成动态删除语句;-->
        <!--enableCountByExample(默认true):MyBatis3Simple为false,指定是否生成动态查询总条数语句(用于分页的总条数查询);-->
        <!--enableUpdateByExample(默认true):MyBatis3Simple为false,指定是否生成动态修改语句(只修改对象中不为空的属性);-->
        <table tableName="agent" domainObjectName="Agent"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>

        <!-- geelynote mybatis插件的搭建 -->
    </context>
</generatorConfiguration>

The figure in the configuration file contains almost everything you might use the configuration parameters and annotated, eventually you see in the IDEA project structure should look like this:
Here Insert Picture Description

Maven command execution

IDEA has the right MAVEN Toolbar
way: by MAVEN enforcement tools of IDEA
Here Insert Picture Description
way: by MAVEN command

配置命令:mybatis-generator:generate -e

Here Insert Picture Description
Then Run like:
Here Insert Picture Description

Possible BUG

1. Your maven profile problems
2. reference mysql-connector-java version does not match with driverClassName
If your driver is com.mysql.cj.jdbc.Driver, you need version 6.xx of. If com.mysql.jdbc.Driver is a version of 5.xx.

Published 45 original articles · won praise 1 · views 1092

Guess you like

Origin blog.csdn.net/lqq404270201/article/details/103473760