Spring JPA 封装常用类

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37059838/article/details/85004034
public interface BaseService<entity, type> {
	/**
	 * 分页查询
	 *
	 * @param pageNum  当前页
	 * @param pageSize 每页的数据条数
	 * @param entity   可根对象信息进行精确查找
	 * @return Map
	 */

	Map<String, Object> page(int pageNum, int pageSize, entity entity);

	/**
	 * 分页查询
	 *
	 * @param pageNum  当前页
	 * @param pageSize 每页的数据条数
	 * @return Map
	 */
	Map<String, Object> page(int pageNum, int pageSize);

	/**
	 * 查询所有
	 *
	 * @return
	 */
	List<entity> selectAll();

	/**
	 * 根据主键查询,未查找到会抛出异常
	 *
	 * @param key
	 * @return
	 */
	entity selectByKey(type key);

	/**
	 * 根据主键查询,未查找到不会抛出异常
	 *
	 * @param id
	 * @return
	 */
	Optional<entity> selectById(type id);

	/**
	 * 保存实体
	 *
	 * @param entity
	 * @return
	 */
	entity save(entity entity);
	/**
	 * 批量新增
	 *
	 * @param entity
	 * @return
	 */
	List<entity> saveBatch(List<entity> entity);

	/**
	 * 删除对象
	 *
	 * @param entity
	 */
	void delete(entity entity);

	/**
	 * 批量删除对象
	 *
	 * @param entities entities
	 */
	void deleteBatch(List<entity> entities);

	/**
	 * 批量删除根据id
	 *
	 * @param ids ids
	 */
	void deleteBatch(type[] ids);

	/**
	 * 根据主键删除
	 *
	 * @param key
	 */
	void deleteById(type key);

	/**
	 * 更新实体(实体成员变量为null的不会更新)
	 *
	 * @param entity
	 * @return
	 */
	entity update(entity entity, type id);

	/**
	 * 批量更新对象
	 *
	 * @param entities entities
	 */
	void updateBatch(List<entity> entities, type[] ids);


}

@Transactional(propagation = Propagation.REQUIRED, readOnly = true, rollbackFor = Exception.class)
public abstract class BaseServiceResult<entity, type> implements BaseService<entity, type> {

    //总记录数
    protected final String PAGE_TOTAL = "total";
    //查询集合信息
    protected final String PAGE_ROWS = "rows";
    //验证
    protected final String PAGE_VALID = "valid";

    //完成JpaRepository注入
    private JpaRepository<entity, type> repository;

    @Autowired
    public void setRepository(JpaRepository<entity, type> repository) {
        this.repository = repository;
    }

    /**
     * 条件分页查询
     *
     * @param pageNum  当前页
     * @param pageSize 每页的数据条数
     * @param entity   可根对象信息进行精确查找
     * @return map集合
     */
    @Override
    public Map<String, Object> page(int pageNum, int pageSize, entity entity) {
        Map<String, Object> map = new HashMap<>();
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        //查询的实例(条件)
        Example<entity> example = Example.of(entity);
        Page<entity> page = repository.findAll(example, pageable);
        map.put(PAGE_TOTAL, page.getTotalElements());
        map.put(PAGE_ROWS, page.getContent());
        return map;
    }

    /**
     * 分页查询全部
     *
     * @param pageNum  当前页
     * @param pageSize 每页的数据条数
     * @return map集合
     */
    @Override
    public Map<String, Object> page(int pageNum, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        //查询的实例(条件)
        Page<entity> page = repository.findAll(pageable);
        map.put(PAGE_TOTAL, page.getTotalElements());
        map.put(PAGE_ROWS, page.getContent());
        return map;
    }

    /**
     * 查询全部
     *
     * @return list集合
     */
    @Override
    public List<entity> selectAll() {
        return repository.findAll();
    }

    /**
     * 单条件根据主键查询,未查找到会抛出异常
     *
     * @param key id
     * @return domain
     */
    @Override
    public entity selectByKey(type key) {
        return repository.getOne(key);
    }

    /**
     * 单条件根据主键查询,未查找到不会抛出异常
     *
     * @param id id
     * @return Optional
     */
    @Override
    public Optional<entity> selectById(type id) {
        return repository.findById(id);
    }

    /**
     * 保存
     *
     * @param entity 对象
     * @return domain
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public entity save(entity entity) {
        return repository.save(entity);
    }

    /**
     * 批量新增
     *
     * @param entity
     * @return
     */
    @Override
    public List<entity> saveBatch(List<entity> entity) {
        return repository.saveAll(entity);
    }

    /**
     * 删除对象
     *
     * @param entity 对象
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void delete(entity entity) {
        repository.delete(entity);
    }

    /**
     * 根据id删除
     *
     * @param key id
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteById(type key) {
        repository.deleteById(key);
    }

    /**
     * 批量删除根据id
     *
     * @param ids ids
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteBatch(type[] ids) {
        List<entity> list = new ArrayList<>();
        for (type id : ids) {
            entity entity = repository.getOne(id);
            list.add(entity);
        }
        repository.deleteInBatch(list);
    }

    /**
     * 批量删除对象
     *
     * @param entities entities
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteBatch(List<entity> entities) {
        repository.deleteInBatch(entities);
    }

    /**
     * 更新
     *
     * @param entity 更新参数
     * @param id     需要根据id过滤null值
     * @return 更新的结果
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public entity update(entity entity, type id) {
        entity object = repository.getOne(id);
        //将object复制给entity 忽略的属性是有值的属性(将object复制到entity属性为null)
        //import org.springframework.beans.BeanUtils;
        BeanUtils.copyProperties(object, entity, getNoNullProperties(entity));
        repository.save(entity);
        return entity;
    }

    /**
     * 批量更新
     *
     * @param entities entities
     * @param ids
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateBatch(List<entity> entities, type[] ids) {
        List<entity> list = new ArrayList<>();
        for (entity entity : entities) {
            for (type id : ids) {
                entity object = repository.getOne(id);
                BeanUtils.copyProperties(object, entity, getNoNullProperties(entity));
                list.add(entity);
            }
        }
        repository.saveAll(list);
    }

    /**
     * 获取对象不为空的属性
     *
     * @param o 参数
     * @return 将目标源中不为空的字段取出
     */
    protected static String[] getNoNullProperties(Object o) {
        //获取对象的bean的包装类
        BeanWrapper bean = new BeanWrapperImpl(o);
        //获取属性(字段)的描述
        PropertyDescriptor[] descriptors = bean.getPropertyDescriptors();
        Set<String> set = new HashSet<>();
        for (PropertyDescriptor descriptor : descriptors) {
            Object value = bean.getPropertyValue(descriptor.getName());
            if (!Objects.equals(value, null)) set.add(descriptor.getName());
        }
        String[] result = new String[set.size()];
        return set.toArray(result);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_37059838/article/details/85004034