使用JDBC编写通用的查询方法

实现的效果:
通过传入SQL语句和参数值,返回相应的实体类,而又不依赖实体类。也即模拟MyBatis结果集与相应的实体类的映射。

思路:
- SQL查询参数可以使用可变参数列表。
- 利用反射来设置实体类的相应的字段。

public static <T> List<T> query(Class<T> tClass, String sql, Object ...args) {
        T entity = null;
        List<T> list = null;
        Connection conn = null;
        PreparedStatement statement = null;
        ResultSet rs = null;

        try {
            conn = getConnection();
            statement = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                statement.setObject(i + 1, args[i]);
            }
            rs = statement.executeQuery();

            //创建 ResultSetMetaData 来获得数据库表的字段名等元数据
            ResultSetMetaData metaData = rs.getMetaData();
            // 创建一个 Map<String, Object> 对象
            // 键: SQL 查询的列的别名
            // 值: SQL 查询的列的值
            Map<String, Object> map = new HashMap<>();
            //保存查询得到的数据
            list = new ArrayList<>();

            while (rs.next()) {
                map.clear();
                for (int i = 0; i < metaData.getColumnCount(); i++) {
                    String columnLabel = metaData.getColumnLabel(i + 1);
                    Object columnValue = rs.getObject(columnLabel);
                    map.put(columnLabel, columnValue);
                }
                //若 Map 不为空集, 利用反射创建 tClass 对应的对象
                if (!map.isEmpty()) {
                    try {
                        entity = tClass.newInstance();
                        //遍历 Map 对象, 利用反射为 Class 对象的对应的属性赋值
                        for (Map.Entry<String, Object> entry : map.entrySet()) {
                            String name = entry.getKey();
                            Object value = entry.getValue();
                            try {
                                //利用反射设置实体类的字段
                                Field field = tClass.getDeclaredField(name);
                                field.setAccessible(true);
                                field.set(entity, value);
                            } catch (NoSuchFieldException e) {
                                e.printStackTrace();
                            }
                        }
                    } catch (InstantiationException | IllegalAccessException e) {
                        e.printStackTrace();
                    }
                    list.add(entity);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeQuietly(conn, statement, rs);
        }
        return list;
    }

对应的Person实体类:

package com.xiya.entity;

import java.util.Date;

/**
 * Created by N3verL4nd on 2017/4/17.
 */
public class Person {
    private int id;
    private String name;
    private int age;
    private Date birth;
    private String email;

    public Person() {
    }

    public Person(int id, String name, int age, Date birth, String email) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.birth = birth;
        this.email = email;
    }

    public Person(String name, int age, Date birth, String email) {
        this.name = name;
        this.age = age;
        this.birth = birth;
        this.email = email;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", birth=" + birth +
                ", email='" + email + '\'' +
                '}';
    }
}

测试:

String sql = "SELECT * FROM persons";
        List<Person> list = JDBCTools.query(Person.class, sql);
        list.forEach(System.out::println);

完整测试代码:
JDBCTools.java

package com.xiya.test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.*;

/**
 * Created by N3verL4nd on 2017/4/18.
 */
public class JDBCTools {

