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. ...
随机推荐
- Qt5 程序发布打包
关于qt5在win7下发布 & 打包 ----------------------------------------------------------------------------- ...
- Java实现TCP之Echo客户端和服务端
Java实现TCP之Echo客户端和服务端 代码内容 采用TCP协议编写服务器端代码(端口任意) 编写客户机的代码访问该端口 客户机按行输入 服务器将收到的字符流和接收到的时间输出在服务器consol ...
- 关于sqlserver身份登录失败的解决方法
前几天写程序需要用到数据库,下载了一个用用,出现了不少的小问题(都怪我的32bit不争气的笔记本),有问题不要怕,至少证明我们在思考解决方案.废话不说了,直接上正题. Sqlserver有两种登陆方式 ...
- Java Day 12
包 编译格式 javac -d . **.java 包之间的访问 类找不到: 类名写错,包名.类名 包不存在:指定classpath 其他包的类无法访问:权限 public protected 包导入 ...
- NET Core中使用Redis
NET Core中使用Redis 注:本文提到的代码示例下载地址> https://code.msdn.microsoft.com/How-to-use-Redis-in-ASPNET-0d82 ...
- Android -- 资源使用和总结经验分享
颜色资源 颜色XML文件格式 ...
- LeetCode Shell Problems
195. Tenth Line -- 第十行 How would you print just the 10th line of a file? Solution: awk 'NR==10' file ...
- 使用Npoi向Excel中插入图片
先把数据库中的数据都导入到Excel表格中,把图片地址的路径全部转成绝对路径. 使用Npoi读取刚导出的Excle表格,把图片那个单元格的图片路径读出来,然后用文件流读取图片,然后通过Npoi把图片放 ...
- hdu 3487 Play with Chain
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3487 YaoYao is fond of playing his chains. He has a c ...
- Codeforces Round #274 (Div. 2)
A http://codeforces.com/contest/479/problem/A 枚举情况 #include<cstdio> #include<algorithm> ...