Springboot整合mybatis多数据源(注解完整版)
16lz
2021-01-22
并发量的不断增加,单个数据库承受不了这么大的压力,因此一个项目使用多个数据库也越来越重要,当然使用数据库的模式可能不一样,比如说主从模式、分布式模式。不管是哪种模式都是使用的多数据源。Springboot整合mybatis实现多数据源有两种方式:分包和AOP。这里使用的分包,因为层次更加清晰。
一、环境配置
名称 | 版本 |
---|---|
Idea | 2018专业版(已破解) |
Maven | 3.6.0 |
SpringBoot | 2.2.2 |
Mybatis | 5.1.44 |
Navicat(可视化工具) | 12(已破解) |
jdk | 1.8 |
环境很简单,下面开始整合。
二、代码整合
新建一个Springboot项目名字叫做SpringbootMybatisMutil,开始下面的操作。
第一步:添加依赖
1<dependencies>
2 <!--springboot开发web项目的起步依赖-->
3 <dependency>
4 <groupId>org.springframework.boot</groupId>
5 <artifactId>spring-boot-starter-web</artifactId>
6 </dependency>
7 <!-- 加载mybatis整合springboot -->
8 <dependency>
9 <groupId>org.mybatis.spring.boot</groupId>
10 <artifactId>mybatis-spring-boot-starter</artifactId>
11 <version>1.3.1</version>
12 </dependency>
13 <!-- MySQL的jdbc驱动包 -->
14 <dependency>
15 <groupId>mysql</groupId>
16 <artifactId>mysql-connector-java</artifactId>
17 <version>5.1.45</version>
18 </dependency>
19 </dependencies>
第二步:修改属性文件
1#数据库1的配置
2spring.datasource.test1.driver-class-name = com.mysql.jdbc.Driver
3spring.datasource.test1.jdbc-url = jdbc:mysql://localhost:3306/uav
4?useUnicode=true&characterEncoding=utf-8&useSSL=false
5spring.datasource.test1.username = root
6spring.datasource.test1.password = root
7#数据库2的配置
8spring.datasource.test2.driver-class-name = com.mysql.jdbc.Driver
9spring.datasource.test2.jdbc-url = jdbc:mysql://localhost:3306/uav2
10?useUnicode=true&characterEncoding=utf-8&useSSL=false
11#上面的这一行在uav后面添加,代码太长我换成了两行,
12spring.datasource.test2.username = root
13spring.datasource.test2.password = root
14#mybatis依赖
15mybatis.type-aliases-package = com.fdd.bean
第三步:新建一个bean包,创建Person类
1@Mapper
2public class Person implements Serializable {
3 private Integer id ;
4 private String name;
5 private Integer age;
6 public Person() {
7 }
8 public Person(Integer id, String name, Integer age) {
9 this.id = id;
10 this.name = name;
11 this.age = age;
12 }
13 //getter和setter方法
14 //toString方法
15}
第四步:新建config包,创建DataSourceConfig1l类
1@Configuration //注册到springboot 容器中
2@MapperScan(basePackages = "com.fdd.mapper1",
3 sqlSessionTemplateRef = "test1SqlSessionTemplate")
4public class DataSourceConfig1 {
5 @Bean(name = "test1DataSource")
6 @Primary
7 @ConfigurationProperties(prefix = "spring.datasource.test1")
8 public DataSource testDataSource() {
9 return DataSourceBuilder.create().build();
10 }
11 @Primary
12 @Bean(name = "test1SqlSessionFactory")
13 public SqlSessionFactory testSqlSessionFactory
14 (@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
15 SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
16 bean.setDataSource(dataSource);
17
18 return bean.getObject();
19 }
20 @Bean(name = "test1TransactionManager")
21 @Primary
22 public DataSourceTransactionManager testTransactionManager
23 (@Qualifier("test1DataSource") DataSource dataSource) {
24 return new DataSourceTransactionManager(dataSource);
25 }
26 @Bean(name = "test1SqlSessionTemplate")
27 @Primary
28 public SqlSessionTemplate testSqlSessionTemplate
29 (@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
30 return new SqlSessionTemplate(sqlSessionFactory);
31 }
32}
再建一个DataSourceConfig2类,里面的代码和DataSourceConfig1的一样,把里面的1换成2即可。
第五步:新建mapper1包,创建PersonMapper类
1@Mapper
2@Repository
3public interface PersonMapper {
4 //增加一个Person
5 @Insert("insert into person(id,name,age)values(#{id},#{name},#{age})")
6 int insert(Person person);
7 //删除一个Person
8 @Delete("delete from person where id = #{id}")
9 int deleteByPrimaryKey(Integer id);
10 //更改一个Person
11 @Update("update person set name =#{name},age=#{age} where id=#{id}")
12 int updateByPrimaryKey(Integer id);
13 //查询一个Person
14 @Select("select id,name ,age from person where id = #{id}")
15 Person selectByPrimaryKey(Integer id);
16 //查询所有的Person
17 @Select("select id,name,age from person")
18 List<Person> selectAllPerson();
19}
在这里使用注解的形式进行整合。
第六步:新建mapper2包,创建UserMapper类
1@Mapper
2@Repository
3public interface UserMapper {
4 //增加一个Person
5 @Insert("insert into person(id,name,age)values(#{id},#{name},#{age})")
6 int insert(Person person);
7 //删除一个Person
8 @Delete("delete from person where id = #{id}")
9 int deleteByPrimaryKey(Integer id);
10 //更改一个Person
11 @Update("update person set name =#{name},age=#{age} where id=#{id}")
12 int updateByPrimaryKey(Integer id);
13 //查询一个Person
14 @Select("select id,name ,age from person where id = #{id}")
15 Person selectByPrimaryKey(Integer id);
16 //查询所有的Person
17 @Select("select id,name,age from person")
18 List<Person> selectAllPerson();
19}
内容和PersonMapper是一样的,但是我在测试的时候出现了一些bean重复定义的错误,所以没有使用Person2Mapper这种方法进行命名。
第七步:新建controller包,创建MyController类
1@RestController
2public class MyController {
3 @Autowired
4 private PersonMapper personService;
5 @Autowired
6 private UserMapper personService2;
7 @GetMapping("/add")
8 public String test1(){
9 Person person = new Person();
10 person.setId(14);
11 person.setName("java的架构师技术栈");
12 person.setAge(18);
13 int result = personService.insert(person);
14 int result2 = personService2.insert(person);
15 System.out.println("插入的结果是:"+result+" "+result2);
16 return "插入的结果是:"+result+" "+result2;
17 }
18}
这里只测试了插入的方法,当然如果你需要service层,自行添加就好了。不过对于基本的增删改查功能,mapper足够。
OK,这个就是多数据源的整合,在我自己的电脑亲测成功。
更多相关文章
- 阿里的OceanBase数据库世界第一,底层原来是用了Paxos协议
- 数据库面试题(开发者必看)
- binlog有哪些工作模式?Linux云计算运维入门
- Web开发模式【Mode I 和Mode II的介绍、应用案例】
- 《JAVA与模式》之观察者模式
- 从模版方法模式到 SPI 演变 :好的思想通用而持久
- 漫谈设计模式在 Spring 框架中的良好实践
- Flyway 助力数据库脚本自动化管理攻略
- 设计模式之适配器模式