Java-Code-CRUD:CrudService.java

ylbtech-Java-Code-CRUD:CrudService.java
1.返回顶部
1、
package com.sp.manager.util.common.service;

import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.sp.manager.util.common.persistence.CrudDao;
import com.sp.manager.util.common.persistence.DataEntity;
import com.sp.manager.util.common.persistence.Page;

/**
 * Service基类
 * @author
 * @version 2014-05-16
 */
@Transactional(readOnly = true)
public abstract class CrudService<D extends CrudDao<T>, T extends DataEntity<T>> extends BaseService {
    
    /**
     * 持久层对象
     */
    @Autowired
    protected D dao;
    
    /**
     * 获取单条数据
     * @param id
     * @return
     */
    public T get(String id) {
        return dao.get(id);
    }
    
    /**
     * 获取单条数据
     * @param entity
     * @return
     */
    public T get(T entity) {
        return dao.get(entity);
    }

    /**
     * 获取单条数据
     * @param entity
     * @return
     */
    public T getNew(T entity) {
        return dao.getNew(entity);
    }

    /**
     * 查询列表数据
     * @param entity
     * @return
     */
    public List<T> findList(T entity) {
        return dao.findList(entity);
    }

    /**
     * 查询列表数据
     * @param entity
     * @return
     */
    public List<T> findAllList(T entity) {
        return dao.findAllList(entity);
    }

    /**
     * 查询分页数据
     * @param page 分页对象
     * @param entity
     * @return
     */
    public Page<T> findPage(Page<T> page, T entity) {
        entity.setPage(page);
        page.setList(dao.findList(entity));
        return page;
    }
    
    /**
     * 查询分页数据
     * @param page 分页对象
     * @param entity
     * @return
     */
    public Page<T> findPageOver(Page<T> page, T entity) {
        entity.setPage(page);
        page.setList(dao.findListOver(entity));
        return page;
    }
    /**
     * 查询没有通过审核的分页数据
     * @param page 分页对象
     * @param entity
     * @return
     */
    public Page<T> findPageByStatus(Page<T> page, T entity) {
        entity.setPage(page);
        page.setList(dao.findListByStatus(entity));
        return page;
    }

    /**
     * 保存数据(插入或更新)
     * @param entity
     */
    @Transactional(readOnly = false)
    public void save(T entity) {
        if (entity.getIsNewRecord()){
            entity.preInsert();
            entity.setDelFlag("0");
            dao.insert(entity);
        }else{
            entity.preUpdate();
            dao.update(entity);
        }
    }
    
    /**
     * 删除数据
     * @param entity
     */
    @Transactional(readOnly = false)
    public void delete(T entity) {
        dao.delete(entity);
    }
    
    /**
     * 逻辑删除数据
     * @param entity
     */
    @Transactional(readOnly = false)
    public void deleteByLogic(T entity) {
        dao.deleteByLogic(entity);
    }
    
    
    /**
     * 删除全部数据
     * @param entitys
     */
    @Transactional(readOnly = false)
    public void deleteAll(Collection<T> entitys) {
        for (T entity : entitys) {
            dao.delete(entity);
        }
    }

    
    /**
     * 获取单条数据
     * @param propertyName
     * @return
     */
    public T findUniqueByProperty(String propertyName, Object value){
        return dao.findUniqueByProperty(propertyName, value);
    }

}
2、
2.返回顶部
 
3.返回顶部
 
4.返回顶部
 
5.返回顶部
 
 
6.返回顶部
 
warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/storebook/p/9605462.html