zoukankan      html  css  js  c++  java
  • SpringBoot之SpringBoot整合多数据源

    SpringBoot之SpringBoot整合多数据源

    本来按照视屏来说,到上一章的打包运行就没有了,但是我百度翻了好一大波,找到了他的上一期中的其他剩余视屏

    本来想写一章整合JSP的,但是其中存在一个问题,就是SpringBoot对JSP的支持不友好,强制整合完成后,打成jar包运行时会报找不到页面,只有打成war包才能用,对此我表示推荐使用Thymeleaf

    概念:

      多数据源?什么是多数据源,emmm,一听就是多个数据源,在单体式项目中一般不会使用到多数据源,一般单数据源就可以玩转了

    创建数据库和表:

    之前已经存在了一个数据库和表了,为了实现多数据源就再创建一个

     使用navicat创建的,然后创建表,表的话换一个名字吧

    建表语句

    CREATE TABLE `dts` (
      `id` int(11) NOT NULL,
      `dts` varchar(255) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    添加Maven依赖:

    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- springboot 整合mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>

    开始配置多数据源

    为了不动原来的配置,我决定再次拷贝一个dts的环境出来

     然后在主要的application.yml中激活

     修改application-dts.yml配置

    这里注意一个问题,如果是SpringBoot2以上配置多数据源需要把url改为jdbc-url,不然会报错

    原来的数据源配置

     新的数据库配置

    多数据源其实对于作用领域是有多种实现形式,最常见的就是注解和分包,我采用分包来完成作用域的划分

    重新定义目录结构,按照数据源的配置分了两个包,之前的向springboot中移动,下面的是新加的,关于

     把Mapper按照不同的包分开,然后增加了新表的也就是新数据库中dts表的Mapper和Service

    DtsMapper.java

    package com.springboot.demo.springbootdts.mapper;
    
    import org.apache.ibatis.annotations.Insert;
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Param;
    
    /**
     * @author ZYGisComputer
     */
    @Mapper
    public interface DtsMapper {
    
        @Insert("insert into dts value(#{id},#{dts});")
        int insertUser(@Param("id")String id,@Param("dts")String dts);
    
    }

    DtsService.java

    package com.springboot.demo.service;
    
    import com.springboot.demo.springbootdts.mapper.DtsMapper;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @author ZYGisComputer
     */
    @RestController
    public class DtsService {
    
        @Autowired
        private DtsMapper dtsMapper;
    
        @GetMapping("/dts")
        public String insertDts(String id, String dts) {
            return dtsMapper.insertUser(id, dts) > 0 ? "success" : "error";
        }
    }

    编写配置类:

    在config包下创建两个配置类,分别用于配置两个数据源

     SpringBootDataSourceConfig.java

    package com.springboot.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.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.jdbc.datasource.DataSourceTransactionManager;
    
    import javax.sql.DataSource;
    
    @Configuration
    @MapperScan(basePackages = "com.springboot.demo.springboot.mapper", sqlSessionFactoryRef = "springbootSqlSessionFactory")
    public class SpringBootDataSourceConfig {
    
        /**
         * 将会员db注册到容器中
         *
         * @return
         */
        @Bean(name = "springbootDataSource")
        @ConfigurationProperties(prefix = "spring.datasource.springboot")
        public DataSource springbootDataSource() {
            return DataSourceBuilder.create().build();
        }
    
        /**
         * 将会员SqlSessionFactory注册到容器中
         *
         * @param dataSource
         * @return
         * @throws Exception
         */
        @Bean(name = "springbootSqlSessionFactory")
        public SqlSessionFactory springbootSqlSessionFactory(@Qualifier("springbootDataSource") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            sqlSessionFactoryBean.setDataSource(springbootDataSource());
            return sqlSessionFactoryBean.getObject();
        }
    
        /**
         * 创建会员管理器
         *
         * @param dataSource
         * @return
         */
        @Bean(name = "springbootTransactionManager")
        public DataSourceTransactionManager springbootTransactionManager(@Qualifier("springbootDataSource") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    
        /**
         * 创建订单sqlSesion模版
         *
         * @param sqlSessionFactory
         * @return
         * @throws Exception
         */
        @Bean(name = "springbootSqlSessionTemplate")
        public SqlSessionTemplate springbootSqlSessionTemplate(
                @Qualifier("springbootSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    
    
    }

    SpringBootDtsDataSourceConfig.java

    package com.springboot.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.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.jdbc.datasource.DataSourceTransactionManager;
    
    import javax.sql.DataSource;
    
    @Configuration
    @MapperScan(basePackages = "com.springboot.demo.springbootdts.mapper", sqlSessionFactoryRef = "springbootdtsSqlSessionFactory")
    public class SpringBootDtsDataSourceConfig {
        /**
         * 将会员db注册到容器中
         *
         * @return
         */
        @Bean(name = "springbootdtsDataSource")
        @ConfigurationProperties(prefix = "spring.datasource.springbootdts")
        public DataSource springbootdtsDataSource() {
            return DataSourceBuilder.create().build();
        }
    
        /**
         * 将会员SqlSessionFactory注册到容器中
         *
         * @param dataSource
         * @return
         * @throws Exception
         */
        @Bean(name = "springbootdtsSqlSessionFactory")
        public SqlSessionFactory springbootdtsSqlSessionFactory(@Qualifier("springbootdtsDataSource") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            sqlSessionFactoryBean.setDataSource(springbootdtsDataSource());
            return sqlSessionFactoryBean.getObject();
        }
    
        /**
         * 创建会员管理器
         *
         * @param dataSource
         * @return
         */
        @Bean(name = "springbootdtsTransactionManager")
        public DataSourceTransactionManager springbootdtsTransactionManager(@Qualifier("springbootdtsDataSource") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    
        /**
         * 创建订单sqlSesion模版
         *
         * @param sqlSessionFactory
         * @return
         * @throws Exception
         */
        @Bean(name = "springbootdtsSqlSessionTemplate")
        public SqlSessionTemplate springbootdtsSqlSessionTemplate(
                @Qualifier("springbootdtsSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }

    两个配置文件相似度90%只有其中的名字不一样

    配置完成后启动项目,但是中途报了一个错误,找不到JdbcTemplate,如果是从前面的文章一直看到后面,因为之前整合过JdbcTemplate,所以他需要单独的数据源,直接注释掉

    启动项目测试:

    测试SpringBoot数据库插入数据

     调用接口成功,查看数据库数据

     这条就是新插入的

    测试SpringBoot_dts数据库插入数据

    查看数据库数据

     插入成功,到此多数据源,分包策略整合完成

    作者:彼岸舞

    时间:2021128

    内容关于:SpringBoot

    本文来源于网络,只做技术分享,一概不负任何责任  

  • 相关阅读:
    jieba的使用
    如何用python查看自己的电脑有几个核
    NLTK实现文本切分
    nltk的安装和简单使用
    初识NLP 自然语言处理
    mysql 查询存在A表中而不存在B表中的数据
    mysql 导出数据报错: row must be in range 0-65535
    python3 re模块正则匹配字符串中的时间信息
    Python语法速查: 14. 测试与调优
    Python语法速查: 20. 线程与并发
  • 原文地址:https://www.cnblogs.com/flower-dance/p/14339148.html
Copyright © 2011-2022 走看看