以前都是在mybatis.xml中来配置,但是spring boot不想再用xml配置文件。网上搜寻了好久,才找到设置办法:sessionFactoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
db配置文件源码:
package com.vip.qa.vop.config; import com.alibaba.druid.pool.DruidDataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; import java.util.Properties; /** * Created by danny.yao on 2017/10/25. */ @Configuration @MapperScan(basePackages = VOPDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "vopSqlSessionFactory") public class VOPDataSourceConfig { static final String PACKAGE = "com.vip.qa.vop.mapper.vop"; @Value("${vop.datasource.url}") private String dbUrl; @Value("${vop.datasource.username}") private String dbUser; @Value("${vop.datasource.password}") private String dbPassword; @Value("${vop.datasource.driver-class-name}") private String dbDriver; @Bean(name = "vopDataSource") @Qualifier @Primary public DataSource vopDataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(dbDriver); dataSource.setUrl(dbUrl); dataSource.setUsername(dbUser); dataSource.setPassword(dbPassword); return dataSource; } @Bean(name = "vopSqlSessionFactory") @Qualifier @Primary public SqlSessionFactory vopSqlSessionFactory(@Qualifier("vopDataSource") DataSource scepDataSource) throws Exception { final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); sessionFactoryBean.setDataSource(scepDataSource); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/vop/*.xml")); sessionFactoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true); return sessionFactoryBean.getObject(); } // @Bean(name = "vopTransactionManager") // @Qualifier // public DataSourceTransactionManager testDataTransactionManager() { // return new DataSourceTransactionManager(vopDataSource()); // } }
参考:http://www.cnblogs.com/gods/p/4423422.html