Java tutorial
/** * Program : AbstractBaseGenericORMDaoImpl.java Author : long Create : * 2014-3-11 ?12:43:04 Copyright 2013 by Embedded Internet Solutions Inc., All * rights reserved. This software is the confidential and proprietary * information of Embedded Internet Solutions Inc.("Confidential Information"). * You shall not disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into with * Embedded Internet Solutions Inc. */ package com.iwancool.dsm.dao.impl; import java.io.Serializable; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.math.BigInteger; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.Query; import org.hibernate.SQLQuery; import org.hibernate.Session; import org.hibernate.SessionFactory; import com.iwancool.dsm.base.AbstractBaseModel; import com.iwancool.dsm.dao.IGeneralORMDao; /** * ORM DAO * * @author long * @version 1.0.0s * @2014-3-11 ?12:43:04 */ @SuppressWarnings("unchecked") public class AbstractBaseGenericORMDaoImpl<T extends AbstractBaseModel, PK extends Serializable> implements Serializable, IGeneralORMDao<T, PK> { private static final long serialVersionUID = -7219280318442447324L; protected Log log = LogFactory.getLog(getClass()); @Resource(name = "sessionFactory") private SessionFactory sessionFactory; protected Class<T> entityClass; protected Class<PK> pkClass; public AbstractBaseGenericORMDaoImpl() { super(); this.entityClass = getSuperClassGenricType(getClass(), 0); this.pkClass = getSuperClassGenricType(getClass(), 1); } /** * ?session * * @return org.hibernate.Session */ public Session getCurrentSession() { return this.sessionFactory.getCurrentSession(); } @Override public Serializable save(T entity) { if (entity != null) { return this.getCurrentSession().save(entity); } return null; } @Override public void saveOrUpdate(T entity) { if (entity != null) { this.getCurrentSession().saveOrUpdate(entity); } } @Override public void update(T entity) { if (entity != null) { this.getCurrentSession().update(entity); } } @Override public void delete(T entity) { if (entity != null) { this.getCurrentSession().delete(entity); } } @Override public T getByPrimaryKey(PK pk) { return (T) this.getCurrentSession().get(entityClass, pk); } @Override public void deleteByPrimaryKey(PK pk) { getCurrentSession().delete(getByPrimaryKey(pk)); } @Override public T get(String hql) { Query query = this.getCurrentSession().createQuery(hql); List<T> list = query.list(); if (list != null && list.size() > 0) { return list.get(0); } return null; } @Override public T get(String hql, Map<String, Object> params) { Query q = this.getCurrentSession().createQuery(hql); if (params != null && !params.isEmpty()) { for (String key : params.keySet()) { q.setParameter(key, params.get(key)); } } List<T> l = q.list(); if (l != null && l.size() > 0) { return l.get(0); } return null; } @Override public List<T> find(String hql) { Query q = this.getCurrentSession().createQuery(hql); return q.list(); } @Override public List<T> find(String hql, Map<String, Object> params) { Query q = this.getCurrentSession().createQuery(hql); if (params != null && !params.isEmpty()) { for (Object key : params.keySet()) { q.setParameter((String) key, params.get(key)); } } return q.list(); } @Override public List<T> find(String hql, int page, int rows) { Query q = this.getCurrentSession().createQuery(hql); return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list(); } @Override public List<T> find(String hql, Map<String, Object> params, int page, int rows) { Query q = this.getCurrentSession().createQuery(hql); if (params != null && !params.isEmpty()) { for (String key : params.keySet()) { q.setParameter(key, params.get(key)); } } return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list(); } @Override public List<T> findByOffset(String hql, Map<String, Object> params, int offset, int limit) { Query q = this.getCurrentSession().createQuery(hql); if (params != null && !params.isEmpty()) { for (String key : params.keySet()) { q.setParameter(key, params.get(key)); } } return q.setFirstResult(offset).setMaxResults(limit).list(); } @Override public Long count(String hql) { Query q = this.getCurrentSession().createQuery(hql); return (Long) q.uniqueResult(); } @Override public Long count(String hql, Map<String, Object> params) { Query q = this.getCurrentSession().createQuery(hql); if (params != null && !params.isEmpty()) { for (String key : params.keySet()) { q.setParameter(key, params.get(key)); } } return (Long) q.uniqueResult(); } @Override public int executeHql(String hql) { Query q = this.getCurrentSession().createQuery(hql); return q.executeUpdate(); } @Override public int executeHql(String hql, Map<String, Object> params) { Query q = this.getCurrentSession().createQuery(hql); if (params != null && !params.isEmpty()) { for (String key : params.keySet()) { q.setParameter(key, params.get(key)); } } return q.executeUpdate(); } @Override public List<Object[]> findBySql(String sql) { SQLQuery q = this.getCurrentSession().createSQLQuery(sql); return q.list(); } @Override public List<Object[]> findBySql(String sql, int page, int rows) { SQLQuery q = this.getCurrentSession().createSQLQuery(sql); return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list(); } @Override public List<Object[]> findBySql(String sql, Map<String, Object> params) { SQLQuery q = this.getCurrentSession().createSQLQuery(sql); if (params != null && !params.isEmpty()) { for (String key : params.keySet()) { q.setParameter(key, params.get(key)); } } return q.list(); } @Override public List<Object[]> findBySql(String sql, Map<String, Object> params, int page, int rows) { SQLQuery q = this.getCurrentSession().createSQLQuery(sql); if (params != null && !params.isEmpty()) { for (String key : params.keySet()) { q.setParameter(key, params.get(key)); } } return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list(); } @Override public int executeSql(String sql) { SQLQuery q = this.getCurrentSession().createSQLQuery(sql); return q.executeUpdate(); } @Override public int executeSql(String sql, Map<String, Object> params) { SQLQuery q = this.getCurrentSession().createSQLQuery(sql); if (params != null && !params.isEmpty()) { for (String key : params.keySet()) { q.setParameter(key, params.get(key)); } } return q.executeUpdate(); } @Override public BigInteger countBySql(String sql) { SQLQuery q = this.getCurrentSession().createSQLQuery(sql); return (BigInteger) q.uniqueResult(); } @Override public BigInteger countBySql(String sql, Map<String, Object> params) { SQLQuery q = this.getCurrentSession().createSQLQuery(sql); if (params != null && !params.isEmpty()) { for (String key : params.keySet()) { q.setParameter(key, params.get(key)); } } return (BigInteger) q.uniqueResult(); } @SuppressWarnings("rawtypes") public Class getSuperClassGenricType(final Class clazz, final int index) { Type genType = clazz.getGenericSuperclass(); if (!(genType instanceof ParameterizedType)) { return Object.class; } Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); if (index >= params.length || index < 0) { return Object.class; } if (!(params[index] instanceof Class)) { return Object.class; } return (Class) params[index]; } @Override public Object getObject(String sql, Map<String, Object> params) { SQLQuery q = this.getCurrentSession().createSQLQuery(sql); if (params != null && !params.isEmpty()) { for (String key : params.keySet()) { q.setParameter(key, params.get(key)); } } return q.uniqueResult(); } }