Spring Boot+MyBabits静态连接多个数据库

1.修改.properties

first.datasource.jdbc-url=jdbc:mysql://localhost/forwind
first.datasource.username=root
first.datasource.password=root
first.datasource.driver-class-name=com.mysql.jdbc.Driver
first.datasource.type=com.alibaba.druid.pool.DruidDataSource

second.datasource.jdbc-url=jdbc:mysql://172.21.10.53/hue
second.datasource.username=root
second.datasource.password=root
second.datasource.driver-class-name=com.mysql.jdbc.Driver
second.datasource.type=com.alibaba.druid.pool.DruidDataSource

#如果不使用默认的数据源 (com.zaxxer.hikari.HikariDataSource)
spring.datasource.type =com.alibaba.druid.pool.DruidDataSource
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

2.SpringBootApplication类修改注解

@SpringBootApplication(exclude = {
        DataSourceAutoConfiguration.class
})

3.添加数据源配置类

package com.example.demo.config;

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 javax.sql.DataSource;

/**
 * @author baiyan
 * @date 2018/09/26
 * @description
 */
@Configuration
public class DataSourceConfig {
    //配置数据源
    @Bean(name = "first")
    @ConfigurationProperties(prefix = "first.datasource") // application.properteis中对应属性的前缀
    public DataSource dataSource1() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "second")
    @ConfigurationProperties(prefix = "second.datasource") // application.properteis中对应属性的前缀
    public DataSource dataSource2() {
        return DataSourceBuilder.create().build();
    }
}

4.给每个数据源生成SQLSessionFactory类

  • 第一个类:
package com.example.demo.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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;

/**
 * @author baiyan
 * @date 2018/09/26
 * @description
 */
@Configuration
@MapperScan(basePackages = {"com.example.demo.mapper.first"}, sqlSessionFactoryRef = "sqlSessionFactory1")
public class MybatisDbAConfig {

    @Autowired
    @Qualifier("first")
    private DataSource ds1;

    @Bean
    public SqlSessionFactory sqlSessionFactory1() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(ds1); // 使用first数据源, 连接first数据库
        return factoryBean.getObject();

    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate1() throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory1()); // 使用上面配置的Factory
        return template;
    }
}
  • 第二个类
package com.example.demo.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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = {"com.example.demo.mapper.second"}, sqlSessionFactoryRef = "sqlSessionFactory2")
public class MybatisDbBConfig {
    @Autowired
    @Qualifier("second")
    private DataSource ds2;

    @Bean
    public SqlSessionFactory sqlSessionFactory2() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(ds2);
        return factoryBean.getObject();

    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate2() throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory2());
        return template;
    }
}

5.根据basePackages的值生成Mapper类

  • 第二个mapper类:
package com.example.demo.mapper.first;

import com.example.demo.module.HueShell;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author baiyan
 * @date 2018/09/26
 * @description
 */
@Service
public interface LocalDBMapper {
    @Select("select * from hueshell")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "execute_time", property = "executeTime"),
            @Result(column = "name", property = "name"),
            @Result(column = "gmt_create", property = "gmtCreate"),
            @Result(column = "gmt_modify", property = "gmtModify"),
            @Result(column = "shellcontent", property = "shellContent"),
            @Result(column = "shellname", property = "shellName"),
            @Result(column = "type", property = "type"),
            @Result(column = "input", property = "input"),
            @Result(column = "output", property = "output"),
            @Result(column = "execute_rate", property = "executeRate"),
            @Result(column = "paraments", property = "paraments")
    })
    List<HueShell> getAll();
}

  • 第二个mapper类:
package com.example.demo.mapper.second;

import com.example.demo.module.HueShell;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author baiyan
 * @date 2018/09/26
 * @description
 */
@Service
public interface ServerMapper {
    @Select("select search from desktop_document2")
    List<String> getAll();
}

6.END

目录结构如下

.

注意:我用的是spring boot 2.x,如果spring boot版本是1.4,那么.properties中的datasource.jdbc-url和driver-class-name记得修改

posted @ 2018-09-26 16:46  周景白炎  阅读(1160)  评论(0编辑  收藏  举报