[TOC]
### 使用方法:
~~~
@Test
public void testGet() {
String sql = "SELECT id, name, email, birth "
+ "FROM customers WHERE id = ?";
// System.out.println(sql);
Customer customer = get(Customer.class, sql, 5);
System.out.println(customer);
sql = "SELECT flow_id flowId, type, id_card idCard, "
+ "exam_card examCard, student_name studentName, "
+ "location, grade " + "FROM examstudent WHERE flow_id = ?";
// System.out.println(sql);
Student stu = get(Student.class, sql, 5);
System.out.println(stu);
}
~~~
### 通用的查询方法 返回一个对象
~~~
/**
* 通用的查询方法:可以根据传入的 SQL、Class 对象返回 SQL 对应的记录的对象
* @param clazz: 描述对象的类型
* @param sql: SQL 语句。可能带占位符
* @param args: 填充占位符的可变参数。
* @return
*/
public <T> T get(Class<T> clazz, String sql, Object... args) {
T entity = null;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
//1. 得到 ResultSet 对象
connection = JDBCTools.getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
preparedStatement.setObject(i + 1, args[i]);
}
resultSet = preparedStatement.executeQuery();
//2. 得到 ResultSetMetaData 对象
ResultSetMetaData rsmd = resultSet.getMetaData();
//3. 创建一个 Map<String, Object> 对象, 键: SQL 查询的列的别名,
//值: 列的值
Map<String, Object> values = new HashMap<String, Object>();
//4. 处理结果集. 利用 ResultSetMetaData 填充 3 对应的 Map 对象
if(resultSet.next()){
for(int i = 0; i < rsmd.getColumnCount(); i++){
String columnLabel = rsmd.getColumnLabel(i + 1);
Object columnValue = resultSet.getObject(i + 1);
values.put(columnLabel, columnValue);
}
}
//5. 若 Map 不为空集, 利用反射创建 clazz 对应的对象
if(values.size() > 0){
entity = clazz.newInstance();
//5. 遍历 Map 对象, 利用反射为 Class 对象的对应的属性赋值.
for(Map.Entry<String, Object> entry: values.entrySet()){
String fieldName = entry.getKey();
Object value = entry.getValue();
ReflectionUtils.setFieldValue(entity, fieldName, value);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.releaseDB(resultSet, preparedStatement, connection);
}
return entity;
}
~~~
### 分开写代码展示
~~~
public Customer getCustomer(String sql, Object... args) {
Customer customer = null;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JDBCTools.getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
preparedStatement.setObject(i + 1, args[i]);
}
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
// stu = new Student();
// stu.setFlowId(resultSet.getInt(1));
// stu.setType(resultSet.getInt(2));
// stu.setIdCard(resultSet.getString(3));
customer = new Customer();
customer.setId(resultSet.getInt(1));
customer.setName(resultSet.getString(2));
customer.setEmail(resultSet.getString(3));
customer.setBirth(resultSet.getDate(4));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.releaseDB(resultSet, preparedStatement, connection);
}
return customer;
}
public Student getStudent(String sql, Object... args) {
Student stu = null;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JDBCTools.getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
preparedStatement.setObject(i + 1, args[i]);
}
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
stu = new Student();
stu.setFlowId(resultSet.getInt(1));
stu.setType(resultSet.getInt(2));
stu.setIdCard(resultSet.getString(3));
// ...
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.releaseDB(resultSet, preparedStatement, connection);
}
return stu;
}
~~~
### 整体展示
![](https://box.kancloud.cn/921fd8f5e80a121a70596598d00924bd_865x563.png)
![](https://box.kancloud.cn/4b8400e9886151e763ce26a2a39df4e6_733x545.png)
![](https://box.kancloud.cn/0daa8d3684d84097a18cd6fcd5b5c8ea_763x543.png)
![](https://box.kancloud.cn/72e205659999da869f0e3828997f9cd9_763x561.png)
![](https://box.kancloud.cn/815a12bb0fc6d0f1b56fc75729690f52_835x563.png)
![](https://box.kancloud.cn/672b7a477f771282baabe89d0eb85313_829x521.png)
![](https://box.kancloud.cn/dbfab1398b83610845b09418b24d6810_723x559.png)
![](https://box.kancloud.cn/41c4bf101b4089a8b002981683dde126_837x585.png)
![](https://box.kancloud.cn/3c14b169cb75b23f7a7eb97f48595d7d_841x567.png)
![](https://box.kancloud.cn/1a810e3a54c6356a065cf6d1d9b7c65b_767x557.png)