本帖最后由 yuZhe_toString 于 2014-5-29 15:28 编辑
由于jdbc遍历的时候需要手动将结果集中的值set给bean对象中,如下段代码
while(rs.next()){
bean.setID(rs.getString("ID"));
。。。。。。
}
这样非常麻烦,所以写了一段程序解决这个问题,如果大家有好的改进方法可以提出来。
楼主通过反射出数据bean中的属性信息,并把每条属性信息存入BeanInformation中,然后把BeanInformation对象放入一个Map里。BeanInformation代码如下:
- public class BeanInformation {
- private String fieldName; //属性名称
- private String methodName; //方法名称
- private Class type; //属性类型
-
-
- public String getFieldName() {
- return fieldName;
- }
- public void setFieldName(String fieldName) {
- this.fieldName = fieldName;
- }
- public String getMethodName() {
- return methodName;
- }
- public void setMethodName(String methodName) {
- this.methodName = methodName;
- }
- public Class getType() {
- return type;
- }
- public void setType(Class type) {
- this.type = type;
- }
- }
- 下面是解析bean属性的代码,和对bean的赋值
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- public class VOTools {
-
- private Class className; //bean的字节码
- private ResultSet rs; //ResultSet结果集
-
- //VOTools的第一种构造函数
- public VOTools(ResultSet rs,String className){
- try {
- this.className = Class.forName(className);
- this.rs = rs;
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
-
- //VOTools的第二种构造函数
- public VOTools(ResultSet rs,Class className){
- this.className = className;
- this.rs = rs;
- }
-
- //返回List,list中存放的是已被赋值好的bean
- public List getBeanList () throws SecurityException,NoSuchMethodException,IllegalArgumentException,InvocationTargetException{
- return getBeanList(this.rs,this.className);
- }
-
- //获取单个bean
- public Object getBean() throws Exception{
- Object obj = null;
- List<Object> list = getBeanList(this.rs,this.className);
- if(list!=null && list.size()==1){
- obj = list.get(0);
- }
- return obj;
- }
-
- //获取bean中的属性信息
- private Map<String,BeanInformation> getBeanInformationToMap(Class className){
- //创建Map key为属性名 value为该属性的属性解析对象
- Map<String,BeanInformation> beanInformationToMap = new HashMap<String, BeanInformation>();
- Class beanClass = null;
- try {
- beanClass = className;
- //获取传入的bean的所有属性
- Field[] beanField = beanClass.getDeclaredFields();
- //循环遍历
- for(int i=0;i<beanField.length;i++){
- //创建bean解析对象
- BeanInformation bean = new BeanInformation();
- //获取属性名,并大写存入BeanInformation里
- bean.setFieldName(beanField.getName().toUpperCase());
- //获取方法set方法名
- bean.setMethodName("set"+beanField.getName().substring(0, 1).toUpperCase().concat(beanField.getName().substring(1)));
- //获取属性类型
- bean.setType(beanField.getType());
- //将BeanInformation存入map
- beanInformationToMap.put(bean.getFieldName(), bean);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return beanInformationToMap;
- }
-
- //遍历ResultSet结果集,并赋值
- private List getBeanList(ResultSet result,Class beanName)throws SecurityException,NoSuchMethodException,IllegalArgumentException,InvocationTargetException{
- Map<String,BeanInformation> beanInformationMap = getBeanInformationToMap(beanName);
- List list = new ArrayList();
- ResultSetMetaData resm;
- try {
- //获取结果集信息
- resm = result.getMetaData();
- while(result.next()){
- Class classz = beanName;
- //反射实例化对象
- Object obj = classz.newInstance();
- //循环结果集的列
- for(int i=0;i<resm.getColumnCount();i++){
- //获取结果集每列的名称
- String key = resm.getColumnName(i+1).toUpperCase();
- //如果这个名称在之前解析的bean中能匹配上 就对其进行赋值
- if(beanInformationMap.get(key) != null){
- //获取方法名
- String methodName = beanInformationMap.get(key).getMethodName();
- //获取属性类型
- Class type = beanInformationMap.get(key).getType();
- //类型匹配
- if(type.equals(String.class)){
- //反射赋值
- classz.getMethod(methodName,type).invoke(obj, new Object[]{result.getString(key)});
- }else if(type.equals(int.class) || type.equals(Integer.class)){
- classz.getMethod(methodName,type).invoke(obj, new Object[]{result.getInt(key)});
- }else if(type.equals(double.class) || type.equals(Double.class)){
- classz.getMethod(methodName,type).invoke(obj, new Object[]{result.getDouble(key)});
- }else if(type.equals(char.class) || type.equals(Character.class)){
- classz.getMethod(methodName,type).invoke(obj, new Object[]{result.getCharacterStream(key)});
- }
- }
- //String value = result.getString(key);
- }
-
- list.add(obj);
- }
-
- } catch (InstantiationException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }finally{
- try {
- this.rs.close();
- } catch (SQLException e) {
- rs = null;
- e.printStackTrace();
- }
- }
- return list;
- }
-
-
- }
复制代码
|
|