Java反射机制 完成通用的BaseDao

只是简单的练习了一下如何写一个通用的BaseDao的增删改查。

在这里插入图片描述

ConnectionPool 方法

两个静态方法:
getConnection(),用来得到一个连接。
close(Connection connection, Statement state, ResultSet reS),用于关闭资源。

package com.zhuqu.studyjdbc;

import com.alibaba.druid.pool.DruidDataSource;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class ConnectionPool {
   

    public static Connection getConnection(){
   
        Connection conn = null;
        Properties properties = new Properties();
        try {
   
            properties.load(ConnectionPool.class.getClassLoader().getResourceAsStream("DruidJDBC.properties"));
        } catch (IOException e) {
   
            e.printStackTrace();
            System.out.println("找不到资源文件 DruidJDBC.properties... ");
        }

        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.configFromPropety(properties);
        try {
   
           conn = druidDataSource.getConnection();
        } catch (SQLException e) {
   
            e.printStackTrace();
        }
        return conn;
    }

    public static void close(Connection connection, Statement state, ResultSet reS){
   
        if (reS != null){
   
            try {
   
                reS.close();
            } catch (SQLException e1) {
   
                e1.printStackTrace();
            }
        }
        if (state != null){
   
            try {
   
                state.close();
            } catch (SQLException e2) {
   
               e2.printStackTrace();
            }
        }
        if (connection != null){
   
            try {
   
                connection.close();
            } catch (SQLException e3) {
   
               e3.printStackTrace();
            }
        }
    }
}

BaseDao 接口

package com.zhuqu.studyjdbc;

import java.util.List;

public interface BaseDao<T> {
   

    int Save(T t);

    int Delete(T t);

    /**
     * 修改数据
     * @param t POJO
     * @param arrUpdate 要修该的opjo对象的字段集
     * @param arrKey 关键条件集
     * @return
     */
    int Update(T t, String[] arrUpdate,String[] arrKey);

    /**
     * 查找一个
     * @param tClass 
     * @param i
     * @return
     */
    T SelectByID(Class<T> tClass,int i);

    /**
     * 查找多个
     * @return
     */
    List<T> SelectAll();
}

接口 BaseDao 的实现类 BaseDaoImpl

package com.zhuqu.studyjdbc;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class BaseDaoImpl<T> implements BaseDao<T> {
   

    protected Connection conn = null;
    protected PreparedStatement stat = null;
    protected int returnI = -1;
    protected ResultSet resultSet = null;
    protected String Sql = null;

    @Override
    public int Save(T t) {
   
        returnI = -1;
        Object o = t;
        // 定义SQL:Insert Into T Values(?,?,?)
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,我无法为您提供完整的基于JavaBaseDao代码,因为BaseDao的实现可能因不同的应用程序而异。 但是,我可以提供一个基本的代码框架,以便您可以开始编写您自己的BaseDao类。 ```java public abstract class BaseDao<T> { private final Connection connection; public BaseDao(Connection connection) { this.connection = connection; } protected abstract String getTableName(); protected abstract T fromResultSet(ResultSet resultSet) throws SQLException; public List<T> findAll() throws SQLException { String query = "SELECT * FROM " + getTableName(); List<T> entities = new ArrayList<>(); try (PreparedStatement statement = connection.prepareStatement(query)) { ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { T entity = fromResultSet(resultSet); entities.add(entity); } } return entities; } public T findById(int id) throws SQLException { String query = "SELECT * FROM " + getTableName() + " WHERE id = ?"; try (PreparedStatement statement = connection.prepareStatement(query)) { statement.setInt(1, id); ResultSet resultSet = statement.executeQuery(); if (resultSet.next()) { return fromResultSet(resultSet); } } return null; } public void save(T entity) throws SQLException { // implement saving entity to database } public void update(T entity) throws SQLException { // implement updating entity in database } public void delete(T entity) throws SQLException { // implement deleting entity from database } } ``` 这是一个基本的BaseDao类,它使用Java的泛型来支持不同类型的实体对象。它包括以下方法: - `findAll()` - 返回一个包含所有实体对象的列表。 - `findById(int id)` - 根据ID返回一个实体对象。 - `save(T entity)` - 将一个实体对象保存到数据库中。 - `update(T entity)` - 更新一个实体对象在数据库中的记录。 - `delete(T entity)` - 从数据库中删除一个实体对象。 您需要根据您的应用程序的需要实现这些方法,以便BaseDao类能够正常工作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值