JavaWeb项目开发案例精粹-第6章报价管理系统-03Dao层
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层的更多相关文章
- JavaWeb项目开发案例精粹-第6章报价管理系统-05Action层
0. <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC &quo ...
- JavaWeb项目开发案例精粹-第6章报价管理系统-07View层
1. 2.back_index.html <HTML> <HEAD> <META HTTP-EQUIV="Content-Type" CONTENT= ...
- JavaWeb项目开发案例精粹-第6章报价管理系统-06po层
1. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www ...
- JavaWeb项目开发案例精粹-第6章报价管理系统-04Service层
1. package com.sanqing.service; import com.sanqing.dao.DAO; import com.sanqing.po.Customer; /** * 客户 ...
- JavaWeb项目开发案例精粹-第6章报价管理系统-002辅助类及配置文件
1. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www ...
- JavaWeb项目开发案例精粹-第6章报价管理系统-001需求分析及设计
1. 2. 3. 4. 5. 6.
- JavaWeb项目开发案例精粹-第2章投票系统-006view层
1.index.jsp <%@ page language="java" import="java.util.*" pageEncoding=" ...
- JavaWeb项目开发案例精粹-第2章投票系统-004action层
1. package com.sanqing.action; import java.util.UUID; import com.opensymphony.xwork2.ActionSupport; ...
- JavaWeb项目开发案例精粹-第2章投票系统-003Dao层
1. package com.sanqing.dao; import java.util.List; import com.sanqing.bean.Vote; import com.sanqing. ...
随机推荐
- UAT测试,PPT测试
UAT:user acceptable testing 用户验收测试 PPT:product produce test 产品生产验证
- linux下在某行的前一行或后一行添加内容
linux的sed工具是十分强大的,能很容易的实现在某关键词的前一行或后一行增加内容.今天在批量修改tomcat的日志时就用到了该功能. 一.在某行的前一行或后一行添加内容 具休操作如下: #匹配行前 ...
- Visual Studio 2013 各版本注册码
Visual Studio Ultimate 2013 KEY(密钥):BWG7X-J98B3-W34RT-33B3R-JVYW9 Visual Studio Premium 2013 KEY(密钥) ...
- 【BZOJ】【1031】【JSOI2007】字符加密Cipher
后缀数组 当年感觉好神的题现在好像变水了…… 题意其实有点蛋疼……一开始没看懂<_< 将原串复制一遍接在后面,用后缀数组求一下SA,那么SA<n的就是所找到的那n个字符串,然后把它们 ...
- 【BZOJ】【1449】【JSOI2009】球队收益
网络流/费用流/二分图最小权匹配 题解:http://blog.csdn.net/huzecong/article/details/9119741 太神了!由于一赢一输不好建图,就先假设全部都输,再将 ...
- Leetcode#103 Binary Tree Zigzag Level Order Traversal
原题地址 基本数据结构操作,二叉树的层次遍历. 代码: vector<vector<int> > zigzagLevelOrder(TreeNode *root) { vect ...
- eclipse中本地项目怎么和svn中的项目关联?
网速不好,通过别的方式把项目下载到本地硬盘,然后导入eclipse,代码修改后怎么提交到svn呢? 这个纠结了好久的问题... 首先要确定eclipse中安装了svn插件. 然后在“svn资源库研究” ...
- 新浪微博之XSS蠕虫脚本源码讲解
主要是因为新浪的广场页面有几个链接对输入参数过滤不严导致的反射性XSS.======================================== 微博XSS漏洞点 weibo.com/pub/ ...
- VC5509的通用GEL代码
GEL是通用扩展语言(General Extension Language)的英文缩写,GEL是一个大小写敏感但缺少类型检测的解释性语言,只有int类型,在语法上可看作是C语言的一个子集.GEL主要用 ...
- Yarn的服务库和事件库
对于生命周期较长的对象,YARN采用了基于服务对象管理模型对其进行管理. 该模型有一下特点: 每个被服务化的对象都分为4个状态 任何服务状态变化都可以触发另外一些动作 可以通过组合方式对任意服务进行组 ...