SpringBoot___整合多数据源Mybaits
结构
依赖
<dependencies>
<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>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
</dependencies>
这值得一提的是这里使用数据库连接池,使用的是Druid,这里是Druid是使用的是com.alibaba.druid-spring-boot-starter,该starter是由第三方提供的,MyBatis和Druid依赖的命名和其他库的命名不太一样,但是它所支持的mysql是mysql8,这由于数据库版本的问题,所以我们需要锁定数据库版本。
在application.properties中配置多数据源的基本信息,这里配置两个数据源,两个DataSource
spring.datasource.one.url=jdbc:mysql:///db?useUnicode=true&characterEncoding=utf-8
spring.datasource.one.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.one.username=root
spring.datasource.one.password=rootzsl
spring.datasource.two.url=jdbc:mysql:///demo?useUnicode=true&characterEncoding=utf-8
spring.datasource.two.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.two.username=root
spring.datasource.two.password=rootzsl
数据源配置类
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.one")
DataSource dsOne(){
return DruidDataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource.two")
DataSource dsTwo(){
return DruidDataSourceBuilder.create().build();
}
}
因为这里有两个数据源,所以Mybaits配置类有两个
MybatisDsOne
/**
* @author ZSL
* @ClassName MybatisDsOne
* @description
* @date 2019/8/10
*
*@MapperScan(basePackages = "com.zsl.mybatis02.mapper1",
* sqlSessionFactoryRef = "sqlSessionFactoryOne",
* sqlSessionTemplateRef = "sqlSessionTemplateOne")
*
*配置类中要扫描的包是com.zsl.mybatis02.mapper1,该包下的接口操作的是数据源deOne中的数据
*
*sqlSessionFactoryRef:指定该数据源在Mybaits中对应的SqlSessionFactory
*
* sqlSessionTemplateRef:指定该数据源在Mybaits中对应的SqlSessionTemplate
*
* 在MybatisDsOne这个Mybatis配置类中提供SqlSessionFactory和SqlSessionTemplate并指定数据源dsOne
*
*SqlSessionTemplate是Mybaits-Spring中的类,根据SqlSessionFactory去创建的
*
*/
@Configuration
@MapperScan(basePackages = "com.zsl.mybatis02.mapper1",
sqlSessionFactoryRef = "sqlSessionFactoryOne",
sqlSessionTemplateRef = "sqlSessionTemplateOne")
public class MybatisDsOne {
@Resource(name = "dsOne")
DataSource dataSource;
@Bean
SqlSessionFactory sqlSessionFactoryOne() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
SqlSessionFactory object = factoryBean.getObject();
return object;
}
@Bean
SqlSessionTemplate sqlSessionTemplateOne() throws Exception {
return new SqlSessionTemplate(sqlSessionFactoryOne());
}
}
MybatisDsTwo
/**
* @author ZSL
* @ClassName MybatisDsTwo
* @description
* @date 2019/8/10
*
*@MapperScan(basePackages = "com.zsl.mybatis02.mapper2",
* sqlSessionFactoryRef = "sqlSessionFactoryTwo",
* sqlSessionTemplateRef = "sqlSessionTemplateTwo")
*
*配置类中要扫描的包是com.zsl.mybatis02.mapper2,该包下的接口操作的是数据源deTwo中的数据
*
*sqlSessionFactoryRef:指定该数据源在Mybaits中对应的SqlSessionFactory
*
* sqlSessionTemplateRef:指定该数据源在Mybaits中对应的SqlSessionTemplate
*
* 在MybatisDsTwo这个Mybatis配置类中提供SqlSessionFactory和SqlSessionTemplate并指定数据源dsTwo
*
*SqlSessionTemplate是Mybaits-Spring中的类,根据SqlSessionFactory去创建的
*
*/
@Configuration
@MapperScan(basePackages = "com.zsl.mybatis02.mapper2",sqlSessionFactoryRef = "sqlSessionFactoryTwo",sqlSessionTemplateRef = "sqlSessionTemplateTwo")
public class MybatisDsTwo {
@Resource(name = "dsTwo")
DataSource dataSource;
@Bean
SqlSessionFactory sqlSessionFactoryTwo() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
SqlSessionFactory object = factoryBean.getObject();
return object;
}
@Bean
SqlSessionTemplate sqlSessionTemplateTwo() throws Exception {
return new SqlSessionTemplate(sqlSessionFactoryTwo());
}
}
pojo
/**
* @author ZSL
* @ClassName Dept
* @description
* @date 2019/8/10
*/
public class Dept {
private Integer deptno;
private String dname;
private String loc;
public Dept(Integer deptno, String dname, String loc) {
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
public Dept() {
}
@Override
public String toString() {
return "Dept{" +
"deptno=" + deptno +
", dname='" + dname + '\'' +
", loc='" + loc + '\'' +
'}';
}
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
/**
* @author ZSL
* @ClassName Emp
* @description
* @date 2019/8/10
*/
public class Emp {
private Integer id;
private String name;
private String subject;
private Integer grade;
public Emp(Integer id, String name, String subject, Integer grade) {
this.id = id;
this.name = name;
this.subject = subject;
this.grade = grade;
}
public Emp() {
}
@Override
public String toString() {
return "Emp{" +
"id=" + id +
", name='" + name + '\'' +
", subject='" + subject + '\'' +
", grade=" + grade +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public Integer getGrade() {
return grade;
}
public void setGrade(Integer grade) {
this.grade = grade;
}
}
Mapper接口
/**
* @author ZSL
* @ClassName IMapperOne
* @description
* @date 2019/8/10
*/
@Repository
public interface IMapperOne {
List<Emp> getAllEmp();
}
/**
* @author ZSL
* @ClassName IMapperOne
* @description
* @date 2019/8/10
*/
@Repository
public interface IMapperTwo {
List<Dept> getAllDept();
}
Xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zsl.mybatis02.mapper1.IMapperOne">
<select id="getAllEmp" resultType="com.zsl.mybatis02.pojo.Emp">
select * from emp;
</select>
<insert id="addEmp" parameterType="com.zsl.mybatis02.pojo.Emp">
insert into emp (username,address) values (#{username},#{address});
</insert>
<update id="updateEmpById" parameterType="com.zsl.mybatis02.pojo.Emp">
update user set emp=#{username},address=#{address} where id=#{id}
</update>
<delete id="deleteEmpById">
delete from emp where id=#{id}
</delete>
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zsl.mybatis02.mapper2.IMapperTwo">
<select id="getAllDept" resultType="com.zsl.mybatis02.pojo.Dept">
select * from dept;
</select>
</mapper>