package com.controller;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.core.DataSourceContextHolder;
import com.google.gson.Gson;
import com.po.UserinfoPO;
import com.service.UserInfoInterface;
@Controller //标注这是一个控制类,类名不能和注解名一样
@RequestMapping("/uc") //设置访问路径
public class UserinfoController {
/**
* 验证登录
*/
@Autowired
//定义service类型的属性
UserInfoInterface uservice;
@RequestMapping("/login")//为方法设置访问路径
//@RequestParam(required=false)指定一下,map的参数是从request作用域里取的
//通过required=false或者true来要求@RequestParam配置的前端参数是否一定要传
// required=false表示不传的话,会给参数赋值为null,required=true就是必须要有
//例:public String filesUpload(@RequestParam(value="aa", required=true)
// public ModelAndView mav(@RequestParam(required=false) Map map){
// List<Map> ml=uservice.getlogin(map);
// ModelAndView mav=new ModelAndView();
// mav.addObject("ulist", ml);
// mav.setViewName("/main.jsp");
// return mav;
// }
//登录
public String ulogin(HttpServletRequest request){
//接收页面的值
String loginname = request.getParameter("loginname");
String loginpass = request.getParameter("loginpass");
UserinfoPO po = new UserinfoPO();
//把接收到的值放入po里
System.out.print(loginname);
po.setLoginname(loginname);
po.setLoginpass(loginpass);
//调service方法去数据库验证
UserinfoPO pojo = uservice.getlogin(po);
if(pojo!=null){
return "/uc/user";
}else{
return "/index.jsp";
}
}
/**
* 查询用户列表
*/
@RequestMapping("/user")//为方法设置访问路径
public String userList(HttpServletRequest request, UserinfoPO po){
//调service里的方法
List<UserinfoPO> ulist = uservice.getuserList(po);
//把值存到request作用域里,传到页面上
request.setAttribute("ulist", ulist);
//跳转的mian.jsp页面
return "/main.jsp";
}
@ResponseBody
@RequestMapping("/getuser")//为方法设置访问路径
public String getuserList2(HttpServletRequest request, UserinfoPO po){
//切换数据库,切换后不需要重新改回默认数据库,每次请求都会从配置文件里面设置成默认数据库
DataSourceContextHolder.setCustomerType("dataSourceTwo");
//调service里的方法
List<UserinfoPO> ulist = uservice.getuserList2(po);
//把值存到request作用域里,传到页面上
request.setAttribute("ulist", ulist);
//跳转的mian.jsp页面
return new Gson().toJson(ulist);
}
@ResponseBody
@RequestMapping("/getuser2")//为方法设置访问路径
public String getuserList3(HttpServletRequest request, UserinfoPO po){
//调service里的方法
List<UserinfoPO> ulist = uservice.getuserList2(po);
//把值存到request作用域里,传到页面上
request.setAttribute("ulist", ulist);
//跳转的mian.jsp页面
return new Gson().toJson(ulist);
}
/**
* 查询修改用户信息的id
*/
@RequestMapping("/uid")//为方法设置访问路径
public String updateid(HttpServletRequest request, UserinfoPO po){
List<UserinfoPO> uid = uservice.getupdateid(po);
request.setAttribute("uid", uid);
return "/update.jsp";
}
/**
* 修改用户信息
*/
@RequestMapping(value="/update")//为方法设置访问路径
public String update(HttpServletRequest request, UserinfoPO po){
String updateUser = uservice.getupdate(po);
request.setAttribute("updateUser", updateUser);
//修改信息后留在当前页
return "/uc/uid";
}
/**
* 添加用户信息
*/
@RequestMapping("/insert")//为方法设置访问路径
public String insert(HttpServletRequest request, UserinfoPO po){
String inserUser = uservice.getinsert(po);
request.setAttribute("inserUser", inserUser);
return "/insert.jsp";
}
/**
* 删除用户 ,根据id删除
*/
//后面传了一个要删除的id值,比如要删除id是30的用户,整体路径是/uc/delete/30
@RequestMapping(value="/delete/{userid}")
public ModelAndView delete(@PathVariable("userid")int userid){
String deleteUser=uservice.getdelete(userid);
ModelAndView mav=new ModelAndView();
mav.addObject("deleteUser", deleteUser);
//跳到提醒页,返回service里定义的方法,提醒删除成功还是失败
mav.setViewName("/tx.jsp");
return mav;
}
/**
* 根据用户名模糊查询,根据权限查询
*/
@RequestMapping("/select")//为方法设置访问路径
public ModelAndView mav(@RequestParam(required=false) Map map){
List<Map> selectUser = uservice.getselect(map);
ModelAndView mav=new ModelAndView();
mav.addObject("ulist", selectUser);
mav.setViewName("/main.jsp");
return mav;
}
}