热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SpringBoot实现多数据源(四)【集成多个Mybatis框架】

上一篇文章《SpringBoot实现多数据源(三)【AOP自定义注解】》四、集成多个Mybatis框架实现步骤创建一个dynamic_mybatis的




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


四、集成多个 Mybatis 框架



在这里插入图片描述



实现步骤



  1. 创建一个 dynamic_mybatis 的springboot项目,导入依赖
    • pom.xml

<dependencies>

<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>

<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>

<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-aopartifactId>
dependency>

<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.4version>
dependency>

<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.2.8version>
dependency>

<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.28version>
dependency>

<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.24version>
dependency>
dependencies>

  1. 配置文件
    • application.yml

spring:
application:
name: dynamic_datasource
# 数据源
datasource:
type: com.alibaba.druid.pool.DruidDataSource
# 读数据源
read:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/read?useSSL&#61;true&useUnicode&#61;true&characterEncoding&#61;utf-8&serverTimezone&#61;Asia/Shanghai
username: root
password: 123456
# 写数据源
write:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/write?useSSL&#61;true&useUnicode&#61;true&characterEncoding&#61;utf-8&serverTimezone&#61;Asia/Shanghai
username: root
password: 123456
# 端口号
server:
port: 3355

  1. 实体类&#xff08;这里省略增删查改的数据库操作&#xff09;
    • People

package com.vinjcent.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
&#64;AllArgsConstructor
&#64;NoArgsConstructor
&#64;Data
public class People {
private String name;
}

  1. 配置读写分离的的MybatisConfiguration配置

  • RMybatisConfiguration&#xff08;读数据源&#xff09;

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是哪个
*/

&#64;Configuration
&#64;MapperScan(basePackages &#61; "com.vinjcent.mapper.read", sqlSessionFactoryRef &#61; "rSqlSessionFactory")
public class RMybatisConfiguration {
&#64;Bean(name &#61; "readDatasource")
&#64;ConfigurationProperties(prefix &#61; "spring.datasource.read")
public DataSource readDatasource() {
// 底层会自动拿到spring.datasource中的配置,创建一个DruidDataSource
return DruidDataSourceBuilder.create().build();
}
&#64;Bean
&#64;Primary
public SqlSessionFactory rSqlSessionFactory(&#64;Qualifier("readDatasource") DataSource dataSource) throws Exception {
final SqlSessionFactoryBean sqlSessionFactory &#61; new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource);
sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath:com/vinjcent/mapper/read/*.xml"));
/* 主库设置sql控制台打印 */
org.apache.ibatis.session.Configuration configuration &#61; new org.apache.ibatis.session.Configuration();
configuration.setLogImpl(StdOutImpl.class);
sqlSessionFactory.setConfiguration(configuration);
sqlSessionFactory.setTypeAliasesPackage("com.vinjcent.pojo");
return sqlSessionFactory.getObject();
}
}

  • WMybatisConfiguration&#xff08;写数据源&#xff09;

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是哪个
*/

&#64;Configuration
&#64;MapperScan(basePackages &#61; "com.vinjcent.mapper.write", sqlSessionFactoryRef &#61; "wSqlSessionFactory")
public class WMybatisConfiguration {
&#64;Bean(name &#61; "writeDatasource")
&#64;ConfigurationProperties(prefix &#61; "spring.datasource.write")
public DataSource writeDatasource() {
// 底层会自动拿到spring.datasource中的配置,创建一个DruidDataSource
return DruidDataSourceBuilder.create().build();
}
&#64;Bean
&#64;Primary
public SqlSessionFactory wSqlSessionFactory(&#64;Qualifier("writeDatasource") DataSource dataSource) throws Exception {
final SqlSessionFactoryBean sqlSessionFactory &#61; new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource);
sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath:com/vinjcent/mapper/write/*.xml"));
/* 主库设置sql控制台打印 */
org.apache.ibatis.session.Configuration configuration &#61; 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;
&#64;Mapper
public interface RPeopleMapper {
List<People> list();
}


DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace&#61;"com.vinjcent.mapper.read.RPeopleMapper">
<select id&#61;"list" resultType&#61;"People">
select * from &#96;people&#96;
select>
mapper>

  • WPeopleMapper

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


DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace&#61;"com.vinjcent.mapper.write.WPeopleMapper">
<insert id&#61;"save">
insert into &#96;people&#96;(name)
values (#{name});
insert>
mapper>

  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;
&#64;Service
public class PeopleServiceImpl implements PeopleService {
private final PeopleService peopleService;
&#64;Autowired
public PeopleController(PeopleService peopleService) {
this.peopleService &#61; peopleService;
}
// 读
&#64;GetMapping("/list")
public List<People> getAllPeople() {
return peopleService.list();
}
// 写
&#64;GetMapping("/insert")
public String addPeople() {
peopleService.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;
&#64;RestController
&#64;RequestMapping("people")
public class PeopleController {
private final WPeopleMapper wPeopleMapper;
private final RPeopleMapper rPeopleMapper;
&#64;Autowired
public PeopleController(WPeopleMapper wPeopleMapper, RPeopleMapper rPeopleMapper) {
this.wPeopleMapper &#61; wPeopleMapper;
this.rPeopleMapper &#61; rPeopleMapper;
}
&#64;GetMapping("/list")
public List<People> getAllPeople() {
return rPeopleMapper.list();
}
&#64;GetMapping("/insert")
public String addPeople() {
wPeopleMapper.save(new People("vinjcent"));
return "添加成功";
}
}

  1. 运行并测试接口

下一篇文章《SpringBoot实现多数据源&#xff08;五&#xff09;【多数据源事务控制】》







推荐阅读
author-avatar
Jiaquan_Sun_106
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有