SSH底层DAO实现添加功能
2013-04-09 20:28
148 查看
public boolean insert(Object obj,String table,String[] array1,String[] array2,String[] array3,Integer[] array4,String[] array5,Date[] array6) { Class model; try {//这里我利用上了反射机制 model = Class.forName("org.cyxl.ssh.entity."+table); if(array6==null||array4==null||array2==null){ if(array6==null){ for(int j =0;j<array3.length;j++){ Method setMethod1 = model.getMethod("set"+array3[j],Integer.class); setMethod1.invoke(obj, array4[j]); } for(int j =0;j<array1.length;j++){ Method setMethod2 = model.getMethod("set"+array1[j], String.class); setMethod2.invoke(obj, array2[j]); } } if(array4==null){ for(int j =0;j<array5.length;j++){ Method setMethod = model.getMethod("set"+array5[j],Date.class); setMethod.invoke(obj, array6[j]); } for(int j =0;j<array1.length;j++){ Method setMethod2 = model.getMethod("set"+array1[j], String.class); setMethod2.invoke(obj, array2[j]); } } if(array2==null){ for(int j =0;j<array5.length;j++){ Method setMethod = model.getMethod("set"+array5[j],Date.class); setMethod.invoke(obj, array6[j]); } for(int j =0;j<array3.length;j++){ Method setMethod1 = model.getMethod("set"+array3[j],Integer.class); setMethod1.invoke(obj, array4[j]); } } if(array2==null&array4==null){ for(int j =0;j<array5.length;j++){ Method setMethod = model.getMethod("set"+array5[j],Date.class); setMethod.invoke(obj, array6[j]); } } if(array2==null&array6==null){ for(int j =0;j<array3.length;j++){ Method setMethod1 = model.getMethod("set"+array3[j],Integer.class); setMethod1.invoke(obj, array4[j]); } } if(array4==null&array6==null){ for(int j =0;j<array1.length;j++){ Method setMethod2 = model.getMethod("set"+array1[j], String.class); setMethod2.invoke(obj, array2[j]); } } if(array4==null&array6==null&array2==null){ return false; } }else{ for(int j =0;j<array3.length;j++){ Method setMethod1 = model.getMethod("set"+array3[j],Integer.class); setMethod1.invoke(obj, array4[j]); } for(int j =0;j<array1.length;j++){ Method setMethod2 = model.getMethod("set"+array1[j], String.class); setMethod2.invoke(obj, array2[j]); } for(int j =0;j<array5.length;j++){ Method setMethod = model.getMethod("set"+array5[j],Date.class); setMethod.invoke(obj, array6[j]); } } session.save(obj); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException 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(); } return true; }
参数:Object obj,String table,String[] array1,String[] array2,String[] array3,Integer[] array4,String[] array5,Date[] array6obj是与数据库的表相连的类实例化对象后的名字;
table是与数据库的表相连的类的名字;
array1是字符串组成的数组,是与array2紧密相连的,array1里面放着字段名,array2放着字段值,相组合就成了。需要注意的是,这里的array2也是字符串(String)组成的数组,所以对应着数据库中的属性为varchar、text的字段。所以array1里面的字段名都是属性为varchar、text。
array3是字符串组成的数组,是与array4紧密相连的,array3里面放着字段名,array4放着字段值,相组合就成了。需要注意的是,这里的array4也是数字(Integer)组成的数组,所以对应着数据库中的属性为Integert的字段。所以array1里面的字段名都是属性为Integer。
array5是字符串组成的数组,是与array6紧密相连的,array5里面放着字段名,array6放着字段值,相组合就成了。需要注意的是,这里的array6也是Date组成的数组,所以对应着数据库中的属性为date的字段。所以array1里面的字段名都是属性为date。
而我写了那么多的if,主要是因为我也不知道哪个表里面有什么类型的数据,可能我所分的类还不够多,以后如果还有更多的(非String,Integer,date)类型,我在改下底层的设计。
Action类:
public String insert_content(){ if(classification == null){ classification="default"; } comment_num=0; comment="Allow_comments"; top=0; articles_archive=""; Date date1 = new Date(); long l = date1.getTime(); new_date = new java.sql.Date(l); String dateStr = new SimpleDateFormat("yyyy-MM").format(date1); articles_archive=dateStr; if(typ.equals("Published") ){ typ="1"; }else{ typ="0"; } Content content = new Content(); String table = "Content"; String[] array1 = {"Title", "Contents","Typ", "Classification", "Comment", "Tag", "Articles_archive"}; String[] array2 = {title, contents,typ, classification, comment, tag, articles_archive}; String[] array3 = {"Top","Comment_num"}; Integer[] array4 = {top,comment_num}; String[] array5={"New_date"}; java.sql.Date[] array6 = {new_date}; userService.insert(content,table,array1,array2,array3,array4,array5,array6); return "insert_content"; }
第一种方法很容易出错,注意Action的设计。
相关文章推荐
- 【SSH网上商城项目实战09】添加和更新商品类别功能的实现
- 【SSH网上商城项目实战09】添加和更新商品类别功能的实现
- 【SSH网上商城项目实战09】添加和更新商品类别功能的实现
- 实例:SSH结合KindEditor实现新闻的添加、修改和显示功能
- 【SSH网上商城项目实战12】添加和更新商品功能的实现
- 【SSH网上商城项目实战12】添加和更新商品功能的实现
- 【SSH网上商城项目实战09】添加和更新商品类别功能的实现
- 【SSH网上商城项目实战12】添加和更新商品功能的实现
- python实现LDAP中组(group)添加用户(user)功能
- android 中如何添加新的键值,实现更多功能
- 实现新闻信息的修改和添加功能
- IOS添加ZXing库实现二维码功能简述d
- JS实现很酷的EMAIL地址添加功能实例
- MVVM范例:实现 用户列表绑定、编辑 、删除、添加及筛选功能
- 实现添加添加主从表的功能
- jQuery实现添加“成绩单” 搜索功能和清除功能
- 构建Postfix+Mysql+Dovecot邮件系统,实现以Web页面访问的功能、添加SASL认证以及TLS加密传输 推荐
- Yii2框架实现登陆添加验证码功能示例
- 2. 为视图添加工具栏,实现新建,打开,删除的功能
- Java Dao实现group by 分组查询功能