公用Dao实现

  1. IcommDao示例
  2. package com.cgh.dao.imp;
    
    import java.io.Serializable;
    import java.lang.reflect.ParameterizedType;
    import java.util.Collection;
    import java.util.LinkedHashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.annotation.Resource;
    
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    
    import com.cgh.dao.ICommonDao;
    import com.cgh.util.GenerSuperclass1;
    import com.sun.corba.se.impl.encoding.OSFCodeSetRegistry.Entry;
    
    public class CommonDaoImp<T> implements ICommonDao<T> {
    	Class Entity = GenerSuperclass1.getgener(this.getClass());
    	@Resource(name = "sessionFactory")
    	private SessionFactory sessionFactory;
    
    	public SessionFactory getSessionFactory() {
    		return sessionFactory;
    	}
    
    	public void setSessionFactory(SessionFactory sessionFactory) {
    		this.sessionFactory = sessionFactory;
    	}
    
    	public void save(T Object) {
    		Session session = this.sessionFactory.openSession();
    		Transaction ts = session.beginTransaction();
    		session.save(Object);
    		ts.commit();
    		session.close();
    
    	}
    
    	@Override
    	public void deleteByIds(Serializable... ids) {
    		Session session = this.sessionFactory.openSession();
    		Transaction ts = session.beginTransaction();
    		// Class clazz = this.getClass();
    		// System.out.println("clazz:" + clazz);
    		// ParameterizedType type = (ParameterizedType) clazz
    		// .getGenericSuperclass();
    		// System.out.println("type" + type);
    		// Class Entity= (Class) type.getActualTypeArguments()[0];
    
    		for (Serializable id : ids) {
    			Object o = session.get(Entity, id);
    			if (o != null && !o.equals("")) {
    				session.delete(o);
    			}
    		}
    		ts.commit();
    		session.close();
    
    	}
    
    	@Override
    	public void deleteByCollection(Collection list) {
    
    		Session session = this.sessionFactory.openSession();
    		Transaction ts = session.beginTransaction();
    		for (Object o : list) {
    			session.delete(o);
    		}
    		ts.commit();
    
    	}
    
    	@Override
    	public List findObjectsByConditionWithNoPage(String whereql,
    			Map<String,Object> params, LinkedHashMap<String, String> orderby) {
    		Session session = this.sessionFactory.openSession();
    		Transaction ts = session.beginTransaction();
    		String sql = "select o from " + Entity.getSimpleName() + " o where 1=1 ";
    		sql += whereql;
    		System.out.println(sql);
    		Query query = session.createQuery(sql);
    		// 处理参数问题
    //		if (params != null && !params.equals("")) {
    //			for (int i = 0; i < params.length; i++) {
    //				//query.setParameter(i, params[i]);
    //		       
    //				System.out.println(params[i]);
    //			}
    //		}
    		for(String o:params.keySet()){
    			if(params.get(o) instanceof Object[]){
    				query.setParameterList(o, (Object[])params.get(o));
    			}else if(params.get(o) instanceof String){
    				query.setParameter(o, params.get(o));
    			}
    		}
    
    		return query.list();
    	}
    
    }
    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值