这是我以前写的一个系统中自己写的连个工具,通过看这个代码你会对Java的泛型和反射有深刻的了解,下面是项目的目录:
主要就是ResultSetToData和TranesformToSql文件的代码:
ResultSetToData.java
package com.lirui.order.dao.tool;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ResultSetToData {
/**
* 根据clazz和ResultSet返回对象(这个对象里的全部属性)集合
*
* @param rs
* @param clazz
* @return
*/
public static <T> ArrayList<T> getDataByClassFiled(ResultSet rs,
Class<T> clazz) {
ArrayList<T> domains = new ArrayList<>();
try {
// 获得自己的属性
Field[] fields = clazz.getDeclaredFields();
// 获得父类的属性
Field[] superFields = clazz.getSuperclass().getDeclaredFields();
// 自己的和父类的属性相加
Field[] allFields = addFields(fields, superFields);
while (rs.next()) {
T vo = clazz.newInstance();
// 历遍所有的属性
for (Field field : allFields) {
String methodName = getSetterMethodName(field.getName());
Method method = clazz
.getMethod(methodName, field.getType());
invokeMothod(rs, field, method, vo);
}
domains.add(vo);
}
} catch (InstantiationException | IllegalAccessException
| SecurityException | SQLException | NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return domains;
}
/**
* 根据返回的resultSet来决定生成对象有那些属性
*
* @param rs
* @param clazz
* @return
*/
public static <T> ArrayList<T> getDataByResultSet(ResultSet rs,
Class<T> clazz) {
ArrayList<T> domains = new ArrayList<>();
// 通过metaData获取到的属性生成对应的FieldList
List<Field> allFields = new ArrayList<>();
try {
//如果未空值返回
if (null==rs) {
return null;
}
// 先获取元数据再通过元数据进行细化的操作
java.sql.ResultSetMetaData metaData = rs.getMetaData();
// 通过ResultSet里面的column的名字来决定用这个类的哪一个Field
for (int i = 0; i < metaData.getColumnCount(); i++) {
allFields
.add(clazz.getDeclaredField(metaData.getColumnName(i+1)));
// allFields.add(clazz.getSuperclass().getDeclaredField(
// metaData.getColumnName(i+1)));
}
while (rs.next()) {
T vo = clazz.newInstance();
// 历遍所有的属性
for (Field field : allFields) {
String methodName = getSetterMethodName(field.getName());
Method method = clazz
.getMethod(methodName, field.getType());
invokeMothod(rs, field, method, vo);
}
domains.add(vo);
}
} catch (InstantiationException | IllegalAccessException
| SecurityException | SQLException | NoSuchMethodException
| NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return domains;
}
/**
* 相加f1和f2的Field
*
* @param f1
* @param f2
* @return
*/
private static Field[] addFields(Field[] f1, Field[] f2) {
List<Field> fields = new ArrayList<>();
for (Field field : f1) {
fields.add(field);
}
for (Field field : f2) {
fields.add(field);
}
return fields.toArray(new Field[f1.length + f2.length]);
}
/**
* 根据属性名得到set的方法名
*
* @param name
* @return
*/
private static String getSetterMethodName(String name) {
String begin = name.substring(0, 1).toUpperCase();
String mothodName = "set" + begin + name.substring(1, name.length());
return mothodName;
}
/**
* 根据ResultSet和Field的getName从结果集取出
*
* @param rs
* @param field
* @param method
* @param object
*/
private static void invokeMothod(ResultSet rs, Field field, Method method,
Object object) {
try {
Object o=rs.getObject(field.getName());
method.invoke(object, o);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
TansformToSql.java
package com.lirui.order.dao.tool;
import java.lang.reflect.Field;
public class TransformToSql {
/**
* 通过反射机制生成插入sql语句
*
* @param clazz
* @return
*/
public static String transformToInsertSql(Class<?> clazz) {
StringBuffer bufferFront = new StringBuffer();
StringBuffer bufferLast = new StringBuffer();
String tableName = clazz.getSimpleName();
bufferFront.append("insert into "
+ tableName.substring(0, 1).toLowerCase()
+ tableName.substring(1, tableName.length()) + "(");
bufferLast.append(" values(");
// 通过class得到所有的属性不受访问控制符空值
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
bufferFront.append(field.getName() + ",");
bufferLast.append("?,");
}
bufferFront.delete(bufferFront.length() - 1, bufferFront.length());
bufferLast.delete(bufferLast.length() - 1, bufferLast.length());
bufferFront.append(")");
bufferLast.append(")");
bufferFront.append(bufferLast);
return bufferFront.toString();
}
/**
* 通过反射机制生成通用的更新语句
*
* @param clazz
* @return
*/
public static String transformToUpdateSql(Class<?> clazz) {
StringBuffer bufferFront = new StringBuffer();
StringBuffer bufferLast = new StringBuffer();
String tableName = clazz.getSimpleName();
bufferFront.append("update " + tableName.substring(0, 1).toLowerCase()
+ tableName.substring(1, tableName.length()) + " set ");
bufferLast.append(" where id=?");
// 通过class得到所有的属性不受访问控制符空值
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (field.getName().equals("id"))
continue;
bufferFront.append(field.getName() + "=?,");
}
bufferFront.delete(bufferFront.length() - 1, bufferFront.length());
bufferFront.append(bufferLast);
return bufferFront.toString();
}
}
具体意思以后说