    /**
     * 获取数据库连接
     * @return Connection
     */
    public static Connection getConnection() throws SQLException{
        Properties properties = new Properties();
        InputStream in;
        in = JDBCTools.class.getClassLoader().getResourceAsStream("jdbc.properties");
        try {
            properties.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //String driverClassName = properties.getProperty("jdbc.driverClassName");
        String jdbcUrl = properties.getProperty("jdbc.url");
        String user = properties.getProperty("jdbc.username");
        String password = properties.getProperty("jdbc.password");

        /*
        * 因为 ServiceLoader 所以不再需要如下函数调用。
        try {
            Class.forName(driverClassName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }*/

        return DriverManager.getConnection(jdbcUrl, user, password);
    }


    /**********Copied from DBUtil**********/
    public static void close(ResultSet rs) throws SQLException {
        if (rs != null) {
            rs.close();
        }
    }

    public static void close(Statement stmt) throws SQLException {
        if (stmt != null) {
            stmt.close();
        }
    }

    public static void close(Connection conn) throws SQLException {
        if (conn != null) {
            conn.close();
        }
    }

    public static void closeQuietly(Connection conn) {
        try {
            close(conn);
        } catch (SQLException e) {
            //e.printStackTrace();
        }
    }

    public static void closeQuietly(ResultSet rs) {
        try {
            close(rs);
        } catch (SQLException e) {
            //e.printStackTrace();
        }
    }

    public static void closeQuietly(Statement stmt) {
        try {
            close(stmt);
        } catch (SQLException e) {
            //e.printStackTrace();
        }
    }

    /**
     * 释放数据库资源
     * @param conn  Connection
     * @param stmt  Statement
     * @param rs    ResultSet
     */
    public static void closeQuietly(Connection conn, Statement stmt, ResultSet rs) {
        try {
            closeQuietly(rs);
        } finally {
            try {
                closeQuietly(stmt);
            } finally {
                closeQuietly(conn);
            }
        }
    }
    /**********Copied from DBUtil**********/
}

Dao.java

package com.xiya.test;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;

import java.lang.reflect.InvocationTargetException;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Dao {

    /**
     * 获取结果集的 ColumnLabel 对应的 List
     * @param rs
     * @return
     * @throws SQLException
     */
    private List<String> getColumnLabels(ResultSet rs) throws SQLException {
        ResultSetMetaData metaData = null;
        List<String> labels = new ArrayList<>();
        metaData = rs.getMetaData();
        for (int i = 0; i < metaData.getColumnCount(); i++) {
            labels.add(metaData.getColumnLabel(i + 1));
        }
        return labels;
    }

    /**
     * 通用查询方法
     * @param tClass
     * @param sql
     * @param args
     * @param <T>
     * @return
     */
    public <T> List<T> query(Class<T> tClass, String sql, Object ...args) {
        T entity = null;
        List<T> list = null;
        Connection conn = null;
        PreparedStatement statement = null;
        ResultSet rs = null;

        try {
            conn = JDBCTools.getConnection();
            statement = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                statement.setObject(i + 1, args[i]);
            }
            rs = statement.executeQuery();

            //数据库字段别名列表
            List<String> labels = getColumnLabels(rs);

            // 创建一个 Map<String, Object> 对象
            // 键: SQL 查询的列的别名
            // 值: SQL 查询的列的值
            Map<String, Object> map = new HashMap<>();
            //保存查询得到的数据
            list = new ArrayList<>();

            while (rs.next()) {
                map.clear();
                //每个 Map 对象对应一条数据库记录
                for (String columnLabel : labels) {
                    map.put(columnLabel, rs.getObject(columnLabel));
                }
                //若 Map 不为空集, 利用反射创建 tClass 对应的对象
                if (!map.isEmpty()) {
                    try {
                        entity = tClass.newInstance();
                        //遍历 Map 对象, 利用反射为 Class 对象的对应的属性赋值
                        for (Map.Entry<String, Object> entry : map.entrySet()) {
                            String name = entry.getKey();
                            Object value = entry.getValue();
                            try {

                                //利用反射设置实体类的字段
                                //Field field = tClass.getDeclaredField(name);
                                //field.setAccessible(true);
                                //field.set(entity, value);

                                //自定义转换格式(不设置,如果有Date字段为null,则出现异常)
                                ConvertUtils.register(new DateConverter(null), java.util.Date.class);
                                BeanUtils.setProperty(entity, name, value);
                            } catch (InvocationTargetException e) {
                                e.printStackTrace();
                            }
                        }
                    } catch (InstantiationException | IllegalAccessException e) {
                        e.printStackTrace();
                    }
                    list.add(entity);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.closeQuietly(conn, statement, rs);
        }
        return list;
    }

    /**
     * 通用更新方法
     * @param sql sql语句
     * @param args  参数
     */
    public int update(String sql, Object ...args) {
        Connection conn = null;
        PreparedStatement statement = null;
        int result = 0;
        try {
            conn = JDBCTools.getConnection();
            statement = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                statement.setObject(i + 1, args[i]);
            }
            result = statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.closeQuietly(statement);
            JDBCTools.closeQuietly(conn);
        }
        return result;
    }

    public <E> E getValue(String sql, Object... args) {
        Connection conn = null;
        PreparedStatement statement = null;
        ResultSet rs = null;

        try {
            conn = JDBCTools.getConnection();
            statement = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                statement.setObject(i + 1, args[i]);
            }
            rs = statement.executeQuery();
            if (rs.next()) {
                return (E) rs.getObject(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.closeQuietly(conn, statement, rs);
        }
        return null;
    }

}
JDBC通用查询封装是指将JDBC查询操作封装为通用方法,方便开发者在项目中进行调用,避免了重复的代码编写。 以下是一个简单的JDBC通用查询封装的示例代码: ```java public class JdbcUtil { private static String driver = "com.mysql.cj.jdbc.Driver"; // 数据库驱动 private static String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false"; // 数据库连接地址 private static String user = "root"; // 数据库用户名 private static String password = "123456"; // 数据库密码 static { try { // 加载数据库驱动 Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); } } /** * 获取数据库连接 * * @return Connection对象 */ public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url, user, password); } /** * 关闭数据库连接 * * @param conn Connection对象 * @param stmt Statement对象 * @param rs ResultSet对象 */ public static void close(Connection conn, Statement stmt, ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 执行查询操作,返回结果集 * * @param sql SQL语句 * @param args 参数列表 * @return ResultSet对象 */ public static ResultSet executeQuery(String sql, Object... args) throws SQLException { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = getConnection(); // 获取数据库连接 pstmt = conn.prepareStatement(sql); // 创建PreparedStatement对象 // 设置SQL语句中的参数 for (int i = 0; i < args.length; i++) { pstmt.setObject(i + 1, args[i]); } rs = pstmt.executeQuery(); // 执行查询操作 } catch (SQLException e) { e.printStackTrace(); throw e; } finally { close(conn, pstmt, null); // 关闭连接 } return rs; } } ``` 以上代码中,JdbcUtil类封装了获取数据库连接、关闭数据库连接、执行查询操作等通用方法。开发者只需要调用`executeQuery`方法即可实现查询操作,无需重复编写获取连接、执行SQL语句、关闭连接等代码。 示例调用代码如下: ```java String sql = "SELECT * FROM user WHERE age > ?"; ResultSet rs = JdbcUtil.executeQuery(sql, 18); while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); int age = rs.getInt("age"); System.out.println("id=" + id + ", name=" + name + ", age=" + age); } JdbcUtil.close(null, null, rs); // 关闭ResultSet对象 ``` 以上代码中,开发者只需要传入SQL语句和参数列表即可实现查询操作。同时,JdbcUtil类也封装了关闭ResultSet对象的方法,避免了重复的代码编写
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

N3verL4nd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值