二.五 数据多数据源配置
主要配置 数据源 DataSource 和关于扫描库,扫描实体。
二.五.一 DataSource 数据源配置
package top.yueshushu.learn.config; import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; import javax.sql.DataSource; /** * @ClassName:DataSourceConfig * @Description 数据库源的配置 * @Author zk_yjl * @Date 2021/9/2 10:42 * @Version 1.0 * @Since 1.0 **/ @Component public class DataSourceConfig { /** * 创建 springboot 的数据库的数据源 DataSource * @return */ @Bean("dataSourceOne") @ConfigurationProperties("spring.datasource.one") @Primary //通过注解 @Primary 表明默认的库 public DataSource dataSourceOne(){ return DruidDataSourceBuilder.create().build(); } /** * 创建 springboot2 的数据库的数据源 DataSource * @return */ @Bean("dataSourceTwo") @ConfigurationProperties("spring.datasource.two") public DataSource dataSourceTwo(){ return DruidDataSourceBuilder.create().build(); } }
在 dataSourceOne() 方法上,多添加了一个注解 @Primary ,指定默认的库。 默认库为 springboot
二.五.二 配置扫描库和扫描实体
二.五.二.一 配置主库 dataSourceOne 数据源
package top.yueshushu.learn.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties; import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.annotation.Resource; import javax.persistence.EntityManager; import javax.sql.DataSource; /** * @ClassName:JpaOneConfig * @Description 数据库 springboot的Jpa 主库 配置信息 * @Author zk_yjl * @Date 2021/9/6 18:00 * @Version 1.0 * @Since 1.0 **/ @Configuration @EnableTransactionManagement @EnableJpaRepositories( basePackages = "top.yueshushu.learn.repository.one", // 指定扫描仓库的位置 entityManagerFactoryRef = "localContainerEntityManagerFactoryBeanOne", //指定扫描实体的位置 transactionManagerRef = "platformTransactionManagerOne") //指定事务 public class JpaOneConfig { @Resource(name="dataSourceOne") DataSource dataSourceOne; @Autowired JpaProperties jpaProperties; @Primary //配置默认 @Bean(name = "entityManagerPrimaryOne") public EntityManager entityManagerOne(EntityManagerFactoryBuilder builder) { return localContainerEntityManagerFactoryBeanOne(builder).getObject().createEntityManager(); } @Bean @Primary //配置默认 LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBeanOne(EntityManagerFactoryBuilder builder) { return builder.dataSource(dataSourceOne) // 设置实体的包 .packages("top.yueshushu.learn.pojo.one") //设置配置信息 .properties(jpaProperties.getProperties()) //设置持久化的名称 .persistenceUnit("onePersistenceUnit") .build(); } @Bean @Primary //配置默认 PlatformTransactionManager platformTransactionManagerOne(EntityManagerFactoryBuilder builder) { LocalContainerEntityManagerFactoryBean factoryBeanOne = localContainerEntityManagerFactoryBeanOne(builder); return new JpaTransactionManager(factoryBeanOne.getObject()); } }
二.五.二.二 配置从库 dataSourceOne 数据源
package top.yueshushu.learn.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties; import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.annotation.Resource; import javax.persistence.EntityManager; import javax.sql.DataSource; /** * @ClassName:JpaOneConfig * @Description 数据库 springboot的Jpa配置信息 * @Author zk_yjl * @Date 2021/9/6 18:00 * @Version 1.0 * @Since 1.0 **/ @Configuration @EnableJpaRepositories(basePackages = "top.yueshushu.learn.repository.two", // 指定扫描仓库的位置 entityManagerFactoryRef ="localContainerEntityManagerFactoryBeanTwo", //指定扫描实体的位置 transactionManagerRef = "platformTransactionManagerTwo") //指定事务 @EnableTransactionManagement public class JpaTwoConfig { @Resource(name="dataSourceTwo") DataSource dataSourceTwo; @Autowired JpaProperties jpaProperties; @Bean(name = "entityManagerTwo") public EntityManager entityManager(EntityManagerFactoryBuilder builder) { return localContainerEntityManagerFactoryBeanTwo(builder).getObject().createEntityManager(); } @Bean LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBeanTwo(EntityManagerFactoryBuilder builder) { return builder.dataSource(dataSourceTwo) .packages("top.yueshushu.learn.pojo.two") .properties(jpaProperties.getProperties()) //设置持久化的名称 .persistenceUnit("twoPersistenceUnit") .build(); } @Bean PlatformTransactionManager platformTransactionManagerTwo(EntityManagerFactoryBuilder builder) { LocalContainerEntityManagerFactoryBean factoryBeanTwo = localContainerEntityManagerFactoryBeanTwo(builder); return new JpaTransactionManager(factoryBeanTwo.getObject()); } }
二.六 接口及其实现
UserService.java 接口
public interface UserService { void addUser(User user); List<User> listUser(); void addDept(Dept dept); List<Dept> listDept(); }
UserServiceImpl.java 实现类
@Service public class UserServiceImpl implements UserService { // 数据源1 放置到 springboot 数据库里面 @Resource private UserRepository userRepository; //数据源2, 放置到 springboot2 数据库里面 @Resource private DeptRepository deptRepository; /** * 使用的是数据源 springboot */ @Override public void addUser(User user) { userRepository.save(user); } /** * 使用的是数据源 springboot */ @Override public List<User> listUser() { return userRepository.findAll(); } /** * 使用的是数据源 springboot2 */ @Override public void addDept(Dept dept) { deptRepository.save(dept); } /** * 使用的是数据源 springboot2 */ @Override public List<Dept> listDept() { return deptRepository.findAll(); } }
二.七 测试
二.七.一 创建测试类
@SpringBootTest @Log4j2 public class MultipeDataSourceApplicationTests { @Autowired private UserService userService; @Test public void addUserTest(){ //1. 构建对象 User user=new User(); user.setName("周小欢"); user.setAge(22); user.setSex("女"); user.setDescription("一个非常可爱的女孩纸"); //2. 添加方法 userService.addUser(user); log.info("添加员工成功"); } @Test public void listUserTest(){ List<User> userList=userService.listUser(); userList.forEach(n->log.info(n)); } @Test public void addDeptTest(){ //1. 构建对象 Dept dept=new Dept(); dept.setName("信息管理部"); //2. 添加方法 userService.addDept(dept); log.info("添加部门成功"); } @Test public void listDeptTest(){ List<Dept> deptList=userService.listDept(); deptList.forEach(n->log.info(n)); } /** * 数据源切换配置 */ @Test public void allDataSourceTest(){ addUserTest(); listDeptTest(); addDeptTest(); listUserTest(); } }
二.七.二 测试数据源
数据库源1
添加
查询
数据库 springboot 的 user 表里面,也只存储了这一条数据
数据库源2
添加
查询
数据库 springboot2 的 dept 表里面,也只存储了这一条数据
数据源切换配置 测试
再次查询数据库表
数据源切换配置成功.
本章节的代码放置在 github 上:
https://github.com/yuejianli/springboot/tree/develop/MultipeDataSource_Jpa
谢谢您的观看,如果喜欢,请关注我,再次感谢 !!!