SpringBoot 整合 Mybatis Generator自动生成代码

SpringBoot 整合 Mybatis Generator自动生成代码


javaClientGenerator 有三种模式(type),type=“ANNOTATEDMAPPER”,生成Java Model 和基于注解的Mapper对象,
type=“MIXEDMAPPER”,生成基于注解的Java Model 和相应的Mapper对象,
type=“XMLMAPPER”,生成SQLMap 的XML格式文件和独立的Mapper接口

上面是配置的XMLMAPPER类型,进而将Mapper.Java文件生成到com.example.demo.dao包中,将Mapper.xml生成到src/main/resources的mapper文件夹中。

<?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>

    <!--classPathEntry:数据库的JDBC驱动,换成你自己的驱动位置 可选 -->
    <classPathEntry location="C:\Users\Xuxian\.m2\repository\mysql\mysql-connector-java\8.0.20\mysql-connector-java-8.0.20.jar"/>

    <!-- 一个数据库一个context,defaultModelType="flat" 大数据字段,不分表 -->
    <context id="MysqlTables" targetRuntime="MyBatis3Simple" defaultModelType="flat">

        <!-- 自动识别数据库关键字,默认false,如果设置为true,根据SqlReservedWords中定义的关键字列表;一般保留默认值,遇到数据库关键字(Java关键字),使用columnOverride覆盖 -->
        <property name="autoDelimitKeywords" value="true"/>

        <!-- 生成的Java文件的编码 -->
        <property name="javaFileEncoding" value="utf-8"/>

        <!-- beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,比如ORACLE就是双引号,MYSQL默认是`反引号; -->
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <!-- 格式化java代码 -->
        <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>

        <!-- 格式化XML代码 -->
        <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>

        <!-- 注释 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/><!-- 是否取消注释 -->
            <property name="suppressDate" value="false"/> <!-- 是否生成注释代时间戳-->
        </commentGenerator>

        <!-- jdbc连接-->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/db1?serverTimezone=UTC" userId="root"
                        password="root"/>

        <!-- 类型转换 -->
        <javaTypeResolver>
            <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.-->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 生成实体类地址 -->
        <javaModelGenerator targetPackage="com.example.demo.entity" targetProject="src/main/java">
            <!-- 是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false"/>
            <!-- 从数据库返回的值去掉前后空格 -->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 生成接口dao -->
        <!--type="XMLMAPPER":生成SQLMap 的XML格式文件和独立的Mapper接口-->
<!--        <javaClientGenerator targetPackage="com.example.demo.dao" targetProject="src/main/java" type="XMLMAPPER">-->
<!--            <property name="enableSubPackages" value="false"/>-->
<!--        </javaClientGenerator>-->
        <!-- 生成map.xml文件存放地址 -->
<!--        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">-->
<!--            <property name="enableSubPackages" value="false"/>-->
<!--        </sqlMapGenerator>-->

        <!--type="ANNOTATEDMAPPER":生成Java Model 和基于注解的Mapper对象-->
        <javaClientGenerator targetPackage="com.example.demo.dao" targetProject="src/main/java" type="ANNOTATEDMAPPER">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>

        <!--type="MIXEDMAPPER":生成基于注解的Java Model 和相应的Mapper对象(MyBatis3Simple 不支持)-->
<!--        <javaClientGenerator targetPackage="com.example.demo.dao" targetProject="src/main/java" type="MIXEDMAPPER">-->
<!--            <property name="enableSubPackages" value="false"/>-->
<!--        </javaClientGenerator>-->

        <!-- table可以有多个,每个数据库中的表都可以写一个table,tableName表示要匹配的数据库表,
        也可以在tableName属性中通过使用%通配符来匹配所有数据库表,
        只有匹配的表才会自动生成文件 enableSelectByPrimaryKey相应的配置表示是否生成相应的接口 -->
        <table tableName="t_user" enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"
               enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"
               enableDeleteByPrimaryKey="true">
            <property name="useActualColumnNames" value="true"/>
        </table>

    </context>
</generatorConfiguration>

参考文件:https://blog.csdn.net/weixin_42476498/article/details/100511477

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值