初用Hibernate4寫了個BaseDao實現請多多指教
阿新 • • 發佈:2019-01-05
import java.io.Serializable; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.transform.Transformers; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Repository; /** * * @author kingschan * date:2014-1-9 * @param <T> */ @Repository("BaseDao") public class BaseDao { public Logger log = LoggerFactory.getLogger(BaseDao.class); private SessionFactory sessionfactory; public SessionFactory getSessionfactory() { return sessionfactory; } @Resource(name="sessionFactory") public void setSessionfactory(SessionFactory sessionfactory) { this.sessionfactory = sessionfactory; log.info("注入sessionfactory"); } public Session getSession() { Session session = null; try { session = sessionfactory.getCurrentSession(); //log.info("只有在事務管理下,才能通過 getCurrentSession() 得到 session ,如果顯示這個資訊,則說明事務管理工作起作用了。"); } catch(HibernateException ex) { session = sessionfactory.openSession(); log.info("*************************************************************"); log.info("* *"); log.info("* 通過 openSession() 得到。事務管理沒有起作用,需要檢查配置。 *"); log.info("* *"); log.info("*************************************************************"); } return session; } /** * 儲存一個物件 * @param obj */ public void save(Object obj) { Session s = getSession(); s.save(obj); } /** * 根據hql返回一個list 如果有引數剛hql用?來表示條件 * @param hql * @param args 從1開始賦值 * @return */ public List<?> queryForList(String hql,Object...args) { List<?> lis =null; Session s = getSession(); Query q = s.createQuery(hql); if (null!=args&&args.length>0) { for (int i = 0; i < args.length; i++) { q.setParameter(i, args[i]); } } lis = q.list(); return lis; } /** * 分頁查詢 * @param hql * @param pageindex * @param limit * @param args * @return */ public List<?> Pagination(String hql,int pageindex,int limit,Object...args) { List<?> lis =null; Session s = getSession(); Query q = s.createQuery(hql); if (null!=args&&args.length>0) { for (int i = 0; i < args.length; i++) { q.setParameter(i, args[i]); } } q.setFirstResult((pageindex-1)*limit); q.setMaxResults((pageindex-1)*limit+limit); lis = q.list(); return lis; } /** * 修改一個實體 * @param obj */ public void update(Object obj) { getSession().update(obj); } /** * 刪除一個實體 * @param obj */ public void delete(Object obj) { getSession().delete(obj); } /** * 根據主鍵返回一個實體 * @param clazz * @param id * @return */ public Object get(Class<?> clazz,Object id) { return getSession().get(clazz, (Serializable) id); } /** * 執行HQL * @param hql * @param obj */ public int executeHQL(String hql,Object...args) { Query q = getSession().createQuery(hql); if (null!=args&&args.length>0) { for (int i = 0; i < args.length; i++) { q.setParameter(i, args[i]); } } return q.executeUpdate(); } /** * 執行SQL * @param hql * @param obj */ public int executeSQL(String sql,Object...args) { Query q = getSession().createSQLQuery(sql); if (null!=args&&args.length>0) { for (int i = 0; i < args.length; i++) { q.setParameter(i, args[i]); } } return q.executeUpdate(); } /** * hql單一值查值 * @param hql * @param args * @return */ public Object uniqueQuery(String hql,Object...args) { Query q = getSession().createQuery(hql); if (null!=args&&args.length>0) { for (int i = 0; i < args.length; i++) { q.setParameter(i, args[i]); } } return q.uniqueResult(); } /*** * sql 單一值查值 * @param sql * @param args * @return */ public Object uniqueQueryBySql(String sql,Object...args) { Query q = getSession().createSQLQuery(sql); if (null!=args&&args.length>0) { for (int i = 0; i < args.length; i++) { q.setParameter(i, args[i]); } } return q.uniqueResult(); } /** * 本地命名查詢返回List<Map> * @return * @throws Exception */ @SuppressWarnings("unchecked") public List<?> getNameQuery(String key,Object...args)throws Exception{ List<Map<String, Object>> lis = null; Session s = getSession(); Query q = s.getNamedQuery(key); if (null!=args&&args.length>0) { for (int i = 0; i < args.length; i++) { q.setParameter(i, args[i]); } } q.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); lis = q.list(); return lis; } /** * <font color='red'>執行sql語句返回list<map<String,object>></font> * @param sql * @param objects * @return */ public List<?> executeSQLQuery(String sql ,Object...objects){ List<?> lis =null; Session s = getSession(); Query q = s.createSQLQuery(sql); if (null!=objects&&objects.length>0) { for (int i = 0; i < objects.length; i++) { q.setParameter(i, objects[i]); } } q.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); return lis = q.list(); } }