• java代码实现数据源切换(连接池简单粗暴)


    --最佳实践配置(.yml)

    gs.oracle:
    #driverClassName: oracle.jdbc.driver.OracleDriver
    driverClassName: oracle.jdbc.OracleDriver
    username: myuser
    password: root
    url: jdbc:oracle:thin:@127.0.0.1:1521:orcl

    --最佳实践(注意在springboot的.yml中配置gs.oracle,若使用gs_oracle,@@ConfigurationProperties会提示使用规范配置)

    package com.dhht.config;

    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;

    /**
    * @Author: sh
    * @Description: oracleDataCfg 前置数据库配置类
    * @Date: 9:26 2019/7/23
    */
    @Component
    @ConfigurationProperties(prefix = "gs.oracle")
    @Data
    public class OrcaleDataCfg {

    private String driverClassName;
    private String username;
    private String password;
    private String url;
    }

    --最佳实践DbConnect

    package com.dhht.wechat.util;

    import com.alibaba.druid.pool.DruidDataSource;
    import com.dhht.config.OrcaleDataCfg;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.stereotype.Component;

    import javax.annotation.Resource;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.util.Date;
    import java.util.List;
    import java.util.Map;

    /**
    * @Author: sh
    * @Description: DbConnect
    * @Date: 23:01 2019/7/22
    */
    @Component
    public class DbConnect {

    @Resource
    OrcaleDataCfg orcaleDataCfg;

    private static DruidDataSource dataSource = null;
    private static JdbcTemplate jdbcTemplate = null;

    /**
    * 构造函数完成数据库的连接和连接对象的生成
    *
    * @throws Exception
    */
    private DbConnect() {

    }

    public synchronized DruidDataSource GetDbConnect() throws Exception {
    try {
    if (dataSource == null) {
    dataSource = new DruidDataSource();
    //设置连接参数
    dataSource.setUrl(orcaleDataCfg.getUrl());
    dataSource.setDriverClassName(orcaleDataCfg.getDriverClassName());
    dataSource.setUsername(orcaleDataCfg.getUsername());
    dataSource.setPassword(orcaleDataCfg.getPassword());
    //配置初始化大小、最小、最大
    dataSource.setInitialSize(5);
    dataSource.setMinIdle(5);
    dataSource.setMaxActive(5);
    //连接泄漏监测
    dataSource.setRemoveAbandoned(true);
    dataSource.setRemoveAbandonedTimeout(30);
    //配置获取连接等待超时的时间
    dataSource.setMaxWait(20000);
    //配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
    dataSource.setTimeBetweenEvictionRunsMillis(20000);
    //防止过期,Oracle必须添加 FROM DUAL!
    dataSource.setValidationQuery("SELECT 'x' FROM DUAL");
    dataSource.setTestWhileIdle(true);
    dataSource.setTestOnBorrow(true);
    return dataSource;
    }
    } catch (Exception e) {
    return null;
    }
    return null;
    }

    /**
    * 取得已经构造生成的数据库连接
    *
    * @return 返回数据库连接对象
    * @throws Exception
    */
    public Connection getConnect() throws Exception {
    Connection con = null;
    try {
    GetDbConnect();
    con = dataSource.getConnection();
    } catch (Exception e) {
    throw e;
    }
    return con;
    }

    public synchronized JdbcTemplate getJdbcTemplate() throws Exception{
    DruidDataSource druidDataSource = GetDbConnect();
    if(jdbcTemplate==null){
    jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setDataSource(druidDataSource);
    }
    return jdbcTemplate;
    }


    public static void main(String[] args) throws Exception {
    DbConnect dbConnect = new DbConnect();
    Connection connection = dbConnect.getConnect();
    String sql = "SELECT * from DZHY_GAYZ_KZLOG";
    PreparedStatement ps = connection.prepareStatement(sql);
    ResultSet resultSet = ps.executeQuery();
    if (resultSet.next()) {
    System.out.println(resultSet.getInt(1));
    }
    JdbcTemplate jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setDataSource(dataSource);
    List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
    String SQL = "insert into DZHY_GAYZ_KZLOG (id,uniscid,entname,startdate,enddate,kz_uniscid,kz_entname) values (?,?,?,?,?,?,?)";
    jdbcTemplate.update(SQL,DateUtil.get32UUIDMilli(),"333","需要刻章33",new Date(),new Date(),"33-33","刻章店33");
    }
    }
  • 相关阅读:
    Spring Security -- 添加图形验证码(转载)
    Spring Security -- 自定义用户认证(转载)
    pandas agg 后降低df表层
    判断是否有人跟踪车辆的方案
    格隆汇笔记-黄勇演讲
    Mac mysql 忘记root密码的解决方法
    sql 同步2个表中的一个字段数据
    Linux下配置用msmtp和mutt发邮件
    spark StructType的应用,用在处理mongoDB keyvalue
    Idea2018旗舰版破解方法
  • 原文地址:https://www.cnblogs.com/sung1024/p/11240139.html
Copyright © 2020-2023  润新知