您的位置:首页 > 其它

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[] array6
obj是与数据库的表相连的类实例化对象后的名字;

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的设计。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: