SpringBoot项目多数据源及mybatis驼峰失效的问题解决方法
1 配置多数据源时,application.yml 的有关mybatis的配置是失效的,因为他不知道配置哪一个数据源
2 application.yml 配置文件
server:
# 服务器的Http端口
port: 8097
spring:
# 配置数据源
datasource:
master:
jdbc-url: jdbc:Mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC
username: root
passWord: root
driver-class-name: com.mysql.cj.jdbc.Driver
test:
jdbc-url: jdbc:mysql://localhost:3306/test2?useSSL=false&serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
# MyBatis配置
mybatis:
# 搜索指定包别名
# (如果 typeAliasesPackage不进行配置,resultType就得写全名resultType="com.liyh.entity.User",
# 但是如果配置了别名,就可以不用写全路径直接写resultType="User"即可)
type-aliases-package: com.liyh.entity
# 配置mapper的扫描,找到所有的mapper.xml映射文件
mapper-locations: classpath*:mapper*Mapper.xml
# 使用驼峰命名
# 数据库表列:user_name
# 实体类属性:userName
configuration:
map-underscore-to-camel-case: true
3 解决方法一
1 application.yml 的配置驼峰保留
2 定义全局的配置
@Bean
@ConfigurationProperties(prefix = "mybatis.configuration")
public org.apache.ibatis.session.Configuration configuration() {
return new org.apache.ibatis.session.Configuration();
}
3 在sqlSessionFactory中设置配置
@Primary
@Bean(name = "masterSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource, org.apache.ibatis.session.Configuration configuration) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:mapper
@Bean
public PlatfORMTransactionManager transactionManager() {
return new DataSourceTransactionManager(this.dataSource());
}
@Bean
@ConfigurationProperties(prefix = "mybatis.configuration")
public org.apache.ibatis.session.Configuration configuration() {
return new org.apache.ibatis.session.Configuration();
}
@Primary
@Bean(name = "masterSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource, org.apache.ibatis.session.Configuration configuration) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:mapper
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(this.dataSource());
}
@Bean
@ConfigurationProperties(prefix = "mybatis.configuration")
public org.apache.ibatis.session.Configuration configuration() {
return new org.apache.ibatis.session.Configuration();
}
@Primary
@Bean(name = "masterSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource, org.apache.ibatis.session.Configuration configuration) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:mapper/*.xml"));
sqlSessionFactoryBean.setTypeAliasesPackage("com.liyh.entity");
sqlSessionFactoryBean.setConfiguration(configuration);
return sqlSessionFactoryBean.getObject();
}
}
到此这篇关于SpringBoot项目多数据源及mybatis 驼峰失效的问题解决的文章就介绍到这了,更多相关SpringBoot多数据源mybatis 驼峰内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相关文章