mybatis配置下划线转驼峰
特别需要注意的是,只可以下划线转驼峰,不可以驼峰转下划线,即只能是从数据库中查出来的结果对应字段(下划线)转成实体类的对应属性(驼峰)
正确
<select id="findByUserName" parameterType="java.lang.String" resultType="com.example.test1.model.Account">select * from account where user_name = #{userName}select>
错误
<select id="findByUserName" parameterType="java.lang.String" resultType="com.example.test1.model.Account">select * from account where userName = #{userName}select>
对于数据库中的字段下划线字段转驼峰,可以通过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">selectews.TABLE_SCHEMA,ews.TABLE_NAME,ews.ROWS_READ,ews.ROWS_CHANGED,ews.ROWS_CHANGED_X_INDEXESfrom information_schema.TABLE_STATISTICS ewswhere ews.TABLE_SCHEMA not in ('perf_stat','mysql','sys','performance_schema','information_schema')and ews.rows_read > 0select>
但是resultmap太过繁琐,我们可以通过简单配置非方式实现
application.yml
mybatis:configuration:#下划线转驼峰map-underscore-to-camel-case: true
application.properties
//旧版本
mybatis.configuration.mapUnderscoreToCamelCase=true
//新版本
mybatis.configuration.map-underscore-to-camel-case=true
api方式
通过@configuration配置的方式
@Bean(name = "sqlSessionFactory")public SqlSessionFactory sqlSessionFactoryBean() throws IOException {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();bean.setMapperLocations(resolver.getResources("classpath:/mapper//.xml"));try {
//开启驼峰命名转换bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);return bean.getObject();} catch (Exception e) {e.printStackTrace();throw new RuntimeException(e);}}
@Configuration@MapperScan(basePackages = {"com.xxx.xxxvvvv.mybatis.mapperr","com.xxx.xxx.mybatis.mapper","com.xxx.xxxcccc.mybatis.mapper"},sqlSessionFactoryRef = "sqlSessionFactory")@Slf4jpublic class MybatisConfig {@Bean(name = "sqlSessionFactory")public SqlSessionFactory sqlSessionFactoryBean() throws IOException {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();bean.setMapperLocations(resolver.getResources("classpath:/mapper//.xml"));try {
//开启驼峰命名转换bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);return bean.getObject();} catch (Exception e) {e.printStackTrace();throw new RuntimeException(e);}}@Bean(name = "sqlSessionFactory")public SqlSessionFactory obSqlSessionFactory(DataSource dataSource) throws Exception {return MybatisConfigurations.sqlSessionFactory(dataSource).configLocation("classpath:mybatis/mybatis-config.xml").mapperLocations("classpath*:persistence/mybatis/mapper/**/*.xml").interceptors(MybatisConfigurations.defaultPaginationInterceptor(dataSource)).build().getConfiguration().setMapUnderscoreToCamelCase(true);}}
config.xml
<configuration><settings><setting name="mapUnderscoreToCamelCase" value="true"/>settings>
configuration>