SpringBoot Maven 结合Mybatis的generator插件以及tk.mybatis的通用mapper实现逆向工程的生成

依赖导入

<!--Mysql连接-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.46</version>
</dependency>
<!--引入阿里巴巴的数据源-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.10</version>
</dependency>
<!--Mybatis-->
<dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.3.2</version>
</dependency>
 <!--通用mapper-->
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-spring-boot-starter</artifactId>
    <version>1.2.4</version>
</dependency>

build逆向工程插件

<build>
    <plugins>
          <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork> <!-- fork=true,用于明确表示编译版本配置的可用 -->
                </configuration>
            </plugin>

            <!-- mybatis逆向工程插件 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                      <!--generatorConfig.xml文件的位置,也可以使用绝对路径 -->
                      <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>
                      <dependency>
                          <groupId>tk.mybatis</groupId>
                          <artifactId>mapper</artifactId>
                          <version>4.0.0</version>
                      </dependency>
                 </dependencies>
          </plugin>
    </plugins>
</build>

创建MyMapper继承tk.mybatis中的通用模板。

package com.bosssoft.bes.basedata.utils.mapper;

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
}

编写generratorConfig.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>
    <classPathEntry
            location="D:\JAVAEE\repository\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar"/>
    <context id="MysqlTables" targetRuntime= "MyBatis3Simple">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>
        <property name=" javaFileEncoding" value="UTF-8"/>
        <!-- 指定生成 Mapper 的继承模板 -->
        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="com.bosssoft.bes.basedata.utils.mapper.MyMapper"/>
        </plugin>

        <commentGenerator>
            <property name=" suppressDate" value="false"/>
            <property name=" addRemarkComments" value=" true"/>
            <property name=" suppressAllComments" value="false"/>
        </commentGenerator>

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/db_basedata_center?serverTimezone=UTC"
                        userId=""
                        password="">
            <property name=" nul1CatalogMeansCurrent" value="true"/>
        </jdbcConnection>

        <javaTypeResolver>
            <property name=" forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 生成实体类的包名和位置 ,targetPackage指的是包名,targetProject值得是路径位置,因为Maven多模块,所以此处使用绝对路径-->
        <javaModelGenerator targetPackage="com.bosssoft.bes.basedata.entity"
                            targetProject="D:\BossSoft\gitlab\FZU_G03\boss-xtrain-basedata-center-g03\boss-bes-basedata-pojo-g03\src\main\java">
            <property name=" enableSubPackages" value="true"/>
            <property name=" trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 对于生成的mapper.xml所在目录,因为Maven多模块,所以此处使用绝对路径 -->
        <sqlMapGenerator targetPackage="mapper" targetProject="D:\BossSoft\gitlab\FZU_G03\boss-xtrain-basedata-center-g03\boss-bes-basedata-dao-g03\src\main\resources">
        </sqlMapGenerator>

        <!--生成Dao存放位置,因为Maven多模块,所以此处使用绝对路径-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.bosssoft.bes.basedata.mapper"
                             targetProject="D:\BossSoft\gitlab\FZU_G03\boss-xtrain-basedata-center-g03\boss-bes-basedata-dao-g03\src\main\java" >
            <property name=" enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--数据库中的表名tableName;生成的类名domainObjectName   -->
        <table tableName="t_category" 	 domainObjectName="Category"></table>
        <table tableName="t_comb_exam_config"     domainObjectName="CombExamConfig"></table>
        <table tableName="t_comb_exam_config_item" 	 domainObjectName="CombExamConfigItem"></table>
        <table tableName="t_subject"     domainObjectName="Subject"></table>
        <table tableName="t_subject_answer" 	 domainObjectName="SubjectAnswer"></table>
        <table tableName="t_subject_type"     domainObjectName="SubjectType"></table>
        <table tableName="t_dictionary"     domainObjectName="Dictionary"></table>
    </context>

</generatorConfiguration>

执行

  • 在IDEA中点击右侧的maven 插件,选择generate即可。

控制层入口类上

  • 需要加上扫包注解@MapperScan("com.bosssoft.bes.basedata.mapper"),需要注意的是MapperScan是
import tk.mybatis.spring.annotation.MapperScan;
posted @ 2020-10-08 21:04  DDDDy  阅读(396)  评论(0编辑  收藏  举报