package com.example.springdemo.controller;
import com.example.springdemo.dto.UserInfo;
import com.example.springdemo.entity.User;
import com.example.springdemo.service.UserService;
import com.sun.xml.internal.bind.v2.model.core.ID;
import org.apache.commons.validator.Msg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.data.repository.query.Param;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Iterator;
import java.util.List;
@RestController
@RequestMapping("/api")
public class UserController {
@Autowired
UserService userService;
private final Logger logger = LoggerFactory.getLogger(getClass());
//查找
@GetMapping(value = "/users")
@ResponseBody
public List<User> getAllUser() {
return userService.findAll();
}
//增加
@PostMapping(value = "/Adduser")
@ResponseBody
public void add(@RequestParam("id") Integer id,@RequestParam("name") String name,@RequestParam("age") Integer age){
User user=new User();
user.setId(id);
user.setName(name);
user.setAge(age);
userService.sava(user);
}
//删除
@DeleteMapping(value = "/delete/{id}")
@ResponseBody
public void delete(@PathVariable String id){
User user = new User();
user.setId(Integer.parseInt(id));
userService.delete(user);//这里有问题**
}
@GetMapping("/getone/{id}")
@ResponseBody
public User getone(@PathVariable Integer id){
//User user=new User();
// user.setId(Integer.parseInt(id));
// user.setAge("age");
// user.setName(name);
return userService.getOne(id);//为什么不出现findOne
}
@PutMapping("/update/{id}")
@ResponseBody
public void update(@PathVariable("id") Integer id,
@RequestParam("name") String name,
@RequestParam("age") Integer age){
User user=new User();
user.setId(id);
user.setAge(age);
user.setName(name);
userService.sava(user);//存在问题
}
@PutMapping("/update1")
public void update1(@RequestBody User user){
// UserInfo userInfo1=new UserInfo();
// userInfo1.setId(id);
// userInfo1.setUsername(username);
// userInfo1.setUserage(userage);
userService.sava(user);//存在问题
}
//模糊查询
@GetMapping("/findByNameLike/{name}")
public List<User> findByNameLike(@PathVariable(value = "name") String name){
// 一定要加 "%"+参数名+"%"
return userService.findByNameLike(name);
}
//多条件查询
@GetMapping("/findByNameAndAge/{name}")
public List<User> findByNameAndAge(@PathVariable(value = "name") String name, @RequestParam("age") Integer age){
return userService.findByNameAndAge(name, age);
}
//分页查询
@GetMapping("/pageable")
public Page<User> getEntryByPageable(@PageableDefault(value = 5, sort = { "id" }, direction = Sort.Direction.DESC)
Pageable pageable) {
return userService.findAll(pageable);
}
@RequestMapping("/")
public ModelAndView index(){
ModelAndView mv = new ModelAndView("login");
return mv;
}
@PostMapping("/login")
public ModelAndView Login(@RequestParam("username") String username, @RequestParam("userpassword") String userpassword){
System.out.println(username+":"+userpassword);
List<User> users= userService.findByNameAndAge(username,Integer.parseInt(userpassword));
// for (Login l:loginService.findAll()) {
// System.out.println(l.getUsername()+":"+l.getUserpassword());
// }
ModelAndView success = new ModelAndView();
if(users.size()>0)
success.setViewName("success");
else success.setViewName("404");
return success;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论










收起资源包目录





































































































共 207 条
- 1
- 2
- 3
资源评论


爱吃西瓜的小松鼠
- 粉丝: 121
- 资源: 6
上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


会员权益专享
安全验证
文档复制为VIP权益,开通VIP直接复制
