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

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

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

最近在学习尚硅谷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();
             
            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 {
    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的真实的类型
        Type actualType = actualTypeArguments[0];

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

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


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

        Connection connection=null;
        try {
            connection=DruidConnection.getConnection();
            List query = queryRunner.query(connection, sql, new BeanListHandler(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(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);
        }
    }

}

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

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

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