07-二次开发教程-Service-v8

2018-03-21 11:20 阅读

Service用于处理业务逻辑和调用DAO操作数据库。

配置文件

系统的Service在com.jspxcms.core.ContextConfig@ComponentScan({"com.jspxcms.core.service.impl", "com.jspxcms.ext.service.impl"})配置。

本例的Service在com.jspxcms.plug.ContextConfig@ComponentScan({ "com.jspxcms.plug.service.impl"})配置。

Service类

package com.jspxcms.plug.service.impl;

@Service
@Transactional(readOnly = true)
public class ResumeServiceImpl implements ResumeService {
    public Page<Resume> findAll(Integer siteId, Map<String, String[]> params,
            Pageable pageable) {
        return dao.findAll(spec(siteId, params), pageable);
    }

    public RowSide<Resume> findSide(Integer siteId,Map<String, String[]> params,
    Resume bean, Integer position, Sort sort) {
        if (position == null) {
            return new RowSide<Resume>();
        }
        Limitable limit = RowSide.limitable(position, sort);
        List<Resume> list = dao.findAll(spec(siteId, params), limit);
        return RowSide.create(list, bean);
    }

    private Specification<Resume> spec(final Integer siteId,
            Map<String, String[]> params) {
        Collection<SearchFilter> filters = SearchFilter.parse(params).values();
        final Specification<Resume> fsp = SearchFilter.spec(filters, Resume.class);
        Specification<Resume> sp = new Specification<Resume>() {
            public Predicate toPredicate(Root<Resume> root,
                    CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate pred = fsp.toPredicate(root, query, cb);
                if (siteId != null) {
                    pred = cb.and(pred, cb.equal(root.get("site")
                            .<Integer> get("id"), siteId));
                }
                return pred;
            }
        };
        return sp;
    }

    private ResumeDao dao;

    @Autowired
    public void setDao(ResumeDao dao) {
        this.dao = dao;
    }
}

该类使用到JPA的Specification查询方式。可实现后台列表点击表头,按任意列排序;列表页按任意字段查询;编辑页面上一条、下一条功能。

QQ咨询
电话
微信
微信扫码咨询