springboot integrates atonikos step-by-step transactions

The first step is to introduce the jar package
		<!-- Introduce the parent project-->
		<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>
The second step creates a normal bean
		public class Emp {
			
			private Integer eid;
			private String ename;
			private String sex;
			
			get set method
		}
The third step is to hand over the two data sources to atonikos for transaction management
		The first data source 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 //Register to the spring container
			@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") //Register the custom data source to the spring container
				@Primary //default data source
				@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 //default data source
				public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource")DataSource dataSource)
					throws Exception{
					SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
					bean.setDataSource(dataSource);
					return bean.getObject();
				}
				
				
				@Bean(name="test1SqlSessionTemplate")
				@Primary //default data source
				public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory")SqlSessionFactory sqlSessionFactory)throws Exception{
					return new SqlSessionTemplate(sqlSessionFactory);
				}
				
			}

		The second data source 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 //Register to the spring container
			@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") //Register the custom data source to the spring container
				@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);
				}
				
			}
The fourth step is to write the mapper interface
                	//Operate the mapper interface of the Test01 data source
			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 {
				//Add to
				@Insert(" insert into emp values(null,#{ename},#{sex}) ")
				void saveEmp(Emp emp);
				//delete
				@Delete(" delete from emp where eid = #{eid} ")
				void deleteEmp(@Param("eid")Integer eid);
				//Revise
				@Update("update emp set ename = #{ename},sex=#{sex} where eid = #{eid}")
				void updateEmp(Emp emp);
				//Inquire
				@Select("select * from emp")
				List<Emp> selectEmp();
				//Query Emp data according to id
				@Select(" select * from emp where eid = #{eid} ")
				Emp getbyid(@Param("eid")Integer eid);
				
				
			}

		Manipulate the mapper interface of the Test02 data source
			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 {
				//Add to
				@Insert(" insert into emp values(null,#{ename},#{sex}) ")
				void saveEmp(Emp emp);
				//delete
				@Delete(" delete from emp where eid = #{eid} ")
				void deleteEmp(@Param("eid")Integer eid);
				//Revise
				@Update("update emp set ename = #{ename},sex=#{sex} where eid = #{eid}")
				void updateEmp(Emp emp);
				//Inquire
				@Select("select * from emp")
				List<Emp> selectEmp();
				//Query Emp data according to id
				@Select(" select * from emp where eid = #{eid} ")
				Emp getbyid(@Param("eid")Integer eid);
				
				
			}
The fifth step is to write the service corresponding to the mapper interface 
			The service corresponding to the Test01 mapper interface
			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;
				//Add to
				public String saveEmp1(Emp emp){
					empmapperTest01.saveEmp(emp);
					return "success1";
				}
				//delete
				public String deleteEmp1(Integer eid){
					empmapperTest01.deleteEmp(eid);
					return "success1";
				}
				//Revise
				public String updateEmp1(Emp emp){
					empmapperTest01.updateEmp(emp);
					return "success1";
				}
				//Inquire
				public Emp getbyid1(Integer eid){
					return empmapperTest01.getbyid(eid);
				}
				// query all
				public List<Emp> EmpList1(){
					return empmapperTest01.selectEmp();
				}
			}

			The service corresponding to the Test02 mapper interface
			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;
				//Add to
				public String saveEmp2(Emp emp){
					empmapperTest02.saveEmp(emp);
					return "success1";
				}
				//delete
				public String deleteEmp2(Integer eid){
					empmapperTest02.deleteEmp(eid);
					return "success1";
				}
				//Revise
				public String updateEmp2(Emp emp){
					empmapperTest02.updateEmp(emp);
					return "success1";
				}
				//Inquire
				public Emp getbyid2(Integer eid){
					return empmapperTest02.getbyid(eid);
				}
				// query all
				public List<Emp> EmpList2(){
					return empmapperTest02.selectEmp();
				}
			}
The sixth step is the control layer 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;
			
			//**************************** Operation Test01 database ******************** ****************************************
			//Add EmpTest1
			@RequestMapping("/saveEmp1")
			@ResponseBody
			public String saveEmp1(Emp emp){
				empServiceTest01.saveEmp1(emp);
				return "success1";
			}
			//Modify EmpTest1
			@RequestMapping("/updateEmp1")
			@ResponseBody
			public String updateEmp1(Emp emp){
				empServiceTest01.updateEmp1(emp);
				return "success1";
			}
			// delete EmpTest1
			@RequestMapping("/deleteEmp1")
			@ResponseBody
			public String deleteEmp1(Integer eid){
				empServiceTest02.deleteEmp2(eid);
				empServiceTest01.deleteEmp1(eid);
				return "success1";
			}
			
			//Query EmpTest1 according to eid
			@RequestMapping("/getbyid1")
			@ResponseBody
			public Emp getbyid1(Integer eid){
				System.err.println(eid);
				return empServiceTest01.getbyid1(eid);
			}
			
			//Query all users EmpTest1
			@RequestMapping("/EmpList1")
			@ResponseBody
			public List<Emp> EmpList1(){
				return empServiceTest01.EmpList1();
			}
			
			//**************************** Operation Test02 database ******************** ****************************************
			//Add EmpTest2
				@RequestMapping("/saveEmp2")
				@ResponseBody
				public String saveEmp2(Emp emp){
					empServiceTest02.saveEmp2(emp);
					return "success2";
				}
				//Modify EmpTest2
				@RequestMapping("/updateEmp2")
				@ResponseBody
				public String updateEmp2(Emp emp){
					empServiceTest02.updateEmp2(emp);
					return "success2";
				}
				//delete EmpTest2
				@RequestMapping("/deleteEmp2")
				@ResponseBody
				public String deleteEmp2(Integer eid){
					empServiceTest02.deleteEmp2(eid);
					return "success2";
				}
				
				//Query EmpTest2 according to eid
				@RequestMapping("/getbyid2")
				@ResponseBody
				public Emp getbyid2(Integer eid){
					System.err.println(eid);
					return empServiceTest02.getbyid2(eid);
				}
				
				//Query all users EmpTest2
				@RequestMapping("/EmpList2")
				@ResponseBody
				public List<Emp> EmpList2(){
					return empServiceTest02.EmpList2();
				}	
		}
The seventh step starts the springboot mybatis atonikos project
		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;
		//Scan the controller control layer
		@ComponentScan(basePackages={
				"com.springboot_mybatis_ManyDatasource.controller",
				"com.springboot_mybatis_ManyDatasource.Datasource",
				"com.springboot_mybatis_ManyDatasource.Test01Service",
				"com.springboot_mybatis_ManyDatasource.Test02Service"})
		//Scan the mapper interface under the dao package
		@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);
			}
			
		}







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324720027&siteId=291194637