BaseDAO.java.svn-base
资源名称:cindyblog.rar [点击查看]
上传用户:cdpainuo
上传日期:2022-07-12
资源大小:5257k
文件大小:2k
源码类别:
Jsp/Servlet
开发平台:
Java
- package com.appspot.cindyblog.dao;
- import java.util.List;
- import javax.persistence.EntityManager;
- import com.appspot.cindyblog.pojo.BaseBean;
- public class BaseDAO implements IBaseDAO{
- public <T extends BaseBean> T getObjectById(Class<T> clazz,Long id,EntityManager em){
- T o = null;
- try {
- o = em.find(clazz, id);
- }catch(Exception ex){
- ex.printStackTrace();
- } finally {
- return o;
- }
- }
- public <T extends BaseBean> boolean addObject(T o,EntityManager em){
- boolean b = false;
- try {
- em.persist(o);
- b = true;
- }catch(Exception ex){
- ex.printStackTrace();
- } finally {
- return b;
- }
- }
- public <T extends BaseBean> boolean deleteObject(Class<T> clazz,Long id,EntityManager em){
- boolean b = false;
- try {
- T o = em.find(clazz, id);
- em.remove(o);
- b = true;
- }catch(Exception ex){
- ex.printStackTrace();
- } finally {
- em.flush();
- return b;
- }
- }
- public <T extends BaseBean> int getObjectCount(Class<T> clazz,EntityManager em){
- int i = 0;
- try{
- String query = "select from "+clazz.getName();
- List<T> list = em.createQuery(query).getResultList();
- if(list == null){
- i = 0;
- }else{
- i = list.size();
- }
- }catch(Exception ex){
- ex.printStackTrace();
- }finally{
- return i;
- }
- }
- public <T extends BaseBean> List<T> getObjectList(Class<T> clazz,EntityManager em,String orderBy,int start,int limit){
- List list = null;
- try{
- String query = "select from "+clazz.getName()+" order by "+orderBy+" desc";
- list = em.createQuery(query).setFirstResult(start).setMaxResults(limit).getResultList();
- }catch(Exception ex){
- ex.printStackTrace();
- }finally{
- return list;
- }
- }
- public <T extends BaseBean> List<T> getObjectList(Class<T> clazz,EntityManager em){
- List list = null;
- try{
- String query = "select from "+clazz.getName();
- list = em.createQuery(query).getResultList();
- }catch(Exception ex){
- ex.printStackTrace();
- }finally{
- return list;
- }
- }
- }