使用SpringBoot配置多数据源的经验分享(springboot配置多数据源 mybatis)

网友投稿 386 2022-08-04


使用SpringBoot配置多数据源的经验分享(springboot配置多数据源 mybatis)

目录1. 引入jar包2. properties配置3. 分别配置两个数据源4. Dao目录5. Entity6. Mapper文件7. Controller测试8. 结果验证

1. 引入jar包

pom.xml文件

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.5.6

com.multi.datasource

demo

0.0.1-SNAPSHOT

demo

Demo project for Spring Boot

1.8

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-web

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.2

com.alibaba

druid

1.1.8

mysql

mysql-connector-java

org.projectlombok

lombok

1.16.22

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-maven-plugin

org.projectlombok

lombok

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.5.6

com.multi.datasource

demo

0.0.1-SNAPSHOT

demo

Demo project for Spring Boot

1.8

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-web

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.2

com.alibaba

druid

1.1.8

mysql

mysql-connector-java

org.projectlombok

lombok

1.16.22

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-maven-plugin

org.projectlombok

lombok

2. properties配置

分别准备两个数据源

server.port=18888

mybatis.mapper-locations=classpath:mapper/*.xml

my1.datasource.url=jdbc:mysql://10.0.0.125:3306/wyl?autoReconnect=true

my1.datasource.driverClassName=com.mysql.cj.jdbc.Driver

my1.datasource.username=root

my1.datasource.password=123456

my2.datasource.url=jdbc:mysql://10.0.0.160:3306/wyl?autoReconnect=true

my2.datasource.driverClassName=com.mysql.cj.jdbc.Driver

my2.datasource.username=root

my2.datasource.password=123456

3. 分别配置两个数据源

第一个数据源

package com.multi.datasource.config;

import com.alibaba.druid.pool.DruidDataSource;

import org.apache.ibatis.session.SqlSessionFactory;

import org.mybatis.spring.SqlSessionFactoryBean;

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.beans.factory.annotation.Value;

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;

@Configuration

@MapperScan(basePackages = My1DataSourceConfig.PACKAGE, sqlSessionFactoryRef = "my1SqlSessionFactory")

public class My1DataSourceConfig {

static final String PACKAGE = "com.multi.datasource.dao.my1";

static final String MAPPER_LOCATION = "classpath:mapper/*.xml";

@Value("${my1.datasource.url}")

private String url;

@Value("${my1.datasource.username}")

private String user;

@Value("${my1.datasource.password}")

private String password;

@Value("${my1.datasource.driverClassName}")

private String driverClass;

@Bean(name = "my1DataSource")

public DataSource my1DataSource() {

DruidDataSource dataSource = new DruidDataSource();

dataSource.setDriverClassName(driverClass);

dataSource.setUrl(url);

dataSource.setUsername(user);

dataSource.setPassword(password);

dataSource.setMaxWait(Integer.MAX_VALUE);

dataSource.setTestOnBorrow(true);

dataSource.setTestOnReturn(true);

dataSource.setTestWhileIdle(true);

return dataSource;

}

@Bean(name = "my1TransactionManager")

public DataSourceTransactionManager my1TransactionManager() {

return new DataSourceTransactionManager(my1DataSource());

}

@Bean(name = "my1SqlSessionFactory")

public SqlSessionFactory my1SqlSessionFactory(@Qualifier("my1DataSource") DataSource my1DataSource)

throws Exception {

final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();

sessionFactory.setDataSource(my1DataSource);

sessionFactory.setMapperLocations(

new PathMatchingResourcePatternResolver().getResources(My1DataSourceConfig.MAPPER_LOCATION));

return sessionFactory.getObject();

}

}

第二个数据源

package com.multi.datasource.config;

import com.alibaba.druid.pool.DruidDataSource;

import org.apache.ibatis.session.SqlSessionFactory;

import org.mybatis.spring.SqlSessionFactoryBean;

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.beans.factory.annotation.Value;

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;

@Configuration

@MapperScan(basePackages = My1DataSourceConfig.PACKAGE, sqlSessionFactoryRef = "my1SqlSessionFactory")

public class My1DataSourceConfig {

static final String PACKAGE = "com.multi.datasource.dao.my1";

static final String MAPPER_LOCATION = "classpath:mapper/*.xml";

@Value("${my1.datasource.url}")

private String url;

@Value("${my1.datasource.username}")

private String user;

@Value("${my1.datasource.password}")

private String password;

@Value("${my1.datasource.driverClassName}")

private String driverClass;

@Bean(name = "my1DataSource")

public DataSource my1DataSource() {

DruidDataSource dataSource = new DruidDataSource();

dataSource.setDriverClassName(driverClass);

dataSource.setUrl(url);

dataSource.setUsername(user);

dataSource.setPassword(password);

dataSource.setMaxWait(Integer.MAX_VALUE);

dataSource.setTestOnBorrow(true);

dataSource.setTestOnReturn(true);

dataSource.setTestWhileIdle(true);

return dataSource;

}

@Bean(name = "my1TransactionManager")

public DataSourceTransactionManager my1TransactionManager() {

return new DataSourceTransactionManager(my1DataSource());

}

@Bean(name = "my1SqlSessionFactory")

public SqlSessionFactory my1SqlSessionFactory(@Qualifier("my1DataSource") DataSource my1DataSource)

throws Exception {

final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();

sessionFactory.setDataSource(my1DataSource);

sessionFactory.setMapperLocations(

new PathMatchingResourcePatternResolver().getResources(My1DataSourceConfig.MAPPER_LOCATION));

return sessionFactory.getObject();

}

}

4. Dao目录

为了区分两个数据源,分别设置了不同的目录

http://

package com.multi.datasource.dao.my1;

import com.multi.datasource.entity.UserEntity;

import org.apache.ibatis.annotations.Mapper;

@Mapper

public interface Test1Mapper {

UserEntity query();

}

package com.multi.datasource.dao.my2;

import com.multi.datasource.entity.UserEntity;

import org.apache.ibatis.annotations.Mapper;

@Mapper

public interface Test2Mapper {

UserEntity query();

}

5. Entity

package com.multi.datasource.entity;

import lombok.Data;

@Data

public class UserEntity {

private String userName;

}

6. Mapper文件

从my1数据源查询

select user_name as userName from t_user

从my2数据源查询

select user_name as userName from t_user

7. Controller测试

package com.multi.datasource.controller;

import com.multi.datasource.dao.my1.Test1Mapper;

import com.multi.datasource.dao.my2.Test2Mapper;

import com.multi.datasource.entity.UserEntity;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController

public class TestController {

@Resource

private Test1Mapper test1Mapper;

@Resource

private Test2Mapper test2Mapper;

@RequestMapping("query")

public void query() {

UserEntity user1 = test1Mapper.query();

System.out.println("my1 dataSource:" + user1);

UserEntity user2 = test2Mapper.query();

System.out.println("my2 dataSource:" + user2);

}

}

两个数据源,对应的user_name分别是zhangsan和lisi

8. 结果验证

访问 http://localhost:18888/query,结果如下


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot实现阿里云短信发送的示例代码(springboot整合阿里云短信)
下一篇:Java中输入与输出的方法总结(java输入和输出)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~