MyBatis逆向工程generatorConfig配置文件的Table中generatedKey的作用_u013282737的博客-CSDN博客_generatedkey

     Mybatis逆向工程生成Entity,Mapper等文件,在有自增ID的时候,在generatorConfig.xml中对表的配置,都要加上一行        generatedKey,如下:

  <table tableName="DICTIONARY_SUMMARY"  enableCountByExample="false" enableUpdateByExample="false"
       enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
       
           <generatedKey column="ID" sqlStatement="Mysql" identity="true"/>
           
  </table>

这样做的意义是,调用mapper插入一条数据到数据库之后,能从当前这个Entity中获取到插入之后的Id,如下:

1:在generatorConfig.xml中没有添加generatedKey,获得的ID为null

2:在generatorConfig.xml中添加generatedKey,获得的ID为数据库中的ID

数据库:

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>    <properties resource="mapper.properties"/>        <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat" >        <property name="beginningDelimiter" value="`"/>        <property name="endingDelimiter" value="`"/>                <plugin type="tk.mybatis.mapper.generator.MapperPlugin">            <property name="mappers" value="ef.accounting_core.util.BaseMapper"/>            <property name="caseSensitive" value="true"/>        </plugin>                <commentGenerator>            <property name ="suppressDate" value="false"/>            <property name="suppressAllComments" value="false"/>            <property name="javaFileEncoding" value="UTF-8"/>        </commentGenerator>                <!--         connectionURL="jdbc:mysql://192.168.20.125:3306/CF_TEST_DB?useSSL=false"          -->        <jdbcConnection connectionURL="${spring.datasource.url}"                        driverClass="${spring.datasource.driver-class-name}"                          userId="${spring.datasource.username}"                          password="${spring.datasource.password}">                  <property name="nullCatalogMeansCurrent" value="true"/>        </jdbcConnection>                <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和          NUMERIC 类型解析为java.math.BigDecimal -->        <javaTypeResolver>            <property name="forceBigDecimals" value="true"/>            <!-- This property is used to specify whether MyBatis Generator should force the use of JSR-310 data types for DATE, TIME,            and TIMESTAMP fields, rather than using java.util.Date -->            <property name="useJSR310Types" value="true"/>        </javaTypeResolver>                <javaModelGenerator targetPackage="ef.accounting_core.entity" targetProject="src/main/java" />          <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources" />          <javaClientGenerator targetPackage="ef.accounting_core.mapper" targetProject="src/main/java" type="XMLMAPPER" />        <!--在 pom.xml 这一级目录的命令行窗口先执行                set MAVEN_OPTS="-Dfile.encoding=UTF-8"                解决中文乱码问题               再执行                mvn mybatis-generator:generate          -->           <!--        <table tableName="ORG_CON_FILE"  enableCountByExample="false" enableUpdateByExample="false"       enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">           </table> -->        <table tableName="DICTIONARY_SUMMARY"  enableCountByExample="false" enableUpdateByExample="false"       enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">                  <generatedKey column="ID" sqlStatement="Mysql" identity="true"/>                      </table>    </context></generatorConfiguration>

Original url: Access
Created at: 2020-08-06 15:58:54
Category: default
Tags: none

请先后发表评论
  • 最新评论
  • 总共0条评论