久違了,最近度過了一段倦怠期,這段時間幹什麼都沒有動力,拖延症復發。好在我回來了.... ——From me .java
進入今天的主題——在Spring Boot 項目中整合mybatis多數據源,其實很簡單,其實並不難。 總體項目結構以及數據源配置application.yml: mysql
看代碼吧.git
/** * @author Lensen * @desc * @since 2018/9/19 10:22 */ @Configuration @MapperScan(basePackages = PrimaryDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "primarySqlSessionFactory") public class PrimaryDataSourceConfig { static final String PACKAGE = "com.developlee.multipartmybatisdatasource.dao.primary"; static final String MAPPER_LOCATION = "classpath:mapper/primary/*.xml"; @Value("${primary.datasource.url}") private String url; @Value("${primary.datasource.username}") private String user; @Value("${primary.datasource.password}") private String password; @Value("${primary.datasource.driverClassName}") private String driverClass; @Bean(name = "primaryDataSource") @Primary public DataSource primaryDataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driverClass); dataSource.setUrl(url); dataSource.setUsername(user); dataSource.setPassword(password); return dataSource; } @Bean(name = "primaryTransactionManager") @Primary public DataSourceTransactionManager primaryTransactionManager() { return new DataSourceTransactionManager(primaryDataSource()); } @Bean(name = "primarySqlSessionFactory") @Primary public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource primaryDataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(primaryDataSource); sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources(PrimaryDataSourceConfig.MAPPER_LOCATION)); return sessionFactory.getObject(); } }
/** * @author Lensen * @desc * @since 2018/9/19 10:22 */ @Configuration @MapperScan(basePackages = SecondaryDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "secondarySqlSessionFactory") public class SecondaryDataSourceConfig { static final String PACKAGE = "com.developlee.multipartmybatisdatasource.dao.secondary"; static final String MAPPER_LOCATION = "classpath:mapper/secondary/*.xml"; @Value("${secondary.datasource.url}") private String url; @Value("${secondary.datasource.username}") private String user; @Value("${secondary.datasource.password}") private String password; @Value("${secondary.datasource.driverClassName}") private String driverClass; @Bean(name = "secondaryDataSource") public DataSource secondaryDataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driverClass); dataSource.setUrl(url); dataSource.setUsername(user); dataSource.setPassword(password); return dataSource; } @Bean(name = "secondaryTransactionManager") public DataSourceTransactionManager secondaryTransactionManager() { return new DataSourceTransactionManager(secondaryDataSource()); } @Bean(name = "secondarySqlSessionFactory") public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("secondaryDataSource") DataSource secondaryDataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(secondaryDataSource); sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources(SecondaryDataSourceConfig.MAPPER_LOCATION)); return sessionFactory.getObject(); } }
其餘業務邏輯代碼就不貼了,代碼可在個人github上找到。文章末尾有地址。github
application.ymlweb
## primary 數據源配置 primary: datasource: url: jdbc:mysql://localhost:3306/javashop?useUnicode=true&characterEncoding=utf8 username: root password: 123456 driverClassName: com.mysql.jdbc.Driver ## secondary 數據源配置 secondary: datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8 username: root password: 123456 driverClassName: com.mysql.jdbc.Driver
pom.xmlspring
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency>
@RunWith(SpringRunner.class) @SpringBootTest public class MultipartMybatisDatasourceApplicationTests { @Autowired UserServiceImpl userService; @Autowired HomeServiceImpl homeService; @Test public void contextLoads() { UserEntity userEntity = new UserEntity(); userEntity.setId(1L); userEntity.setName("Lensen"); userEntity.setMobile("13738718660"); HomeEntity homeEntity = new HomeEntity(); homeEntity.setId(1L); homeEntity.setUserId(1L); homeEntity.setCity("杭州"); homeEntity.setTown("西湖區"); userService.saveUser(userEntity); homeService.saveHome(homeEntity); userService.getUserById(1L); homeService.getHomeByUserId(1L); } }
代碼能夠在個人github.com中找到。sql
個人公衆號:若是有興趣 能夠關注下 session