MyBatis开启mapUnderscoreToCamelCase配置驼峰转换

一般数据库字段不缺分大小写,所以一般使用下划线分割,比如user_id,但是在Java类中我们规范使用驼峰命名为userId,在不使用xml中resultMap标签的配置情况下,mybatis提供mapUnderscoreToCamelCase属性设置自动转换驼峰命名。

一、通过xml配置

<?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>  
    <settings>  
        <setting name="mapUnderscoreToCamelCase" value="true" />  
    </settings>  
</configuration>

二、通过springboot的 properties(yml相同)

properties

mybatis.configuration.mapUnderscoreToCamelCase=true
或
mybatis.configuration.map-underscore-to-camel-case=true

yml

# MyBatis
mybatis:
  # 搜索指定包别名
#  typeAliasesPackage: xyz.hashdog.modules.*.bean
  # 配置mapper的扫描,找到所有的mapper.xml映射文件
  mapperLocations: classpath*:mapper/**/*Mapper.xml
  configuration:
  # 开启驼峰命名
    map-underscore-to-camel-case: true

三、通过config代码

 @Bean(name = "toFactory")
    @Primary
    public SqlSessionFactory toFactory(@Qualifier("toDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        //指定全限定别名
        bean.setTypeAliasesPackage("xyz.hashdog.datasynchronism.bean");
        bean.setDataSource(dataSource);
        //指定该SqlSession对应的mapper.xml文件位置
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/to/*.xml"));
        //开启驼峰转换
        SqlSessionFactory sqlSessionFactory = bean.getObject();
        org.apache.ibatis.session.Configuration configuration =  sqlSessionFactory.getConfiguration();
        configuration.setMapUnderscoreToCamelCase(true);
        return bean.getObject();
    }

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值