PreparedStatement实现针对不同表的通用查询操作
PreparedStatement实现针对不同表的通用查询操作:查询一样和多行
PreparedStatementQueryTest
package com.aff.PreparedStatement; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.util.ArrayList; import java.util.List; import org.junit.Test; import com.aff.bean.Customer; import com.aff.bean.Order; import com.aff.utils.JDBCUtils; //使用PreparedStatement实现针对不同表的通用的查询操作 public class PreparedStatementQueryTest { @Test public void testGetForList() { String sql = "select id ,name,email from customers where id < ? "; List<Customer> list = getForList(Customer.class, sql, 5); list.forEach(System.out::println);//list的遍历方法 String sql1 = "select order_id orderId, order_name orderName from `order`
where order_id < ?"; List<Order> list2 = getForList(Order.class, sql1, 5); list2.forEach(System.out::println); } // 泛型集合方法加<T>List<T> public <T> List<T> getForList(Class<T> clazz, String sql, Object... args) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = JDBCUtils.getConnection(); // 执行,获取结果集 ps = conn.prepareStatement(sql); // 填充占位符 for (int i = 0; i < args.length; i++) { ps.setObject(i + 1, args[i]); } rs = ps.executeQuery(); // 获取结果集的元数据 ResultSetMetaData rsmd = rs.getMetaData(); // 获取列数 int columnCount = rsmd.getColumnCount(); // 创建一个集合对象 ArrayList<T> list = new ArrayList<>(); while (rs.next()) { T t = clazz.newInstance(); // 处理结果集一行数据的每一个列,给t对象指定的属性赋值 for (int i = 0; i < columnCount; i++) { // 获取每个列的列值 Object columnValue = rs.getObject(i + 1); // 获取列的列名,列数 列名为元数据用来修饰ResultSet(结果集)的, // 改为获取列的别名 String ColumnLabel = rsmd.getColumnLabel(i + 1); // 通过反射将对象指定名columnName的属性赋给指定的值columnValue // 先拿到class Field field = clazz.getDeclaredField(ColumnLabel); field.setAccessible(true); field.set(t, columnValue); } // 理解成每遍历完一行就添加进list集合中 list.add(t); } // while循环结束,再把添加过t对象的集合(理解为表中的每一行)返回 return list; } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtils.closeResource(conn, ps, rs); } return null; } @Test public void testGetInstance() { String sql = "select id ,name,email from customers where id = ? "; Customer customer = getInstance(Customer.class, sql, 12); System.out.println(customer); String sql1 ="select order_id orderId,order_name orderName from `order` where order_id = ?"; Order order = getInstance(Order.class, sql1, 2); System.out.println(order); } public <T> T getInstance(Class<T> clazz, String sql, Object... args) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = JDBCUtils.getConnection(); // 执行,获取结果集 ps = conn.prepareStatement(sql); // 填充占位符 for (int i = 0; i < args.length; i++) { ps.setObject(i + 1, args[i]); } rs = ps.executeQuery(); // 获取结果集的元数据 ResultSetMetaData rsmd = rs.getMetaData(); // 获取列数 int columnCount = rsmd.getColumnCount(); if (rs.next()) { T t = clazz.newInstance(); for (int i = 0; i < columnCount; i++) { // 获取每个列的列值 Object columnValue = rs.getObject(i + 1); // 获取列的列名,列数 列名为元数据用来修饰ResultSet(结果集)的, // String columnName = rsmd.getColumnName(i + 1);-- 不推荐使用 // 改为获取列的别名 String ColumnLabel = rsmd.getColumnLabel(i + 1); // 通过反射将对象指定名columnName的属性赋给指定的值columnValue // 先拿到class Field field = clazz.getDeclaredField(ColumnLabel); field.setAccessible(true); field.set(t, columnValue); } return t; } } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtils.closeResource(conn, ps, rs); } return null; } }
All that work will definitely pay off