SpringBoot-Mybatis-MultiDateSource

简单记录一下SpringBoot-Mybatis分包多数据源
只做了最简单的步骤
SpringBoot 1.5.19.RELEASE JAVA 1.8
2.0+版本区别,目前了解只优化了@Primary这个注解可以省略
目录结构
在这里插入图片描述
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.19.RELEASE</version>
		<relativePath />
	</parent>
	<groupId>com.lz</groupId>
	<artifactId>springboot</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>multiDBsource</name>
	<description>Spring Boot - Mybatis - multiDBsource</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.3</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
	</dependencies>
</project>

entity 实体类

package com.lz.entity;

public class User {
	private Integer id;
	private String name;
	//getter()  //setter

application.properties
我以自己的两个库为例: 1808 / 1808c
spring.datasource.1808为前缀(可自定义),在DB1SourceConfig.DataSource 中会通过注解
@ConfigurationProperties(prefix=“spring.datasource.1808”)来指定前缀

#db	1808
spring.datasource.1808.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.1808.url=jdbc:mysql://localhost:3306/1808
spring.datasource.1808.username=root
spring.datasource.1808.password=root

#db 1808c
spring.datasource.1808c.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.1808c.url=jdbc:mysql://localhost:3306/1808c
spring.datasource.1808c.username=root
spring.datasource.1808c.password=root

#控制台打印SQL语句
logging.level.com.lz.scope1.mapper=DEBUG
logging.level.com.lz.scope2.mapper=DEBUG

DB1SourceConfig

package com.lz.DBconfig;

@Configuration
@MapperScan(basePackages = "com.lz.scope1.mapper",sqlSessionFactoryRef="1808SqlSessionFactory")
public class DB1SourceConfig {
	
	@Bean(name="db1808")
	@ConfigurationProperties(prefix="spring.datasource.1808")
	@Primary
	public DataSource getDataSource(){
		return DataSourceBuilder.create().build();
	}
	
	@Bean(name="1808SqlSessionFactory")
	@Primary
	public SqlSessionFactory getSqlSessionFactory(
			@Qualifier("db1808") DataSource dataSource) throws Exception{
		SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
		factoryBean.setDataSource(dataSource);
		return factoryBean.getObject();
	}
	
	@Bean(name="1808TransactionManager")
	@Primary
	public DataSourceTransactionManager getTransactionManager(
			@Qualifier("db1808") DataSource dataSource){
		return new DataSourceTransactionManager(dataSource);
	}
	
	@Bean(name="1808SqlSessionTemplate")
	@Primary
	public SqlSessionTemplate getSqlSessionTemplate(
			@Qualifier("1808SqlSessionFactory")SqlSessionFactory factoryBean){
		return new SqlSessionTemplate(factoryBean);
	}
}

DB2SourceConfig

package com.lz.DBconfig;

@Configuration
@MapperScan(basePackages = "com.lz.scope2.mapper",sqlSessionFactoryRef="1808cSqlSessionFactory")
public class DB2SourceConfig {
	
	@Bean(name="db1808c")
	@ConfigurationProperties(prefix="spring.datasource.1808c")
	public DataSource getDataSource(){
		return DataSourceBuilder.create().build();
	}
	
	@Bean(name="1808cSqlSessionFactory")
	public SqlSessionFactory getSqlSessionFactory(
			@Qualifier("db1808c") DataSource dataSource) throws Exception{
		SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
		factoryBean.setDataSource(dataSource);
		return factoryBean.getObject();
	}
	
	@Bean(name="1808cTransactionManager")
	public DataSourceTransactionManager getTransactionManager(
			@Qualifier("db1808c") DataSource dataSource){
		return new DataSourceTransactionManager(dataSource);
	}
	
	@Bean(name="1808cSqlSessionTemplate")
	public SqlSessionTemplate getSqlSessionTemplate(
			@Qualifier("1808cSqlSessionFactory")SqlSessionFactory factoryBean){
		return new SqlSessionTemplate(factoryBean);
	}
}

UserDao1/UserDao2基本一致就类名改一下,包名区分好即可

package com.lz.scope1.mapper;

import java.util.List;
import org.apache.ibatis.annotations.Select;

import com.lz.entity.User;

public interface UserDao1 {

	@Select("select id,name from t_user where id = #{id}")
	public User getById(Integer id);
	
	@Select("select id,name from t_user")
	public List<User> findAll();
}

UserService1 /
UserService2基本一致,注入时包名区分好
@Autowired
private UserDao1 userDao;

package com.lz.scope1.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lz.entity.User;
import com.lz.scope1.mapper.UserDao1;

@Service
public class UserService1 {

	@Autowired
	private UserDao1 userDao;

	public User getById(Integer id) {
		return userDao.getById(id);
	}

	public List<User> findAll() {
		return userDao.findAll();
	}
}

UserController

package com.lz.controller;

@RestController
public class UserController {
	
	@Autowired
	private UserService1 service1;
	
	@Autowired
	private UserService2 service2;
	
	@GetMapping(value = "/get/{id}")
	public User get(@PathVariable("id") Integer id){
		User user = service1.getById(id);
		return user;
	}
	
	@GetMapping(value = "/find1")
	public List<User> find1(){
		List<User> list = service1.findAll();
		return list;
	}
	
	//====================================

	@GetMapping(value = "/getter/{id}")
	public User getter(@PathVariable("id") Integer id){
		User user = service2.getById(id);
		return user;
	}
	
	@GetMapping(value = "/find2")
	public List<User> find2(){
		List<User> list = service2.findAll();
		return list;
	}
}

启动类MultiDbApplication

package com.lz;

@SpringBootApplication
public class MultiDbApplication {

	public static void main(String[] args) {
		SpringApplication.run(MultiDbApplication.class, args);
	}
}

分别访问如下:

在这里插入图片描述

控制台
==>  Preparing: select id,name from t_user 
==> Parameters: 
<== Total: 7

====================================================================

在这里插入图片描述

控制台
 ==>  Preparing: select id,name from t_user 
 ==> Parameters: 
<==  Total: 2

OK,基本就这些

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值