上一篇文章《SpringBoot实现多数据源(三)【AOP + 自定义注解】》
四、集成多个 Mybatis 框架
实现步骤
- 创建一个 dynamic_mybatis 的springboot项目,导入依赖
- pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.8</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.24</version> </dependency></dependencies>
- 配置文件
- 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" /><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.vinjcent.mapper.read.RPeopleMapper"> <select id="list" resultType="People"> select * from `people` </select></mapper>
- WPeopleMapper
package com.vinjcent.mapper.write;import com.vinjcent.pojo.People;import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapperpublic 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="com.vinjcent.mapper.write.WPeopleMapper"> <insert id="save"> insert into `people`(name) values (#{name}); </insert></mapper>
- 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;@Servicepublic class PeopleServiceImpl implements PeopleService { private final PeopleService peopleService; @Autowired public PeopleController(PeopleService peopleService) { this.peopleService = peopleService; }// 读 @GetMapping("/list") public List<People> getAllPeople() { return peopleService.list(); } // 写 @GetMapping("/insert") public String addPeople() { peopleService.save(new People("vinjcent")); return "添加成功"; }}
- 测试接口
- 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 WPeopleMapper wPeopleMapper; private final RPeopleMapper rPeopleMapper; @Autowired public PeopleController(WPeopleMapper wPeopleMapper, RPeopleMapper rPeopleMapper) { this.wPeopleMapper = wPeopleMapper; this.rPeopleMapper = rPeopleMapper; } @GetMapping("/list") public List<People> getAllPeople() { return rPeopleMapper.list(); } @GetMapping("/insert") public String addPeople() { wPeopleMapper.save(new People("vinjcent")); return "添加成功"; }}
- 运行并测试接口
下一篇文章《SpringBoot实现多数据源(五)【多数据源事务控制】》