数据库里面表的字段中带有“”_“下划线,我们知道插件默认的是将这些带有下划线的字段默认的变成“优美的驼峰式”的。表是肯定不能动的,实体类的字段也是非常多,改起来非常
          数据库里面表的字段中带有“”_“下划线,我们知道插件默认的是将这些带有下划线的字段默认的变成“优美的驼峰式”的。表是肯定不能动的,实体类的字段也是非常多,改起来非常麻烦,所以就研究了下面这种依靠代码来实现的方式。
修改配置文件:
<?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 location="E:\mysql-connector-java-5.1.29.jar" /> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="true" /> <property name="suppressDate" value="true" /> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://172.16.14.40:3306/zhu" userId="zhu" password="zhu" /> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> </javaTypeResolver> <javaModelGenerator targetPackage="com.tt.domain" targetProject="MybatisT/src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="com.tt.domain" targetProject="MybatisT/src/main/resources"> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <table tableName="zlpg_value" enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" selectByExampleQueryId="true" enableUpdateByExample="false"> <property name="useActualColumnNames" value="true"/> <!-- <generatedKey column="ID" sqlStatement="oracle" identity="true" /> --> </table> </context> </generatorConfiguration>
<property name="useActualColumnNames" value="true"/>
补充知识:Mybatis逆向生成,设置不使用小驼峰命名
实际项目中,需要将JSON对象储存下来,但Mybatis逆向生成插件会将数据库中带 下划线_ 的字段生成为小驼峰命名的属性。
只需要在 generatorConfig.xml 中加入
<!--使用实际的字段名-->
<property name="useActualColumnNames" value="true"/>
就可以生成实际字段名的实体类。
package com.sbk.pojo;
public class BoxPushedData {
 private String camera_name;
 private Integer channel;
 private Integer device_id;
 private String img_id;
 
 //省略...
}
generatorConfig.xml 如下:
<?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>
 <!--mysql 连接数据库jar 这里选择自己本地位置-->
 <classPathEntry
   location="X:\xxx\xxxx\mysql-connector-java-8.0.17.jar"/>
 <context id="testTables" targetRuntime="MyBatis3">
  <commentGenerator>
   <!-- 是否去除自动生成的注释 true:是 : false:否 -->
   <property name="suppressAllComments" value="true"/>
  </commentGenerator>
  <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
  <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
      connectionURL="jdbc:mysql://localhost:3306/dbname"
      userId="root"
      password="123456">
  </jdbcConnection>
  <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
   NUMERIC 类型解析为java.math.BigDecimal -->
  <javaTypeResolver>
   <property name="forceBigDecimals" value="false"/>
  </javaTypeResolver>
  <!-- targetProject:生成PO类的位置 -->
  <javaModelGenerator targetPackage="com.sbk.pojo"
       targetProject="src/main/java">
   <!-- enableSubPackages:是否让schema作为包的后缀 -->
   <property name="enableSubPackages" value="false"/>
   <!-- 从数据库返回的值被清理前后的空格 -->
   <property name="trimStrings" value="true"/>
  </javaModelGenerator>
  <!-- targetProject:mapper映射文件生成的位置
   如果maven工程只是单独的一个工程,targetProject="src/main/java"
   若果maven工程是分模块的工程,targetProject="所属模块的名称",例如:
   targetProject="ecps-manager-mapper",
   targetProject="src/main/resources"
   下同-->
  <sqlMapGenerator targetPackage="mapper"
       targetProject="src/main/resources">
   <!-- enableSubPackages:是否让schema作为包的后缀 -->
   <property name="enableSubPackages" value="false"/>
  </sqlMapGenerator>
  <!-- targetPackage:mapper接口生成的位置 -->
  <javaClientGenerator type="XMLMAPPER"
        targetPackage="com.sbk.mapper"
        targetProject="src/main/java">
   <!-- enableSubPackages:是否让schema作为包的后缀 -->
   <property name="enableSubPackages" value="false"/>
  </javaClientGenerator>
  <!-- 指定数据库表 %通配-->
  <table schema="" tableName="talbe_name">
 <!--使用实际的字段名-->
  <property name="useActualColumnNames" value="true"/>
 </table>
  
 </context>
</generatorConfiguration>
以上这篇Mybatis插件之自动生成不使用默认的驼峰式操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。
