mybatis 下划线转驼峰配置

Posted 太白的技术博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis 下划线转驼峰配置相关的知识,希望对你有一定的参考价值。

 一直以来,在sqlmap文件中,对于数据库中的下划线字段转驼峰,我们都是通过resultmap来做的,如下:

<resultMap id="ISTableStatistics" type="com.medsoft.perfstat.pojo.ISTableStatistics" >
<result column="TABLE_SCHEMA" property="tableSchema" jdbcType="VARCHAR" />
<result column="TABLE_NAME" property="tableName" jdbcType="VARCHAR" />
<result column="ROWS_READ" property="rowsRead" jdbcType="BIGINT" />
<result column="ROWS_CHANGED" property="rowsChanged" jdbcType="BIGINT" />
<result column="ROWS_CHANGED_X_INDEXES" property="rowsChangedXIndexes" jdbcType="BIGINT" />
</resultMap>

<select id="selectISTableStatistics" parameterType="java.util.Map" resultMap="ISTableStatistics">
select
ews.TABLE_SCHEMA,
ews.TABLE_NAME,
ews.ROWS_READ,
ews.ROWS_CHANGED,
ews.ROWS_CHANGED_X_INDEXES
from information_schema.TABLE_STATISTICS ews
where ews.TABLE_SCHEMA not in (‘perf_stat‘,‘mysql‘,‘sys‘,‘performance_schema‘,‘information_schema‘)
and ews.rows_read > 0
</select>

这是件挺无聊的事情,其实mybatis是支持自动转的,只要在mybatis-config.xml中加上mapUnderscoreToCamelCase setting即可,如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="conf/db-dialect.properties" />
<settings>
<setting name="logImpl" value="LOG4J"/>
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
</configuration>

这样配置之后,就不需要人工写上面的resultMap了,直接resultType就可以了,对于字段超多的情况,这可以省去不少没意义的时间花费。

以上是关于mybatis 下划线转驼峰配置的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis Generator 下划线转驼峰命名

MyBatis开启驼峰命名法映射作用

Mybatis处理列名—字段名映射— 驼峰式命名映射

求助:word中将下划线的字符转成驼峰格式

go语言实现json编码驼峰统一转下划线,下划线统一转驼峰

go语言实现json编码驼峰统一转下划线,下划线统一转驼峰