19.springboot+mybatis多数据源

Posted 结构化思维wz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了19.springboot+mybatis多数据源相关的知识,希望对你有一定的参考价值。

springboot+mybatis多数据源

1.配置数据库的连接信息

spring.datasource.one.jdbcUrl=jdbc:mysql://localhost:3306/wangze
spring.datasource.one.username=root
spring.datasource.one.password=123456


spring.datasource.two.jdbcUrl=jdbc:mysql://localhost:3306/test01
spring.datasource.two.username=root
spring.datasource.two.password=123456

2.编写配置类

2.1 配置DataSource的实例

@Configuration
public class DataSourceConfig {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.one")
    DataSource dsone(){
        return new HikariDataSource();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.two")
    DataSource dstwo(){
        return new HikariDataSource();
    }


}

2.2 配置mybatis

SqlSessionFactoryBean是生产SqlSessionFactory的一种工厂bean。

SqlSessionFactory是打开SqlSession会话的工厂,是一个接口,可以根据需求自己实现,它的默认实现类DefaultSqlSessionFactory使用了数据库连接池技术。

SqlSession是客户端和数据库服务端之间的会话信息,里面有许多操作数据库的方法。

SqlSessionTemplate是SqlSession的一个具体实现。
@Configuration
@MapperScan(basePackages = "com.example.twosource.mapper1",sqlSessionFactoryRef = "sqlSessionFactory1" ,sqlSessionTemplateRef ="sqlSessionTemplate1" )
public class MyBatisConfigOne {
//这里跟以前spring注解形式整合mybatis基本相同
    @Autowired
        @Qualifier("dsone")
    DataSource ds;
    @Bean
    SqlSessionFactory sqlSessionFactory1(){
        SqlSessionFactory sqlSessionFactory = null;
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(ds);
        try {
            sqlSessionFactory= sqlSessionFactoryBean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sqlSessionFactory;
    }
    @Bean
    SqlSessionTemplate sqlSessionTemplate1(){
        return new SqlSessionTemplate(sqlSessionFactory1());
    }


}

@Configuration
@MapperScan(basePackages = "com.example.twosource.mapper2",sqlSessionFactoryRef = "sqlSessionFactory2" ,sqlSessionTemplateRef ="sqlSessionTemplate2" )
public class MyBatisConfigTwo {
    //这里跟以前spring注解形式整合mybatis基本相同
    @Autowired
    @Qualifier("dstwo")
    DataSource ds;
    @Bean
    SqlSessionFactory sqlSessionFactory2(){
        SqlSessionFactory sqlSessionFactory = null;
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(ds);
        try {
            sqlSessionFactory= sqlSessionFactoryBean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sqlSessionFactory;
    }
    @Bean
    SqlSessionTemplate sqlSessionTemplate2(){
        return new SqlSessionTemplate(sqlSessionFactory2());
    }


}

测试

1.实体类

test01库中user:

package com.example.twosource.model;

/**
 * @author: 王泽
 */

public class User {
    private int id;
    private String username;
    private String address;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\\'' +
                ", address='" + address + '\\'' +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

wangze库中Categary表

package com.example.twosource.model;

/**
 * @author: 王泽
 */

public class Categary {

    private int categoryid;
    private int pid;
    private String categoryName;

    @Override
    public String toString() {
        return "Categary{" +
                "categoryid=" + categoryid +
                ", pid=" + pid +
                ", categoryName='" + categoryName + '\\'' +
                '}';
    }

    public int getCategoryid() {
        return categoryid;
    }

    public void setCategoryid(int categoryid) {
        this.categoryid = categoryid;
    }

    public int getPid() {
        return pid;
    }

    public void setPid(int pid) {
        this.pid = pid;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }
}

2.mapper接口

对应user:

package com.example.twosource.mapper1;


import com.example.twosource.model.User;


import java.util.List;

public interface UserMapper1 {

    List<User> getAllUsers();

}

对应Categary

package com.example.twosource.mapper2;

import com.example.twosource.model.Categary;

import java.util.List;

public interface CategaryMapper {
    List<Categary> getAllCategarys();


}

3.mapper.xml

指定位置:

mybatis.mapper-locations= classpath:mapper/*.xml

usermapper

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.twosource.mapper1.UserMapper1">
<select id="getAllUsers" resultType="com.example.twosource.model.User">
    select * from test01.user;
</select>

</mapper>

categorymapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.twosource.mapper2.CategaryMapper">
    <select id="getAllCategarys" resultType="com.example.twosource.model.Categary">
        select * from category;
    </select>

</mapper>

测试类

@SpringBootTest
class TwoSourceApplicationTests {
    @Autowired
    UserMapper userMapper;
    @Autowired
    CategaryMapper categaryMapper;
    @Test
    void contextLoads() {
        System.out.println("数据库test01中的user"+ userMapper.getAllUsers());
        System.out.println("数据库wangze中的categary"+categaryMapper.getAllCategarys());
    }

}

以上是关于19.springboot+mybatis多数据源的主要内容,如果未能解决你的问题,请参考以下文章

mybatis 多数据源

mybatis一对多关联为啥查询不到数据

springboot mybatis 多数据源配置

springboot+mybatis+Druid配置多数据源(mysql+postgre)

Mybatis-Plus多数据源使用多线程时失效的问题

myBatis多数据源