zoukankan      html  css  js  c++  java
  • SpringBoot+Mybatis配置多数据源,分包方式

    看了不少网上关于多数据源的配置,大致可分为两类,分包方式和通过切面方式;

    样例已上传至github:https://github.com/dadachao/multids

    第一个子项目ds01即时使用分包方式完成多数据源配置。

    总结项目中出现的问题和解决办法:

    数据库的连接信息:

    连接信息是写在db.properties文件中的:

    #数据库ds1
    spring.datasource.ds1.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.ds1.url=jdbc:mysql://localhost:3306/ds1?serverTimezone=UTC
    spring.datasource.ds1.username=root
    spring.datasource.ds1.password=root
    #数据库ds2
    spring.datasource.ds2.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.ds2.url=jdbc:mysql://localhost:3306/ds2?serverTimezone=UTC
    spring.datasource.ds2.username=root
    spring.datasource.ds2.password=root

    这些信息将在配置类DbConfig1.java中引用。一开始我是通过使用注解@ImportResource(...)引进db.properties文件,但在运行时报了org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 前言中不允许有内容的错误;使用这个注解也是我瞎猜的。后是通过使用注解@PropertySource(value = "classpath:/db.properties",encoding = "utf-8")解决问题。

    其次是关于在配置类中使用@ConfigurationProperties注解自动注入连接信息值(value)的问题:spring.datasource.ds1.url=jdbc:mysql://localhost:3306/ds1?serverTimezone=UTC

    注意要使用.url而不是.jdbc-url;

    指定数据连接池类型DataType:

    数据源类型可以在配置类生成DataSource的方法中指定:

    @Bean(name = "ds1DataSource")
        @Primary
        @ConfigurationProperties(prefix = "spring.datasource.ds1")
        public DataSource getDataSource(){
            DataSourceBuilder<?> dataSourceBuilder = DataSourceBuilder.create();
            dataSourceBuilder.type(com.alibaba.druid.pool.DruidDataSource.class);
            return dataSourceBuilder.build();
        }

    指定***Mapper.xml文件的路径扫描问题:(相当重要)

    使用配置类进行数据源相关进行配置后,原先在application.yml中配置的相关参数就不起作用了(原因未知),原先我是在application.yml中配置了.xml文件的扫描路径:

    mybatis:
      mapper-locations: classpath:/mybatis/**/*.xml
      type-aliases-package: com.kong.ds01.model

    但在运行时报错:Mapper Bound Error(not found);后来通过在配置类中写入扫描路径解决:

    public final static String mapperXmlLocation = "classpath:mybatis/*/*.xml";
    
    @Bean(name = "ds1SqlSessionFactory")
        @Primary
        public SqlSessionFactory getSqlSessionFactory(@Qualifier("ds1DataSource") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            sqlSessionFactoryBean.setDataSource(dataSource);
            sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperXmlLocation));
            return sqlSessionFactoryBean.getObject();
        }

    而且通过这种方法表示任意路径不能使用/**/,要使用/*/,否则识别不出来又会报相同的错误,这点真是太坑了!

    指定执行器的类型(Execute.Type):

    可以通过在配置类中的sqlSessionTemplate中指定:

    @Bean(name = "ds1SqlSessionTemplate")
        @Primary
        public SqlSessionTemplate getSqlSessionTemplate(@Qualifier("ds1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
            return new SqlSessionTemplate(sqlSessionFactory, ExecutorType.BATCH);
        }

    指定为BATCH类型后在进行批量操作时效率有明显的提高。

  • 相关阅读:
    DNNClassifier 深度神经网络 分类器
    浏览器对MP4视频 帧宽度 高度的兼容性
    UnicodeEncodeError:'latin-1' codec can't encode character
    文件夹下 文件计数
    the largest value you actually can transmit between the client and server is determined by the amount of available memory and the size of the communications buffers.
    the “identity” of an object
    广告特征 用户特征
    如果一个维度全覆盖,则有效维度应该对该维度全覆盖
    a high-level neural networks AP
    使用 LDA 挖掘的用户喜好主题
  • 原文地址:https://www.cnblogs.com/kongieg/p/12824606.html
Copyright © 2011-2022 走看看