Tkmybatis(即通用Mapper)(二)Spring Boot 集成Mybatis Generator和通用Mapper自动生成代码

一、spring boot的resourcesh/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>
  <!--1.注意:mysql 连接数据库jar 这里选择自己本地位置-->
  <classPathEntry location="C:\Users\18618\.m2\repository\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar" />
  <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
      <property name="javaFileEncoding" value="UTF-8"/>
      <property name="beginningDelimiter" value="`"/>
      <property name="endingDelimiter" value="`"/>
      <property name="useMapperCommentGenerator" value="true"/>
      <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
          <property name="mappers" value="com.zjm.common.util.BaseMapper"/>
          <!-- caseSensitive默认false,当数据库表名区分大小写时,可以将该属性设置为true -->
          <property name="caseSensitive" value="true"/>
          <!--forceAnnotation 是否强制生成注解,默认 false,如果设置为 true,不管数据库名和字段名是否一致,都会生成注解(包含 @Table 和 @Column)。-->
          <property name="forceAnnotation" value="true"/>
          <!-- 生成model的时候生成lombok的注解-->
          <property name="lombok" value="Data"/>
      </plugin>
      <commentGenerator>
          <property name="suppressDate" value="true"/><!-- 是否生成注释代时间戳-->
          <property name="suppressAllComments" value="true"/><!-- 是否取消注释 -->
      </commentGenerator>
    <jdbcConnection connectionURL="jdbc:mysql://127.0.0.1:3306/gwork?useUnicode=true&amp;characterEncoding=utf8"
                    driverClass="com.mysql.jdbc.Driver"
                    password="root"
                    userId="root" />
   <!--2注意:targetProject属性要配置为项目绝对路径,否则无法自动生成相关实体操作类。 -->
    <!-- 生成pojo实体类-->
    <javaModelGenerator targetPackage="com.test.model" targetProject="D:\liying-soft\gwork-api\src\main\java" />
    <!-- 生成Mapper接口对应的XML文件-->
    <sqlMapGenerator targetPackage="com.test.mapper" targetProject="D:\liying-soft\gwork-api\src\main\java" />
    <!-- 生成对应的接口文件,该接口会自动继承前面配置的通用Mapper接口-->
    <javaClientGenerator targetPackage="com.test.mapper" targetProject="D:\liying-soft\gwork-api\src\main\java" type="XMLMAPPER" />
    <!--这里使用SQL通配符%来匹配所有表。generatedKey意味着所有的表都有一个id自增的主键,在生成实体类的时候会根据该配置生成相应的注解 -->
    <!--
      <table tableName="%" >
         mysql的主键配置方式
          <generatedKey column="id" sqlStatement="Mysql" identity="true"/>


          Oracle 序列的配置方式
          <generatedKey column="id" sqlStatement="select SEQ_{1}.nextval from dual" identity="false" type="pre"/>

    </table>

      <table tableName="sr_sys_user"
             enableCountByExample="true" enableUpdateByExample="true"
             enableDeleteByExample="true" enableSelectByExample="true"
             selectByExampleQueryId="true">
      <generatedKey column="id" sqlStatement="Mysql" />
  </table>
    -->
<!--
      <table tableName="sr_credit_apply" domainObjectName="Credit_apply"
          enableCountByExample="true" enableUpdateByExample="true"
          enableDeleteByExample="true" enableSelectByExample="true"
          selectByExampleQueryId="true">
          <property name="useActualColumnNames" value="false"/>
          <generatedKey column="credit_id" sqlStatement="Mysql"  identity="true"/>
      </table>
    -->
      <table tableName="sr_credit_apply" domainObjectName="CreditApply"
             selectByExampleQueryId="false">
          <!--属性名是否和字段名一致 -->
          <property name="useActualColumnNames" value="false"/>
          <!--主键id  -->
          <generatedKey column="credit_id" sqlStatement="Mysql"  identity="true"/>
      </table>
    <table tableName="sr_credit_load" domainObjectName="CreditLoad"
           selectByExampleQueryId="false">
        <!--属性名是否和字段名一致 -->
        <property name="useActualColumnNames" value="false"/>
        <!--主键id  -->
        <generatedKey column="id" sqlStatement="Mysql"  identity="false"/>
    </table>
    <table tableName="sr_s_client" domainObjectName="Client"
             selectByExampleQueryId="false">
          <!--属性名是否和字段名一致 -->
          <property name="useActualColumnNames" value="false"/>
          <!--主键id  -->
          <generatedKey column="id" sqlStatement="Mysql"  identity="false"/>
    </table>
      <table tableName="sr_s_client_abd" domainObjectName="ClientAbd"
             selectByExampleQueryId="false">
          <!--属性名是否和字段名一致 -->
          <property name="useActualColumnNames" value="false"/>
          <!--主键id  -->
          <generatedKey column="id" sqlStatement="Mysql"  identity="false"/>
      </table>
      <table tableName="sr_s_client_relation" domainObjectName="ClientRelation"
             selectByExampleQueryId="false">
          <!--属性名是否和字段名一致 -->
          <property name="useActualColumnNames" value="false"/>
          <!--主键id  -->
          <generatedKey column="id" sqlStatement="Mysql"  identity="false"/>
      </table>
  </context>
</generatorConfiguration>

三、IntellijIdea配置mybatis-generator自动生成

点击菜单项“run”→选择“Edit Configurations”,然后完成如下图

 

Tkmybatis(即通用Mapper)(二)Spring Boot 集成Mybatis Generator和通用Mapper自动生成代码

然后,运行即可:

Tkmybatis(即通用Mapper)(二)Spring Boot 集成Mybatis Generator和通用Mapper自动生成代码