Dorado 7 : 15.3.2 准备后台服务

步骤1:在com.bstek.dorado.sample.standardlesson.service.DeptService类中增加根据部门Id查询部门下所有员工的方法getEmployeeByDeptId,最终代码如下:

package com.bstek.dorado.sample.standardlesson.service;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.data.provider.Page;
import com.bstek.dorado.sample.standardlesson.dao.SlDeptDao;
import com.bstek.dorado.sample.standardlesson.dao.SlEmployeeDao;
import com.bstek.dorado.sample.standardlesson.entity.SlDept;
import com.bstek.dorado.sample.standardlesson.entity.SlEmployee;

@Component
public class DeptService {
    @Resource
    private SlDeptDao slDeptDao;
    
    @Resource
    private SlEmployeeDao slEmployeeDao;
    
    @DataProvider
    public Collection<SlDept> getTopDept(){
        return slDeptDao.find("from SlDept where slDept.deptId is null");
    }
    
    @DataProvider
    public Collection<SlDept> getDeptByParentId(Integer parentId){
        if(null != parentId){
            String hql = "from SlDept where slDept.deptId = :deptId";
            Map param = new HashMap();
            param.put("deptId", parentId);
            return slDeptDao.find(hql, param);
        }else{
            return null;
        }
    }
    
    @DataResolver
    @Transactional
    public void saveAll(Collection<SlDept> depts){
        for(SlDept dept:depts){
            slDeptDao.persistEntity(dept);
            Collection<SlDept> childs = dept.getSlDeptSet();
            if(!(childs ==  null)){
                for(SlDept child:childs){
                    //维护关联关系
                    child.setSlDept(dept);
                }
                slDeptDao.persistEntities(childs);
                saveAll(childs);
            }
        }
    }
    
    @DataProvider
    public void getEmployeeByDeptId(Page<SlEmployee> page, Integer deptId){
        if(null !=  deptId){
            String hql = "from SlEmployee where slDept.deptId = :deptId";
            Map param = new HashMap();
            param.put("deptId", deptId);
            slEmployeeDao.find(page, hql, param);
        }        
    }
}

可以看到getEmployeeByDeptId方法比较简单,由于SlDept下的slEmployeeSet这个Reference设置了pageSize和parameter这2个属性,因此在后台需要接受2个参数,一个是page参数,另一个是deptId部门编号参数,然后根据传入的部门编号进行查询。