【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 @   KoMiles  阅读(8504)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示