基于druid编写数据库连接池通用的工具类

首先先建一个JdbcUtilsDruid 工具类

public class JdbcUtilsDruid {
    private static DataSource ds;

    //在静态代码块执行
    static {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("src/druid.properties"));
            ds = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //编写Connection方法
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

    //关闭资源
         /*
        这里的关闭资源不是真的关闭 , 因为用到了数据库连接池所以 只是断开了 连接对象
      */
    public static void close(ResultSet set, Statement statement, Connection connection){
        try {
            if (set != null){
                set.close();
            }
            if (statement != null){
                statement.close();
            }
            if (connection != null){
                connection.close();
            }
        } catch (SQLException e) {
            //异常转换上抛
            throw new RuntimeException(e);
        }
    }
}

在创建BasicDao 编写主要的DML语句

public class BasicDao<T> {//泛型指定具体类型
    QueryRunner qr = new QueryRunner();

    //开发一个DML方法
    public int update(String sql,Object... parameters){
        Connection connection = null;
        try {
            connection = JdbcUtilsDruid.getConnection();
            return qr.update(connection,sql,parameters);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JdbcUtilsDruid.close(null,null,connection);
        }
    }

    //返回多个对象

    /**
     *
     * @param sql       执行的sql语句
     * @param clazz     传入Class对象,domain类  反射mysql里的表
     * @param parameters        修改的 ?
     * @return
     */
    public List<T> queryMulti(String sql,Class<T> clazz,Object... parameters){
        Connection connection = null;
        try {
            connection = JdbcUtilsDruid.getConnection();
            return qr.query(connection,sql,new BeanListHandler<T>(clazz),parameters);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JdbcUtilsDruid.close(null,null,connection);
        }
    }

    //查询单行结果
    public T querySingle(String sql,Class<T> clazz,Object... parameters){
        Connection connection = null;
        try {
            connection = JdbcUtilsDruid.getConnection();
            return qr.query(connection,sql,new BeanHandler<T>(clazz),parameters);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JdbcUtilsDruid.close(null,null,connection);
        }
    }

    //查询单值结果
    public Object queryScalar(String sql,Object... parameters){
        Connection connection = null;
        try {
            connection = JdbcUtilsDruid.getConnection();
            return qr.query(connection,sql,new ScalarHandler<>(),parameters);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JdbcUtilsDruid.close(null,null,connection);
        }
    }
}

数据库的连接配置文件db.properties(?rewriteBatchedStatements=true为支持批量处理文件,可以将多行sql语句打包一起执行加快了效率)

user=root
password=root
url=jdbc:mysql://localhost:3306/hml?rewriteBatchedStatements=true
driver=com.mysql.jdbc.Driver

druid的配置文件druid.properties

# druid.properties文件的配置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/hml?rewriteBatchedStatements=true
username=root
password=root
# 初始化连接数量
initialSize=10
# 最大连接数
maxActive=50
#最小连接数
minIdle=5
# 最大超时时间  最长等5秒
maxWait=5000

jar包可以在Maven仓库中搜索自己想要的
https://mvnrepository.com/
根据自己的mysql版本选择jar包就好
mysql:https://mvnrepository.com/artifact/mysql/mysql-connector-java
数据库连接池
druid:https://mvnrepository.com/artifact/com.alibaba/druid
对数据集的封装
Apache Commons DbUtils:https://mvnrepository.com/artifact/commons-dbutils/commons-dbutils

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值