Java tutorial
/* * @(#)BaseDaoImpl.java 2014-4-28 ?03:30:22 * * Copyright 2011 Socialmedia, Inc. All rights reserved. * CAS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package baking.dao; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import baking.model.Page; /** * * <p> * @CLASS : BaseDaoImpl * @DESCRIPTION : * @AUTHOR : lvhongqiang * @VERSION : v1.0 * @DATE : 2013-3-27 ?03:30:22 * </p> * */ @Repository public class BaseDao { private SessionFactory sessionFactory; public Session getSession() { return sessionFactory.getCurrentSession(); } /** * Id?? * @description * @author JiaCao * @param clazz * @param id * @return */ public Object get(Class clazz, Integer id) { try { Session session = getSession(); return session.get(clazz, id); } catch (RuntimeException re) { throw re; } } public Object load(Class clazz, Integer id) { try { Session session = getSession(); return session.load(clazz, id); } catch (RuntimeException re) { throw re; } } /** * ? * @description * @author JiaCao * @param obj * @return */ public void save(Object obj) { try { Session session = getSession(); session.save(obj); } catch (RuntimeException re) { throw re; } } public void saveOrUpdate(Object obj) { try { Session session = getSession(); session.saveOrUpdate(obj); } catch (RuntimeException re) { throw re; } } /** * * @description * @author JiaCao * @param obj */ public void update(Object obj) { try { Session session = getSession(); session.update(obj); } catch (RuntimeException re) { throw re; } } /** * * @description * @author JiaCao * @param obj */ public void delete(Object obj) { try { Session session = getSession(); session.delete(obj); } catch (RuntimeException re) { throw re; } } /** * HQL? * @param hql * @return list */ public List find(final String hql) { try { Session session = getSession(); return session.createQuery(hql).list(); } catch (RuntimeException re) { throw re; } } /** * HQL? * @param hql * @param value ? * @return list */ public List find(final String hql, final Object value) { try { Session session = getSession(); return session.createQuery(hql).setParameter(0, value).list(); } catch (RuntimeException re) { throw re; } } /** * HQL?. * * @param hql * @param values ? * @return list */ public List find(final String hql, final Object[] values) { try { Session session = getSession(); Query query = session.createQuery(hql); for (int i = 0; i < values.length; i++) { query.setParameter(i, values[i]); } return query.list(); } catch (RuntimeException re) { throw re; } } /** * HQL? * @param hql HQL? * @param offset ??? * @param pageSize ?? * @return ?list */ public List findList(final String hql, final int offset, final int pageSize) { try { Session session = getSession(); return session.createQuery(hql).setFirstResult(offset).setMaxResults(pageSize).list(); } catch (RuntimeException re) { throw re; } } /** * HQL? * @param hql HQL? * @param offset ??? * @param value ? * @param pageSize ?? * @return ?list */ public List findList(final String hql, final Object value, final int offset, final int pageSize) { try { Session session = getSession(); return session.createQuery(hql).setParameter(0, value).setFirstResult(offset).setMaxResults(pageSize) .list(); } catch (RuntimeException re) { throw re; } } /** * HQL? * @param hql HQL? * @param offset ??? * @param values ? * @param pageSize ?? * @return ?list */ public List findList(final String hql, final Object[] values, final int offset, final int pageSize) { try { Session session = getSession(); Query query = session.createQuery(hql); for (int i = 0; i < values.length; i++) { query.setParameter(i, values[i]); } return query.setFirstResult(offset).setMaxResults(pageSize).list(); } catch (RuntimeException re) { throw re; } } /** * HQL? * @param hql HQL? * @param offset ??? * @param name where in?? * @param vals where in? * @param pageSize ?? * @return ?list */ public List findList(final String hql, final String name, final List vals, final int offset, final int pageSize) { try { Session session = getSession(); return session.createQuery(hql).setParameterList(name, vals).list(); } catch (RuntimeException re) { throw re; } } /** * Execute page. * * @param queryHql the query hql * @param countHql the count hql * @param pageNo the page no * @param pageSize the page size * * @return the page */ public Page findPage(String queryHql, String countHql, int pageNo, int pageSize) { // TODO Auto-generated method stub int total = 0; pageNo = pageNo > 0 ? pageNo : 1; pageSize = pageSize > 0 && pageSize < 100 ? pageSize : 20; total = this.countHql(countHql); if ((pageNo - 1) * pageSize > total) { pageNo = 1; } List list = this.findList(queryHql, (pageNo - 1) * pageSize, pageSize); //?bean Page page = new Page(); //?page page.setList(list); page.setPageSize(pageSize); page.setTotal(total);//?totalPagepageSize page.setPageNo(pageNo);//??start ?end??total?? return page; } /** * Execute page. * * @param queryHql the query hql * @param countHql the count hql * @param value the value * @param pageNo the page no * @param pageSize the page size * * @return the page */ public Page findPage(String queryHql, String countHql, Object value, int pageNo, int pageSize) { // TODO Auto-generated method stub int total = 0; pageNo = pageNo > 0 ? pageNo : 1; pageSize = pageSize > 0 && pageSize < 100 ? pageSize : 20; List list = null; //? if (value == null) { total = this.countHql(countHql); if ((pageNo - 1) * pageSize > total) { pageNo = 1; } list = this.findList(queryHql, (pageNo - 1) * pageSize, pageSize); } else { total = this.countHql(countHql, value); if ((pageNo - 1) * pageSize > total) { pageNo = 1; } list = this.findList(queryHql, value, (pageNo - 1) * pageSize, pageSize); } //?bean Page page = new Page(); //?page page.setList(list); page.setPageSize(pageSize); page.setTotal(total);//?totalPagepageSize page.setPageNo(pageNo);//?start ???7 end???7total????7 return page; } /** * Execute page. * * @param queryHql the query hql * @param countHql the count hql * @param values the values * @param pageNo the page no * @param pageSize the page size * * @return the page */ public Page findPage(String queryHql, String countHql, Object[] values, int pageNo, int pageSize) { // TODO Auto-generated method stub int total = 0; pageNo = pageNo > 0 ? pageNo : 1; pageSize = pageSize > 0 && pageSize < 100 ? pageSize : 20; List list = null; //? if (values == null) { total = this.countHql(countHql); if ((pageNo - 1) * pageSize > total) { pageNo = 1; } list = this.findList(queryHql, (pageNo - 1) * pageSize, pageSize); } else { total = this.countHql(countHql, values); if ((pageNo - 1) * pageSize > total) { pageNo = 1; } list = this.findList(queryHql, values, (pageNo - 1) * pageSize, pageSize); } //?bean Page page = new Page(); //?page page.setList(list); page.setPageSize(pageSize); page.setTotal(total);//?totalPagepageSize page.setPageNo(pageNo);//?start ???7 end???7total????7 return page; } /** * Execute page. * * @param queryHql the query hql * @param countHql the count hql * @param value the value * @param pageNo the page no * @param pageSize the page size * * @return the page */ public Page findPage(String queryHql, String countHql, final String name, final List vals, int pageNo, int pageSize) { int total = 0; pageNo = pageNo > 0 ? pageNo : 1; pageSize = pageSize > 0 && pageSize < 100 ? pageSize : 20; List list = null; //? if (vals == null) { total = this.countHql(countHql); if ((pageNo - 1) * pageSize > total) { pageNo = 1; } list = this.findList(queryHql, (pageNo - 1) * pageSize, pageSize); } else { total = this.countHql(countHql, name, vals); if ((pageNo - 1) * pageSize > total) { pageNo = 1; } list = this.findList(queryHql, name, vals, (pageNo - 1) * pageSize, pageSize); } //?bean Page page = new Page(); //?page page.setList(list); page.setPageSize(pageSize); page.setTotal(total);//?totalPagepageSize page.setPageNo(pageNo);//?start ???7 end???7total????7 return page; } /** * ?count(*)?hql?? * * @param hql the hql * * @return the toatal by hql */ public int countHql(String hql) { // Query query = getSession().createQuery(hql); Long result = (Long) query.uniqueResult(); if (result != null) { return result.intValue(); } return 0; } /** * ?count(*)?hql?? * * @param hql the hql * * @return the toatal by hql */ public int countHql(String hql, Object value) { // Query query = getSession().createQuery(hql); //?? query.setParameter(0, value); Long result = (Long) query.uniqueResult(); if (result != null) { return result.intValue(); } return 0; //return total; } /** * ?count(*)?hql?? * * @param hql the hql * * @return the toatal by hql */ public int countHql(String hql, Object[] values) { // Query query = getSession().createQuery(hql); //?? if (values != null && values.length > 0) { for (int i = 0; i < values.length; i++) query.setParameter(i, values[i]); } Long result = (Long) query.uniqueResult(); if (result != null) { return result.intValue(); } return 0; } /** * ?count(*)where in?hql?? * * @param hql the hql * * @return the toatal by hql */ public int countHql(String hql, String name, List vals) { // Query query = getSession().createQuery(hql); //?? query.setParameterList(name, vals); Long result = (Long) query.uniqueResult(); if (result != null) { return result.intValue(); } return 0; //return total; } /** * ?hql * @description * @author JiaCao * @param hql */ @SuppressWarnings("unchecked") public void executeHql(final String hql) { try { Session session = getSession(); session.createQuery(hql).executeUpdate(); } catch (RuntimeException re) { throw re; } } /** * ?hql * @description * @author JiaCao * @param hql * @param value */ @SuppressWarnings("unchecked") public void executeHql(final String hql, final Object value) { try { Session session = getSession(); session.createQuery(hql).setParameter(0, value).executeUpdate(); } catch (RuntimeException re) { throw re; } } /** * ?hql * @description * @author JiaCao * @param hql * @param values */ @SuppressWarnings("unchecked") public void executeHql(final String hql, final Object[] values) { try { Session session = getSession(); Query query = session.createQuery(hql); for (int i = 0; i < values.length; i++) { query.setParameter(i, values[i]); } query.executeUpdate(); } catch (RuntimeException re) { throw re; } } public SessionFactory getSessionFactory() { return sessionFactory; } @Autowired public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } }