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. ...
随机推荐
- 双系统下利用MbrFix.exe卸载LINUX系统
前言: 不少同学笔记本都装的有双系统,一般都是LIUNX和WINDOWS的两个系统(由于以前对电脑各种无知)装了双系统,再次,小编就不在阐述双系统地各种不便,再次就强调一下,假若要卸载LINUX的话 ...
- Differences Between Xcode Project Templates for iOS Apps
Differences Between Xcode Project Templates for iOS Apps When you create a new iOS app project in Xc ...
- Notes of the scrum meeting(11/3)
meeting time:19:30~20:00p.m.,November 3th,2013 meeting place:20号公寓楼前 attendees: 顾育豪 ...
- The finnacial statements,taxes and cash flow
This chapter-2 we learn about the the financial statements(财务报表),taxes and cash flow.We must pay par ...
- PP生产订单的BADI增强 WORKORDER_UPDATE
METHOD if_ex_workorder_update~before_update. *---------------------->增强1 开始* "当生产订单类型为PP01时, ...
- .NET Framework 4.5、4.5.1 和 4.5.2 中的新增功能
.NET Framework 4.5.4.5.1 和 4.5.2 中的新增功能 https://msdn.microsoft.com/zh-cn/library/ms171868.aspx
- hdu 1028 Ignatius and the Princess III
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1028 题目大意:3=1+1+1=1+2=3 :4=4=1+1+1+1=1+2+1=1+3:所以3有3种 ...
- 【BZOJ】【1059】【ZJOI2007】矩阵游戏
二分图完美匹配/匈牙利算法 如果a[i][j]为黑点,我们就连边 i->j ,然后跑二分图最大匹配,看是否有完美匹配. <_<我们先考虑行变换:对于第 i 行,如果它第 j 位是黑点 ...
- JS 时分秒验证
- 剑指offer--面试题23
//该题原本想用递归实现,但却用循环实现了... //关键用了个队列 #include <queue> void Print(BinaryTreeNode* pRoot, std::que ...