BaseDAO.java
资源名称:cindyblog.rar [点击查看]
上传用户:cdpainuo
上传日期:2022-07-12
资源大小:5257k
文件大小:3k
源码类别:
Jsp/Servlet
开发平台:
Java
- package com.appspot.cindyblog.dao;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- 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> int getObjectCount(Class<T> clazz,EntityManager em,Map<String,String> prames){
- int i = 0;
- try{
- String prame = changeToString(prames);
- String query = "select from "+clazz.getName()+" where "+prame+"1 = 1";
- 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,String orderBy,Map<String,String> prames,int start,int limit){
- List list = null;
- try{
- String prame = changeToString(prames);
- String order = "1 = 1 order by "+orderBy+" desc";
- String query = "select from "+clazz.getName()+" where "+prame+order;
- 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;
- }
- }
- protected String changeToString(Map<String,String> map){
- Set<String> set = map.keySet();
- String query = "";
- Iterator<String> ite = set.iterator();
- while(ite.hasNext()){
- String name = ite.next();
- query = query + name+" = '"+map.get(name)+"' and ";
- }
- return query;
- }
- }