mybatis generator plugin 对表中有下划线字段和大驼峰字段生成代码中转为小驼峰属性

该博客主要介绍了如何使用Mybatis Generator插件将下划线或大驼峰命名的数据库字段在生成的Java实体类中转换为小驼峰属性,并且通过插件自动生成@Data注解,替代传统的get、set方法。配置过程包括针对下划线字段的转换规则设置及生成@Data注解的插件实现。
摘要由CSDN通过智能技术生成

1. 主要内容:

  ① 将下划线字段或者大驼峰字段生成的java实体类转为 小驼峰属性

 ② 用插件生成 @Data 等注解,代替 get,set 方法

1.  一些数据表由于历史原因,表中命名的字段有下划线的,比如 x_name ,也有大驼峰的 CreatedTime,但是在生成的java文件中一般都是要求属性是符合小驼峰规范的

以下是主要配置:

  <!--value = "false"  eg: roomguid ; x_IsYfYy  xisyfyy -->
            <!--<property name="useActualColumnNames" value="false"/>-->
            <!--true 意思是 ①表里的字段 是大驼峰 可以转为小驼峰 ;
            ②  用 表的列名做 属性值 x_EndDate 字段 在实体类里也是  x_EndDate

            -->
            <property name="useActualColumnNames" value="true"/>
            <!-- 如果不加下面的 这一列, 即使开启了小驼峰命名 ,x_EndDate 字段实体类里 还是 x_EndDate -->
            <!--下划线命名的处理-->
            <columnRenamingRule searchString="^x_" replaceString="x"/>

 

完整的配置如下:

<?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" defaultModelType="flat" >
        <!--当我们生成实体类以后,发现默认是没有toString和序列化,但是很多时候需要序列化对象,从而方便在网络上传输-->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
        <!--生成 toString 的插件-->
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />

        <!-- 自动生成lombok注解配置和根据数据库注释为实体添加注释, 需要自己编译文件才可以实现,具体见https://www.jianshu.com/p/58ee7e09fc3f -->
        <!-- 注意这个路径要配置成 自己 插件的 全路径,否则会报  Cannot instantiate object of type ****LombokPlugin  (因为找不到自定义的插件)  -->
        <plugin type="com.springboot.mybatis.plugin.LombokPlugin" >
            <property name="hasLombok" value="true"/>
        </plugin>
        <!-- 关闭注解信息 -->
        <!--<commentGenerator>-->
            <!--<property name="suppressAllComments" value="true" />-->
            <!--&lt;!&ndash; 将数据库中表的字段描述信息添加到注释 &ndash;&gt;-->
            <!--<property name="addRemarkComments" value="true"/>-->
            <!--&lt;!&ndash; 注释里不添加日期 &ndash;&gt;-->
            <!--<property name="suppressDate" value="true"/>-->
        <!--</commentGenerator>-->

        <commentGenerator type="com.springboot.mybatis.plugin.CustomMybatisCommentGenerator">
            <!-- 定义是否生成原生注释,我们这里自定义了,所以选择false -->
            <property name="suppressAllComments" value="false"/>
            <!-- This property is used to specify whether MBG will include the generation timestamp in the generated comments -->
            <property name="suppressDate" value="true"/>
        </commentGenerator>



        <!-- 自己修改JDBC的路径 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/45_sql?useUnicode=true&amp;characterEncoding=utf-8&amp;autoReconnect=true&amp;useSSL=false&amp;serverTimezone=UTC"
                        userId="root"
                        password="123456"/>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
            <!--<property name="useInformationSchema" value="true"/>-->
        </javaTypeResolver>

        <!-- 生成POJO对象,并将类放到com.example.demo.entity.generatePackage包下 -->
        <javaModelGenerator targetPackage="com.example.demo.entity.generatePackage"
                            targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- 生成mapper xml文件,并放到resources下的mapper文件夹下 -->
        <sqlMapGenerator targetPackage="mybatis.mapper"  targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>


        <!-- 生成的dao类 -->
        <javaClientGenerator type="XMLMAPPER"
    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值