mybatis 多数据源

Posted 何山123

tags:

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

1.配置多数据源

spring:

# mybatis 连接多数据源配置
db1-mybatis:
datasource:
jdbcurl: jdbc:mysql://127.0.0.1:3306/cloud-main1?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
db2-mybatis:
datasource:
jdbcurl: jdbc:mysql://127.0.0.1:3306/cloud-main2?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource

2.db1 配置
package com.example.demo.mybatis.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
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 org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.example.demo.mybatis.dbs.db1.dao", sqlSessionFactoryRef = "db1SessionFactory")
public class DB1Config {

    @Primary
    @Bean(name = "db1Mybatis")
    @ConfigurationProperties(prefix = "spring.db1-mybatis.datasource")
    public DataSource db1DataSource() {

        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "db1TransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("db1Mybatis") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Primary
    @Bean(name = "db1SessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("db1Mybatis") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        bean.setConfiguration(configuration);
        bean.setDataSource(dataSource);
        ResourcePatternResolver rsourcePatternResolver = new PathMatchingResourcePatternResolver();
        bean.setMapperLocations(rsourcePatternResolver.getResources("com/example/demo/mybatis/dbs/db1/mapper/*.xml"));
        return bean.getObject();
    }

    @Primary
    @Bean(name = "db1Template")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("db1SessionFactory") SqlSessionFactory sqlSessionFactory) {
        SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
        return sqlSessionTemplate;
    }
}

  3.db2配置

package com.example.demo.mybatis.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.example.demo.mybatis.dbs.db2.mapper", sqlSessionFactoryRef = "db2SessionFactory")
public class DB2Config {


    @Bean(name = "db2Mybatis")
    @ConfigurationProperties(prefix = "spring.db2-mybatis.datasource")
    public DataSource mysqlDataSource() {

        return DataSourceBuilder.create().build();
    }

    @Bean(name = "db2TransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("db1Mybatis") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "db2SessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("db1Mybatis") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        bean.setConfiguration(configuration);
        bean.setDataSource(dataSource);
        ResourcePatternResolver rsourcePatternResolver = new PathMatchingResourcePatternResolver();
        bean.setMapperLocations(rsourcePatternResolver.getResources("classpath*:com/example/demo/mybatis/dbs/db2/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "db2Template")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("db2SessionFactory") SqlSessionFactory sqlSessionFactory) {
        SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
        return sqlSessionTemplate;
    }
}

  3.实体Modle

public class User {
………………
}

4.*Mapper.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.demo.mybatis.dbs.db1.dao.UserMapper" >
<resultMap id="BaseResultMap" type="com.example.demo.mybatis.dbs.db1.model.User" >
<constructor >
<idArg column="USER_ID" jdbcType="CHAR" javaType="java.lang.String" />
<arg column="LOGIN_NAME" jdbcType="VARCHAR" javaType="java.lang.String" />
<arg column="PASSWORD" jdbcType="VARCHAR" javaType="java.lang.String" />
<arg column="PHONE" jdbcType="VARCHAR" javaType="java.lang.String" />
<arg column="EMAIL" jdbcType="VARCHAR" javaType="java.lang.String" />
<arg column="FULL_NAME" jdbcType="VARCHAR" javaType="java.lang.String" />
<arg column="AVATAR_URL" jdbcType="VARCHAR" javaType="java.lang.String" />
<arg column="JOB_TITLE" jdbcType="VARCHAR" javaType="java.lang.String" />
<arg column="COM_ID" jdbcType="VARCHAR" javaType="java.lang.String" />
<arg column="DEPT_ID" jdbcType="CHAR" javaType="java.lang.String" />
<arg column="ROLE_ID" jdbcType="CHAR" javaType="java.lang.String" />
<arg column="IS_DISABLED" jdbcType="CHAR" javaType="java.lang.String" />
<arg column="QUICK_CODE" jdbcType="VARCHAR" javaType="java.lang.String" />
<arg column="CREATED_BY" jdbcType="CHAR" javaType="java.lang.String" />
<arg column="CREATED_ON" jdbcType="TIMESTAMP" javaType="java.util.Date" />
<arg column="MODIFIED_ON" jdbcType="TIMESTAMP" javaType="java.util.Date" />
<arg column="MODIFIED_BY" jdbcType="CHAR" javaType="java.lang.String" />
<arg column="TOKEN" jdbcType="VARCHAR" javaType="java.lang.String" />
</constructor>
</resultMap>

…………
</mapper>

5.调用
 @Autowired
    @Resource(name = "db1Template")
    private SqlSessionTemplate sqlSessionTemplate;


    public Test getTest(Long id) {
        return sqlSessionTemplate.selectOne("com.example.demo.mybatis.dbs.db1.dao.UserMapper.getuser",id);
    }

  注意:

  2、3注意路径, 不需要dao层

 

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

mybatis学习(39):动态sql片段

SSM-MyBatis-05:Mybatis中别名,sql片段和模糊查询加getMapper

mybatis动态sql片段与分页,排序,传参的使用

MYBATIS05_ifwherechoosewhentrimsetforEach标签sql片段

MyBatis动态SQL标签用法

mysql多数据库文件导入的脚本