【spring-boot】Mybatis Generator 如何生成Example類


背景

我發現上傳到github上的項目,有的有Example類,有的沒有Example類,怎么回事呢?

過程

對比項目,發現/src/main/resources/mybatis/generatorConfig.xml 類不一樣。

 

原因

targetRuntime="Mybatis" 和 targetRuntime="MyBatis3Simple"

MyBatis3模式默認生成的對象將包含很多"by Example"的方法,如果不想生成這些,可以在后續的table元素中配置取消;MyBatis3Simple模式默認每個表生成一個實體對象,生成的Mapper接口僅包含必須的5個方法:deleteByPrimaryKey、insert、selectByPrimaryKey、selectAll、updateByPrimaryKey。

文件源碼

<?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="mysqlTables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="false"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--目標數據庫配置-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/komo?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false&amp;nullNamePatternMatchesAll=true"
                userId="root" password="123456"/>
        <!-- 指定生成的類型為java類型,避免數據庫中number等類型字段 -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 生成model模型,對應的包,存放位置可以指定具體的路徑,如/ProjectName/src,也可以使用MAVEN來自動生成 -->
        <javaModelGenerator targetPackage="com.example.mybatisexampledemo.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
            <property name="trimStrings" value="true"/>
            <property name="immutable" value="false"/>
        </javaModelGenerator>
        <!--對應的xml mapper文件  -->

        <sqlMapGenerator targetPackage="mapper"  targetProject="src/main/resources/mybatis">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>
        <!-- 對應的dao接口 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mybatisexampledemo.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>
        <!--定義需要操作的表及對應的DTO名稱-->
<!-- <table tableName="t_user" domainObjectName="User" enableCountByExample="false"-->
<!-- enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"-->
<!-- selectByExampleQueryId="false"/>-->
<table tableName="t_user" domainObjectName="User" />
</context> </generatorConfiguration>

 

參考地址:https://javatech.wang/index.php/archives/27/

項目demo地址:https://github.com/KoMiles/spring-example/tree/master/mybatis-example-demo


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM