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记得修改

Spring Boot+MyBabits静态连接多个数据库的更多相关文章

  1. Java Spring Boot VS .NetCore (四)数据库操作 Spring Data JPA vs EFCore

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  2. spring boot配置druid连接池连接mysql

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  3. Spring Boot中静态资源(JS, 图片)等应该放在什么位置

    Spring Boot的静态资源,比如图片应该放在什么位置呢, 如果你放在传统WEB共的类似地方, 比如webapp或者WEB-INF下,你会得到一张示意文件未找到的破碎图片.那应该放哪里呢? 百度一 ...

  4. Spring boot 默认静态资源路径与手动配置访问路径的方法

    这篇文章主要介绍了Spring boot 默认静态资源路径与手动配置访问路径的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下   在application.propertis中配置 ##端口号 ...

  5. 不常见偏门的Bug,Spring Boot IDEA 静态资源 图片访问404,初学者之殇

    用过Idea朋友都知道,它有一个非常让人喜欢的功能就是:打算在某个a目录下创建一个hello.class文件,那么你仅需要右键点击New-Java Class- 然后输入名字:a.hello 即可. ...

  6. Spring boot Jpa添加对象字段使用数据库默认值

    Spring boot Jpa添加对象字段使用数据库默认值 jpa做持久层框架,项目中数据库字段有默认值和非空约束,这样在保存对象是必须保存一个完整的对象,但在开发中我们往往只是先保存部分特殊的字段其 ...

  7. Spring Boot 的静态资源处理

    做web开发的时候,我们往往会有很多静态资源,如html.图片.css等.那如何向前端返回静态资源呢?以前做过web开发的同学应该知道,我们以前创建的web工程下面会有一个webapp的目录,我们只要 ...

  8. spring boot 开静态资源访问,配置视图解析器

    配置视图解析器spring.mvc.view.prefix=/pages/spring.mvc.view.suffiix= spring boot 开静态资源访问application.proerti ...

  9. spring boot 配置静态路径

    一  前言 最近有个项目,需要上传一个zip文件(zip文件就是一堆的html压缩组成)的压缩文件,然后后端解压出来,用户可以预览上传好的文件. 查看资料,spring boot对静态文件,可以通过配 ...

随机推荐

  1. php的error_log()记录日志

    <?php date_default_timezone_set('PRC');//设置时区,否则会有警告 //把This s a error保存到/home/log-yyyy-MM-dd.txt ...

  2. [笔记] Python字典

    好记忆不如烂笔头: #__author:Mifen #date: 2018/11/28 #不可变类型:整型,字符串,浮点型(不等于小数,但包括小数),元组(只读,不可修改) #可变类型:列表,字典(键 ...

  3. Struts ongl 集合伪属性

    首先了解下OGNL的概念: OGNL是Object-Graph Navigation Language的缩写,全称为对象图导航语言,是一种功能强大的表达式语言,它通过简单一致的语法,可以任意存取对象的 ...

  4. Visual Studio最常用的快捷键

    Ctrl + J:快捷提示,强迫智能感知: Ctrl + 空格键:使用 IntelliSense(智能感知)自动完成: Ctrl + Z:撤销,回退键: Ctrl + Shift + 空格:强迫显示参 ...

  5. 关于发布程序之后js文件存在缓存问题

    把js文件加上版本号即可解决 如: <script src="../Static/js/Contract/ContractRateEdit.js?t=20181210"> ...

  6. SQL SERVER 原来还可以这样玩 FOR XML PATH

    FOR XML PATH 有的人可能知道有的人可能不知道,其实它就是将查询结果集以XML形式展现,有了它我们可以简化我们的查询语句实现一些以前可能需要借助函数活存储过程来完成的工作.那么以一个实例为主 ...

  7. 设计模式学习--装饰者模式(Decorator Pattern)

    概念: 装饰者模式(Decorator Pattern): 动态地将功能添加到对象,相比生成子类更灵活,更富有弹性. 解决方案: 装饰者模式的重点是对象的类型,装饰者对象必须有着相同的接口,也也就是有 ...

  8. IList,ICollection,IEnumerable,IEnumerator,IQueryable

    http://www.cnblogs.com/edison1105/archive/2012/07/30/2616082.html 1.首先看一个简单的例子 int[] myArray = { 1, ...

  9. Java - 延迟初始化

    延迟初始化(lazy initialization),也就是在真正被使用的时候才开始初始化的技巧. 不论是静态还是实例,都可以进行延迟初始化. 其本质是初始化开销和访问开销之间的权衡. 毕竟是一种优化 ...

  10. 转载:SQL中的case when then else end用法

    SQL中的case when then else end用法 来源: http://www.cnblogs.com/prefect/p/5746624.html Case具有两种格式.简单Case函数 ...