精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (1)
|
|
---|---|
作者 | 正文 |
发表时间:2011-08-11
最后修改:2011-08-14
承接上篇:Spring3MVC+MyBatis+ExtJs3整合开发系列之二:菜单模块,这次带来了Spring3MVC+MyBatis+ExtJs3整合开发系列之第三篇:人员管理模块。 这次改动比较大,下面是模块结构图,总算把之前搭建的package都填满了: 界面预览: 人员管理模块主界面: 新增/修改人员信息界面: 修改密码界面: 代码预览: persistence层 public interface UserMapper { Long getId(); User login(Map<String,Object> param); User getUser(Map<String,Object> param); List<User> getUserList(); void insertUser(User user); void insertUserRole(Map<String,Object> param); void updateUser(User user); void updateUserRole(Map<String,Object> param); void deleteUser(Map<String,Object> param); void deleteUserRole(Map<String,Object> param); void changeUserPassword(Map<String,Object> param); } <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.kimho.user.persistence.UserMapper"> <cache /> <select id="getId" resultType="long"> SELECT max(id)+1 FROM t_user </select> <select id="login" parameterType="map" resultType="User"> SELECT id, name, password, create_date FROM t_user WHERE name = #{name} AND password = #{password} </select> <select id="getUser" parameterType="map" resultType="User"> SELECT id, name, password, create_date FROM t_user <where> <if test="id != null"> id = #{id} </if> <if test="name != null"> AND name = #{name} </if> <if test="password != null"> AND password = #{password} </if> </where> </select> <select id="getUserList" resultMap="userResult"> SELECT u.id as user_id, u.name as user_name, u.password, u.create_date, r.id as role_id, r.name as role_name, r.description FROM t_user u LEFT JOIN t_user_role ur ON(u.id=ur.user_id) LEFT JOIN t_role r ON(r.id=ur.role_id) </select> <resultMap id="userResult" type="User"> <id property="id" column="user_id" /> <result property="name" column="user_name"/> <result property="createDate" column="create_date"/> <collection property="roles" ofType="Role"> <id property="id" column="role_id"/> <result property="name" column="role_name"/> </collection> </resultMap> <insert id="insertUser" parameterType="User" > INSERT INTO t_user (id,name, password, create_date) VALUES (#{id},#{name}, #{password}, #{createDate,jdbcType=TIMESTAMP}) </insert> <insert id="insertUserRole" parameterType="map"> INSERT INTO t_user_role (user_id,role_id) VALUES (#{user_id}, #{role_id}) </insert> <update id="updateUser" parameterType="User"> UPDATE t_user <set> <if test="name != null">name=#{name},</if> <if test="password != null">password=#{password}</if> </set> WHERE id=#{id} </update> <update id="updateUserRole" parameterType="map"> UPDATE t_user_role SET role_id=#{role_id} WHERE user_id=#{user_id} </update> <delete id="deleteUser" parameterType="map"> DELETE FROM t_user WHERE id=#{id} </delete> <delete id="deleteUserRole" parameterType="map"> DELETE FROM t_user_role WHERE user_id=#{user_id} </delete> <update id="changeUserPassword" parameterType="map"> UPDATE t_user SET password=#{newPassword} WHERE id=#{id} </update> </mapper> 这里特别说明下xml配置中的"<cache />"缓存配置项,如果开启了缓存(这里就开启了),需要注意数据的刷新问题;我之前的菜单模块,就开启了缓存机制,导致角色变更后,菜单权限没相应变化,这就是缓存导致的。 service层 @Service public class UserService { @Autowired private UserMapper userMapper; /** * 获取主键id * @return */ public Long getId() { return userMapper.getId(); } /** * 登录验证 * @param param * @return */ public User login(Map<String,Object> param) { return userMapper.login(param); } /** * 获取某个用户 * @param param * @return */ public User getUser(Map<String,Object> param) { return userMapper.getUser(param); } /** * 获取用户列表 * @return */ public List<User> getUserList() { return userMapper.getUserList(); } /** * 新增用户 * @param user * @param param */ @Transactional public void insertUser(User user,Map<String,Object> param) { //新增用户 userMapper.insertUser(user); String roleList = (String)param.get("roleList"); if(!"".equals(roleList)) { //新增用户角色关联 insertUserRole(param); } } /** * 编辑用户 * @param user * @param param */ @Transactional public void updateUser(User user,Map<String,Object> param) { //编辑用户 userMapper.updateUser(user); //删除原有的用户角色关联 userMapper.deleteUserRole(param); //新增用户角色关联 insertUserRole(param); } /** * 删除用户 * @param param */ @Transactional public void deleteUser(Map<String,Object> param) { String ids = (String)param.get("ids"); String[] idsArray = ids.split(","); for(int i=0;i<idsArray.length;i++) { param.put("id", idsArray[i]); param.put("user_id", idsArray[i]); userMapper.deleteUser(param); userMapper.deleteUserRole(param); } } /** * 用户角色关联 * @param param */ private void insertUserRole(Map<String,Object> param) { String roleList = (String)param.get("roleList"); String[] roleArray = roleList.split(","); for(int i=0;i<roleArray.length;i++) { param.put("role_id", roleArray[i]); userMapper.insertUserRole(param); } } /** * 修改密码 * @param param */ public void changeUserPassword(Map<String,Object> param) { userMapper.changeUserPassword(param); } } controller层 @Controller @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @Autowired private MenuService menuService; /** * 登录验证 * @param request * @param response * @param user * @return */ @RequestMapping(value="/login",method=RequestMethod.POST) public @ResponseBody Map<String,String> loginCheck(HttpServletRequest request, HttpServletResponse response){ //spring会利用jackson自动将返回值封装成JSON对象 Map<String,String> responseMap = new HashMap<String,String>(); String captcha = request.getParameter("captcha"); String name = request.getParameter("name"); String password = request.getParameter("password"); String vcode = (String)request.getSession().getAttribute("vcode"); if(vcode==null || !captcha.equals(vcode)) { responseMap.put("success", "false"); responseMap.put("info", "验证码错误!"); return responseMap; } try { Map<String,Object> param = new HashMap<String,Object>(); param.put("name", name); param.put("password", password); User user = userService.login(param); if(user!=null) { responseMap.put("success", "true"); responseMap.put("info", "登录成功!"); request.getSession().setAttribute("user", user); request.getSession().setAttribute("loginUserName", user.getName()); return responseMap; }else { responseMap.put("success", "false"); responseMap.put("info", "用户名或密码错误!"); return responseMap; } }catch(Exception e) { e.printStackTrace(); responseMap.put("info", e.getClass()+":"+e.getMessage()); return responseMap; } } /** * 获取所有菜单 * @param request * @param response * @return */ @RequestMapping(value="/menus",method=RequestMethod.POST) public @ResponseBody Map<String,Object> getMenus(HttpServletRequest request, HttpServletResponse response){ Map<String,Object> responseMap = new HashMap<String,Object>(); User user = (User)request.getSession().getAttribute("user"); List<Menu> list = menuService.getMenuListByUserId(user.getId()); responseMap.put("success", "true"); responseMap.put("data", list); return responseMap; } /** * 获取用户列表 * @param request * @param response * @return */ @RequestMapping(value="/users",method=RequestMethod.POST) public @ResponseBody Map<String,Object> getUsers(HttpServletRequest request, HttpServletResponse response){ Map<String,Object> responseMap = new HashMap<String,Object>(); List<User> userList = userService.getUserList(); responseMap.put("totalCount", userList.size()); responseMap.put("rows", userList); return responseMap; } /** * 保存用户信息(新增用户or编辑用户) * @param request * @param response * @param user * @return */ @RequestMapping(value="/save",method=RequestMethod.POST) public @ResponseBody Map<String,Object> saveOrUpdate(HttpServletRequest request, HttpServletResponse response){ Map<String,Object> responseMap = new HashMap<String,Object>(); String id = request.getParameter("id"); String name = request.getParameter("name"); String password = request.getParameter("password"); String roleList = request.getParameter("roleList"); try { //编辑用户信息 if(!"".equals(id)) { Map<String,Object> param = new HashMap<String,Object>(); param.put("id", id); User user = userService.getUser(param); user.setName(name); //表示修改了密码 if(!"".equals(password)) { user.setPassword(password); } param.put("user_id", id); param.put("roleList", roleList); userService.updateUser(user, param); responseMap.put("success", "true"); responseMap.put("info", "编辑成功!"); } //新增用户信息 else { User user = new User(); Long newId = userService.getId(); user.setId(newId); user.setCreateDate(new Date()); user.setName(name); user.setPassword(password); Map<String,Object> param = new HashMap<String,Object>(); param.put("user_id", newId); param.put("roleList", roleList); userService.insertUser(user, param); responseMap.put("method", "Create"); responseMap.put("success", "true"); responseMap.put("info", "新增成功!"); } return responseMap; }catch(Exception e) { e.printStackTrace(); responseMap.put("info", e.getClass()+":"+e.getMessage()); return responseMap; } } /** * 删除用户 * @param request * @param response * @return */ @RequestMapping(value="/remove",method=RequestMethod.POST) public @ResponseBody Map<String,Object> remove(HttpServletRequest request, HttpServletResponse response){ Map<String,Object> responseMap = new HashMap<String,Object>(); String ids = request.getParameter("ids"); Map<String,Object> param = new HashMap<String,Object>(); param.put("ids", ids); try { userService.deleteUser(param); responseMap.put("success", "true"); responseMap.put("info", "删除成功!"); return responseMap; }catch(Exception e) { e.printStackTrace(); responseMap.put("info", e.getClass()+":"+e.getMessage()); return responseMap; } } /** * 修改密码 * @param request * @param response * @return */ @RequestMapping(value="/changePassword",method=RequestMethod.POST) public @ResponseBody Map<String,Object> changePassword(HttpServletRequest request, HttpServletResponse response){ Map<String,Object> responseMap = new HashMap<String,Object>(); String oldPassword = request.getParameter("oldPassword"); String newPassword = request.getParameter("newPassword"); User user =(User)request.getSession().getAttribute("user"); try { //验证通过,允许修改密码 if(oldPassword.equals(user.getPassword())) { Map<String,Object> param = new HashMap<String,Object>(); param.put("id", user.getId()); param.put("newPassword", newPassword); userService.changeUserPassword(param); user.setPassword(newPassword); //更新session中的user密码信息 request.getSession().setAttribute("user", user); responseMap.put("success", "true"); responseMap.put("info", "修改密码成功!"); }else { responseMap.put("success", "false"); responseMap.put("info", "原密码错误!"); } return responseMap; }catch(Exception e) { e.printStackTrace(); responseMap.put("info", e.getClass()+":"+e.getMessage()); return responseMap; } } } 好了,一个完整的人员管理模块就完成了,测试下模块功能吧。 …… 下篇:Spring3MVC+MyBatis+ExtJs3整合开发系列之四:角色管理模块 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2011-08-11
mark一下 晚上研究研究
|
|
返回顶楼 | |
发表时间:2011-08-11
持续关注... |
|
返回顶楼 | |
发表时间:2011-08-11
最近看到extjs的风格都觉得腻歪。。可能用这个东西的人太多了。。不过支持lz,加油
|
|
返回顶楼 | |
发表时间:2011-08-11
不错不错,也想找时间做自己的东西!
|
|
返回顶楼 | |
发表时间:2011-08-11
dfwang 写道 最近看到extjs的风格都觉得腻歪。。可能用这个东西的人太多了。。不过支持lz,加油
extjs给了我们开发人员一个偷懒的借口,不需要专业的美工,也能做出用户体验一致的系统。 extjs的优势在于组件化,提供了一大堆强大的组件。 |
|
返回顶楼 | |
发表时间:2011-08-11
我现在做的项目用到了SPRIGMVC和MYbatis,但是没用EXTJS,我对那个新版本的MVC很是无语,换了个交叉编译的GWT,感觉还不错~
|
|
返回顶楼 | |
发表时间:2011-08-11
asbdzxln118 写道 我现在做的项目用到了SPRIGMVC和MYbatis,但是没用EXTJS,我对那个新版本的MVC很是无语,换了个交叉编译的GWT,感觉还不错~
从代码职能划分角度看,extjs搞个mvc模式也未尝不可 |
|
返回顶楼 | |
发表时间:2011-08-11
想问问楼主有没试过整合后用到ORACLE的大字段有没出现过问题,我在整合后用到大字段,但是却报:Spring transaction synchronization needs to be active for setting values in iBATIS TypeHandlers that delegate to a Spring LobHandler
请问知道吗 |
|
返回顶楼 | |
发表时间:2011-08-11
楼主,我下载运行了,怎么登录不了?输入用户名和密码、验证码,点击确定后还是在登录界面,是哪里有问题?
|
|
返回顶楼 | |