A pure Smile
Source: http://www.ityouknow.com/
All rights reserved, welcome to keep the original link to reprint:)
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.
No more nonsense, just go straight to the code.
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.xmlSpring.datasource.test1.driverClassName= com.mysql.jdbc.driverspring.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-8spring.datasource.test2.username = Rootspring.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")PublicClassDatasource1config{@Bean(Name="Test1datasource")@ConfigurationProperties(Prefix="Spring.datasource.test1")@PrimaryPublicDataSourceTestdatasource(){ReturnDatasourcebuilder.Create().Build();}@Bean(Name="Test1sqlsessionfactory")@PrimaryPublicSqlsessionfactoryTestsqlsessionfactory(@Qualifier("Test1datasource")DataSourceDataSource)ThrowsException{SqlsessionfactorybeanBean=NewSqlsessionfactorybean();Bean.Setdatasource(DataSource);Bean.Setmapperlocations(NewPathmatchingresourcepatternresolver().Getresources("Classpath:mybatis/mapper/test1/*.xml"));ReturnBean.GetObject();}@Bean(Name="Test1transactionmanager")@PrimaryPublicDatasourcetransactionmanagerTesttransactionmanager(@Qualifier("Test1datasource")DataSourceDataSource){ReturnNewDatasourcetransactionmanager(DataSource @Bean (name = " Test1sqlsessiontemplate ") @Primary public Sqlsessiontemplate testsqlsessiontemplate ( @Qualifier ( "test1sqlsessionfactory" ) sqlsessionfactory span class= "n" >sqlsessionfactory) throws exception {return new sqlsessiontemplate ( sqlsessionfactory} /span>
The most important place is this one, layer by layer, first create DataSource, create Sqlsessionfactory in Create transaction, and finally wrap to sqlsessiontemplate. Where the mapper file address of the library needs to be developed, and the library-to-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
need to 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< Span class= "O" > (); userentity getone (long id); void insert (userentity user< Span class= "O" >); void update (userentity user< Span class= "O" >); void delete (long id /span>
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" ><mapperNamespace="Com.neo.mapper.test1.User1Mapper"><resultmapId="Baseresultmap"Type="Com.neo.entity.UserEntity"><idcolumn="id"property="id"Jdbctype="BIGINT"/><resultcolumn="UserName"property="UserName"Jdbctype="VARCHAR"/><resultcolumn="PassWord"property="PassWord"Jdbctype="VARCHAR"/><resultcolumn="User_sex"property="Usersex"Javatype="Com.neo.enums.UserSexEnum"/><resultcolumn="Nick_name"property="Nickname"Jdbctype="VARCHAR"/></resultMap><sqlId="Base_column_list"> ID, userName, PassWord, User_sex, Nick_name</sql><selectId="GetAll"resultmap="Baseresultmap"> SELECT<includeRefid="Base_column_list"/> from users</select><selectId="GetOne"Parametertype="Java.lang.Long"resultmap="Baseresultmap"> SELECT<includeRefid="Base_column_list"/> from users WHERE id = #{id}</select><insertId="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 put in a controller, where only the controller layer can be used
@RestControllerPublicClassUsercontroller{@AutowiredPrivateUser1mapperUser1mapper;@AutowiredPrivateUser2mapperUser2mapper;@RequestMapping("/getusers")PublicList<Userentity>Getusers(){List<Userentity>Users=User1mapper.GetAll();ReturnUsers;}@RequestMapping("/getuser")PublicUserentityGetUser(LongId){UserentityUser=User2mapper.GetOne(Id);ReturnUser;}@RequestMapping("/add")PublicvoidSave(UserentityUser){User2mapper.Insert(User);}@RequestMapping(Value="Update")PublicvoidUpdate(Userentityuser) {user2mapper. Update (user @RequestMapping (value= "/delete/{id}" ) public void ( @PathVariable ( "id" long id) {user1mapper delete (id} } /span>
The last source address is here Spring-boot-mybatis-mulidatasource
Spring+mybatis minimal multiple data source configuration