首页 技术 正文
技术 2022年11月15日
0 收藏 824 点赞 4,615 浏览 4905 个字

项目架构

SpringBoot整合多数据源实现

1.导入相关依赖

 <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> <!--freemarker支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency> <!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency> <!--整合mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>

2.application.properties

 ## test1 数据源配置
#spring.datasource.test1.driverClassName=com.mysql.jdbc.Driver
#spring.datasource.test1.jdbc-url=jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf8
#spring.datasource.test1.username=root
#spring.datasource.test1.password=123 ## test2 数据源配置
#spring.datasource.test2.driverClassName=com.mysql.jdbc.Driver
#spring.datasource.test2.jdbc-url=jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf8
#spring.datasource.test2.username=root
#spring.datasource.test2.password=123

3.创建datasource包  下TestMyBatisConfig1

 @Configuration
@MapperScan(basePackages = "cn.happy.test1", sqlSessionFactoryRef = "test1SqlSessionFactory")
public class DataSource1Config {
@Bean(name = "test1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test1")
@Primary
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
} @Bean(name = "test1SqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//读取mybatis小配置文件
// bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"));
return bean.getObject();
} @Bean(name = "test1TransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
} @Bean(name = "test1SqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}

@Primary注解标识默认使用的数据源

如果不加启动会报如下错误:意思是有两个数据源  不知需要使用哪个数据源

SpringBoot整合多数据源实现

3.创建datasource包  下TestMyBatisConfig2

 @Configuration
@MapperScan(basePackages = "cn.happy.test2", sqlSessionFactoryRef = "test2SqlSessionFactory")
public class DataSource2Config {
@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);
//读取mybatis小配置文件
// bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test2/*.xml"));
return bean.getObject();
} @Bean(name = "test2TransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
} @Bean(name = "test2SqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
} }

4.entity层

 /**
* author: 刘涛
*
* @create 2018-04-04 11:18
*/
public class Book { private Integer bookid; private String bookname; private Integer bookprice; get set省略。。
}

5.test1下的dao中BookMapperTest1

 public interface BookMapperTest1 {     @Select("select * from book where bookname=#{bookname}")
public Book findByName(@Param("bookname") String bookname); @Insert("insert into book(bookname,bookprice) values (#{bookname},#{bookprice})")
public int insertBook(@Param("bookname") String bookname, @Param("bookprice") Double bookprice); @Select("select * from book")
public Book findBook(@Param("Book") Book book);
}

6.test2下的dao中BookMapperTest2

 public interface BookMapperTest2 {     @Select("select * from book where bookname=#{bookname}")
public Book findByName(@Param("bookname") String bookname); @Insert("insert into book(bookname,bookprice) values (#{bookname},#{bookprice})")
public int insertBook(@Param("bookname") String bookname, @Param("bookprice") Double bookprice);
}

7.controller层

@RestController
public class BookController {
@Autowired
private BookMapperTest1 bookMapperTest1; @Autowired
private BookMapperTest2 bookMapperTest2; @RequestMapping("insert001")
public String insert001(String bookname,Double bookprice){
bookMapperTest1.insertBook(bookname,bookprice);
return "success";
} @RequestMapping("insert002")
public String insert002(String bookname,Double bookprice){
bookMapperTest2.insertBook(bookname,bookprice);
return "success";
}
}

8.启动项目

SpringBoot整合多数据源实现

test1数据库

SpringBoot整合多数据源实现

test2数据库

SpringBoot整合多数据源实现

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905