1.

 package com.sanqing.dao;

 import java.io.Serializable;
import java.util.LinkedHashMap; import com.sanqing.util.QueryResult; public interface DAO<T> {
public long getCount();//获得记录总数
public void clear();//清除一级缓存的数据
public void save(Object entity);//保存记录
public void update(Object entity);//更新记录
public void delete(Serializable ... entityids);//删除记录
public T find(Serializable entityId);//通过主键获得记录
public QueryResult<T> getScrollData(int firstindex, int maxresult, String wherejpql,
Object[] queryParams,LinkedHashMap<String, String> orderby);//获得分页记录
public QueryResult<T> getScrollData(int firstindex, int maxresult,
String wherejpql, Object[] queryParams);//获得分页记录
public QueryResult<T> getScrollData(int firstindex, int maxresult,
LinkedHashMap<String, String> orderby);//获得分页记录
public QueryResult<T> getScrollData(int firstindex, int maxresult); //获得分页记录
public QueryResult<T> getScrollData();//获得分页记录
}

2.

 package com.sanqing.dao;

 import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.LinkedHashMap; import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import com.sanqing.util.GenericsUtils;
import com.sanqing.util.QueryResult; @SuppressWarnings("unchecked")
@Transactional
public abstract class DaoSupport<T> implements DAO<T>{
protected Class<T> entityClass = GenericsUtils.getSuperClassGenricType(this.getClass());//获得该类的父类的泛型参数的实际类型
@PersistenceContext protected EntityManager em;
public void clear(){//清除一级缓存的数据
em.clear();
}
public void delete(Serializable ... entityids) {//删除记录
for(Object id : entityids){
em.remove(em.getReference(this.entityClass, id));
}
}
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public T find(Serializable entityId) {//通过主键获得记录
if(entityId==null) throw new RuntimeException(this.entityClass.getName()+ ":传入的实体id不能为空");
return em.find(this.entityClass, entityId);
}
public void save(Object entity) {//保存记录
em.persist(entity);
}
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public long getCount() { //获得记录总数
return (Long)em.createQuery("select count("+ getCountField(this.entityClass) +") from "+ getEntityName(this.entityClass)+ " o").getSingleResult();
} protected static <E> String getCountField(Class<E> clazz){
String out = "o";
try {
PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(clazz).getPropertyDescriptors();
for(PropertyDescriptor propertydesc : propertyDescriptors){
Method method = propertydesc.getReadMethod();
if(method!=null && method.isAnnotationPresent(EmbeddedId.class)){
PropertyDescriptor[] ps = Introspector.getBeanInfo(propertydesc.getPropertyType()).getPropertyDescriptors();
out = "o."+ propertydesc.getName()+ "." + (!ps[1].getName().equals("class")? ps[1].getName(): ps[0].getName());
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return out;
} protected static void setQueryParams(Query query, Object[] queryParams){
if(queryParams!=null && queryParams.length>0){
for(int i=0; i<queryParams.length; i++){
query.setParameter(i+1, queryParams[i]);
}
}
}
protected static String buildOrderby(LinkedHashMap<String, String> orderby){//组装order by语句
StringBuffer orderbyql = new StringBuffer("");
if(orderby!=null && orderby.size()>0){
orderbyql.append(" order by ");
for(String key : orderby.keySet()){
orderbyql.append("o.").append(key).append(" ").append(orderby.get(key)).append(",");
}
orderbyql.deleteCharAt(orderbyql.length()-1);
}
return orderbyql.toString();
}
protected static <E> String getEntityName(Class<E> clazz){//获取实体的名称
String entityname = clazz.getSimpleName();
Entity entity = clazz.getAnnotation(Entity.class);
if(entity.name()!=null && !"".equals(entity.name())){
entityname = entity.name();
}
return entityname;
} public void update(Object entity) {//更新记录
em.merge(entity);
}
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public QueryResult<T> getScrollData(int firstindex,
int maxresult, LinkedHashMap<String, String> orderby) {//获得分页记录
return getScrollData(firstindex,maxresult,null,null,orderby);
}
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public QueryResult<T> getScrollData(int firstindex,
int maxresult, String wherejpql, Object[] queryParams) {//获得分页记录
return getScrollData(firstindex,maxresult,wherejpql,queryParams,null);
}
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public QueryResult<T> getScrollData(int firstindex, int maxresult) {//获得分页记录
return getScrollData(firstindex,maxresult,null,null,null);
}
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public QueryResult<T> getScrollData() {
return getScrollData(-1, -1);
}
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public QueryResult<T> getScrollData(int firstindex, int maxresult, String wherejpql,
Object[] queryParams,LinkedHashMap<String, String> orderby) {//获得分页记录
QueryResult qr = new QueryResult<T>();//查询记录结果
String entityname = getEntityName(this.entityClass);//获得实体名称
Query query = em.createQuery("select o from "+
entityname+ " o "+(wherejpql==null
|| "".equals(wherejpql.trim())? "":
"where "+ wherejpql)+ buildOrderby(orderby));//执行查询
setQueryParams(query, queryParams);//设置查询参数
if(firstindex!=-1 && maxresult!=-1) //两个参数都不是-1的时候才分页
query.setFirstResult(firstindex).
setMaxResults(maxresult);//设置查询记录的起始位置和查询最大数
qr.setResultlist(query.getResultList());//设置查询的记录
query = em.createQuery("select count(" +
getCountField(this.entityClass)+ ") " +
"from "+ entityname+ " o "+(wherejpql==null ||
"".equals(wherejpql.trim())? "": "where "+ wherejpql));
setQueryParams(query, queryParams);//设置查询参数
qr.setTotalrecord((Long)query.getSingleResult());//设置查询记录总数
return qr;//返回查询记录结果
} }

JavaWeb项目开发案例精粹-第6章报价管理系统-03Dao层的更多相关文章

  1. JavaWeb项目开发案例精粹-第6章报价管理系统-05Action层

    0. <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC &quo ...

  2. JavaWeb项目开发案例精粹-第6章报价管理系统-07View层

    1. 2.back_index.html <HTML> <HEAD> <META HTTP-EQUIV="Content-Type" CONTENT= ...

  3. JavaWeb项目开发案例精粹-第6章报价管理系统-06po层

    1. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www ...

  4. JavaWeb项目开发案例精粹-第6章报价管理系统-04Service层

    1. package com.sanqing.service; import com.sanqing.dao.DAO; import com.sanqing.po.Customer; /** * 客户 ...

  5. JavaWeb项目开发案例精粹-第6章报价管理系统-002辅助类及配置文件

    1. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www ...

  6. JavaWeb项目开发案例精粹-第6章报价管理系统-001需求分析及设计

    1. 2. 3. 4. 5. 6.

  7. JavaWeb项目开发案例精粹-第2章投票系统-006view层

    1.index.jsp <%@ page language="java" import="java.util.*" pageEncoding=" ...

  8. JavaWeb项目开发案例精粹-第2章投票系统-004action层

    1. package com.sanqing.action; import java.util.UUID; import com.opensymphony.xwork2.ActionSupport; ...

  9. JavaWeb项目开发案例精粹-第2章投票系统-003Dao层

    1. package com.sanqing.dao; import java.util.List; import com.sanqing.bean.Vote; import com.sanqing. ...

随机推荐

  1. GitFlow教程

    GitFlow教程 这份教程是博主学到的git基础,仅适合小团队使用,仅供参考 配置Git 配置github上面的账号,首先需要自己在git上注册一个账号 git config --global us ...

  2. 二、IRIG_B解码AC信号

    AC-----过零检测(MAX913ESA)---1khzB码信号(以0v为界大于0为高,小于0为低,的 方波信号) AC-----信号放大(TLE2022ID--mv到5v) ---系统电压转换(M ...

  3. C++ vector介绍

    <span style="font-family: Arial; ">在此总结一下模板vector的使用介绍</span> 标准库vector类型使用需要的 ...

  4. 设置搜狗输入法在任何时候按左右两侧的shift激活

    如上图,搜狗输入法for linux最近与广大用户见面了,现在的版本是1.0.0.0014,本人系统是ubuntu 14.04非麒麟版本 使用过程中有个习惯就是在任何窗口内只要按任意一侧的shift就 ...

  5. 基于PBOC电子钱包的消费过程详解

    智能卡金融行业应用电子钱包的消费交易流程,开发人员可参考 首先终端和卡片有一个共同的密钥叫做消费密钥:PurchKey (针对每种特定的交易,比如,圈存,消费,都有特定的密钥与之对应) 假设Purch ...

  6. xml基础学习笔记04

    今天继续xml学习,主要是:SimpleXML快速解析文档.xml与数组相互转换 .博客中只是简单的做一个学习记录.积累.更加详细的使用方法,可以查看php手册 1.SimpleXML快速解析文档 前 ...

  7. 向Array中添加改进的冒泡排序

    改进冒泡思路 如果在某次的排序中没有出现交换的情况,那么说明在无序的元素现在已经是有序了,就可以直接返回了. 改进冒泡实现 Function.prototype.method = function(n ...

  8. ReactJS入门

    React介绍 React是facebook开发基于组件驱动开发(CDD ) 的UI类库,相对于双向绑定的类库,如AngularJS,它采用单向数据流绑定.通过采用虚拟DOM的概念,是的他在性能和处理 ...

  9. 使用第三方工具覆写Object中方法

    我们在实际开发中,经常需要覆写Object中的equals,hashcode,toString方法,其实编写这些代码并不是很难,但很枯燥和乏味. 下面推荐Google的Guava jar包来覆写上面的 ...

  10. Java Synchronized Blocks vs. Methods

    It's possible to synchronize both an entire method and a section of code within a method, and you ma ...