SpringBoot多数据源mybatis 驼峰
helloliyh 人气:01 配置多数据源时,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(); } }
加载全部内容