Talking about multi-data sources, generally to solve those problems, master-slave mode or business complex need to connect different sub-Libraries to support the business. Our project is the latter model, the Internet to find a lot, mostly based on JPA to do a long data source solution, or is the old spring Multi-data source solution, but also some of the use of AOP dynamic switching, feeling a little complex, in fact, I just want to find a simple majority supported by it, Two hours of tossing and finishing out for your reference.
Configuration file
POM package is not affixed to the relatively simple dependency on the dependency, mainly the database side of the configuration:
Mybatis.config-locations=classpath:mybatis/mybatis-config.xml
Spring.datasource.test1.driverClassName = Com.mysql.jdbc.Driver
Spring.datasource.test1.url = Jdbc:mysql://localhost:3306/test1?useunicode=true&characterencoding=utf-8
Spring.datasource.test1.username = root
Spring.datasource.test1.password = root
Spring.datasource.test2.driverClassName = Com.mysql.jdbc.Driver
Spring.datasource.test2.url = Jdbc:mysql://localhost:3306/test2?useunicode=true&characterencoding=utf-8
Spring.datasource.test2.username = root
Spring.datasource.test2.password = root
A test1 library and a test2 library, where the test1-bit main library, in the process of use must be set up the main library, or will be error.
Data Source Configuration
@Configuration
@MapperScan (basepackages = "Com.neo.mapper.test1", Sqlsessiontemplateref = "Test1sqlsessiontemplate")
public class Datasource1config {
@Bean (name = "Test1datasource")
@ConfigurationProperties (prefix = "Spring.datasource.test1")
@Primary
Public DataSource Testdatasource () {
Return Datasourcebuilder.create (). build ();
}
@Bean (name = "Test1sqlsessionfactory")
@Primary
Public Sqlsessionfactory testsqlsessionfactory (@Qualifier ("Test1datasource") DataSource DataSource) throws Exception {
Sqlsessionfactorybean bean = new Sqlsessionfactorybean ();
Bean.setdatasource (DataSource);
Bean.setmapperlocations (New Pathmatchingresourcepatternresolver (). Getresources ("classpath:mybatis/mapper/test1/ *.xml "));
return Bean.getobject ();
}
@Bean (name = "Test1transactionmanager")
@Primary
Public Datasourcetransactionmanager Testtransactionmanager (@Qualifier ("Test1datasource") DataSource DataSource) {
return new Datasourcetransactionmanager (DataSource);
}
@Bean (name = "Test1sqlsessiontemplate")
@Primary
Public Sqlsessiontemplate testsqlsessiontemplate (@Qualifier ("Test1sqlsessionfactory") sqlsessionfactory Sqlsessionfactory) throws Exception {
return new Sqlsessiontemplate (sqlsessionfactory);
}
}
The most important place is this one, layer by layer, first create DataSource, create Sqlsessionfactory in Create transaction, and finally wrap to sqlsessiontemplate. It is necessary to make the mapper file address of the library and the library to the layer code.
@MapperScan (basepackages = "Com.neo.mapper.test1", Sqlsessiontemplateref = "Test1sqlsessiontemplate")
This note refers to scanning the DAO layer and injecting the specified sqlsessiontemplate into the DAO layer. All @bean must be specified correctly by name.
DAO layer and XML layer
The DAO layer and XML need to be divided in different directories according to libraries authoring, for example: Test1 Library DAO layer under Com.neo.mapper.test1 package, Test2 Library in Com.neo.mapper.test1.
Public interface User1mapper {
List<userentity> GetAll ();
Userentity GetOne (Long ID);
void Insert (userentity user);
void update (userentity user);
void Delete (Long ID);
}
XML layer
<?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.neo.mapper.test1.User1Mapper" >
<resultmap id= "Baseresultmap" type= "com.neo.entity.UserEntity" >
<id column= "id" property= "id" jdbctype= "BIGINT"/>
<result column= "UserName" property= "UserName" jdbctype= "VARCHAR"/>
<result column= "PassWord" property= "PassWord" jdbctype= "VARCHAR"/>
<result column= "User_sex" property= "Usersex" javatype= "Com.neo.enums.UserSexEnum"/>
<result column= "Nick_name" property= "nickname" jdbctype= "VARCHAR"/>
</resultMap>
<sql id= "Base_column_list" >
ID, UserName, PassWord, User_sex, Nick_name
</sql>
<select id= "GetAll" resultmap= "Baseresultmap" >
SELECT
<include refid= "Base_column_list"/>
From users
</select>
<select id= "GetOne" parametertype= "Java.lang.Long" resultmap= "Baseresultmap" >
SELECT
<include refid= "Base_column_list"/>
From users
WHERE id = #{id}
</select>
<insert id= "Insert" parametertype= "com.neo.entity.UserEntity" >
INSERT into
Users
(Username,password,user_sex)
VALUES
(#{username}, #{password}, #{usersex})
</insert>
<update id= "Update" parametertype= "Com.neo.entity.UserEntity" >
UPDATE
Users
SET
<if test= "UserName! = null" >username = #{username},</if>
<if test= "PassWord! = null" >password = #{password},</if>
Nick_name = #{nickname}
WHERE
id = #{id}
</update>
<delete id= "Delete" parametertype= "Java.lang.Long" >
DELETE from
Users
WHERE
ID =#{id}
</delete>
</mapper>
Test
The test can be springboottest or placed in a controller, where only the controller layer is used.
@RestController
public class Usercontroller {
@Autowired
Private User1mapper User1mapper;
@Autowired
Private User2mapper User2mapper;
@RequestMapping ("/getusers")
Public list<userentity> getusers () {
List<userentity> Users=user1mapper.getall ();
return users;
}
@RequestMapping ("/getuser")
Public userentity GetUser (Long ID) {
Userentity User=user2mapper.getone (ID);
return user;
}
@RequestMapping ("/add")
public void Save (userentity user) {
User2mapper.insert (user);
}
@RequestMapping (value= "Update")
public void Update (userentity user) {
User2mapper.update (user);
}
@RequestMapping (value= "/delete/{id}")
public void Delete (@PathVariable ("id") Long ID) {
User1mapper.delete (ID);
}
}
Example code:
Https://github.com/ityouknow/spring-boot-examples
Springboot (vii): Springboot + MyBatis solution for multiple data sources