Spring Boot整合Mybatis配置多数据源
mybatis是目前比较流行的的orm框架了,在spring中,我们知道只需要通过构造器的方式在dao层注入slaveSqlSession就可以指定对应的数据库了,那么Spring Boot是怎么实现的呢?
我们现在有两个库,一个Dynamic,一个Moment,那么我们分别配置两个数据源
@Configuration @MapperScan(basePackages = "com.huoli.trip.dao.dynamic", sqlSessionTemplateRef = "dynamicSqlSessionTemplate") public class DynamicDataSourceConfig { //配置数据源 @Bean(name = "dynamicDataSource") @ConfigurationProperties(prefix = "spring.datasource.mysql.dynamic") public DataSource dynamicDatasource() { return DataSourceBuilder.create().build(); } @Bean(name = "dynamicSqlSessionFactory") public SqlSessionFactory dynamicSqlSessionFactory(@Qualifier("dynamicDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setTypeAliasesPackage("com.huoli.trip.entity.dynamic"); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/dynamic/*.xml")); bean.setDataSource(dataSource); return bean.getObject(); } //配置事务 @Bean(name = "dynamicTransactionManger") public DataSourceTransactionManager dynamicTransactionManger(@Qualifier("dynamicDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "dynamicSqlSessionTemplate") public SqlSessionTemplate dynamicSqlSessionTemplate(@Qualifier("dynamicSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
@Configuration @MapperScan(basePackages = "com.huoli.trip.dao.moment", sqlSessionTemplateRef = "momentSqlSessionTemplate") public class MomentDataSourceConfig { private static final Logger logger = LoggerFactory.getLogger(MomentDataSourceConfig.class); //配置数据源 @Bean(name = "momentDataSource") @ConfigurationProperties(prefix = "spring.datasource.mysql.moment") @Primary public DataSource momentDatasource() { return DataSourceBuilder.create().build(); } @Bean(name = "momentSqlSessionFactory") @Primary public SqlSessionFactory momentSqlSessionFactory(@Qualifier("momentDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setTypeAliasesPackage("com.huoli.trip.entity.moment"); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/moment/*.xml")); bean.setDataSource(dataSource); return bean.getObject(); } //配置事务 @Bean(name = "momentTransactionManger") @Primary public DataSourceTransactionManager momentTransactionManger(@Qualifier("momentDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "momentSqlSessionTemplate") @Primary public SqlSessionTemplate momentSqlSessionTemplate(@Qualifier("momentSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
这里我们看到配置模板、事务、数据源都相同,处理配置数据库工厂的时候指定的setMapperLocations不同,另外通过@MapperScan注解扫描不同包下面的Mapper文件并指定sqlSessionTemplateRef对应的数据库模板。 Dao及xml对应的文件
@Mapper public interface AirportInfoMapper { List<Airport> selectAll(); String findName(String airportCode); }
public interface MomentMapper { int deleteByPrimaryKey(Integer id); int insert(Moment record); MomentVo selectByPrimaryKey(@Param("id") Integer id); }
xml就不贴了,和spring里面的配置一样,写对应的方法及SQL语句,另外在application.yml中配置需要的数据源
spring: datasource: mysql: moment: driverClassName: com.mysql.jdbc.Driver jdbcUrl: jdbc:mysql://host:port/moment?useUnicode=true&characterEncoding=utf-8 username: xxx password: xxxx type: com.zaxxer.hikari.HikariDataSource dynamic: driverClassName: com.mysql.jdbc.Driver jdbcUrl: jdbc:mysql://host:port/dynamic?useUnicode=true&characterEncoding=utf-8 username: xxx password: xxxx type: com.zaxxer.hikari.HikariDataSource
在service层就可以通过AirportInfoMapper和MomentMapper访问不同的数据库了