项目结构

application数据库主要配置
# Tomcat 配置
server:
port: 8081
spring:
application:
name: spring-boot
#数据库
datasource:
druid:
test1:
jdbc-url: jdbc:mysql://127.0.0.1:3306/test1?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
username: root
password: 123456
test2:
jdbc-url: jdbc:mysql://127.0.0.1:3306/test2?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
username: root
password: 123456
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.xukehong.springbootvue.entity
主要DataSources配置
1.DataSourceConfig 注入Spring容器
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 javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
// 将这个对象放入Spring容器中
@Bean(name = "tes1tDataSource")
// 表示这个数据源是默认数据源
@Primary
// 读取application.properties中的配置参数映射成为一个对象
@ConfigurationProperties(prefix = "spring.datasource.druid.test1")
public DataSource dataSource1(){
return DataSourceBuilder.create().build();
}
@Bean(name = "test2DataSource")
@ConfigurationProperties(prefix = "spring.datasource.druid.test2")
public DataSource dataSource2(){
return DataSourceBuilder.create().build();
}
}
2.数据库test1和test2对应的配置
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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = {"com.xukehong.springbootvue.mapper.test1"}, sqlSessionFactoryRef = "test1SqlSessionFactory")
public class MyBatisDbTest1Config {
@Autowired
@Qualifier("tes1tDataSource")
private DataSource tes1tDataSource;
@Bean(name = "test1SqlSessionFactory")
// @Qualifier表示查找Spring容器中名字为test1DataSource的对象
public SqlSessionFactory test1SqlSessionFactory()
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(tes1tDataSource);
bean.setMapperLocations(
// 设置mybatis的xml所在位置
new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/test1/*.xml"));
return bean.getObject();
}
@Bean("test1SqlSessionTemplate")
public SqlSessionTemplate test1sqlsessiontemplate(
@Qualifier("test1SqlSessionFactory") SqlSessionFactory sessionfactory) {
return new SqlSessionTemplate(sessionfactory);
}
}
@Configuration
@MapperScan(basePackages = {"com.xukehong.springbootvue.mapper.test2"}, sqlSessionFactoryRef = "test2SqlSessionFactory")
public class MyBatisDbTest2Config {
@Autowired
@Qualifier("test2DataSource")
private DataSource test2DataSource;
@Bean(name = "test2SqlSessionFactory")
public SqlSessionFactory test2SqlSessionFactory()
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(test2DataSource);
bean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/test2/*.xml"));
return bean.getObject();
}
@Bean("test2SqlSessionTemplate")
public SqlSessionTemplate test2sqlsessiontemplate(
@Qualifier("test2SqlSessionFactory") SqlSessionFactory sessionfactory) {
return new SqlSessionTemplate(sessionfactory);
}
}
以上配置完成,接下来就是写一个测试demo
1.Mapper
@Mapper
public interface Book1Mapper {
List<Book> getAllBooks();
}
2.Mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xukehong.springbootvue.mapper.test1.Book1Mapper">
<select id="getAllBooks" resultType="com.xukehong.springbootvue.entity.Book">
select * from book;
</select>
</mapper>
3.Controller
@RestController
@RequestMapping("/book")
public class BookController {
@Autowired
private Book1Mapper book1Mapper;
@Autowired
private Book2Mapper book2Mapper;
@GetMapping("/test1")
@ApiOperation("测试")
public List<Book> test1(){
return book1Mapper.getAllBooks();
}
@GetMapping("/test2")
@ApiOperation("测试")
public List<Book> test2(){
return book2Mapper.getAllBooks();
}
}
4.数据库内容
数据库test1 表 book

数据库test2 表 book

运行项目,测试
http://localhost:8081/book/test1

http://localhost:8081/book/test2
