📄 daobase.java
字号:
package com.royee.ecport.dao.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.royee.ecport.dao.CommonDao;
import com.royee.ecport.pojo.EntityBase;
/**
* DAO基类
*
* @author cfgxy
*
* @param <E>
*/
public abstract class DaoBase<E extends EntityBase> implements CommonDao<E> {
SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
protected Session getCurrentSession(){
return sessionFactory.getCurrentSession();
}
/**
* 从数据库删除
*
* @param po
* 持久化的pojo
*/
public void delete(E po) {
getCurrentSession().delete(po);
}
/**
* 从数据库删除
*
* @param id
*
*/
public void deleteById(Long id){
delete(getById(id));
}
/**
* 通过持久化标识查找
*
* @param id
* 标识
* @return 持久化的pojo
*/
@SuppressWarnings("unchecked")
public E getById(Long id) {
return (E) getCurrentSession().load(getPojoType(), id);
}
/**
* 将托管的pojo变成持久化的pojo
*
* @param vo
*/
public void lock(E vo) {
getCurrentSession().lock(vo, LockMode.NONE);
}
/**
* 插入记录
*
* @param vo
* 插入后vo变为持久态
*/
public void save(E vo) {
getCurrentSession().save(vo);
}
/**
* 更新记录
*
* @param povo
* vo变为持久态
*/
public void saveOrUpdate(E povo) {
getCurrentSession().saveOrUpdate(povo);
}
/**
* HQL+Value_Map方式查询
*
* @param hql
* HQL
* @param values
* Value Map
* @param start
* 分页开始
* @param length
* 分页大小
* @return Pojo List
*/
@SuppressWarnings("unchecked")
protected List<E> findByHQL(String hql, Map<String, Object> values, int start,
int length) {
Query q = getCurrentSession().createQuery(hql);
if (values != null) {
Set<String> keys = values.keySet();
for (String key : keys) {
q.setParameter(key, values.get(key));
}
}
if (start >= 0)
q.setFirstResult(start);
if (length >= 0)
q.setMaxResults(length);
return q.list();
}
/**
* 无分页的HQL+Value_Map方式查询
*
* @param hql
* @param values
* @return
*/
protected List<E> findByHQL(String hql, Map<String, Object> values) {
return findByHQL(hql, values, -1, -1);
}
/**
* HQL+Value_List方式查询
*
* @param hql
* HQL
* @param values
* Value List
* @param start
* 分页开始
* @param length
* 分页长度
* @return Pojo List
*/
@SuppressWarnings("unchecked")
protected List<E> findByHQL(String hql, Object[] values, int start, int length) {
Query q = getCurrentSession().createQuery(hql);
if (values != null) {
int i = 0;
for (Object value : values) {
q.setParameter(i++, value);
}
}
if (start >= 0)
q.setFirstResult(start);
if (length >= 0)
q.setMaxResults(length);
return q.list();
}
/**
* 无分页的HQL+Value_List方式查询
*
* @param hql
* @param values
* @return
*/
protected List<E> findByHQL(String hql, Object[] values) {
return findByHQL(hql, values, -1, -1);
}
/**
* 无条件分页查询
*
* @param hql
* @param start
* @param length
* @return
*/
protected List<E> findByHQL(String hql, int start, int length) {
return findByHQL(hql, (Object[]) null, start, length);
}
/**
* 无条件查询(不分页)
*
* @param hql
* @return
*/
protected List<E> findByHQL(String hql) {
return findByHQL(hql, (Object[]) null, -1, -1);
}
protected List<E> findByHQL(String hql,Object value) {
return findByHQL(hql, new Object[]{value}, -1, -1);
}
protected List<E> findByHQL(String hql,String key,Object value) {
Map<String,Object> map=new HashMap<String,Object>();
map.put(key, value);
return findByHQL(hql, map, -1, -1);
}
protected List<E> findByHQL(String hql,Object value,int start,int length) {
return findByHQL(hql, new Object[]{value}, start, length);
}
protected List<E> findByHQL(String hql,String key,Object value,int start,int length) {
Map<String,Object> map=new HashMap<String,Object>();
map.put(key, value);
return findByHQL(hql, map, start, length);
}
/**
* 执行HQL查询
*
* @param hql
* @return
*/
protected int execute(String hql) {
Query q = getCurrentSession().createQuery(hql);
return q.executeUpdate();
}
protected int execute(String hql,Object value) {
Query q = getCurrentSession().createQuery(hql);
q.setParameter(0, value);
return q.executeUpdate();
}
protected int execute(String hql,String key,Object value) {
Query q = getCurrentSession().createQuery(hql);
q.setParameter(key, value);
return q.executeUpdate();
}
/**
* Value_Map方式执行HQL
*
* @param hql
* @param values
* @return
*/
protected int execute(String hql, Map<String, Object> values) {
Query q = getCurrentSession().createQuery(hql);
Set<String> keys = values.keySet();
for (String key : keys) {
q.setParameter(key, values.get(key));
}
return q.executeUpdate();
}
/**
* Value_List方式执行HQL
*
* @param hql
* @param values
* @return
*/
protected int execute(String hql, Object[] values) {
Query q = getCurrentSession().createQuery(hql);
int i = 0;
for (Object value : values) {
q.setParameter(i++, value);
}
return q.executeUpdate();
}
public int count(){
return countByWhere(null,null);
}
public int countByWhere(String where){
return countByWhere(where,null);
}
public int countByWhere(String where,Object value){
return countByWhere(where,new Object[]{value});
}
public int countByWhere(String where,Object[] params){
StringBuilder sb=new StringBuilder();
sb.append("select count(itm) from ")
.append(getPojoType().getSimpleName())
.append(" as itm");
if(where!=null)
sb.append(" where ").append(where);
Query q=getCurrentSession().createQuery(sb.toString());
int i;
if(params!=null)
for(i=0;i<params.length;i++){
q.setParameter(i, params[i]);
}
return ((Long)q.list().get(0)).intValue();
}
public void flush(){
getCurrentSession().flush();
}
public void update(E po){
getCurrentSession().update(po);
}
@SuppressWarnings("unchecked")
public E merge(E po){
return (E) getCurrentSession().merge(po);
}
public List<E> loadAll(int start, int length) {
// TODO Auto-generated method stub
return findByHQL("from "+getPojoType().getSimpleName(),start,length);
}
protected abstract Class<? extends EntityBase> getPojoType();
public void clear(){
getCurrentSession().clear();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -