06通用DAO层实现分页和查询

1.一个通用接口
public interface  GenericDAOI <Obj,String>{
	int ADD=1;
	int DEL=2;
	int UPT=3;
	public boolean addObj(Obj obj) throws Exception;
	public boolean delObj(String id)throws Exception;
	public boolean uptObj(Obj obj) throws Exception;
	public Obj  getObjById(String id) throws Exception;
	//public List <Obj> getObjs(String hql,List<Object> params)throws Exception;
	public List <Obj> getObjs(String hql,Map<String,Object> params,int pageNO,int pageSize)throws Exception;
	public void setClzz(Class clzz);

}

2.实现接口

public class GenericDAOImpl <Obj,String>  implements GenericDAOI<Obj, String>{
	private Class clzz;
	private static SessionFactory sessionFactory;
	static{
		sessionFactory=new Configuration().configure("config/hibernate/cfg.xml").buildSessionFactory();
	}
	
	public void setClzz(Class clzz) {
		this.clzz = clzz;
	}

	public Class getClzz() {
		return clzz;
	}

	public boolean cud(Obj obj ,int opt){
		boolean flag=false;
		Session session=null;
		try {
		session=sessionFactory.openSession();
		session.beginTransaction();		
		switch(opt){
		case DEL: session.delete(obj);break;
		case ADD: session.persist(obj);break;
		case UPT: session.update(obj);break;
		default:break;
		}
		
		session.getTransaction().commit();
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return flag;
		
	}
	
	
	
	public boolean addObj(Obj obj) throws Exception {
		return cud(obj,ADD);
	}

	public boolean delObj(String id) throws Exception {
		return cud(getObjById(id),DEL);
	}

	public boolean uptObj(Obj obj) throws Exception {
		return cud(obj,UPT);
	}

	public Obj getObjById(String id) throws Exception {
		Obj obj=null;
		Session session=null;
		try {
		session=sessionFactory.openSession();
		session.beginTransaction();
		obj=(Obj)session.get(clzz,(Serializable)id);
	    session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return obj;
	}

	public List <Obj> getObjs(String hql,Map<String,Object>Params,int pageNO,int pageSize) throws Exception {
		List <Obj> objs=null;
		Session session=null;
		try {
		session=sessionFactory.openSession();
		session.beginTransaction();
		//HQL
		Query query=session.createQuery( (java.lang.String) hql);
		//设置参数
		
		//if(Params!=null){
			query.setProperties(Params);
		//}
		//设置分页	
		if(pageNO>=0){
			query.setFirstResult((pageNO-1)*pageSize);
			query.setMaxResults(pageSize);
		}
		
			
		objs=query.list();
		/*if(Params!=null){
			for(int idx=0;idx<Params.size();idx++){
				query.setParameter(idx, Params.get(idx));
			}*/
			
		
	    session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return objs;
	}

}

3.具体类别

public interface PersonDAOI extends GenericDAOI<PersonPO, String> {
	public List<PersonPO> getPersons(int pageNO,int pageSize) throws Exception;
}
    
public class PersonDAOImpl  extends GenericDAOImpl<PersonPO, String> implements PersonDAOI{
	public List<PersonPO> getPersons(int pageNO,int pageSize) throws Exception {
	//  String hql="from PersonPO person";
	//	String hql="from PersonPO person where person.birthday between ? and ?";//按条件查询  占位符
	 //  List<Object> params=new ArrayList<Object>();
	 //  params.add("1980-1-1");
	 //  params.add("2017-12-30");
		String hql="from PersonPO person where person.name like:name";
		Map<String,Object> params=new  HashMap<String, Object>();
		params.put("name", "%cmj%");
		return this.getObjs(hql, params,pageNO,pageSize);
		
	}	
}

3.测试类
public class PersonHibernateTest {
    public PersonDAOI personDAO;
    public PersonHibernateTest() {
		personDAO=new PersonDAOImpl();
	}		
     @Test
     public void testGetPersons() throws Exception{
    	 List <PersonPO> persons=personDAO.getPersons(1,2);
    	 System.out.println(persons.size());
     }

}


发布了33 篇原创文章 · 获赞 5 · 访问量 6834
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览