Spring Boot integrates mybatis with multiple data sources. springmybatis
Preface
This tutorial is applicable to practical scenarios. You can simply add the copy code to your project for simple repair, modification, and usage. springboot and mybatis are not described here, if you want to learn more, you can leave a message for me and continue to pay attention to it. I will update it later. (For the code in the black area, you can manually move the Android phone to the left to view all the codes)
Integration
In fact, integration is very simple. If gradle is used, add it to the build. gradle file.
compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.1')
If maven is used, add it to the pom. xml file.
Single-Database Configuration:
After the introduction, by default, Spring Boot will automatically configure a DataSource for us. It will search for jar packages of memory databases such as H2 and hsqldb in classpath. If yes, A DataSource of the memory database is automatically configured.
In application. yml orapplication.property
Specifiedspring.datasource.*
Spring Boot will use this configuration to create a DataSource.
Then, the SqlSessionFactoryBean and SqlSessionTemplate that use the DataSource are automatically created. It will automatically scan your Mappers, connect to SqlSessionTemplate, and register it in the Spring context.
spring.datasource.url=jdbc:mysql://localhost/test spring.datasource.username=dbuser spring.datasource.password=dbpass spring.datasource.driver-class-name=com.mysql.jdbc.Driver
For more parameters, see performanceproperties.
Multi-Database Configuration:
Due to business needs, the project must use multiple databases for business development at the same time:
First, we must. property to customize the configuration of two data sources, one using first. datasource. *, the other uses second. datasource. * To help others see the connected database at a glance, you can use the database name, such as the user database, you can use the user. datasource. * When using multiple data sources, all necessary configurations cannot be omitted.
First. datasource. url = jdbc: mysql: // localhost/firstfirst. datasource. username = dbuser1first. datasource. password = dbpass1first. datasource. driver-class-name = com. mysql. jdbc. driverfirst. datasource. type = com. alibaba. druid. pool. druidDataSource // I use Druid, or I can add the default second. datasource. url = jdbc: mysql: // localhost/secondsecond. datasource. username = dbuser2second. datasource. password = dbpass2second. datasource. driver-class-name = com. mysql. jdbc. driversecond. datasource. type = com. alibaba. druid. pool. druidDataSource
Directly run the code. My approach is to create two data sources with two configuration classes:
@ Configuration @ MapperScan (basePackages = {"com. user. server. dao "}, sqlSessionTemplateRef =" userSqlSessionTemplate ") public class UserMybatisConfig {@ Bean (name =" userDataSource ") @ Primary // This annotation must be added. Otherwise, an error is reported, the next class does not need to be added @ ConfigurationProperties (prefix = "first. datasource ") // The prefix value must be application. the prefix of the attribute in properteis public DataSource userDataSource () {return performancebuilder. create (). build () ;}@ Bean public SqlSessionFactory userSqlSessionFactory (@ Qualifier ("userDataSource") DataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean (); bean. setDataSource (dataSource); // Add the XML directory ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver (); try {bean. setMapperLocations (resolver. getResources ("classpath *: com/user/server/dao/mapping /*. xml "); return bean. getObject ();} catch (Exception e) {e. printStackTrace (); throw new RuntimeException (e) ;}@ Bean public SqlSessionTemplate userSqlSessionTemplate (@ Qualifier ("userSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {SqlSessionTemplate template = new SqlSessionTemplate (sqlSessionFactory); // use the Factory return template configured above; }}@ Configuration @ MapperScan (basePackages = {"com. airmi. server. dao "}, sqlSessionTemplateRef =" autoTestSqlSessionTemplate ") public class AutoTestMybatisConfig {@ Bean @ ConfigurationProperties (prefix =" autotest. datasource ") public DataSource autoTestDataSource () {return performancebuilder. create (). build () ;}@ Bean public SqlSessionTemplate autoTestSqlSessionTemplate (@ Qualifier ("autoTestSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {SqlSessionTemplate template = new SqlSessionTemplate (sqlSessionFactory); return template ;} @ Bean public SqlSessionFactory autoTestSqlSessionFactory (@ Qualifier ("autoTestDataSource") DataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean (); bean. setDataSource (dataSource); // Add the XML directory ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver (); try {bean. setMapperLocations (resolver. getResources ("classpath *: com/airmi/server/dao/mapping /*. xml "); return bean. getObject ();} catch (Exception e) {e. printStackTrace (); throw new RuntimeException (e );}}}
@ Primary // This annotation indicates which one is selected by default when multiple implementation classes of the same interface can be injected, instead of making the autowire annotation report an error. When multiple data sources are required on the official website, one datasource must be specified, and another datasource is not added.
@ Qualifier Injection Based on the name. Generally, an injection is performed on multiple instances of the same type (for example, multiple instances of the DataSource type ).
@ MapperScan (basePackages = {"com. user. server. dao"}, sqlSessionTemplateRef = "userSqlSessionTemplate") indicates the package of the mapper, and sqlSessionTemplateRef indicates the instance to reference.
The user code structure is as follows:
Summary
The above section describes how Spring Boot integrates mybatis to use multiple data sources. I hope it will help you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!