二.五 启动类 去掉 MapperScan 扫描注解
// @MapperScan(value = "top.yueshushu.learn.mapper") //去掉扫描注解 @SpringBootApplication public class MyBatisApplication { public static void main(String[] args) { SpringApplication.run(MyBatisApplication.class,args); System.out.println("运行 Mybatis 多数据源配置文件"); } }
二.六 数据多数据源配置
主要配置 数据源 DataSource 和关于扫描库,扫描Mapper接口。
二.六.一 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.Configuration; import org.springframework.context.annotation.Primary; import javax.sql.DataSource; /** * @ClassName:DataSourceConfig * @Description TODO * @Author zk_yjl * @Date 2021/9/2 11:50 * @Version 1.0 * @Since 1.0 **/ @Configuration public class DataSourceConfig { /** * Mybatis 多数据源配置 springboot 数据库 * @return */ @Bean(name="dataSourceOne") @ConfigurationProperties("spring.datasource.one") @Primary public DataSource dataSourceOne(){ return DruidDataSourceBuilder.create().build(); } /** * Mybatis 多数据源配置 springboot2 数据库 * @return */ @Bean(name="dataSourceTwo") @ConfigurationProperties("spring.datasource.two") public DataSource dataSourceTwo(){ return DruidDataSourceBuilder.create().build(); } }
在 dataSourceOne() 方法上,多添加了一个注解 @Primary ,指定默认的库。 默认库为 springboot
二.六.二 配置扫描库和扫描实体
二.六.二.一 配置主库 dataSourceOne 扫描Mapper和事务
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()); } }
二.六.二.二 配置从库 dataSourceTwo 扫描Mapper和事务
package top.yueshushu.learn.config; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.annotation.Resource; import javax.sql.DataSource; /** * @ClassName:DataSourceTwoMapperConfig * @Description TODO * @Author zk_yjl * @Date 2021/9/2 12:29 * @Version 1.0 * @Since 1.0 **/ @Configuration @MapperScan( basePackages="top.yueshushu.learn.mapper.mapper2", //配置扫描的mapper接口 sqlSessionFactoryRef = "sqlSessionFactoryTwo", //使用的Factory sqlSessionTemplateRef = "sqlSessionTemplateTwo") //使用的SqlSessionTemplate public class DataSourceTwoMapperConfig { @Resource(name="dataSourceTwo") private DataSource dataSourceTwo; /**本数据源扫描的mapper路径*/ static final String MAPPER_LOCATION = "classpath:mybatis/mapper/mapper2/**/*.xml"; /** * 创建SqlSessionFactory 对象 */ @Bean(name="sqlSessionFactoryTwo") public SqlSessionFactory sqlSessionFactoryTwo(){ try{ SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSourceTwo); //设置mapper配置文件 sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION)); return sqlSessionFactoryBean.getObject(); }catch (Exception e){ e.printStackTrace(); return null; } } /** * 配置事务 * @param dataSourceTwo * @return */ @Bean public DataSourceTransactionManager dataSourceTransactionManagerTwo( @Qualifier("dataSourceTwo") DataSource dataSourceTwo) { return new DataSourceTransactionManager(dataSourceTwo); } /** * 通过SqlSessionFactory 创建 SqlSessionTemplate * @return */ @Bean(name="sqlSessionTemplateTwo") public SqlSessionTemplate sqlSessionTemplate(){ return new SqlSessionTemplate(sqlSessionFactoryTwo()); } }
二.七.一 UserService 接口及其实现类
UserService.java
public interface UserService { void addUser(User user); List<User> listUser(); }
UserServiceImpl.java
@Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public void addUser(User user) { userMapper.addUser(user); } @Override public List<User> listUser() { return userMapper.listUser(); } }
二.七.二 DeptService 接口及其实现类
DeptService.java
public interface DeptService { void addDept(Dept user); List<Dept> listDept(); }
DeptServiceImpl.java
@Service public class DeptServiceImpl implements DeptService { @Autowired private DeptMapper deptMapper; @Override public void addDept(Dept user) { deptMapper.addDept(user); } @Override public List<Dept> listDept() { return deptMapper.listDept(); } }