例如如下:
public class PersonDaoBean impl... PersonDao{
public void save(Person person);
pulblic void update(Person person);
public void delete(Integer id);
}
(假如为什么使用了spring)
@Trancational
public class PersonServiceBean impl... PersonService{
@Resource PersonDao personDao;
public void save(Person person){
personDao.save(person);
}
pulblic void update(Person person){
personDao.upadte(person);
}
public void delete(Integer id){
personDao.delete(id);
}
}
如上我们dao提供了提供了增改查方法,我们的业务层也需要提供这些方法,他紧紧是简单的调用了dao的这些方法,如果这样做的话,那么service里面的一些代码就是多余重复的,就出现了样板代码。
这样会增加开发工作量,并且也不优雅。
当你的数据来源有多个的时候,假设一个来自mysql、oracle、xml。那么我们就有必要定义dao层了。
假设我们多种数据库都需要保持:
public class PersonDaoBean impl... PersonDao{
public void save(Person person);
pulblic void update(Person person);
public void delete(Integer id);
}
public class XmlDaoBean impl... PersonDao{
public void save(Person person);
pulblic void update(Person person);
public void delete(Integer id);
}
public class OracleDaoBean impl... PersonDao{
public void save(Person person);
pulblic void update(Person person);
public void delete(Integer id);
}
(假如为什么使用了spring)
@Trancational
public class PersonServiceBean impl... PersonService{
@Resource PersonDao personDao;
@Resource XmlDao xmlDao;
@Resource OracleDao oracleDao;
public void save(Person person){
personDao.save(person);
xmlDao.save(person);
oracleDao.save(person);
}
pulblic void update(Person person){
personDao.upadte(person);
}
public void delete(Integer id){
personDao.delete(id);
}
具体我没需要省略Dao层的时候,我们应该如何使用?如
public interface DAO<T> {
/**
* 获取记录总数
* @param entityClass 实体类
* @return
*/
public long getCount();
/**
* 清除一级缓存的数据
*/
public void clear();
/**
* 保存实体
* @param entity 实体id
*/
public void save(Object entity);
/**
* 更新实体
* @param entity 实体id
*/
public void update(Object entity);
/**
* 删除实体
* @param entityClass 实体类
* @param entityids 实体id数组
*/
public void delete(Serializable ... entityids);
/**
* 获取实体
* @param <T>
* @param entityClass 实体类
* @param entityId 实体id
* @return
*/
public T find(Serializable entityId);
/**
* 获取分页数据
* @param <T>
* @param entityClass 实体类
* @param firstindex 开始索引
* @param maxresult 需要获取的记录数
* @return
*/
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();
}
@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();
}
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) 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;
}
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]);
}
}
}
/**
* 组装order by语句
* @param orderby
* @return
*/
protected static String buildOrderby(LinkedHashMap<String, String> orderby){
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();
}
/**
* 获取实体的名称
* @param <E>
* @param clazz 实体类
* @return
*/
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;
}
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;
}
}
public interface OrderContactInfoService extends DAO<OrderContactInfo> {
}
@Service
public class OrderContactInfoServiceBean extends DaoSupport<OrderContactInfo> implements OrderContactInfoService {
}