Moment For Technology

Springboot + Mybatis multi-data source

Posted on Oct. 3, 2022, 4:30 a.m. by Amy Smith
Category: The back-end Tag: The back-end

Springboot + Mybatis multi-data source

This is the 15th day of my participation in Gwen Challenge

Test: multiple mysql connections, using Druid connection pool

1. Dependence, druid, web, lombok, mysql, mybatis

dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId scoperuntime/scope /dependency  dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId optionaltrue/optional /dependency dependency groupIdorg.mybatis.spring.boot/groupId  artifactId  mybatis - spring - the boot - starter  / artifactId   version  2.1.0  / version   / dependency  ! --Druid-- dependency groupIdcom.alibaba/groupId artifactIddruid-spring-boot-starter/artifactId The  version  1.1.10  / version   / dependency Copy the code

2. Application. properties Configure two data sources

#mysql spring.datasource.one.url=jdbc:mysql://localhost:3306/test? serverTimezone=UTC spring.datasource.one.username=root spring.datasource.one.password= spring.datasource.one.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.two.url=jdbc:mysql://localhost:3306/workflow? serverTimezone=UTC spring.datasource.two.username=root spring.datasource.two.password= spring.datasource.two.type=com.alibaba.druid.pool.DruidDataSourceCopy the code

3. Configure two data sources and two Mapper

MybatisConfigOne .java

import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import javax.annotation.Resource; import javax.sql.DataSource; @Configuration @MapperScan(basePackages = "com.mybatis.demo.mapper.mapper1", sqlSessionFactoryRef = "sqlSessionFactoryOne", sqlSessionTemplateRef = "sqlSessionTemplateOne") public class MybatisConfigOne { @Resource(name = "dsOne") DataSource dsOne; @Bean SqlSessionFactory sqlSessionFactoryOne() { SqlSessionFactory sessionFactory = null; SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dsOne); / / add the hump mapping org. Apache. Ibatis. Session. The Configuration Configuration = new org. Apache. Ibatis. Session. The Configuration (); configuration.setMapUnderscoreToCamelCase(true); bean.setConfiguration(configuration); try { sessionFactory = bean.getObject(); } catch (Exception e) { e.printStackTrace(); } return sessionFactory; } @Bean SqlSessionTemplate sqlSessionTemplateOne() { return new SqlSessionTemplate(sqlSessionFactoryOne()); }}Copy the code

MybatisConfigOne .java

import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.annotation.Resource; import javax.sql.DataSource; @Configuration @MapperScan(basePackages = "com.mybatis.demo.mapper.mapper2", sqlSessionFactoryRef = "sqlSessionFactoryTwo", sqlSessionTemplateRef = "sqlSessionTemplateTwo") public class MybatisConfigTwo { @Resource(name = "dsTwo") DataSource dsTwo; @Bean SqlSessionFactory sqlSessionFactoryTwo() { SqlSessionFactory sessionFactory = null; SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dsTwo); / / add the hump mapping org. Apache. Ibatis. Session. The Configuration Configuration = new org. Apache. Ibatis. Session. The Configuration (); configuration.setMapUnderscoreToCamelCase(true); bean.setConfiguration(configuration); try { sessionFactory = bean.getObject(); } catch (Exception e) { e.printStackTrace(); } return sessionFactory; } @Bean SqlSessionTemplate sqlSessionTemplateTwo() { return new SqlSessionTemplate(sqlSessionFactoryTwo()); }}Copy the code

Two Mappers are placed in different directories

import com.mybatis.demo.domain.Test;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface TestDao1 {
//public interface TestDao2 {
    @Select("select * from mybatis")
    ListTest listTest();

}
Copy the code

4. Data can be queried. Ok

5. Other issues:

1) In the case of multiple data sources, the hump mapping does not take effect and the field is null

In the case of multiple data sources, Spring does not know which data source to enable camel name for, and manually add it when configuring the data source, as shown below

@Bean SqlSessionFactory sqlSessionFactoryOne() { SqlSessionFactory sessionFactory = null; SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dsOne); / / add the hump mapping org. Apache. Ibatis. Session. The Configuration Configuration = new org. Apache. Ibatis. Session. The Configuration (); configuration.setMapUnderscoreToCamelCase(true); bean.setConfiguration(configuration); try { sessionFactory = bean.getObject(); } catch (Exception e) { e.printStackTrace(); } return sessionFactory; }Copy the code

Druid = druid; druid = jdbC-URL; druid = jdbC-URL;

@Configuration public class DataSourceConfig { @Bean @ConfigurationProperties(prefix = "spring.datasource.one") DataSource dsOne() { // return DruidDataSourceBuilder.create().build(); return DataSourceBuilder.create().build(); } @Bean @ConfigurationProperties(prefix = "spring.datasource.two") DataSource dsTwo() { // return DruidDataSourceBuilder.create().build(); return DataSourceBuilder.create().build(); }}Copy the code
Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.