This tutorial is more practical. Programmers directly adds copy code to their own projects and can use it by simply modifying and modifying it. However, springboot and mybatis are not introduced here. If any readers want to know, they can leave me a message and continue to pay attention. I will update it slowly in the future. (Black area code section, Android phone can manually swipe left to view all codes)
In fact, the integration is very simple. If you use gradle, add it in the build.gradle file.
compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.1')If you use maven, add it in the pom.xml file
Single library configuration:
After introduction, by default, Spring Boot will automatically configure a DataSource for us, which will search for the classpath for jar packages of H2, hsqldb and other in memory databases. If found, it will automatically configure a DataSource for the memory database.
If the relevant configuration parameters of spring.datasource.* are specified in application.yml or application.property , Spring Boot will use this configuration to create a DataSource.
Then, the SqlSessionFactoryBean and SqlSessionTemplate using the DataSource will be automatically created. Your Mappers will be automatically scanned, connected to SqlSessionTemplate, and registered to 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, please see DataSourceProperties
Multi-library configuration:
Due to business needs, the project needs to use multiple databases for business development at the same time:
First, we must customize the configuration of two data sources in application.property, one uses first.datasource.* and the other uses second.datasource.*. In order to let others see what library is connected at a glance, you can use database naming. For example, the user library, and you can use 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 am using Druid, too You can do not add the default second.datasource.url=jdbc:mysql://localhost/secondsecond.datasource.username=dbuser2second.datasource.password=dbpass2second.datasource.driver-class-name=com.mysql.jdbc.Driverse.datasource.type=com.alibaba.druid.pool.DruidDataSource
I directly upload 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 will be reported. The next class does not need to add @ConfigurationProperties(prefix = "first.datasource") // The prefix value must be the prefix of the corresponding property in application.properteis public DataSource userDataSource() { return DataSourceBuilder.create().build(); } @Bean public SqlSessionFactory userSqlSessionFactory(@Qualifier("userDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); //Add 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 configured above to return template; }}@Configuration@MapperScan(basePackages = {"com.airmi.server.dao"}, sqlSessionTemplateRef = "autoTestSqlSessionTemplate") public class AutoTestMybatisConfig { @Bean @ConfigurationProperties(prefix = "autotest.datasource") public DataSource autoTestDataSource() { return DataSourceBuilder.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 dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); //Add 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 means that when there are multiple implementation classes to inject in the same interface, it is not necessary to allow the autowire annotation to report an error. The official website requires that when multiple data sources, one datasource must be specified, and another datasource does not need to be added.
@Qualifier Injection by name is usually injected in one instance with multiple types (for example, there are multiple instances of DataSource type).
@MapperScan (basePackages = {"com.user.server.dao"}, sqlSessionTemplateRef = "userSqlSessionTemplate") basePackages is the package where the mapper is located, the instance to which sqlSessionTemplateRef is to be referenced.The user code structure is as follows:
Summarize
The above is the implementation method of Spring Boot integrating mybatis using multiple data sources introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!