springboot多数据源分包式实现

本文详细介绍了如何在Spring Boot项目中配置和使用多个数据源,并通过MyBatis进行数据库操作。包括Maven依赖设置、application.properties配置、数据源配置类编写、启动类设置以及Controller、Service和DAO层实现。

创建maven项目并添加依赖 我这里用的是SQLserver mybatis

<parent>
	<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.3.RELEASE</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>com.microsoft.sqlserver</groupId>
			<artifactId>sqljdbc4</artifactId>
			<version>4.0</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.5</version>
		</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>

application.properties配置文件中添加 多个数据源

spring.datasource.test1.jdbcUrl=jdbc:sqlserver://ip:port;DatabaseName=xxglptcsDB
spring.datasource.test1.username=sa
spring.datasource.test1.password=123
spring.datasource.test1.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver

spring.datasource.test2.jdbcUrl=jdbc:sqlserver://ip:port;DatabaseName=xxglptcsDB1
spring.datasource.test2.username=sa
spring.datasource.test2.password=123
spring.datasource.test2.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver

项目结构

启动类

这里扫包除了controller层 service层 还要添加DataSource包 (博主因为没扫这个包找了两个小时的错……)

在网上找了两个小时也没有一个管用的 ,然后博主突然想到这配置文件跟数据源配置类好像也没有关联,于是就添加了扫DataSource包

如果没有扫数据源配置包 可能会报这个错Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

或者'url' attribute is not specified and no embedded datasource could be configured.


package com.vhukze.App;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

/**
 * @author zsz
 * @version
 * @创建时间:2019年9月2日 下午1:08:00
 */

@SpringBootApplication
@ComponentScan(basePackages= {"com.vhukze.controller","com.vhukze.test1.service","com.vhukze.test2.service","com.vhukze.datasource"})
@MapperScan({"com.vhukze.test1.mapper","com.vhukze.test2.mapper"})
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

数据源配置类

test1


package com.vhukze.datasource;


import javax.sql.DataSource;

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.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**
* @author zsz
* @version 
* @创建时间:2019年9月2日 上午11:19:09
*/
@Configuration
@MapperScan(basePackages="com.vhukze.test1.mapper",sqlSessionFactoryRef="test1SqlSessionFactory")//扫描对应的dao包     
public class Datasource1 {

	
	@Bean(name="test1DataSource")
	@ConfigurationProperties(prefix="spring.datasource.test1")//这里是配置文件里定义的属性名
	@Primary //默认优先使用
	public DataSource testDataSource() {
		return DataSourceBuilder.create().build();
	}
	
	//@Qualifier 从spring容器中查找名为test1DataSource的bean
	@Bean(name="test1SqlSessionFactory")
	@Primary //默认优先使用
	public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception { 
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		
		//需要加载配置文件的话
//		bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:*.xml"));
		
		return bean.getObject();
	}
	
	
	/**
	 * @author zsz
	 * @编辑时间 2019年9月2日上午11:45:07
	 * @编辑说明  事务管理
	 * @param dataSource
	 * @return
	 */
	@Bean(name="test1TuansactionManager")
	@Primary //默认优先使用
	public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource){ 
		return new DataSourceTransactionManager(dataSource);
	}
	
	@Bean(name="test1SqlSessionTemplate")
	@Primary //默认优先使用
	public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) { 
		return new SqlSessionTemplate(sqlSessionFactory);
	}
}


 

test2


package com.vhukze.datasource;


import javax.sql.DataSource;

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.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**
* @author zsz
* @version 
* @创建时间:2019年9月2日 上午11:53:15
*/
@Configuration
@MapperScan(basePackages="com.vhukze.test2.mapper",sqlSessionFactoryRef="test2SqlSessionFactory")//扫描对应的dao包     
public class Datasource2 {

	
	@Bean(name="test2DataSource")
	@ConfigurationProperties(prefix="spring.datasource.test2")//这里是配置文件里定义的属性名
	public DataSource testDataSource() {
		return DataSourceBuilder.create().build();
	}
	
	@Bean(name="test2SqlSessionFactory")
	public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception { 
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		
		//需要加载配置文件的话
//		bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:*.xml"));
		
		return bean.getObject();
	}
	
	
	/**
	 * @author zsz
	 * @编辑时间 2019年9月2日上午11:54:08
	 * @编辑说明  事务管理
	 * @param dataSource
	 * @return
	 */
	@Bean(name="test2TuansactionManager")
	public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource){ 
		return new DataSourceTransactionManager(dataSource);
	}
	
	@Bean(name="test2SqlSessionTemplate")
	public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) { 
		return new SqlSessionTemplate(sqlSessionFactory);
	}
}


到此所有的配置都完成了

接下来就是走一下流程了

controller层

@RestController
public class UserController {

	@Autowired
	private User1Service service1;
	@Autowired
	private User2Service service2;
	
	@RequestMapping("index")
	public String index() {
		service1.insert();
		service2.insert();
		return "success";
	}
}

service层 (1和2都是一样的  这里贴一个就可以了)

@Service
public class User1Service {

	@Autowired
	private User1Mapper mapper1;
	
	public void insert() {
		
		mapper1.insert();
	}

}

dao层(1和2都是一样的 这里贴一个就可以了)

@Repository
public interface User1Mapper{

	@Insert("insert into user_demo values('1','1')")
	public void insert();
}

启动项目访问映射路径

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿演

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值