In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the method of tk-mybatis integrating springBoot using two data sources". In daily operations, I believe that many people have doubts about what is the method of tk-mybatis integrating springBoot using two data sources. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what is the method of tk-mybatis integrating springBoot using two data sources". Next, please follow the editor to study!
According to the functional requirements of the project, we need to interface with the third-party common library, and we need to operate on the common library. Because we do not want to use native jdbc, we use mybaits for multi-data configuration.
There are a lot of online materials simply using mybaits for multi-data configuration, but in order to facilitate the development in the early stage, tk-mybaits is adopted. There is no relevant material about tk-mybaits multi-data source configuration, and the official does not provide the relevant function description, so finally use the strategy used by mybatis+tk-mybaits to implement the multi-data source configuration function. The specific code is as follows
I. Configure connection Information # Development Runtime Database spring.datasource.local.url=jdbc:mysql://0.0.0.0:3306/zhddspring.datasource.local.username=rootspring.datasource.local.password=spring.datasource.local.driver-class-name=com.mysql.jdbc.Driverspring.datasource.local.druid.initial-size=1spring.datasource.local.druid.min-idle=1spring.datasource.local.druid.max-active=20spring.datasource.local.druid.test-on-borrow=truespring.datasource.local.druid.stat-view- Servlet.allow=true # third-party docking general database spring.datasource.public.url=jdbc:mysql://0.0.0.0:3306/zhddspring.datasource.public.username=rootspring.datasource.public.password=spring.datasource.public.driver-class-name=com.mysql.jdbc.Driverspring.datasource.public.druid.initial-size=1spring.datasource.public.druid.min-idle=1spring.datasource.public.druid.max-active=20spring.datasource.public.druid.test-on-borrow=truespring.datasource.public.druid.stat-view-servlet.allow=true II. Configure DataSource data sources
Note that one of the two data sources is the primary data source, and an @ Primary annotation needs to be added, and there can only be one. There is no additional research on more than 2 data sources, but it should be possible to configure multiple data sources with mybatis.
1. Data Source 1 configuration
Package com.hiynn.core.mybatis; import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.jdbc.datasource.DataSourceTransactionManager Import javax.sql.DataSource; / * Development runtime environment database * / @ Configurationpublic class LocalDataSourceConfig {@ Bean (name = "localDataSource") @ ConfigurationProperties (prefix = "spring.datasource.local") @ Primary public DataSource localDataSource () {return DruidDataSourceBuilder.create (). Build ();} @ Bean (name = "localTransactionManager") @ Primary public DataSourceTransactionManager mysqlTransactionManager () {return new DataSourceTransactionManager (localDataSource ()) } @ Bean (name = "localSqlSessionFactory") @ Primary public SqlSessionFactory mysqlSqlSessionFactory (@ Qualifier ("localDataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean (); sessionFactory.setDataSource (dataSource); / / if you do not use xml to configure mapper, you can omit the following line of mapper location configuration. SessionFactory.setMapperLocations (new PathMatchingResourcePatternResolver () .getResources ("classpath*:mapper/*.xml")); return sessionFactory.getObject ();}}
two。 Data Source 2 configuration
Package com.hiynn.core.mybatis; import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource / * Public Universal Database * / @ Configurationpublic class PublicDataSourceConfig {@ Bean (name = "publicDataSource") @ ConfigurationProperties (prefix = "spring.datasource.public") publicDataSource publicDataSource () {return DruidDataSourceBuilder.create () .build ();} @ Bean (name = "publicTransactionManager") public DataSourceTransactionManager mysqlTransactionManager () {return new DataSourceTransactionManager (publicDataSource ()) } @ Bean (name = "publicSqlSessionFactory") publicSqlSessionFactory mysqlSqlSessionFactory (@ Qualifier ("publicDataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean (); sessionFactory.setDataSource (dataSource); / / if you do not use xml to configure mapper, you can omit the following line of mapper location configuration. SessionFactory.setMapperLocations (new PathMatchingResourcePatternResolver () .getResources ("classpath*:mapper2/*.xml")); return sessionFactory.getObject ();}} III. Application configuration package com.hiynn.core; / / pay special attention to the following is tk.MapperScan import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.ServletComponentScan;import org.springframework.boot.web.servlet.support.SpringBootServletInitializer Import org.springframework.transaction.annotation.EnableTransactionManagement;import springfox.documentation.swagger2.annotations.EnableSwagger2;import tk.mybatis.spring.annotation.MapperScan / * @ author zz * @ date 2018-7-2 * / @ ServletComponentScan@SpringBootApplication@EnableTransactionManagement@EnableSwagger2@MapperScan (basePackages = "com.hiynn.core.**.mapper", sqlSessionFactoryRef = "localSqlSessionFactory") @ org.mybatis.spring.annotation.MapperScan (basePackages = "com.hiynn.core.**.mapper2", sqlSessionFactoryRef = "publicSqlSessionFactory") public class Application extends SpringBootServletInitializer {public static void main (String [] args) {SpringApplication.run (Application.class, args);}} IV. Mapper code structure
Add:
First, to solve the problem of automatic mapping of hump naming in mybatis database and printing of sql statements, which is realized by reading configuration files. Both data sources need to be set up
@ Bean (name = "localSqlSessionFactory") @ Primary public SqlSessionFactory mysqlSqlSessionFactory (@ Qualifier ("localDataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean (); sessionFactory.setDataSource (dataSource); / / add mybatis configuration file reading function more sessionFactory.setConfigLocation (new PathMatchingResourcePatternResolver (). GetResource ("mybatis-config.xml")) / / if you do not use xml to configure mapper, you can omit the following line of mapper location configuration. SessionFactory.setMapperLocations (new PathMatchingResourcePatternResolver () .getResources ("classpath*:mapper/*.xml")); return sessionFactory.getObject ();}
Configure the PageHelper paging plug-in and configure it uniformly in application.properties
Database dialect specifies which database dialect pagehelper.helperDialect=mysql# paging parameters rationalize pageNumpages (when more than the total), the last page of pagehelper.reasonable=truepagehelper.supportMethodsArguments=truepagehelper.params=count=countSql will be queried so far, and the study on "tk-mybatis integrates springBoot using two data sources is over" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.