本文利用MyBatis-Spring-Boot-Starter整合springboot和mybatis,無xml配置,實現多資料來源配置。官方文件參考
以下示例程式碼:
1、資料來源1 。指明project.demo.mod_user包下的mappers將使用sqlSessionFactory_mysql1。
@Configuration
@ConfigurationProperties(locations = "classpath:server.yml",prefix = "mybatis.mysql1")
@MapperScan(sqlSessionFactoryRef = "sqlSessionFactory_mysql1",
basePackages = {"project.demo.mod_user"})
public class DataSourceMysql1Conf {
private String url;
private String username;
private String password;
@Bean(name = "dataSource_mysql1")
@Primary
public DataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setTestWhileIdle(true);
dataSource.setValidationQuery("select 1");
return dataSource;
}
/**
* spring transaction mng
*/
@Bean(name = "transactionManager_mysql1")
@Primary
public DataSourceTransactionManager transactionManager(@Qualifier("dataSource_mysql1") DataSource rdsDataSource) {
return new DataSourceTransactionManager(rdsDataSource);
}
/**
* mybatis session factory
*/
@Bean(name = "sqlSessionFactory_mysql1")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource_mysql1")DataSource rdsDataSource) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(rdsDataSource);
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setCacheEnabled(true);
sessionFactory.setConfiguration(configuration);
return sessionFactory.getObject();
}
// setter/getter ...
}
2、資料來源2 。
@Configuration
@ConfigurationProperties(locations = "classpath:server.yml",prefix = "mybatis.mysql2")
@MapperScan(sqlSessionFactoryRef = "sqlSessionFactory_mysql2",
basePackages = {"project.demo.mod_other"})
public class DataSourceMysql2Conf {
private String url;
private String username;
private String password;
@Bean(name = "dataSource_mysql2")
public DataSource dataSource2(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setTestWhileIdle(true);
dataSource.setValidationQuery("select 1");
return dataSource;
}
/**
* spring transaction mng
*/
@Bean(name = "transactionManager_mysql2")
public DataSourceTransactionManager transactionManager(@Qualifier("dataSource_mysql2") DataSource rdsDataSource) {
return new DataSourceTransactionManager(rdsDataSource);
}
@Bean(name = "sqlSessionFactory_mysql2")
public SqlSessionFactory sqlSessionFactory2(@Qualifier("dataSource_mysql2")DataSource rdsDataSource) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(rdsDataSource);
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setCacheEnabled(true);
sessionFactory.setConfiguration(configuration);
return sessionFactory.getObject();
}
// setter/getter ...
}
3、mapper
@Mapper
public interface UserMapper {
@Select("select * from user where id=#{param1}")
User findById(int id);
}
4、執行
@Component
@Transactional(transactionManager = "transactionManager_mysql1",rollbackFor = Exception.class)
public class InitRunner implements CommandLineRunner {
@Autowired
private UserMapper userMapper;
@Override
public void run(String... args) throws Exception {
/** userMapper使用的是mysql1的資料來源 */
System.out.println(userMapper.findById(10));
}
}
5、依賴(gradle)
compile("org.springframework.boot:spring-boot-starter-web")
compile `org.mybatis:mybatis:3.4.2`
compile `mysql:mysql-connector-java:5.1.30`
compile group: `com.alibaba`, name: `druid`, version: `1.0.26`
compile group: `org.mybatis.spring.boot`, name: `mybatis-spring-boot-starter`, version: `1.2.0`