提取经常操作表如新增、修改、删除、查询、分页查询、统计等业务功能,形成基类,用泛型传参,有利于每个实体对象数据层继承。

package com.base.dao;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set; import javax.annotation.Resource; import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.CriteriaSpecification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; /**
* 数据库操作接口实现类,where条件使用命名参数
*
*/
@Repository("baseDao")
@SuppressWarnings("all")
public class BaseDao<T> { private SessionFactory sessionFactory; public SessionFactory getSessionFactory() {
return sessionFactory;
} @Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
} private Session getCurrentSession() {
return sessionFactory.getCurrentSession();//获取数据库链接
} /**
* 保存一个对象
*
* @param T 要保存的JavaBean 对象
*
*/
public Serializable save(T o) {
return this.getCurrentSession().save(o);
} /**
* 删除一个对象
*
* @param T 对象
*
*/
public void delete(T o) {
this.getCurrentSession().delete(o);
} /**
* 修改一个对象
*
* @param T 对象
*
*/
public void update(T o) {
this.getCurrentSession().update(o);
} /**
* 查询对象集合
*
* @param Hql格式查询语句
* @return 对象集合
*/
public List<T> find(String hql) {
return this.getCurrentSession().createQuery(hql).list();
} /**
* 查询对象集合
*
* @param Hql格式语句
* @param map参数
* @return 对象集合
*/
public List<T> find(String hql, Map<String, Object> map) {
Query q = this.getCurrentSession().createQuery(hql);
if (map != null) {
Set<String> keySet = map.keySet();
for (String string : keySet) {
Object obj = map.get(string);
if (obj instanceof Collection<?>) {
q.setParameterList(string, (Collection<?>) obj);
} else if (obj instanceof Object[]) {
q.setParameterList(string, (Object[]) obj);
} else {
q.setParameter(string, obj);
}
}
}
return q.list();
} /**
* 分页查询对象集合
*
* @param Hql格式语句
* @param map参数
* @param 页码
* @param 每页记录数
* @return 对象集合
**/
public List<T> find(String hql, Map<String, Object> map, Integer page, Integer rows) {
if (page == null || page < 1) {
page = 1;
}
if (rows == null || rows < 1) {
rows = 10;
}
Query q = this.getCurrentSession().createQuery(hql);
if (map != null) {
Set<String> keySet = map.keySet();
for (String string : keySet) {
Object obj = map.get(string);
if (obj instanceof Collection<?>) {
q.setParameterList(string, (Collection<?>) obj);
} else if (obj instanceof Object[]) {
q.setParameterList(string, (Object[]) obj);
} else {
q.setParameter(string, obj);
}
}
}
return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
} /**
* 查询指定对象
*
* @param 对象
* @param 主键值
* @return 获取对象
*/
public T get(Class<T> c, Serializable id) {
return (T) this.getCurrentSession().get(c, id);
} /**
* 查询指定对象
*
* @param hql查询语句
* @param map参数
* @return 相应对象
*/
public T get(String hql, Map<String, Object> map) {
List<T> l = this.find(hql, map);
if (l != null && l.size() > 0) {
return l.get(0);
} else {
return null;
}
} /**
* 查询记录数
*
* @param hql查询语句
* @param map参数
* @return 记录数
*/
public Long count(String hql, Map<String, Object> map) {
Query q = this.getCurrentSession().createQuery(hql);
if (map != null) {
Set<String> keySet = map.keySet();
for (String string : keySet) {
Object obj = map.get(string);
if (obj instanceof Collection<?>) {
q.setParameterList(string, (Collection<?>) obj);
} else if (obj instanceof Object[]) {
q.setParameterList(string, (Object[]) obj);
} else {
q.setParameter(string, obj);
}
}
}
return (Long) q.uniqueResult();
} /**
* 执行更新语句
*
* @param hql查询语句
* @param map参数
* @return 响应数目
*/
public Integer executeHql(String hql, Map<String, Object> map) {
Query q = this.getCurrentSession().createQuery(hql);
if (map != null) {
Set<String> keySet = map.keySet();
for (String string : keySet) {
Object obj = map.get(string);
if (obj instanceof Collection<?>) {
q.setParameterList(string, (Collection<?>) obj);
} else if (obj instanceof Object[]) {
q.setParameterList(string, (Object[]) obj);
} else {
q.setParameter(string, obj);
}
}
}
return q.executeUpdate();
} /**
* 执行查询语句
*
* @param hql查询语句
* @param map 参数
* @return 响应数目
*/
public List<Object> queryHql(String hql, Map<String, Object> map) {
Query q = this.getCurrentSession().createQuery(hql);
if (map != null) {
Set<String> keySet = map.keySet();
for (String string : keySet) {
Object obj = map.get(string);
if (obj instanceof Collection<?>) {
q.setParameterList(string, (Collection<?>) obj);
} else if (obj instanceof Object[]) {
q.setParameterList(string, (Object[]) obj);
} else {
q.setParameter(string, obj);
}
}
}
return q.list();
} /**
* 执行查询语句
*
* @param hql查询语句
* @param map 参数
* @return Map记录返回集合
*/
public List<Object> queryHqlMap(String hql, Map<String, Object> map) {
Query q = this.getCurrentSession().createQuery(hql);
if (map != null) {
Set<String> keySet = map.keySet();
for (String string : keySet) {
Object obj = map.get(string);
if (obj instanceof Collection<?>) {
q.setParameterList(string, (Collection<?>) obj);
} else if (obj instanceof Object[]) {
q.setParameterList(string, (Object[]) obj);
} else {
q.setParameter(string, obj);
}
}
}
q.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
return q.list();
}
}

 

调用示例

@Repository("softwareDao")
public class SoftwareDao extends BaseDao<Software> { public void delete(String terminalId) {
String hql = "delete from Software WHERE terminalId = :terminalId";
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("terminalId", terminalId);
executeHql(hql, paramMap);
} }

  

 

最新文章

  1. Asp.Net Core 项目实战之权限管理系统(6) 功能管理
  2. Linux下几款好用的录屏软件
  3. 关于cookie 取不到值的问题
  4. 安装android studio
  5. Comon.Logging与Log4net联合使用
  6. 有关&lt;table&gt;的几个问题
  7. Code the Tree
  8. 剪花布条 - HDU 2087(简单KMP | 暴力)
  9. 基于.NET打造IP智能网络视频监控系统
  10. mysql批量插入之提高插入效率
  11. bounding box的简单理解
  12. 2018-2019-2 20175305实验一《Java开发环境的熟悉》实验报告
  13. 百战程序员9- IO流
  14. 每日分享!canvas的使用~
  15. Orleans学习总结(五)--监控篇
  16. Floyd算法简介
  17. wpf 如果列表加载超多数据变的卡顿时,使用VirtualizingStackPanel
  18. EditText 中文文档
  19. GUI程序设计3
  20. GPS项目小结

热门文章

  1. 牛客剑指offer(持续更新~)
  2. [问题记录]——log4net记录多个级别文件
  3. 设置QQ环境变量
  4. Vue项目中实现用户登录及token验证
  5. Spring Cloud第十篇 | 分布式配置中心Config
  6. 面试连环炮系列(十五):说说Eureka的高可用方案
  7. [FPGA] Verilog 燃气灶控制器的设计与实现
  8. 基于Vue的前后端分离项目实践
  9. AE单词备忘
  10. 松软科技Web课堂:JavaScript JSON