栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springBoot多数据源连接池(传统xml配置)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springBoot多数据源连接池(传统xml配置)

零、需求需要在一个项目中访问多个数据源,解决方案。项目的数据源是通过Druid配置的数据库连接池方案,通过配置Druid的主副数据源配置,实现在应用方可以便捷切换数据库设置。

一、pom依赖

implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.25'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.4'
compile group: 'com.alibaba', name: 'druid', version: '1.2.4'

二、application.properties配置文件

#淘宝主数据源
taobao.jdbc.driverClassName=com.mysql.cj.jdbc.Driver
taobao.jdbc.url=jdbc:mysql://1.2.3.4:3306/taobaodb?allowMultiQueries=true  //允许多sql执行
taobao.jdbc.username=taobao
taobao.jdbc.password=taobao123
taobao.jdbc.maxActive=10 #最大连接数
taobao.jdbc.maxIdle=10 #最大维持空闲连接
taobao.jdbc.minIdle=2 #最小维持空闲连接数
#腾讯副数据源
tencent.jdbc.driverClassName=com.mysql.cj.jdbc.Driver
tencent.jdbc.url=jdbc:mysql://10.20.30.40:3306/tencentdb?allowMultiQueries=true
tencent.jdbc.username=tengcent
tencent.jdbc.password=tengcent1233
tencent.jdbc.maxActive=10 
tencent.jdbc.maxIdle=10
tencent.jdbc.minIdle=2

#其他可配置连接池参数
initialSize 初始化连接数
maxWait 连接池出借连接最长期限,单位毫秒
maxAge 连接寿命,超过寿命连接池会删除连接

三、Main函数,引用xml配置

@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
@ImportResource({"classpath:applicationContext.xml"})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application .class,args);
    }
}

四、applicationContext.xml 配置数据库连接池文件





    


    
    

    
        
    


    
        
        
        
        

        
        
        
    

    
        
        
        
        
        
    

    
    
        
        
    

    
    
        
        
        
        
        
        
        
    
    
        
        
        
    

    
        
    



五、很一般的mybatis-ciconfig.xml文件





    
        
        
        
    

六、Mybatis引用数据源工具类 MySqlUtil.java

路径:com/longze/MySqlUtil.java

@Component
public class MysqlUtil {

    private static WebApplicationContext context;

    public static WebApplicationContext getContext() {
        return context;
    }

    @Autowired
    public void setContext(WebApplicationContext context) {
        setContextStatic(context);
    }
    //taobao主数据源
    public static SqlSession getTaobaoSqlSession() {
        return (SqlSession) context.getBean("sqlSession");
    }
    //tencent副数据源
    public static SqlSession getTengcentSqlSession() {
        return (SqlSession) context.getBean("tencentSqlSession");
    }

    public static void setContextStatic(WebApplicationContext context){
        MyBatisUtil.context=context;
    }
}

七、两种调用sql方案

在一个普通service文件层

@Service
public class DateServiceImpl {
    public Integer insTaobao(List params){
        Integer result = MyBatisUtil.getTaoBaoSqlSession().insert("com.longze.taobao.dao.taobaoinsert",params);
        return result;
    }

    public Integer insTencent(){
        Integer result = MyBatisUtil.getTencentSqlSession().insert("com.longze.tencent.dao.tancentinsert");
        return result;
    }
}

八、要注意两个文件的xml,sql脚本文件是分开的。对应的分别是配置好的

taobao -> classpath:mappers/*.xml

tencent -> classpath:tencentMappers/*.xml

因此对应的sql的xml文件再如下路径中:

taobao: src/main/resource/mappers/sqldao1.xml

tencent: src/main/resource/tencentMappers/sqldao2.xml

对应的两个sql的xml文件抬头如下

淘宝:





        

腾讯:


        

九、完成

完成了如上配置,就可以在代码上自由的加上多数据源配置了。同时还是数据库连接池

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/891090.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号