当前位置: 代码迷 >> JavaScript >> Spring3MVC+MyBatis+ExtJs3调整开发系列之三:人员管理模块
  详细解决方案

Spring3MVC+MyBatis+ExtJs3调整开发系列之三:人员管理模块

热度:895   发布时间:2012-08-08 14:32:45.0
Spring3MVC+MyBatis+ExtJs3整合开发系列之三:人员管理模块
    hi,boys and girls,i come back again!

    承接上篇: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整合开发系列之四:角色管理模块。

源码地址:http://code.google.com/p/kika/
1 楼 mjjs04 2011-08-11  
最近也和楼主不差不多在搭建一个SpringMVC+iBATIS+Shiro的基础框架,刚折腾完Shiro的权限管理这一块,发现shiro资料特别少。楼主有考虑要使用什么安全框架吗?
2 楼 KimHo 2011-08-11  
mjjs04 写道
最近也和楼主不差不多在搭建一个SpringMVC+iBATIS+Shiro的基础框架,刚折腾完Shiro的权限管理这一块,发现shiro资料特别少。楼主有考虑要使用什么安全框架吗?

Ralasafe可以考虑下,一款国内的开源权限框架
3 楼 macrotea 2011-09-23  
  <select id="getId" resultType="long"> 
    SELECT max(id)+1 FROM t_user 
  </select> 

不应该这么干,应该 select last_insert_id()
4 楼 KimHo 2011-09-23  
macrotea 写道
  <select id="getId" resultType="long"> 
    SELECT max(id)+1 FROM t_user 
  </select> 

不应该这么干,应该 select last_insert_id()

多谢你的建议
5 楼 zccmp 2012-01-13  
你好,现在正在学习springmvc这方面的知识,觉得你的这个例子特别的好,想学习一下,不知道该在哪里下载该实例源码?另外,想问一下:你说该实例源码及数据库文件都包在Spring3MvcMyBatisExtjs3.rar这个压缩包,但是,我不知道这个压缩包在哪里下载?你能否告诉我呢?或者把这发给我呢?我的邮箱为:zccmp@126.com
麻烦你了,非常感谢!谢谢!!!!
  相关解决方案