package com.example.controller;
import com.example.entity.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
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 java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**http://localhost:8009/MavenDemo1/MavenDemo1Controller/hello
* @author HYZX
* 控制层
* @ResponseBody是作用在方法上的,@ResponseBody 表示该方法的返回结果直接写入 HTTP response body 中,
* 一般在异步获取数据时使用【也就是AJAX】,在使用 @RequestMapping后,返回值通常解析为跳转路径,
* 但是加上 @ResponseBody 后返回结果不会被解析为跳转路径,而是直接写入 HTTP response body 中。
* 比如异步获取 json 数据,加上 @ResponseBody 后,会直接返回 json 数据。@RequestBody 将 HTTP 请求正文插入方法中,
* 使用适合的 HttpMessageConverter 将请求体写入某个对象。
*/
@Controller
@RequestMapping("/MavenDemo1Controller")
public class UserController {
@Autowired
private UserService userService ;
@RequestMapping(value = "/index")
@ResponseBody
public String index(){
return "index";
}
@RequestMapping(value ="/hello")
@ResponseBody
public String hello(){
return "hello";
}
/**
根据用户名查找
@param username
http://localhost:8009/MavenDemo1/MavenDemo1Controller/showByName?name=wl
*/
@RequestMapping(value = "/showByName",method = RequestMethod.GET)
@ResponseBody
public String showByName(@RequestParam(value = "name") String username){
User user = null ;
user = userService.findUserByName(username) ;
return user.toString() ;
}
/**
根据id查找
@param userid
http://localhost:8009/MavenDemo1/MavenDemo1Controller/showByUserId?userid=14
*/
@RequestMapping(value = "/showByUserId",method = RequestMethod.GET)
@ResponseBody
public String showByUserId(@RequestParam("userid") Long userid){
User user = null ;
user = userService.findByUserId(userid) ;
return user.toString() ;
}
/**
* 根据用户名删除
* @param username
* http://localhost:8009/MavenDemo1/MavenDemo1Controller/deleteByUsername?username=wl88
*/
@RequestMapping(value = "/deleteByUsername",method = RequestMethod.GET)
@ResponseBody
public String deleteByUsername(@RequestParam("username") String username){
User user = null;
String msg = "";
String remsg = "";
try {
msg = userService.deleteUserByName(username);
if(msg.equals("0")){
remsg = "你要删除的用户名不存在!";
}else {
remsg = "删除成功!";
}
} catch (Exception e) {
e.printStackTrace();
}
return remsg;
}
/**
* 查询所有
* @return List<User>
* http://localhost:8009/MavenDemo1/MavenDemo1Controller/showAll
*/
@RequestMapping(value = "/showAll",method = RequestMethod.GET)
@ResponseBody
public String showAll(){
List<User> list = null ;
try {
list = userService.findAllUser() ;
return list.toString() ;
} catch (Exception e) {
return e.getMessage() ;
}
}
/**
* 添加
* @param user
* @return user
* 正常:http://localhost:8009/MavenDemo1/MavenDemo1Controller/save?userid=107&username=wl7&age=97&password=wl7
* 正常:localhost:8009/MavenDemo1/MavenDemo1Controller/save?username=wl8&age=98&password=wl8
*/
@RequestMapping(value = "/save",method = RequestMethod.GET)
@ResponseBody
public String save(User user) {
User user1 = null;
User user2 = null;
user1 = userService.findUserByName(user.getUsername());
if (user == null||user.getUsername().equals("")) {
return "controller层:用户名为空,添加失败!";
} else if(user1!=null){
return "controller层:用户名已占用,请重新输入!";
}else{
user2 = userService.save(user);
return "用户 【"+user2.toString()+"】添加成功!";
}
}
/**
* 修改
* @param
* /localhost:8009/MavenDemo1/MavenDemo1Controller/updateUser?userid=15&username=wanglan8&age=999&password=wanglan8
* localhost:8009/MavenDemo1/MavenDemo1Controller/updateUser?userid=12&username=wanglan9&age=999&password=wanglan9
*/
@RequestMapping(value = "/updateUser",method = RequestMethod.GET)
@ResponseBody
public String updateUser(String username,int age,String password,Long userid){
try {
//先找到待修改的user
User user1 = userService.findByUserId(userid) ;
//进行修改
User user2 = userService.update(username,age,password,userid);
user2 = new User(userid,username,age,password);
if(user2==null){
return "controller层:该用户不存在,无法进行修改操作!";
}else {
// user2 = userService.findByUserId(user2.getUserid());
return "▷修改前:" + " 【" + user1.toString() + "" + " 】;\n" +
" ▷修改后:【" + user2.toString() + "】 修改成功!";
}
} catch (Exception e) {
return e.getMessage() ;
}
}
/**
* localhost:8009/MavenDemo1/MavenDemo1Controller/findAllToPage
* 以3条记录为一页,查找第三页的三条数据,按ID降序排列。page=0是第一页
* localhost:8009/MavenDemo1/MavenDemo1Controller/findAllToPage?page=1&size=11
*/
@RequestMapping(value = "/findAllToPage",method = RequestMethod.GET)
@ResponseBody
public String findAllToPage(@RequestParam(value = "page",defaultValue = "0")Integer page,
@RequestParam(value = "size",defaultValue = "15")Integer size){
Sort sort = new Sort(Sort.Direction.DESC,"userid");
Pageable pageable = new PageRequest(page,size,sort);
Page<User> pageAll = userService.findAllToPage(pageable);
Iterator<User> iterator = pageAll.iterator() ;
List<User> list = new ArrayList<User>();
while (iterator.hasNext()){
list.add((User) iterator.next()) ;
}
return list.toString()+pageable.toString();
}
}