首页>代码>SSM+JQ+Ajax实现学生信息管理系统>/ssm-stu/stu_ssm/src/com/hp/school/controller/UserController.java
package com.hp.school.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hp.school.entity.User;
import com.hp.school.page.Page;
import com.hp.school.service.UserService;

@Controller
@RequestMapping("/user")
/**
 * 完成用户的增删改查 ,分页
 * @author yuan
 *
 */
public class UserController {
	@Autowired
	private UserService userService;
	
	/**
	 * 跳转到用户 列表jsp页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(ModelAndView model){
		model.setViewName("user/user_list");
		return model;
	}
	
	/**
	 * 添加用户
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,String> add(User user){
		
		Map<String,String> map = new HashMap<String, String>();
		if(StringUtils.isEmpty(user.getUsername())){
			map.put("type", "error");
			map.put("msg", "用户名不能为空!");
			return map;
		}
		if(StringUtils.isEmpty(user.getPassword())){
			map.put("type", "error");
			map.put("msg", "密码不能为空!");
			return map;
		}
		//TODO 访问业务层
		User existsUser = userService.findUserByUserName(user.getUsername());
		if(existsUser!=null){
			map.put("type", "error");
			map.put("msg", "该用户已存在!");
			return map;
		}
		// 更新 /删除 /添加操作 最终 返回的是影响的行数 
		int result = userService.add(user);
		
		if(result<=0){
			map.put("type", "error");
			map.put("msg", "用户保存失败!");
			return map;
		}
		map.put("type", "success");
		map.put("msg", "添加用户成功!");
		return map;
	}
	
	/**
	 * 获取用户列表数据 --  包含 条件查询 分页
	 * @return
	 */
	@RequestMapping(value="/get_list",method=RequestMethod.POST)
	@ResponseBody
	/**
	 * @param username	模糊查询条件
	 * @param page		分页类
	 * @return
	 */
	public Map<String,Object> getList(
			@RequestParam(name="username",required=false,defaultValue="") String username,
			Page page
			){
		Map<String,Object> map = new HashMap<>();	// 最终数据在这里
		// 这个map 等同于  QueryBean 
		Map<String,Object> queryMap = new HashMap<>();	// 是一个查询条件类
		//拼装 limit ?,? 
		queryMap.put("offset", page.getOffset());
		queryMap.put("pageSize", page.getRows());	
		queryMap.put("username", "%"+username+"%");
		
		map.put("rows", userService.getList(queryMap));		//比如查询的第2页显示的一个集合数据
		map.put("total", userService.getTotal(queryMap));	//接收总数量
		
		return map;
	}
	
	
	/**
	 * 编辑用户
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,String> editUser(User user){
		
		Map<String,String> map = new HashMap<String, String>();
		if(StringUtils.isEmpty(user.getUsername())){
			map.put("type", "error");
			map.put("msg", "用户名不能为空!");
			return map;
		}
		if(StringUtils.isEmpty(user.getPassword())){
			map.put("type", "error");
			map.put("msg", "密码不能为空!");
			return map;
		}
		//TODO 访问业务层
		User existsUser = userService.findUserByUserName(user.getUsername());
		if(existsUser!=null){
			if(user.getId()!=existsUser.getId()){
				map.put("type", "error");
				map.put("msg", "该用户已存在!");
				return map;
			}
		}
		// 更新 /删除 /添加操作 最终 返回的是影响的行数 
		int result = userService.edit(user);
		
		if(result<=0){
			map.put("type", "error");
			map.put("msg", "用户编辑失败!");
			return map;
		}
		map.put("type", "success");
		map.put("msg", "编辑用户成功!");
		return map;
	}
	
	
	
	/**
	 * 删除用户
	 * @param user
	 * @return
	 * 
	 *  delete from user where id in (23,24,17)
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,String> delete(
			@RequestParam(name="ids[]",required=true)Integer[] ids){
		Map<String,String> map = new HashMap<>();
		// ids 非空判断 可以不写
		//需将 数组id转成 23,24,17
		String idsParam="";
		for (Integer id : ids) {
			idsParam += id+",";		// 23,24,17,
		}
		idsParam = idsParam.substring(0, idsParam.length()-1);
		//  通过业务层 调用删除方法 , 根据返回值判断 
		int result = userService.delete(idsParam);
		if(result<=0){
			map.put("type", "error");
			map.put("msg", "用户删除失败!");
			return map;
		}
		map.put("type", "success");
		map.put("msg", "删除用户成功!");
		return map;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 修改用户
	 * @param user
	 * @return
	 *//*
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,String> edit(User user){
		
		Map<String,String> map = new HashMap<String, String>();
		if(StringUtils.isEmpty(user.getUsername())){
			map.put("type", "error");
			map.put("msg", "用户名不能为空!");
			return map;
		}
		if(StringUtils.isEmpty(user.getPassword())){
			map.put("type", "error");
			map.put("msg", "密码不能为空!");
			return map;
		}
		//TODO 访问业务层
		User existsUser = userService.findUserByUserName(user.getUsername());
		if(existsUser!=null){
			if(user.getId()!=existsUser.getId()){	//
				map.put("type", "error");
				map.put("msg", "该用户已存在!");
				return map;
			}
		}
		// 更新 /删除 /添加操作 最终 返回的是影响的行数 
		int result = userService.edit(user);
		
		if(result<=0){
			map.put("type", "error");
			map.put("msg", "用户修改失败!");
			return map;
		}
		
		map.put("type", "success");
		map.put("msg", "修改用户成功!");
		return map;
	}
	
	*//**
	 * 删除用户
	 * @param user
	 * @return
	 *//*
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,String> delete(@RequestParam(name="ids[]",required=true)Long[] ids){
		
		Map<String,String> map = new HashMap<String, String>();
		if(ids==null){
			map.put("type", "error");
			map.put("msg", "请选择要删除的数据!");
			return map;
		}
		
		String idsParam = "";
		for (Long id : ids) {
			idsParam += id+",";	//(2,7,8,9,)
		}
		System.out.println("idsParam==="+idsParam);
		//去掉最后一个逗号
		idsParam = idsParam.substring(0, idsParam.length()-1);
		
		int result = userService.delete(idsParam);
		if(result<=0){
			map.put("type", "error");
			map.put("msg", "删除失败!");
			return map;
		}
		
		map.put("type", "success");
		map.put("msg", "删除成功!");
		return map;
	}*/
}













最近下载更多
潘潘123456  LV2 2023年12月30日
uni-code_0123  LV1 2023年8月4日
douhongwen  LV1 2023年7月21日
ice_candy  LV1 2023年6月19日
493240689  LV3 2023年6月3日
微信网友_6469820124057600  LV6 2023年5月30日
liuchang183  LV5 2023年4月22日
1379585889  LV11 2022年12月17日
and123456  LV11 2022年7月11日
chen_jw  LV10 2022年6月20日
最近浏览更多
呵呵喝  LV4 3月11日
3320151533  LV1 1月7日
暂无贡献等级
WBelong  LV7 2023年12月25日
潘潘123456  LV2 2023年12月24日
颜菜菜  LV2 2023年12月23日
Magic丶M  LV6 2023年12月12日
小芳同学  LV1 2023年12月8日
52013145 2023年11月30日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友