相关文章推荐

一、前言

本教程主要内容

本教程开发环境

1、操作系统: Windows 10 X64
2、Java SDK: jdk-8u141
3、Maven:3.5
4、IDE:IntelliJ IDEA 2017
5、Spring Boot:1.5.6

本项目构建基于: https://ken.io/note/springboot-course-basic-curd-annotation

二、多数据源配置

1、创建数据库&表

CREATE DATABASE test;
  • 创建表:note
  • DROP TABLE IF EXISTS `note`;
    CREATE TABLE `note` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `title` varchar(255) DEFAULT NULL,
      `body` longtext,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
    

    2、修改配置文件增加数据源

    修改数据库连接配置(application.yml),将之前的spring.datasource更换为自定义的courseDataSource、testDataSource

    #数据库连接配置
    courseDataSource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/course?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8
        username: root
        password: root
    testDataSource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/test?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8
        username: root
        password: root
    

    3、调整数据源对应实体

    1、在io.ken.springboot.course.model下新建两个package:course、test
    2、将User.java移动到io.ken.springboot.course.model.course
    3、在io.ken.springboot.course.model.test下新建Note.java

    package io.ken.springboot.course.model.test;
    public class Note {
        private int id;
        private String title;
        private String body;
        public int getId() {
            return id;
        public void setId(int id) {
            this.id = id;
        public String getTitle() {
            return title;
        public void setTitle(String title) {
            this.title = title;
        public String getBody() {
            return body;
        public void setBody(String body) {
            this.body = body;
    

    4、调整数据访问接口

    1、在io.ken.springboot.course.dao下新建两个package:course、test
    2、将UserMapper.java移动到io.ken.springboot.course.dao.course
    3、在io.ken.springboot.course.dao.test下新建NoteMapper.java

    package io.ken.springboot.course.dao.test;
    import io.ken.springboot.course.model.test.Note;
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Select;
    import java.util.List;
    @Mapper
    public interface NoteMapper {
        @Select("SELECT * FROM note")
        List<Note> queryAll();
    

    5、配置数据源对应Configuration

    1、在io.ken.springboot.course下新建package:config
    2、在io.ken.springboot.course.config下创建CourseDbConfig.java

    package io.ken.springboot.course.config;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    import javax.sql.DataSource;
    @Configuration
    @MapperScan(basePackages = "io.ken.springboot.course.dao.course", sqlSessionFactoryRef = "courseSqlSessionFactory")
    public class CourseDbConfig {
        @Bean(name = "courseDataSource")
        @ConfigurationProperties(prefix = "courseDataSource")
        @Primary
        public DataSource courseDataSource() {
            return DataSourceBuilder.create().build();
        @Bean(name = "courseSqlSessionFactory")
        @Primary
        public SqlSessionFactory courseSqlSessionFactory(@Qualifier("courseDataSource") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(dataSource);
            return bean.getObject();
    

    3、在io.ken.springboot.course.config下创建TestDbConfig.java

    package io.ken.springboot.course.config;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import javax.sql.DataSource;
    @Configuration
    @MapperScan(basePackages = "io.ken.springboot.course.dao.test", sqlSessionFactoryRef = "testSqlSessionFactory")
    public class TestDbConfig {
        @Bean(name = "testDataSource")
        @ConfigurationProperties(prefix = "testDataSource")
        public DataSource testDataSource() {
            return DataSourceBuilder.create().build();
        @Bean(name = "testSqlSessionFactory")
        public SqlSessionFactory testSqlSessionFactory(@Qualifier("testDataSource") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(dataSource);
            return bean.getObject();
    

    6、创建NoteController并提供API

    package io.ken.springboot.course.controller;
    import io.ken.springboot.course.dao.test.NoteMapper;
    import io.ken.springboot.course.model.test.Note;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import java.util.List;
    @Controller
    @RequestMapping("/note")
    public class NoteController {
        @Autowired
        NoteMapper noteMapper;
        @RequestMapping("/queryall")
        @ResponseBody
        List<Note> queryAll() {
            return noteMapper.queryAll();
    

    7、API测试

     
    推荐文章