package core.dao;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Resource;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import core.support.BaseParameter;
import core.support.QueryResult;
/**
* @author Yang Tian
* @email 1298588579@qq.com
*/
public class BaseDao<E> implements Dao<E> {
protected final Logger log = Logger.getLogger(BaseDao.class);
private static Map<String, Method> MAP_METHOD = new HashMap<String, Method>();
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public Session getSession() {
return sessionFactory.getCurrentSession();
}
@Resource(name = "sessionFactory")
public void setSF(SessionFactory sessionFactory) {
setSessionFactory(sessionFactory);
}
protected Class<E> entityClass;
public BaseDao(Class<E> entityClass) {
this.entityClass = entityClass;
}
public void persist(E entity) {
getSession().save(entity);
}
public boolean deleteByPK(Serializable... id) {
boolean result = false;
if (id != null && id.length > 0) {
for (int i = 0; i < id.length; i++) {
E entity = get(id[i]);
if (entity != null) {
getSession().delete(entity);
result = true;
}
}
}
return result;
}
public void deleteByProperties(String[] propName, Object[] propValue) {
if (propName != null && propName.length > 0 && propValue != null && propValue.length > 0 && propValue.length == propName.length) {
StringBuffer sb = new StringBuffer("delete from " + entityClass.getName() + " o where 1=1 ");
appendQL(sb, propName, propValue);
Query query = getSession().createQuery(sb.toString());
setParameter(query, propName, propValue);
query.executeUpdate();
}
}
public void delete(E entity) {
getSession().delete(entity);
}
public void deleteByProperties(String propName, Object propValue) {
deleteByProperties(new String[] { propName }, new Object[] { propValue });
}
public void updateByProperties(String[] conditionName, Object[] conditionValue, String[] propertyName, Object[] propertyValue) {
if (propertyName != null && propertyName.length > 0 && propertyValue != null && propertyValue.length > 0 && propertyName.length == propertyValue.length && conditionValue != null && conditionValue.length > 0) {
StringBuffer sb = new StringBuffer();
sb.append("update " + entityClass.getName() + " o set ");
for (int i = 0; i < propertyName.length; i++) {
sb.append(propertyName[i] + " = :p_" + propertyName[i] + ",");
}
sb.deleteCharAt(sb.length() - 1);
sb.append(" where 1=1 ");
appendQL(sb, conditionName, conditionValue);
Query query = getSession().createQuery(sb.toString());
for (int i = 0; i < propertyName.length; i++) {
query.setParameter("p_" + propertyName[i], propertyValue[i]);
}
setParameter(query, conditionName, conditionValue);
query.executeUpdate();
} else {
throw new IllegalArgumentException("Method updateByProperties in BaseDao argument is illegal!");
}
}
public void updateByProperties(String[] conditionName, Object[] conditionValue, String propertyName, Object propertyValue) {
updateByProperties(conditionName, conditionValue, new String[] { propertyName }, new Object[] { propertyValue });
}
public void updateByProperties(String conditionName, Object conditionValue, String[] propertyName, Object[] propertyValue) {
updateByProperties(new String[] { conditionName }, new Object[] { conditionValue }, propertyName, propertyValue);
}
public void updateByProperties(String conditionName, Object conditionValue, String propertyName, Object propertyValue) {
updateByProperties(new String[] { conditionName }, new Object[] { conditionValue }, new String[] { propertyName }, new Object[] { propertyValue });
}
public void update(E entity) {
getSession().update(entity);
}
public void update(E entity, Serializable oldId) {
deleteByPK(oldId);
persist(entity);
}
public E merge(E entity) {
return (E) getSession().merge(entity);
}
public E getByProerties(String[] propName, Object[] propValue, Map<String, String> sortedCondition) {
if (propName != null && propName.length > 0 && propValue != null && propValue.length > 0 && propValue.length == propName.length) {
StringBuffer sb = new StringBuffer("select o from " + entityClass.getName() + " o where 1=1 ");
appendQL(sb, propName, propValue);
if (sortedCondition != null && sortedCondition.size() > 0) {
sb.append(" order by ");
for (Entry<String, String> e : sortedCondition.entrySet()) {
sb.append(e.getKey() + " " + e.getValue() + ",");
}
sb.deleteCharAt(sb.length() - 1);
}
Query query = getSession().createQuery(sb.toString());
setParameter(query, propName, propValue);
List<E> list = query.list();
if (list != null && list.size() > 0)
return list.get(0);
}
return null;
}
public E get(Serializable id) {
return (E) getSession().get(entityClass, id);
}
public E load(Serializable id) {
return (E) getSession().load(entityClass, id);
}
public E getByProerties(String[] propName, Object[] propValue) {
return getByProerties(propName, propValue, null);
}
public E getByProerties(String propName, Object propValue) {
return getByProerties(new String[] { propName }, new Object[] { propValue });
}
public E getByProerties(String propName, Object propValue, Map<String, String> sortedCondition) {
return getByProerties(new String[] { propName }, new Object[] { propValue }, sortedCondition);
}
public List<E> queryByProerties(String[] propName, Object[] propValue, Map<String, String> sortedCondition, Integer top) {
if (propName != null && propValue != null && propValue.length == propName.length) {
StringBuffer sb = new StringBuffer("select o from " + entityClass.getName() + " o where 1=1 ");
appendQL(sb, propName, propValue);
if (sortedCondition != null && sortedCondition.size() > 0) {
sb.append(" order by ");
for (Entry<String, String> e : sortedCondition.entrySet()) {
sb.append(e.getKey() + " " + e.getValue() + ",");
}
sb.deleteCharAt(sb.length() - 1);
}
Query query = getSession().createQuery(sb.toString());
setParameter(query, propName, propValue);
if (top != null) {
query.setFirstResult(0);
query.setMaxResults(top);
}
return query.list();
}
return null;
}
public List<E> queryByProerties(String[] propName, Object[] propValue, Integer top) {
return queryByProerties(propName, propValue, null, top);
}
public List<E> queryByProerties(String[] propName, Object[] propValue, Map<String, String> sortedCondition) {
return queryByProerties(propName, propValue, sortedCondition, null);
}
public List<E> queryByProerties(String propName, Object propValue, Map<String, String> sortedCondition, Integer top) {
return queryByProerties(new String[] { propName }, new Object[] { propValue }, sortedCondition, top);
}
public List<E> queryByProerties(String propName, Object propValue, Map<String, String> sortedCondition) {
return queryBy
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Java源码 Spring+MVC4+Hibernate4+ExtJS5 系统说明如下: 1、系统导入后发布到应用服务器,例如tomcat上即可运行。 2、导入项目后请将项目编码设置为UTF-8,不是GBK。 3、导入后如果出现感叹号,请修改实际的Java Build Path。 4、登录页面:http://localhost:8080/forestry/login.jsp, 用户名:admin 密码:123456。 5、SQL在sql文件夹里,直接执行即可。 6、applicationContext.xml配置文件里的mysql用户名是root,密码是123456;如果和你的不一样,请修改即可运行。
资源推荐
资源详情
资源评论
收起资源包目录
Java源码 Spring+MVC4+Hibernate4+ExtJS5 (2000个子文件)
sql server 2005.bak 1.64MB
BaseDao.class 25KB
ForestryController.class 19KB
AuthorityController.class 13KB
SysUserController.class 11KB
ForestryTypeController.class 10KB
BaseService.class 10KB
ConfigController.class 9KB
SensorController.class 9KB
ExtJSBaseController.class 8KB
BeanUtils.class 7KB
SystemInitListener.class 7KB
DepartmentController.class 6KB
SensorDaoImpl.class 5KB
Authority.class 4KB
ForestryServiceImpl.class 4KB
Service.class 4KB
Dao.class 4KB
BaseParameter.class 4KB
HtmlUtils.class 4KB
SysUser.class 4KB
Forestry.class 4KB
AuthorityServiceImpl.class 3KB
ForestryType.class 3KB
Attachment.class 3KB
MonitorLog.class 3KB
Sensor.class 3KB
SensorLastData.class 3KB
SensorData.class 3KB
SysUserServiceImpl.class 3KB
LoginFilter.class 3KB
PageView.class 2KB
AttachmentController.class 2KB
ConfigServiceImpl.class 2KB
RoleAuthorityController.class 2KB
SensorDataServiceImpl.class 2KB
RoleAuthority.class 2KB
ForestryTypeServiceImpl.class 2KB
Config.class 2KB
Department.class 2KB
AuthorityDaoImpl.class 2KB
SensorServiceImpl.class 2KB
SensorDataController.class 2KB
AttachmentDaoImpl.class 2KB
ForestryDaoImpl.class 2KB
ForestryParameter.class 2KB
HighPreciseComputor.class 2KB
SpringBeanFactoryUtils.class 2KB
DESede.class 2KB
MD5.class 2KB
CustomDateEditor.class 2KB
MonitorLogController.class 1KB
SensorDataDaoImpl.class 1KB
ForestryUtils.class 1KB
IPChecker.class 1KB
MonitorLogDaoImpl.class 1KB
ServiceException.class 1KB
ForestryBaseController.class 1KB
AttachmentServiceImpl.class 1KB
DateTimeSerializer.class 1KB
SysUserParameter.class 1KB
DateSerializer.class 1KB
JavaUtilDateConverter.class 1KB
GetHttpServletRequestWrapper.class 1KB
SSHTest.class 1KB
Group.class 1KB
MonitorLogServiceImpl.class 1KB
QueryResult.class 1KB
ForestryTypeParameter.class 1KB
ListView.class 1KB
SensorDataParameter.class 1KB
SensorLastDataServiceImpl.class 953B
RoleAuthorityServiceImpl.class 943B
SessionThreadLocal.class 941B
DepartmentServiceImpl.class 913B
ExtJSBaseParameter.class 896B
CacheFactory.class 893B
AttachmentParameter.class 873B
AuthorityService.class 841B
SensorService.class 804B
SensorLastDataController.class 795B
SensorDao.class 776B
Item.class 695B
ConfigParameter.class 685B
SensorLastDataDaoImpl.class 666B
SensorParameter.class 664B
RoleAuthorityDaoImpl.class 659B
ForestryTypeDaoImpl.class 652B
DepartmentDaoImpl.class 638B
SysUserDaoImpl.class 617B
ConfigDaoImpl.class 610B
AuthorityDao.class 583B
ForestryService.class 577B
SensorDataService.class 576B
JdbcBaseDao.class 570B
SystemCache.class 503B
JdbcBaseService.class 486B
AttachmentService.class 466B
ForestryTypeService.class 464B
AttachmentDao.class 438B
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
码码迷呀
- 粉丝: 6
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功