spring-boot多数据源导致下划线转不了小驼峰
2021-05-26 本文已影响0人
码男将将
1.问题描述
- mysql数据字段表
id int(11)
user_name varchar(45)
user_age varchar(45)
- spring-boot中的Entity
public class UserEntity {
private Integer id;
private String userName;
private String userAge;
}
- application.yml中配置
# 下划线自动映射小驼峰
mybatis:
configuration:
map-underscore-to-camel-case: true
正常情况下做了自动映射配置,做全量查询(select * from talbe)操作时mybatis会将下划线字段映射到小驼峰属性。但是由于配置了多数据源导致此项配置没有生效.
- 多数据源配置
目录树
project--|
|-src
|-main--|
|-java.com.xxx--|
|-config--|
|-MysqlServerConfig
|-xxServerConfig
|-controller
|-entity
其中的MysqlServerConfig和xxServerConfig为两个数据源,下面已MysqlServerConfig为例
MysqlServerConfig中
因种种原因不能上源码,故从大佬哪里copy了一份原文地址:https://blog.csdn.net/qq_32512453/article/details/113752810
// “com.zhxd.mysql.mapper”这里需要注意mysql和taosdb的目录是不同的
@Configuration
@MapperScan(basePackages = {"com.zhxd.mysql.mapper"},sqlSessionFactoryRef = "mysqlSqlSessionFactory")
//这里一定要注意,这个basePackages是你的mapper接口及service所在的包名,
// 而下面的红线所标注的classpath是mapper.xml所在的位置,这个xml是配置文件,
// 处在resources里,他的路径也要格外区分开。
public class MySqlConfiguration {
@Bean(name="mysqlDataSource")
//下面的注解作用就是从application.properties中读取以这个字符串开头的那些配置,设置为数据源的配置
@ConfigurationProperties(prefix ="spring.datasource.mysql")
public DataSource mysqlDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name="mysqlSqlSessionFactory")
public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource)throws Exception{
SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
// 这里配置的是mysql需要读取的xml文件路径
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/mysql/*Mapper.xml"));
SqlSessionFactory sqlSessionFactory = bean.getObject();
return sqlSessionFactory;
}
@Bean(name = "mysqlTransactionManager")
public DataSourceTransactionManager mysqlTransactionManager(@Qualifier("mysqlDataSource")DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
@Bean(name="mysqlSqlSessionTemplate")
public SqlSessionTemplate mysqlSqlSessionTemplate(@Qualifier("mysqlSqlSessionFactory")SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
2.问题刨析
因为在多数据源中自定义的sqlsessionFactory导致了不会加载yml文件所以mybatis的配置也就不会生效了。
3.问题解决
更新后的MysqlServerConfig
@Configuration
@MapperScan(basePackages = {"com.zhxd.mysql.mapper"},sqlSessionFactoryRef = "mysqlSqlSessionFactory")
public class MySqlConfiguration {
// 配置一个全局配置方法
@Bean
@ConfiguraitonProperties(prefix = "mybatis.configuration")
public org.apache.ibatis.session.Configuration globalConfiguration(){
return new org.apche.ibatis.session.Configuration();
}
@Bean(name="mysqlDataSource")
@ConfigurationProperties(prefix ="spring.datasource.mysql")
public DataSource mysqlDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name="mysqlSqlSessionFactory")
public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource)throws Exception{
SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/mysql/*Mapper.xml"));
// 将新对象加入bean
bean.setConfiguration(globalConfiguration());
SqlSessionFactory sqlSessionFactory = bean.getObject();
return sqlSessionFactory;
}
@Bean(name = "mysqlTransactionManager")
public DataSourceTransactionManager mysqlTransactionManager(@Qualifier("mysqlDataSource")DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
@Bean(name="mysqlSqlSessionTemplate")
public SqlSessionTemplate mysqlSqlSessionTemplate(@Qualifier("mysqlSqlSessionFactory")SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}