SpringBoot项目多数据源及mybatis驼峰失效的问题解决方法

SpringBoot项目多数据源及mybatis驼峰失效的问题解决方法

目录

1 配置多数据源时,application.yml 的有关mybatis的配置是失效的,因为他不知道配置哪一个数据源

2 application.yml 配置文件

3 解决方法一

4 完整代码

4解决方法二

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/*.xml")); sqlSessionFactoryBean.setTypeAliasesPackage("com.liyh.entity"); sqlSessionFactoryBean.setConfiguration(configuration); return sqlSessionFactoryBean.getObject(); } 4 完整代码 package com.liyh.config; 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.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; 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 org.springframework.transaction.PlatformTransactionManager; import javax.sql.DataSource; @Configuration @MapperScan(basePackages = {"com.liyh.mapper"}, sqlSessionFactoryRef = "masterSqlSessionFactory") public class MybatisMasterConfig { @Bean(name = "masterDataSource") @ConfigurationProperties(prefix = "spring.datasource.master") public DataSource dataSource() { return DataSourceBuilder.create().build(); } /** * 配置事务管理器,不然事务不起作用 * * @return */ @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(); } } 4解决方法二

1 在 sqlSessionFactory 返回的 bean 中设置驼峰

sqlSessionFactoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true); @Primary @Bean(name = "masterSqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources("classpath*:mapper/*.xml")); sqlSessionFactoryBean.setTypeAliasesPackage("com.liyh.entity"); sqlSessionFactoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true); return sqlSessionFactoryBean.getObject(); }

2 完整代码

package com.liyh.config; 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.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; 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 org.springframework.transaction.PlatformTransactionManager; import javax.sql.DataSource; @Configuration @MapperScan(basePackages = {"com.liyh.mapper"}, sqlSessionFactoryRef = "masterSqlSessionFactory") public class MybatisMasterConfig { @Bean(name = "masterDataSource") @ConfigurationProperties(prefix = "spring.datasource.master") public DataSource dataSource() { return DataSourceBuilder.create().build(); } /** * 配置事务管理器,不然事务不起作用 * * @return */ @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 驼峰内容请搜索易知道(ezd.cc)以前的文章或继续浏览下面的相关文章希望大家以后多多支持易知道(ezd.cc)!

推荐阅读