[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[备忘]springboot集成druid,mybatis,通用mytatis,多数据源

上一篇:[备忘]UIAutomator2一直不点按钮,Could not detect idle state
下一篇:[备忘]linux查找文件,并替换

添加日期:2020/5/28 8:14:20 快速返回   返回列表 阅读1321次
配置文件这样:


spring.datasource.monitor.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.monitor.url=jdbc:mysql://1.1.1.1:3306/monitor?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true
spring.datasource.monitor.username=aaa
spring.datasource.monitor.password=123
spring.datasource.monitor.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.monitor.initialSize=10 
spring.datasource.monitor.minIdle=10
spring.datasource.monitor.maxActive=200
spring.datasource.monitor.maxWait=6000
spring.datasource.monitor.timeBetweenEvictionRunsMills=6000
spring.datasource.monitor.minEvictableIdleTimeMillis=30000
spring.datasource.monitor.validationQuery=SELECT 'x'
spring.datasource.monitor.testWhileIdle=false
spring.datasource.monitor.testOnBorrow=true
spring.datasource.monitor.testOnReturn=false

spring.datasource.order.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.order.url=jdbc:mysql://1.1.1.1:3306/order?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true
spring.datasource.order.username=aaa
spring.datasource.order.password=123
spring.datasource.order.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.order.initialSize=10 
spring.datasource.order.minIdle=10
spring.datasource.order.maxActive=200
spring.datasource.order.maxWait=6000
spring.datasource.order.timeBetweenEvictionRunsMills=6000
spring.datasource.order.minEvictableIdleTimeMillis=30000
spring.datasource.order.validationQuery=SELECT 'x'
spring.datasource.order.testWhileIdle=false
spring.datasource.order.testOnBorrow=true
spring.datasource.order.testOnReturn=false


-------------------------------
然后写俩配置类。


package com.xxx;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;

@Configuration
public class DataSourceOrderConfig {

    @Bean(name = "dataSource_order")
    @ConfigurationProperties(prefix = "spring.datasource.order")
    public DataSource testDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "sqlSessionFactory_order")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("dataSource_order") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/**/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "transactionManager_order")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("dataSource_order") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

}


另外一个,照葫芦画瓢。
xml文件不区分,应该没有关系(我实际试验了,xml放一起,也没问题的)。
接口类的包名区分开就行。
注意这句话:
DruidDataSourceBuilder.create().build();
不这么写,你会发现莫名其妙的错误,哈哈。
---------------------------------------------------


package com.xxx;

import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;
import java.util.Properties;

/**
 * 通用mapper配置类
 * 
 */
@Configuration
@AutoConfigureAfter(MybatisAutoConfiguration.class)
public class MapperScannerConfig {

    /**
     * Monitor的Mapper.
     * @return
     */
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory_monitor");
        mapperScannerConfigurer.setBasePackage("com.xxx.monitor.persistence");

        // 这里properties的相关配置
        Properties properties = new Properties();
        properties.setProperty("mappers",
                "tk.mybatis.mapper.common.Mapper,tk.mybatis.mapper.common.special.InsertListMapper");
        mapperScannerConfigurer.setProperties(properties);

        return mapperScannerConfigurer;
    }
    
    /**
     * 订单系统的Mapper.
     * @return
     */
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurerOrder() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory_order");
        mapperScannerConfigurer.setBasePackage("com.xxx.order.persistence");

        // 这里properties的相关配置
        Properties properties = new Properties();
        properties.setProperty("mappers",
                "tk.mybatis.mapper.common.Mapper,tk.mybatis.mapper.common.special.InsertListMapper");
        mapperScannerConfigurer.setProperties(properties);

        return mapperScannerConfigurer;
    }
}


其实就是,把xml翻译成了java类,也真够累的,图啥。
===================================
注意,如果xml文件有多个路径,使用逗号分隔是不行的。需要传数组进去.
参考:


    SqlSessionFactoryBean bean= new SqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    bean.setMapperLocations(resolveMapperLocations());
    return bean.getObject();

    public Resource[] resolveMapperLocations() {
        ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
        List<String> mapperLocations = new ArrayList<>();
        mapperLocations.add("classpath*:com/aaa/mapper/*Mapper*.xml");
        mapperLocations.add("classpath*:com/bbb/mapper/*Mapper*.xml");
        mapperLocations.add("classpath*:com/ccc/mapper/*Mapper*.xml");
        List<Resource> resources = new ArrayList();
        if (mapperLocations != null) {
            for (String mapperLocation : mapperLocations) {
                try {
                    Resource[] mappers = resourceResolver.getResources(mapperLocation);
                    resources.addAll(Arrays.asList(mappers));
                } catch (IOException e) {
                    // ignore
                }
            }
        }
        return resources.toArray(new Resource[resources.size()]);
    }


 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved