第一步 引入 jar 包
<!-- 引入父类工程 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency>第二步 创建普通的 bean
public class Emp { private Integer eid; private String ename; private String sex; get set 方法 }第三步将两个 数据源交给 atonikos 进行事务管理
第一个数据源 test01 package com.springboot_mybatis_ManyDatasource.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.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource; @Configuration //注册到 spring 容器中 @MapperScan(basePackages="com.springboot_mybatis_ManyDatasource.Test01Mapper",sqlSessionFactoryRef="test1SqlSessionFactory") public class DataSource1Config { @Value("${spring.datasource.test01.url}") private String url; @Value("${spring.datasource.test01.username}") private String password; @Value("${spring.datasource.test01.password}") private String username; @Bean(name="test1DataSource") //将自定义的数据源注册到 spring 容器中 @Primary //默认数据源 @ConfigurationProperties(prefix="spring.datasource.test01") public DataSource testDataSource(){ MysqlXADataSource mysqlXADataSource = new MysqlXADataSource(); mysqlXADataSource.setURL(url); mysqlXADataSource.setPassword(password); mysqlXADataSource.setUser(username); System.err.println(url); AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean(); xaDataSource.setXaDataSource(mysqlXADataSource); xaDataSource.setUniqueResourceName("test1DataSource"); return xaDataSource; } @Bean(name="test1SqlSessionFactory") @Primary //默认数据源 public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource")DataSource dataSource) throws Exception{ SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); return bean.getObject(); } @Bean(name="test1SqlSessionTemplate") @Primary //默认数据源 public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory")SqlSessionFactory sqlSessionFactory)throws Exception{ return new SqlSessionTemplate(sqlSessionFactory); } } 第二个数据源 test02 package com.springboot_mybatis_ManyDatasource.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.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource; @Configuration //注册到 spring 容器中 @MapperScan(basePackages="com.springboot_mybatis_ManyDatasource.Test02Mapper",sqlSessionFactoryRef="test2SqlSessionFactory") public class DataSource2Config { @Value("${spring.datasource.test02.url}") private String url; @Value("${spring.datasource.test02.username}") private String password; @Value("${spring.datasource.test02.password}") private String username; @Bean(name="test2DataSource") //将自定义的数据源注册到 spring 容器中 @ConfigurationProperties(prefix="spring.datasource.test02") public DataSource testDataSource(){ MysqlXADataSource mysqlXADataSource = new MysqlXADataSource(); mysqlXADataSource.setURL(url); mysqlXADataSource.setPassword(password); mysqlXADataSource.setUser(username); AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean(); xaDataSource.setXaDataSource(mysqlXADataSource); xaDataSource.setUniqueResourceName("test2DataSource"); return xaDataSource; } @Bean(name="test2SqlSessionFactory") public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource")DataSource dataSource) throws Exception{ SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); return bean.getObject(); } @Bean(name="test2SqlSessionTemplate") public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory")SqlSessionFactory sqlSessionFactory)throws Exception{ return new SqlSessionTemplate(sqlSessionFactory); } }第四步 写 mapper 接口
//操作 Test01 数据源的 mapper 接口 package com.springboot_mybatis_ManyDatasource.Test01Mapper; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import org.springframework.data.repository.query.Param; import com.springboot_mybatis_ManyDatasource.bean.Emp; public interface EmpmapperTest01 { //添加 @Insert(" insert into emp values(null,#{ename},#{sex}) ") void saveEmp(Emp emp); //删除 @Delete(" delete from emp where eid = #{eid} ") void deleteEmp(@Param("eid")Integer eid); //修改 @Update("update emp set ename = #{ename},sex=#{sex} where eid = #{eid}") void updateEmp(Emp emp); //查询 @Select("select * from emp") List<Emp> selectEmp(); //根据 id 查询 Emp 数据 @Select(" select * from emp where eid = #{eid} ") Emp getbyid(@Param("eid")Integer eid); } 操作 Test02 数据源的 mapper 接口 package com.springboot_mybatis_ManyDatasource.Test02Mapper; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import org.springframework.data.repository.query.Param; import com.springboot_mybatis_ManyDatasource.bean.Emp; public interface EmpmapperTest02 { //添加 @Insert(" insert into emp values(null,#{ename},#{sex}) ") void saveEmp(Emp emp); //删除 @Delete(" delete from emp where eid = #{eid} ") void deleteEmp(@Param("eid")Integer eid); //修改 @Update("update emp set ename = #{ename},sex=#{sex} where eid = #{eid}") void updateEmp(Emp emp); //查询 @Select("select * from emp") List<Emp> selectEmp(); //根据 id 查询 Emp 数据 @Select(" select * from emp where eid = #{eid} ") Emp getbyid(@Param("eid")Integer eid); }第五步写 mapper 接口对应的 service
Test01 mapper 接口所对应的 service package com.springboot_mybatis_ManyDatasource.Test01Service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.springboot_mybatis_ManyDatasource.Test01Mapper.EmpmapperTest01; import com.springboot_mybatis_ManyDatasource.bean.Emp; @Service public class EmpServiceTest01 { @Autowired private EmpmapperTest01 empmapperTest01; //添加 public String saveEmp1(Emp emp){ empmapperTest01.saveEmp(emp); return "success1"; } //删除 public String deleteEmp1(Integer eid){ empmapperTest01.deleteEmp(eid); return "success1"; } //修改 public String updateEmp1(Emp emp){ empmapperTest01.updateEmp(emp); return "success1"; } //查询 public Emp getbyid1(Integer eid){ return empmapperTest01.getbyid(eid); } //查询所有 public List<Emp> EmpList1(){ return empmapperTest01.selectEmp(); } } Test02 mapper 接口所对应的 service package com.springboot_mybatis_ManyDatasource.Test02Service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.springboot_mybatis_ManyDatasource.Test02Mapper.EmpmapperTest02; import com.springboot_mybatis_ManyDatasource.bean.Emp; @Service public class EmpServiceTest02 { @Autowired private EmpmapperTest02 empmapperTest02; //添加 public String saveEmp2(Emp emp){ empmapperTest02.saveEmp(emp); return "success1"; } //删除 public String deleteEmp2(Integer eid){ empmapperTest02.deleteEmp(eid); return "success1"; } //修改 public String updateEmp2(Emp emp){ empmapperTest02.updateEmp(emp); return "success1"; } //查询 public Emp getbyid2(Integer eid){ return empmapperTest02.getbyid(eid); } //查询所有 public List<Emp> EmpList2(){ return empmapperTest02.selectEmp(); } }第六步控制层 controller
package com.springboot_mybatis_ManyDatasource.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.springboot_mybatis_ManyDatasource.Test01Service.EmpServiceTest01; import com.springboot_mybatis_ManyDatasource.Test02Service.EmpServiceTest02; import com.springboot_mybatis_ManyDatasource.bean.Emp; @Controller public class EmpController { @Autowired private EmpServiceTest01 empServiceTest01; @Autowired private EmpServiceTest02 empServiceTest02; //************************* 操作 Test01 数据库 *********************************************************** //添加 EmpTest1 @RequestMapping("/saveEmp1") @ResponseBody public String saveEmp1(Emp emp){ empServiceTest01.saveEmp1(emp); return "success1"; } //修改 EmpTest1 @RequestMapping("/updateEmp1") @ResponseBody public String updateEmp1(Emp emp){ empServiceTest01.updateEmp1(emp); return "success1"; } //删除 EmpTest1 @RequestMapping("/deleteEmp1") @ResponseBody public String deleteEmp1(Integer eid){ empServiceTest02.deleteEmp2(eid); empServiceTest01.deleteEmp1(eid); return "success1"; } //根据 eid 查询 EmpTest1 @RequestMapping("/getbyid1") @ResponseBody public Emp getbyid1(Integer eid){ System.err.println(eid); return empServiceTest01.getbyid1(eid); } //查询所有的用户 EmpTest1 @RequestMapping("/EmpList1") @ResponseBody public List<Emp> EmpList1(){ return empServiceTest01.EmpList1(); } //************************* 操作 Test02 数据库 *********************************************************** //添加 EmpTest2 @RequestMapping("/saveEmp2") @ResponseBody public String saveEmp2(Emp emp){ empServiceTest02.saveEmp2(emp); return "success2"; } //修改 EmpTest2 @RequestMapping("/updateEmp2") @ResponseBody public String updateEmp2(Emp emp){ empServiceTest02.updateEmp2(emp); return "success2"; } //删除 EmpTest2 @RequestMapping("/deleteEmp2") @ResponseBody public String deleteEmp2(Integer eid){ empServiceTest02.deleteEmp2(eid); return "success2"; } //根据 eid 查询 EmpTest2 @RequestMapping("/getbyid2") @ResponseBody public Emp getbyid2(Integer eid){ System.err.println(eid); return empServiceTest02.getbyid2(eid); } //查询所有的用户 EmpTest2 @RequestMapping("/EmpList2") @ResponseBody public List<Emp> EmpList2(){ return empServiceTest02.EmpList2(); } }第七步 启动 springboot mybatis atonikos 项目
package com.springboot_mybatis_ManyDatasource.RunData; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; //扫描 controller 控制层 @ComponentScan(basePackages={ "com.springboot_mybatis_ManyDatasource.controller", "com.springboot_mybatis_ManyDatasource.Datasource", "com.springboot_mybatis_ManyDatasource.Test01Service", "com.springboot_mybatis_ManyDatasource.Test02Service"}) //扫描 dao 包下的 mapper 接口 @MapperScan(basePackages={ "com.springboot_mybatis_ManyDatasource.Test01Mapper", "com.springboot_mybatis_ManyDatasource.Test02Mapper"}) @EnableAutoConfiguration public class RunData { public static void main(String[] args) { SpringApplication.run(RunData.class, args); } }