package com.example.backendmanage.controller; import cn.hutool.core.io.IoUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.http.server.HttpServerResponse; import cn.hutool.poi.excel.ExcelReader; import cn.hutool.poi.excel.ExcelUtil; import cn.hutool.poi.excel.ExcelWriter; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.example.backendmanage.common.AjaxResult; import com.example.backendmanage.info.User; import com.example.backendmanage.mapper.UserMapper; import jakarta.servlet.ServletOutputStream; import jakarta.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.HashMap; import java.util.List; @RestController @RequestMapping("/user") public class UserController { @Autowired UserMapper userMapper; @RequestMapping("/login") public AjaxResult login(@RequestBody User user){ if (user == null|| StrUtil.isBlank(user.getUsername())||StrUtil.isBlank(user.getPassword())) { return AjaxResult.fail(-1,"参数为空"); } User findUser=userMapper.login(user.getUsername()); if (user.getPassword().equals(findUser.getPassword())){ return AjaxResult.success(1); }else { return AjaxResult.fail(-1,"密码错误"); } } @RequestMapping("/findAllUser") public AjaxResult findAllUser(){ return AjaxResult.success(userMapper.findAll()); } @RequestMapping("/addUser") public AjaxResult addUser(@RequestBody User user){ userMapper.addUser(user); return AjaxResult.success(1); } @RequestMapping("/page") public AjaxResult findPage(@RequestParam Integer pageIndex,@RequestParam Integer pageSize){ Integer pageNum=(pageIndex-1)*pageSize; List<User> findUser=userMapper.pageFind(pageNum,pageSize); int total=userMapper.findAll().size(); HashMap<String,Object> res=new HashMap<>(); res.put("total",total); res.put("findUser",findUser); return AjaxResult.success(res); } @RequestMapping("/search") public AjaxResult search(@RequestParam(defaultValue = "") String username, @RequestParam(defaultValue = "") String email, @RequestParam(defaultValue = "") String address){ System.out.println(username+" email->"+email+" address->"+address); List<User> users= userMapper.search(username,email,address); if (users.size()>0)System.out.println(users.get(0)); HashMap<String,Object> hashMap=new HashMap<>(); hashMap.put("findUser",users); hashMap.put("total",users.size()); return AjaxResult.success(hashMap); } @RequestMapping("/updateUser") public AjaxResult updateUser(@RequestBody User user){ return AjaxResult.success(userMapper.UpdateUser(user)); } @RequestMapping("/delUser") public AjaxResult delUser(@RequestParam Integer id){ System.out.println(id); return AjaxResult.success(userMapper.delUser(id)); } @RequestMapping("/DelUsers") public AjaxResult DelUsers(@RequestParam("ids") List<Integer> ids){ for (Integer id : ids) { userMapper.delUser(id); } return AjaxResult.success(1); } @RequestMapping("/export") public void export(HttpServletResponse response){ try { List<User> list=userMapper.findAll(); ExcelWriter excelWriter= ExcelUtil.getWriter(true); excelWriter.write(list,true); //response为HttpServletResponse对象 response.setContentType("application/vnd.ms-excel;charset=utf-8"); //test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码 String filename= null; filename = URLEncoder.encode("用户信息","UTF-8"); response.setHeader("Content-Disposition","attachment;filename="+filename+".xls"); ServletOutputStream out = response.getOutputStream(); excelWriter.flush(out, true); // 关闭writer,释放内存 excelWriter.close(); //此处记得关闭输出Servlet流 IoUtil.close(out); } catch (Exception e){ e.printStackTrace(); } } @RequestMapping("/import") public Boolean imports(MultipartFile file){ try { InputStream inputStream=file.getInputStream(); ExcelReader excelReader=ExcelUtil.getReader(inputStream); List<User> list=excelReader.readAll(User.class); for (User u : list) { userMapper.addUser(u); } System.out.println(list); return true; } catch (IOException e) { e.printStackTrace(); } return false; } }
标签:12,return,RequestMapping,userMapper,15,AjaxResult,2023,import,public From: https://www.cnblogs.com/wllovelmbforever/p/17903899.html