现在的位置: 首页 > 综合 > 正文

SSH 增删改查综合

2013年09月19日 ⁄ 综合 ⁄ 共 6070字 ⁄ 字号 评论关闭
package org.cyxl.ssh.dao.hibernate.impl;  
  
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Date;
import java.util.List;  
import org.cyxl.ssh.dao.UserDao;  
import org.cyxl.ssh.entity.Classification;
import org.cyxl.ssh.entity.Comments;
import org.cyxl.ssh.entity.Content;
import org.cyxl.ssh.entity.Tag;
import org.hibernate.Query;  
import org.hibernate.Session;  
import org.hibernate.SessionFactory;  

/** 
 * Hibernate底层增删改查
 * @author cyxl 
 * 
 */  
public class UserDaoImpl implements UserDao{  
    
	
	private SessionFactory factory;  
    private Session session;  
    
    public SessionFactory getFactory() {  
        return factory;  
    }  
    
    public void setFactory(SessionFactory factory) {  
        this.factory = factory;  
        
        this.session=factory.openSession();  
    }  
    
	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&array3!=null&array1!=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&array6!=null&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<array1.length;j++){
 	 					Method setMethod2 = model.getMethod("set"+array1[j], String.class);
 	 					setMethod2.invoke(obj, array2[j]);
 	 		    	}
 				}
 				if(array2==null&array4!=null&array6!=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&array5!=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&array4!=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&array2!=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;  
    }  
    
    /**
     * table 表
     * fieldName 字段名
     * fieldValue 字段值
     * desc 顺序或倒序
     * descId 用来标识用什么来顺序或倒序
     */
    public List selete(String fieldName,String fieldValue,String desc,String table,String descId) {  
        String hql;
        
    	if(fieldName == null&desc==null&descId ==null){
        	hql="from "+table;  
        }else if(desc == null&fieldName!=null&descId ==null){
        	hql="from "+table+" where "+fieldName+" = '"+fieldValue+"'";  
        }else if(desc==null&fieldName==null&descId!=null){
        	hql="from "+table+" order by "+ descId;  
        }else if(desc!=null&fieldName==null&descId!=null){
        	hql="from "+table+" order by "+ descId +" "+desc;  
        }else{
        	hql="from "+table+" where "+fieldName+" = '"+fieldValue+"' order by "+ descId+" "+desc;  
        }
        Query query=session.createQuery(hql);  
        List list=query.list();
        
        return list;
            
    }
    
    /**
     * @param obj
     * @param table
     * @id
     * 
     */
    
    public boolean delete(Object obj,String table,Integer id){
    	
    	String fieldName="id";
    	Integer id2 = id;
    	String fieldValue = String.valueOf(id2);
    	String desc = null;
    	String descId = null;
    	List list = this.selete(fieldName,fieldValue,desc,table,descId);
    	
    	for(int i=0;i<list.size();i++){
    		obj = (Object)list.get(i);
    	}
    	session.delete(obj);
    	session.beginTransaction().commit();
    	return true;
       
    }
    
    /**
     * 修改数据
     * @param obj
     * @param table
     * @param id
     * @param array1 String字段名集合
     * @param array2 String字段值集合
     * @param array3 Integer字段名集合
     * @param array4 Integer字段值集合
     * @param array5 Date字段名集合
     * @param array6 Date字段值集合
     * @return
     */
    public boolean edit(Object obj,String fieldName,String fieldValue,String table,String[] array1,String[] array2,String[] array3,Integer[] array4,String[] array5,Date[] array6){
    	
    	String desc = null;
    	String descId = null;
    	List list = this.selete(fieldName,fieldValue,desc,table,descId);
    	
    	for(int i=0;i<list.size();i++){
			obj = (Object)list.get(i);
			Class model;
			try {//这里我利用上了反射机制
				model = Class.forName("org.cyxl.ssh.entity."+table);
				
				if(array6==null&array4==null){
				
					for(int j =0;j<array1.length;j++){
						
						Method setMethod = model.getMethod("set"+array1[j],String.class);
						setMethod.invoke(obj, array2[j]);
						session.save(obj);
				    	session.beginTransaction().commit();
			    	}
				}else if(array6==null&array2==null){
					for(int j =0;j<array3.length;j++){
						
						Method setMethod = model.getMethod("set"+array3[j],Integer.class);
						setMethod.invoke(obj, array4[j]);
						session.save(obj);
				    	session.beginTransaction().commit();
				    	
			    	}
				}else{
					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);
				    	session.beginTransaction().commit();
			    	}
				}
			} 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;
    }
    
  
}  

抱歉!评论已关闭.