首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

【J2SE】利用反照机制实现的sql语句自动生成、简化实体类封装

2012-12-19 
【J2SE】利用反射机制实现的sql语句自动生成、简化实体类封装现在所学的东西,有很多的共性。Dao层对于臃肿,很

【J2SE】利用反射机制实现的sql语句自动生成、简化实体类封装
现在所学的东西,有很多的共性。Dao层对于臃肿,很多都是ctrl+c和ctrl+v 完成的,这种事情纯粹就是苦力代码。利用双周的时间,用反射机制实现了sql自动生成,简化list封装。

大家看看还有什么需要改进的地方吧。

sql工具类




反射工具类:ReflecTionUtil

package com.util;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.List;import java.util.SortedMap;import javax.servlet.jsp.jstl.sql.Result;import com.entity.Nr_users;public class ReflecTionUtil {/** * @author fule * 反射工具类  * 封装数据结果到集合 * 传入result 实体和 实体类具体url */private String[] classMethods = new String[20];// set方法数组private Class[] classParams = new Class[20];// set方法参数类型private int classMethodsNum = 0;// 实体类属性个数private Class cs = null;// 会话管理器private Object object = null;// 定义对象private List list = null;// 实体类属性字段名的集合public void getStandardManager(String url) throws ClassNotFoundException {cs = Class.forName(url);}public void getProtect(String url) throws ClassNotFoundException {// 实体类变量字段list = new ArrayList();this.getStandardManager(url);Field[] fields = cs.getDeclaredFields();for (int i = 0; i < fields.length; i++) {list.add(fields[i].getName());}}public void getConsructor(String url) throws ClassNotFoundException {// set方法和参数类型this.getStandardManager(url);Method[] methods = cs.getMethods();int count = 0;for (Method m : methods) {if (m.getName().substring(0, 3).equals("set")) {Class[] parms = m.getParameterTypes();classMethods[count] = m.getName();classParams[count] = parms[0];//count++;}}classMethodsNum = count;}public void getObject(String url) throws SecurityException,NoSuchMethodException, ClassNotFoundException,IllegalArgumentException, InstantiationException,IllegalAccessException, InvocationTargetException {/** * 创建类对象 */this.getStandardManager(url);Constructor constructor = cs.getConstructor();object = constructor.newInstance();}public Result checkResult(Result rs) {/** * 验证数据库中的数据 */for (int i = 0; i < rs.getRowCount(); i++) {SortedMap map = rs.getRows()[i];for (int j = 0; j < list.size(); j++) {Object value = map.get(list.get(j));//testtestif(value==null){System.out.println("数据验证失败,检查实体类与数据表规范!");try {throw new Exception("数据验证失败,检查实体类与数据表规范!");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}else{map.put(list.get(j), value);}}}return rs;}public List getValue(String url, Result rs) {/** * list列表  value */List resultlist = new ArrayList();try {this.getObject(url);this.getConsructor(url);this.getProtect(url);rs = checkResult(rs);for (int i = 0; i < rs.getRowCount(); i++) {for (int j = 0; j < classMethodsNum; j++) {Method method = cs.getMethod(classMethods[j],classParams[j]);//System.out.println("当前调用set方法:"+method);//System.out.println("表字段名:"+classMethods[j]//.substring(3).toLowerCase());//表字段名String tstr = classMethods[j]   .substring(3).toLowerCase();///System.out.println("表字段值:"+rs.getRows()[i].get(tstr));//表字段值method.invoke(object, rs.getRows()[i].get(tstr));//动态设值//System.out.println((Nr_users)object);}resultlist.add(object);}} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InstantiationException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}return resultlist;}}

热点排行