org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.0 mysql mysql-connector-java runtime
spring.datasource.master.jdbc-url = jdbc:mysql://139.196.20.xxx:3306/spring_boot1?useUnicode=true&characterEncoding=utf-8spring.datasource.master.username = root spring.datasource.master.password = 123456spring.datasource.master.driver-class-name = com.mysql.cj.jdbc.Driverspring.datasource.slave.jdbc-url = jdbc:mysql://139.196.20.xxx:3306/spring_boot2?useUnicode=true&characterEncoding=utf-8spring.datasource.slave.username = root spring.datasource.slave.password = 123456spring.datasource.slave.driver-class-name = com.mysql.cj.jdbc.Driver
@Configurationpublic class DataSourceConfiguration { @Primary @Bean @ConfigurationProperties(prefix = "spring.datasource.master") public DataSource masterDataSource() { return DataSourceBuilder.create().build(); } @Bean @ConfigurationProperties(prefix = "spring.datasource.slave") public DataSource slaveDataSource() { return DataSourceBuilder.create().build(); }}
@Configuration@MapperScan( basePackages = "com.fish.chapter7.master", sqlSessionFactoryRef = "masterSqlSessionFactory", sqlSessionTemplateRef = "masterSqlSessionTemplate")public class MasterConfig { @Bean public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(masterDataSource); return bean.getObject(); } @Bean public SqlSessionTemplate masterSqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory masterSqlSessionFactory) throws Exception { return new SqlSessionTemplate(masterSqlSessionFactory); }}
@Configuration@MapperScan( basePackages = "com.fish.chapter7.slave", sqlSessionFactoryRef = "slaveSqlSessionFactory", sqlSessionTemplateRef = "slaveSqlSessionTemplate")public class SlaveConfig { @Bean public SqlSessionFactory slaveSqlSessionFactory(@Qualifier("slaveDataSource") DataSource slaveDataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(slaveDataSource); return bean.getObject(); } @Bean public SqlSessionTemplate slaveSqlSessionTemplate(@Qualifier("slaveSqlSessionFactory") SqlSessionFactory slaveSqlSessionFactory) throws Exception { return new SqlSessionTemplate(slaveSqlSessionFactory); }}
CREATE TABLE `spring_boot1`.`article` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255) NULL COMMENT '文章标题', `author` varchar(255) NULL COMMENT '作者', PRIMARY KEY (`id`) ) COMMENT = '文章表'; INSERT INTO `spring_boot1`.`article`(`id`, `title`, `author`) VALUES (1, 'master', 'Java程序鱼');
CREATE TABLE `spring_boot2`.`article` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255) NULL COMMENT '文章标题', `author` varchar(255) NULL COMMENT '作者', PRIMARY KEY (`id`) ) COMMENT = '文章表'; INSERT INTO `spring_boot2`.`article`(`id`, `title`, `author`) VALUES (1, 'slave', 'Java程序鱼');
@RestControllerpublic class BlogController { @Autowired public BlogService blogService; /** * 查询所有文章(master数据源) * * @return */ @GetMapping(value = "/getAllByMasterDatabase") public List<Blog> getAllByMasterDatabase() { return blogService.getAllByMasterDatabase(); } /** * 查询所有文章(slave数据源) * * @return */ @GetMapping(value = "/getAllBySlaveDatabase") public List<Blog> getAllBySlaveDatabase() { return blogService.getAllBySlaveDatabase(); }}@Servicepublic class BlogServiceImpl implements BlogService { @Autowired private BlogMapperMaster blogMapperMaster; @Autowired private BlogMapperSlave blogMapperSlave; @Override public List<Blog> getAllByMasterDatabase() { return blogMapperMaster.getAll(); } @Override public List<Blog> getAllBySlaveDatabase() { return blogMapperSlave.getAll(); }}
Gitee:https://gitee.com/hezhiyuan007/spring-boot-study
Github:https://github.com/java-fish-0907/spring-boot-study
联系客服