弱弱的问下各路大侠,如下在nutz中的一个mvc写法的意义何在?
部门信息的Bean
import java.util.List; import org.nutz.dao.entity.annotation.*; /** * 部门信息bean */ @Table("t_department") public class Department { @Id @Column("id") private int id;// 机构ID @Column("name") private String name;// 机构名称 @Column("code") private String code;// 机构编号 @Column("pid") private int pid;// 父机构ID @One(field = "pid", target = Department.class) private Department pdepartment;// 上级机构信息 @Column("remark") private String remark;// 备注 @Column("orderid") private String orderid;// 排序号 @Column("deleted") private int deleted;//是否已删除 @ManyMany(from = "userid", relation = "t_role_department", target = Role.class, to = "roleid") private List<Role> roles;//关联的角色 //省略get和set.... }
部门信息的service接口
public interface DepartmentService { /** * 已删除部门 */ public List<Department> list(); /** * 查询所有部门(已删除、未删除) */ public List<Department> list(boolean haveDeleted); /** * 分页查询接口 */ public Map<String, Object> list(int page, int pagesize, String orderfield, String ordertype, String where, boolean haveDeleted); /** * 新增 * @return */ public Department insert(Department department); /** * 更新 */ public void update(Department department); /** * 批量删除(ID以逗号分割) */ public void remove(String ids); /** * 根据部门ID查询 */ public Department view(Long id); /** * 根据部门名称查询 */ public Department fetchByName(String name); /** * 清除 */ public void clear(String ids); }
部门信息的接口实现
@InjectName @IocBean(fields={"dao"}) public class DepartmentServiceImpl extends IdEntityService<Department> implements DepartmentService { /** * 查询所有部门 * * @param haveDeleted * 是否含已删除 * @return */ public List<Department> list(boolean haveDeleted) { if (haveDeleted) { return query(null, null); } else { return query(Cnd.where("deleted", "=", 0), null); } } /** * 分页查询所有部门 * * @param page * 每页数量 * @param pagesize * 页数 * @param orderfield * 排序字段 * @param ordertype * 排序方式 * @param where * 查询where条件 * @param haveDeleted * 是否含已删除 * @return */ public Map<String, Object> list(int page, int pagesize, String orderfield, String ordertype, String where, boolean haveDeleted) { if (pagesize < 1) { pagesize = 10; } // 创建分页对象 Pager pager = dao().createPager(page, pagesize); Map<String, Object> resultMap = new HashMap<String, Object>(); if (pager != null) { pager.setRecordCount(dao().count(Department.class,haveDeleted?null:Cnd.where("deleted", "=", 0))); resultMap.put("Total", pager.getRecordCount()); }else{ resultMap.put("Total", 0); } resultMap.put("Rows", query(CommonWhere.getWhereCnd(where, haveDeleted), pager)); return resultMap; } /** * 查询已删除部门 * * @return */ public List<Department> list() { return query(Cnd.where("deleted", "=", 1), null); } /** * 更新 * * @param department */ public void update(Department department) { dao().update(department); } /** * 新增 * * @param department */ public Department insert(Department department) { return dao().insertLinks(department, "pdepartment"); } /** * 根据部门ID查询 * * @param id * @return */ public Department view(Long id) { return dao().fetchLinks(fetch(id), "pdepartment"); } /** * 根据部门名称查询 * * @param name * @return */ public Department fetchByName(String name) { return fetch(Cnd.where("name", "=", name)); } /** * 根据ID批量删除部门(逻辑删除) */ @Override public void remove(String ids) { dao().update(Department.class, Chain.make("deleted", 1), Cnd.where("id", "IN", ids)); } /** * 根据ID批量清除部门 */ @Override public void clear(String ids) { dao().clear(Department.class, Cnd.where("id", "IN", ids)); } }
部门信息的Module,只贴一个方法看下
@InjectName("departmentModule") @At("/Department") public class DepartmentModule{ //日志 private static final Log log = Logs.get(); //部门信息服务类 private DepartmentService departmentService; /** * * 部门信息集合 * @param page 页码 * @param pagesize 分页数据大小 * @param sortname 排序字段 * @param sortorder 排序方式:asc desc * @param Where 查询条件 格式: * {"op":"and","rules":[ * {"op":"like", * "field":"text_fenleiname","value":"123","type":"string"} * ] * } * @return JSON格式的数据 例如:{"Rows":[{...},{...}],"Total":10} */ @At("/DepartmentList") @Ok("json:{ignoreNull:false}") public Object list( @Param("page") int page , @Param("pagesize") int pagesize, @Param("sortname")String sortname, @Param("sortorder")String sortorder, @Param("where")String where){ Map<String, Object> rMap = departmentService.list(page, pagesize, sortname, sortorder, where, true); log.debug(Json.toJson(rMap)); return rMap; } /** * 部门列表页 * @param currentid 当前部门ID * @param request * @return */ @At("/ShowDepartmentList") @Ok("jsp:jsp.system.dep.deplist") public Object showDepartmentList(@Param("id")String currentid,HttpServletRequest request){ if( Strings.isBlank(currentid) ){ currentid = "-1"; } request.setAttribute("pid", currentid); return null; } /** * 用户组操作页 * @param currentid 当前部门ID * @param isview 是否是查看状态 * @param request * @return */ @At("/ShowDepartmentDetail") @Ok("jsp:jsp.system.dep.depdetail") public Object showDepartmentDetail(@Param("id")String currentid,@Param("isview")String isview,HttpServletRequest request){ if( Strings.isBlank(currentid) ){ currentid = ""; } request.setAttribute("id", currentid); request.setAttribute("isview", isview); return null; } }
IOC的配置(mvc.js)
var ioc = { departmentService : { type : "com.ds.service.system.impl.DepartmentServiceImpl", fields : { dao : { refer : 'dao' } } }, departmentModule : { type : "com.ds.module.system.DepartmentModule", fields : { departmentService : { refer : 'departmentService' } } } }