SpringBoot实现多数据源(四)【集成多个 Mybatis 框架】
创始人
2024-04-17 00:51:19
0

上一篇文章《SpringBoot实现多数据源(三)【AOP + 自定义注解】》

四、集成多个 Mybatis 框架


在这里插入图片描述

实现步骤

  1. 创建一个 dynamic_mybatis 的springboot项目,导入依赖
    • pom.xml
org.springframework.bootspring-boot-starter-jdbcorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-aoporg.mybatis.spring.bootmybatis-spring-boot-starter2.1.4com.alibabadruid-spring-boot-starter1.2.8mysqlmysql-connector-java8.0.28org.projectlomboklombok1.18.24
  1. 配置文件
    • application.yml
spring:application:name: dynamic_datasource# 数据源datasource:type: com.alibaba.druid.pool.DruidDataSource# 读数据源read:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/read?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghaiusername: rootpassword: 123456# 写数据源write:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/write?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghaiusername: rootpassword: 123456
# 端口号
server:port: 3355
  1. 实体类(这里省略增删查改的数据库操作)
    • People
package com.vinjcent.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@AllArgsConstructor
@NoArgsConstructor
@Data
public class People {private String name;}
  1. 配置读写分离的的MybatisConfiguration配置
  • RMybatisConfiguration(读数据源)
package com.vinjcent.config.mybatis;import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.logging.stdout.StdOutImpl;
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.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import javax.sql.DataSource;/*** 读数据源配置* 1. 指定扫描mapper接口包* 2. 指定使用wSqlSessionFactory是哪个*/
@Configuration
@MapperScan(basePackages = "com.vinjcent.mapper.read", sqlSessionFactoryRef = "rSqlSessionFactory")
public class RMybatisConfiguration {@Bean(name = "readDatasource")@ConfigurationProperties(prefix = "spring.datasource.read")public DataSource readDatasource() {// 底层会自动拿到spring.datasource中的配置,创建一个DruidDataSourcereturn DruidDataSourceBuilder.create().build();}@Bean@Primarypublic SqlSessionFactory rSqlSessionFactory(@Qualifier("readDatasource") DataSource dataSource) throws Exception {final SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();sqlSessionFactory.setDataSource(dataSource);sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/vinjcent/mapper/read/*.xml"));/* 主库设置sql控制台打印 */org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();configuration.setLogImpl(StdOutImpl.class);sqlSessionFactory.setConfiguration(configuration);sqlSessionFactory.setTypeAliasesPackage("com.vinjcent.pojo");return sqlSessionFactory.getObject();}
}
  • WMybatisConfiguration(写数据源)
package com.vinjcent.config.mybatis;import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.logging.stdout.StdOutImpl;
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.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import javax.sql.DataSource;/*** 写数据源配置* 1. 指定扫描mapper接口包* 2. 指定使用wSqlSessionFactory是哪个*/
@Configuration
@MapperScan(basePackages = "com.vinjcent.mapper.write", sqlSessionFactoryRef = "wSqlSessionFactory")
public class WMybatisConfiguration {@Bean(name = "writeDatasource")@ConfigurationProperties(prefix = "spring.datasource.write")public DataSource writeDatasource() {// 底层会自动拿到spring.datasource中的配置,创建一个DruidDataSourcereturn DruidDataSourceBuilder.create().build();}@Bean@Primarypublic SqlSessionFactory wSqlSessionFactory(@Qualifier("writeDatasource") DataSource dataSource) throws Exception {final SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();sqlSessionFactory.setDataSource(dataSource);sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/vinjcent/mapper/write/*.xml"));/* 主库设置sql控制台打印 */org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();configuration.setLogImpl(StdOutImpl.class);sqlSessionFactory.setConfiguration(configuration);sqlSessionFactory.setTypeAliasesPackage("com.vinjcent.pojo");return sqlSessionFactory.getObject();}
}
  1. 读写mapper接口以及对应映射文件.xml
  • RPeopleMapper
package com.vinjcent.mapper.read;import com.vinjcent.pojo.People;
import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapper
public interface RPeopleMapper {List list();}




  • WPeopleMapper
package com.vinjcent.mapper.write;import com.vinjcent.pojo.People;
import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapper
public interface WPeopleMapper {boolean save(People people);}


insert into `people`(name)values (#{name});
  1. Service层
    • PeopleServiceImpl
package com.vinjcent.service.impl;import com.vinjcent.mapper.read.RPeopleMapper;
import com.vinjcent.mapper.write.WPeopleMapper;
import com.vinjcent.pojo.People;
import com.vinjcent.service.PeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class PeopleServiceImpl implements PeopleService {private final WPeopleMapper wPeopleMapper;private final RPeopleMapper rPeopleMapper;@Autowiredpublic PeopleController(WPeopleMapper wPeopleMapper, RPeopleMapper rPeopleMapper) {this.wPeopleMapper = wPeopleMapper;this.rPeopleMapper = rPeopleMapper;}// 读@GetMapping("/list")public List getAllPeople() {return rPeopleMapper.list();}// 写@GetMapping("/insert")public String addPeople() {wPeopleMapper.save(new People("vinjcent"));return "添加成功";}
}
  1. 测试接口
  • PeopleController
package com.vinjcent.controller;import com.vinjcent.mapper.read.RPeopleMapper;
import com.vinjcent.mapper.write.WPeopleMapper;
import com.vinjcent.pojo.People;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
@RequestMapping("people")
public class PeopleController {private final PeopleService peopleService;@Autowiredpublic PeopleController(PeopleService peopleService) {this.peopleService = peopleService;}@GetMapping("/list")public List getAllPeople() {return peopleService.list();}@GetMapping("/insert")public String addPeople() {peopleService.save(new People("vinjcent"));return "添加成功";}}
  1. 运行并测试接口

下一篇文章《SpringBoot实现多数据源(五)【多数据源事务控制】》

上一篇:C++智能指针

下一篇:Vue Class与Style绑定

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...