【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

posted @ 2020-04-30 18:40  KoMiles  阅读(8334)  评论(0编辑  收藏  举报