最近在学习尚硅谷2022版本的javaWeb,然而没有他写的数据库连接代码,我就依靠druid和dbutils重新写了一部分类似的代码

DruidConnection:

package com.myssm.basedao;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DruidConnection {
    private static Properties properties;
    private static DataSource dataSource;
    static {
        try {
             properties= new Properties();
             /*DruidConnection.class.getClassLoader().getResourceAsStream("druid.properties")
             * getClass().getResourceAsStream("/druid.properties");
             * JdbcUtils.class.GetClassLoader().getResourceAsStream(druid配置文件)
              * */
            properties.load(new FileInputStream("D:\\Program Files\\Pro-web\\druid.properties"));
            dataSource = DruidDataSourceFactory.createDataSource(properties);


        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public static Connection getConnection() {
        try {
//            properties= new Properties();
//            properties.load(new FileInputStream("D:\\Program Files\\Pro-web\\pro09-fruit1.4-thymeleaf\\src\\druid.properties"));
            dataSource = DruidDataSourceFactory.createDataSource(properties);
            return dataSource.getConnection();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    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();
        }
    }
}

下面这个和小破站的方法稍微有些许不同

package com.myssm.basedao;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public abstract class BaseDAO<T> {
    private QueryRunner queryRunner=new QueryRunner();
    private Class entityClass ;
//    private DruidConnection druidConnection = new DruidConnection();
    public BaseDAO(){
        Type genericType = getClass().getGenericSuperclass();
        //ParameterizedType 参数化类型
        Type[] actualTypeArguments = ((ParameterizedType) genericType).getActualTypeArguments();
        //获取到的<T>中的T的真实的类型
        Type actualType = actualTypeArguments[0];

        try {
            entityClass = Class.forName(actualType.getTypeName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

//        Class<T> clazz=(Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }


    public List<T> executeQuery(String sql , Object... params){

        Connection connection=null;
        try {
            connection=DruidConnection.getConnection();
            List<T> query = queryRunner.query(connection, sql, new BeanListHandler<T>(entityClass),params);
            return query;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DruidConnection.close(null,null,connection);
        }
    }

//    更新
    public int executeUpdate(String sql,Object... params){
        Connection connection=null;
        try {
            connection=DruidConnection.getConnection();
            int update = queryRunner.update(connection, sql,params);
            return update;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DruidConnection.close(null,null,connection);
        }
    }

    //获取单个
    public T executeSingleQuery(String sql , Object... params){
        Connection connection=null;
        try {
//            String sql="select * from t_fruit where fname like ? ";
            connection=DruidConnection.getConnection();
            return queryRunner.query(connection, sql, new BeanHandler<T>(entityClass),params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DruidConnection.close(null,null,connection);
        }
    }
    //获取列表数量
    public int getSum(){
        Connection connection=null;
        try {
            connection=DruidConnection.getConnection();
            QueryRunner queryRunner = new QueryRunner();
            Object query = queryRunner.query(connection, "select count(*) from t_fruit",new ScalarHandler<>());
            int sum=Integer.parseInt(String.valueOf(query));
            return sum;
        }catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    //模糊查找获取列表数量
    public int getSumLike(String fname){
        Connection connection=null;
        try {
            connection=DruidConnection.getConnection();
            QueryRunner queryRunner = new QueryRunner();
            Object query = queryRunner.query(connection, "select count(*) from t_fruit where fname like ?",new ScalarHandler<>(),"%"+fname+"%");
            int sum=Integer.parseInt(String.valueOf(query));
            return sum;
        }catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值