在上一篇《使用Nutz[1.b.38]对关联数据表的一对一/一对多操作 》中提到对象关联的排序问题,如下:
--------------------------------------------------------------------------------------
发现了dao中有一个问题目前还没有解决,即Nutz中关联查询的排序问题,假如我在查询用户列表时,无法通过departmentInfo.departmentName字段来排序,即无法使用以下代码获取:
List<UserInfo> uiList = this.userInfoService.query(Cnd.orderBy().asc("departmentInfo.departmentName"), null);
---------------------------------------------------------------------------------------
已咨询过灰太狼,目前暂不支持,以后是否支持,需视大家的呼吁强度而定。详情请进>>>
那么,在Nutz支持此功能之前,我们可以通过视图来解决此问题。
OK,进入主题!!
关于一些环境、数据源和dao对象、web.xml的配置,请移步上一篇查看。
其中使用的nutz-1.b.38-jdk6.jar是有更改过的,不是当时最早发布的包,因存在bug,无法使用视图,后来灰太狼修复好了,本文提供的源代码中是更改过的jar包。当然,大家使用最新的1.b.39也是可以的。
一、数据库
此demo涉及两个表DepartmentInfo和UserInfo,和一个视图UserInfoView,SQL如下:
-- ----------------------------
-- Table structure for `departmentinfo`
-- ----------------------------
DROP TABLE IF EXISTS `departmentinfo`;
CREATE TABLE `departmentinfo` (
`departmentInfoId` int(11) NOT NULL AUTO_INCREMENT,
`departmentName` varchar(50) DEFAULT NULL,
PRIMARY KEY (`departmentInfoId`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of departmentinfo
-- ----------------------------
INSERT INTO `departmentinfo` VALUES ('1', 'IT部');
INSERT INTO `departmentinfo` VALUES ('2', '人力资源');
INSERT INTO `departmentinfo` VALUES ('3', '财务部');
INSERT INTO `departmentinfo` VALUES ('4', '市场部');
-- ----------------------------
-- Table structure for `userinfo`
-- ----------------------------
DROP TABLE IF EXISTS `userinfo`;
CREATE TABLE `userinfo` (
`userInfoId` int(11) NOT NULL AUTO_INCREMENT,
`trueName` varchar(20) DEFAULT NULL,
`departmentInfoId` int(11) DEFAULT NULL,
`addDate` datetime DEFAULT NULL,
`addIp` varchar(15) DEFAULT NULL,
PRIMARY KEY (`userInfoId`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of userinfo
-- ----------------------------
INSERT INTO `userinfo` VALUES ('1', 'Gevin', '1', '2011-07-25 19:28:50', '127.0.0.1');
INSERT INTO `userinfo` VALUES ('2', 'Wendal', '1', '2011-07-25 19:29:07', '127.0.0.1');
INSERT INTO `userinfo` VALUES ('3', '小宝', '2', '2011-07-25 19:29:55', '127.0.0.1');
INSERT INTO `userinfo` VALUES ('4', '灰太狼', '3', '2011-07-25 19:30:11', '127.0.0.1');
INSERT INTO `userinfo` VALUES ('5', 'E-Hunter', '3', '2011-07-25 19:30:38', '127.0.0.1');
INSERT INTO `userinfo` VALUES ('8', 'k-wait', '4', '2011-07-26 15:54:01', '127.0.0.1');
二、结构图
二、Model类
DepartmentInfo.java
package demo.nutz.model; import java.util.List; import org.nutz.dao.entity.annotation.Id; import org.nutz.dao.entity.annotation.Many; import org.nutz.dao.entity.annotation.Table; @Table("DepartmentInfo") public class DepartmentInfo { @Id private Integer departmentInfoId; private String departmentName; /*此处省略Getter和Setter*/ }
UserInfo.java类
package demo.nutz.model; import java.util.Date; import org.nutz.dao.entity.annotation.Id; import org.nutz.dao.entity.annotation.Readonly; import org.nutz.dao.entity.annotation.Table; import org.nutz.dao.entity.annotation.View; @Table("UserInfo") @View("UserInfoView") /* 映射视图UserInfoView,Nutz在读取的时候会默认读取视图,插入/更新操作则会使用表格 */ public class UserInfo { @Id private Integer userInfoId; private String trueName; private Integer departmentInfoId; private Date addDate; private String addIp; // 以下字段为视图所用,关联departmentInfo.departmentName @Readonly private String departmentName; /*此处省略Getter和Setter*/ }
三、模块类
DepartmentInfoAction.java跟上一篇一样,没改动。这里主要讲一下UserInfoAction.java类。
package demo.nutz.action; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.nutz.dao.Cnd; import org.nutz.ioc.loader.annotation.Inject; import org.nutz.ioc.loader.annotation.IocBean; import org.nutz.mvc.annotation.At; import org.nutz.mvc.annotation.Ok; import org.nutz.mvc.annotation.Param; import demo.nutz.base.BaseAction; import demo.nutz.model.DepartmentInfo; import demo.nutz.model.UserInfo; import demo.nutz.service.DepartmentInfoService; import demo.nutz.service.UserInfoService; /** * 用户信息模块 * * @author gevin([email protected]) * */ @IocBean @At("/userinfo") public class UserInfoAction extends BaseAction { @Inject // 通过注解@Inject注入对象 private DepartmentInfoService departmentInfoService; @Inject // 通过注解@Inject注入对象 private UserInfoService userInfoService; /** * 跳转到添加页面 * * @return */ @At("/addUserInfo") @Ok("jsp:${obj.msg == null ? '/userinfo/addUserInfo' : '/userinfo/showMessage'}") // 在@Ok中使用NutzEL表达式 public Map<String, Object> addUserInfo() { Map<String, Object> map = new HashMap<String, Object>(); try { // 获取部门列表 List<DepartmentInfo> diList = this.departmentInfoService.query(Cnd.orderBy().asc("departmentName"), null); map.put("departmentInfoList", diList); return success(map); } catch (Exception e) { e.printStackTrace(); return failure(GET_FAILURE, map); } } /** * 添加成功 * * @param ui * @return */ @At("/addUserInfoOk") @Ok("jsp:/userinfo/showMessage") public Map<String, Object> addUserInfoOk(@Param("..") UserInfo ui, HttpServletRequest request) { Map<String, Object> map = new HashMap<String, Object>(); try { if (ui==null || ui.getDepartmentInfoId()==null) return failure(GET_FAILURE, map); // 获取部门信息 DepartmentInfo di = this.departmentInfoService.fetch(ui.getDepartmentInfoId()); if (di == null) return failure(GET_FAILURE, map); ui.setAddDate(new Date()); ui.setAddIp(request.getRemoteAddr()); // 添加成功 if (this.userInfoService.dao().insert(ui) == null) return failure(ADD_FAILURE, map); return success(ADD_SUCCESS, map); } catch (Exception e) { e.printStackTrace(); map.put("msg", "getFailure"); return map; } } /** * 跳转到修改页面 * * @param userInfoId * @return */ @At("/editUserInfo") @Ok("jsp:${obj.msg == null ? '/userinfo/editUserInfo' : '/userinfo/showMessage'}") public Map<String, Object> editUserInfo(@Param("userInfoId") Integer userInfoId) { Map<String, Object> map = new HashMap<String, Object>(); try { if (userInfoId == null) return failure(GET_FAILURE, map); // 获取对象 UserInfo ui = this.userInfoService.fetch(userInfoId); if (ui == null) return failure(GET_FAILURE, map); map.put("userInfo", ui); // 获取部门列表 List<DepartmentInfo> diList = this.departmentInfoService.query(Cnd.orderBy().asc("departmentName"), null); map.put("departmentInfoList", diList); return success(map); } catch (Exception e) { e.printStackTrace(); return failure(GET_FAILURE, map); } } /** * 修改成功 * * @param ui * @return */ @At("/editUserInfoOk") @Ok("jsp:/userinfo/showMessage") public Map<String, Object> editUserInfoOk(@Param("..") UserInfo ui) { Map<String, Object> map = new HashMap<String, Object>(); try { if (ui == null) return failure(GET_FAILURE, map); // 获取原有对象 UserInfo uiOld = this.userInfoService.fetch(ui.getUserInfoId()); if (uiOld == null) return failure(GET_FAILURE, map); // 设置原对象信息 ui.setAddDate(uiOld.getAddDate()); ui.setAddIp(uiOld.getAddIp()); // 修改对象 if (this.userInfoService.dao().update(ui) == 0) return failure(EDIT_FAILURE, map); return success(EDIT_SUCCESS, map); } catch (Exception e) { e.printStackTrace(); return failure(GET_FAILURE, map); } } /** * 删除成功 * * @param ui * @param request * @return */ @At("/delUserInfoOk") @Ok("jsp:/userinfo/showMessage") public Map<String, Object> delUserInfoOk(@Param("..") UserInfo ui, HttpServletRequest request) { Map<String, Object> map = new HashMap<String, Object>(); try { if (ui == null) return failure(GET_FAILURE, map); // 删除对象 if (this.userInfoService.dao().delete(ui) == 0) return failure(DEL_FAILURE, map); return success(DEL_SUCCESS, map); } catch (Exception e) { e.printStackTrace(); return failure(GET_FAILURE, map); } } /** * 显示列表 * * @return */ @At("/showUserInfoList") @Ok("jsp:/userinfo/showUserInfoList") public Map<String, Object> showUserInfoList() { Map<String, Object> map = new HashMap<String, Object>(); try { // 获取用户列表,可以使用视图中的任意字段进行排序 List<UserInfo> uiList = this.userInfoService.query(Cnd.orderBy().asc("departmentName"), null); map.put("userInfoList", uiList); return success(map); } catch (Exception e) { e.printStackTrace(); return failure(GET_FAILURE, map); } } }
四、JSP文件
showUserInfoList.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %> <%@ include file="../include/taglibs.jsp" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>员工信息列表</title> <link href="../css/web.css" rel="stylesheet" type="text/css" /> </head> <body> <br /> <table width="500" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td height="35"><label> <input type="button" name="add" id="add" value="添加员工信息" onclick="window.location='addUserInfo.shtml'" /> <input type="button" name="add" id="add" value="部门信息管理" onclick="window.location='../departmentinfo/showDepartmentInfoList.shtml'" /> </label></td> </tr> </table> <table width="500" border="0" align="center" cellpadding="1" cellspacing="1" class="TableOut"> <tr> <td height="30" colspan="4" align="center" class="TableTop">员工信息列表</td> </tr> <tr class="TableInWhite"> <td width="68" height="30" align="center">序号</td> <td width="167" align="center">部门</td> <td width="130" align="center">姓名</td> <td width="122" align="center">操作</td> </tr> <c:forEach var = "ui" items = "${obj.userInfoList}" varStatus="status"> <tr class="TableInWhite"> <td height="30" align="center"><c:out value="${status.count}" /></td> <td align="center"><c:out value="${ui.departmentName}" /></td> <td align="center"><c:out value="${ui.trueName}" /></td> <td align="center">[<a href="editUserInfo.shtml?userInfoId=${ui.userInfoId}">修改</a>] [<a href="delUserInfoOk.shtml?userInfoId=${ui.userInfoId}" onclick="return confirm('是否真的要删除此员工?')">删除</a>]</td> </tr> </c:forEach> </table> <table width="500" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td height="45">此列表主要用视图来替代对象关联,其中部门名称直接使用{ui.departmentName}获取即可。</td> </tr> </table> </body> </html>
其中获取部门名称的代码是${ui.departmentName}
五、重启Tomcat,访问以下地址:
http://127.0.0.1:204/UserManageSystemView/userinfo/showUserInfoList.shtml
效果图如下:
showUserInfoList.jsp
OK,第三篇教程搞定了。希望对大家有用。
大家在使用Nutz对象关联时,若遇到上述的排序问题,有没有比较好的解决方法呢?若有,希望告诉一声!thx~~
因为不想在项目中一边使用对象关联,一边又使用视图!在未解决此问题前,本人还是继续在项目中只使用视图了。