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" />-->
<!--<!– 将数据库中表的字段描述信息添加到注释 –>-->
<!--<property name="addRemarkComments" value="true"/>-->
<!--<!– 注释里不添加日期 –>-->
<!--<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&characterEncoding=utf-8&autoReconnect=true&useSSL=false&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"