1.工程目录结构
2.pom.xml文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 去掉部分自带依赖-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- druid 实现数据库连接 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
</dependencies>
3.application.properties
## mysql
#cac
spring.datasource.cac.url=jdbc:mysql://localhost:3306/zlits_cac?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.cac.username=root
spring.datasource.cac.password=Rfid123456
spring.datasource.cac.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.cac.type=com.alibaba.druid.pool.DruidDataSource
#db1
spring.datasource.db1.url=jdbc:mysql://localhost:3306/zlits_itmp?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.db1.username=root
spring.datasource.db1.password=Rfid123456
spring.datasource.db1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.db1.type=com.alibaba.druid.pool.DruidDataSource
4.DataSourceCacConfig配置
package com.zit.config.datasource;
import javax.sql.DataSource;
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.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
@Configuration
@MapperScan(basePackages = "com.zit.cac.dao",
sqlSessionTemplateRef = "dbCacSqlSessionTemplate")
public class DataSourceCacConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.cac")
@Primary
public DataSource dbCacDataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean
@Primary
public SqlSessionFactory dbCacSqlSessionFactory(@Qualifier("dbCacDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
bean.setMapperLocations(resolver.
getResources("classpath*:/mybatis/mapper/cac/*.xml"));
bean.setTypeAliasesPackage("com.zit.cac.entity");
return bean.getObject();
}
@Bean
@Primary
public DataSourceTransactionManager dbCacTransactionManager(@Qualifier("dbCacDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean
@Primary
public SqlSessionTemplate dbCacSqlSessionTemplate(@Qualifier("dbCacSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
5.DataSourceDb1Config配置
package com.zit.config.datasource;
import javax.sql.DataSource;
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.boot.context.properties.ConfigurationProperties;
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 com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
@Configuration
@MapperScan(basePackages = "com.zit.itmp.db1.dao",
sqlSessionTemplateRef = "db1SqlSessionTemplate")
public class DataSourceDb1Config {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.db1")
public DataSource db1DataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean
public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().
getResources("classpath*:mybatis/mapper/itmp/db1/*.xml"));
bean.setTypeAliasesPackage("com.zit.itmp.db1.entity");
return bean.getObject();
}
@Bean
public DataSourceTransactionManager db1TransactionManager(@Qualifier("db1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean
public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
6.非默认数据源service层需要指定TransactionManager(dao层不需要特殊处理)
package com.zit.itmp.db1.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.zit.itmp.db1.dao.SceneInfoMapper;
import com.zit.itmp.db1.entity.SceneInfo;
import com.zit.itmp.db1.service.SceneInfoService;
/**
*@author: LiJiangtao
*@date: 2019-8-16上午17:39:00
*@version:
*/
@Service("sceneInfoService")
public class SceneInfoServiceImpl implements SceneInfoService{
@Autowired
private SceneInfoMapper dao;
@Transactional(value = "db1TransactionManager")
@Override
public int deleteByPrimaryKey(Long id) {
int res = dao.deleteByPrimaryKey(id);
return res;
}
@Transactional(value = "db1TransactionManager")
@Override
public int insert(SceneInfo record) {
int res = dao.insert(record);
return res;
}
@Transactional(value = "db1TransactionManager")
@Override
public int insertSelective(SceneInfo record) {
int res = dao.insertSelective(record);
return res;
}
@Transactional(value = "db1TransactionManager")
@Override
public SceneInfo selectByPrimaryKey(Long id) {
SceneInfo res = dao.selectByPrimaryKey(id);
return res;
}
@Transactional(value = "db1TransactionManager")
@Override
public int updateByPrimaryKeySelective(SceneInfo record) {
int res = dao.updateByPrimaryKey(record);
return res;
}
@Transactional(value = "db1TransactionManager")
@Override
public int updateByPrimaryKey(SceneInfo record) {
int res = dao.updateByPrimaryKey(record);
return res;
}
}
7.Application配置
8.效果
9.完整程序GitHub地址