当前位置:   article > 正文

SpringBoot2+Vue2实战(六)登录,注册实现及自定义异常处理,个人信息页面与昵称头像功能实现_基于springboot+vue通过http请求个人信息

基于springboot+vue通过http请求个人信息

一、登录:

UserDto

  1. @Data
  2. public class UserDto {
  3. private String username;
  4. private String password;
  5. @Alias("nickname")
  6. private String nickname;
  7. private String avatarUrl;
  8. }

UserController

  1. //登录
  2. @PostMapping("/login")
  3. public Result login(@RequestBody UserDto userDto) {
  4. String username = userDto.getUsername();
  5. String password = userDto.getPassword();
  6. if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
  7. return Result.error(Constants.CODE_400, "系统错误");
  8. }
  9. UserDto dto = userService.login(userDto);
  10. return success(dto);
  11. }

UserService

  1. public interface UserService extends IService<User> {
  2. UserDto login(UserDto userDto);
  3. }

UserServiceImpl

  1. @Service
  2. public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
  3. @Resource
  4. private UserMapper userMapper;
  5. private static final Log LOG = Log.get();
  6. @Override
  7. public UserDto login(UserDto userDto) {
  8. User one = getUserInfo(userDto);
  9. if (one != null){
  10. BeanUtil.copyProperties(one,userDto,true);
  11. return userDto;
  12. }else {
  13. throw new ServiceException(Constants.CODE_600,"用户名或密码错误");
  14. }
  15. }
  16. private User getUserInfo(UserDto userDto) {
  17. QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  18. queryWrapper.eq("username",userDto.getUsername());
  19. queryWrapper.eq("password",userDto.getPassword());
  20. User one;
  21. try{
  22. one = getOne(queryWrapper);
  23. }catch (Exception e){
  24. LOG.error(e);
  25. throw new ServiceException(Constants.CODE_500,"系统错误");
  26. }
  27. return one;
  28. }
  29. }

Login.vue 

  1. <template>
  2. <div class="wrapper">
  3. <div style="margin: 200px auto; background-color: #fff; width: 350px; height: 300px; padding: 20px; border-radius: 10px">
  4. <div style="margin: 20px 0; text-align: center; font-size: 24px"><b>登 录</b></div>
  5. <el-form :model="user" :rules="rules" ref="userForm">
  6. <el-form-item prop="username">
  7. <el-input size="medium" style="margin: 10px 0" prefix-icon="el-icon-user" v-model="user.username"></el-input>
  8. </el-form-item>
  9. <el-form-item prop="password">
  10. <el-input size="medium" style="margin: 10px 0" prefix-icon="el-icon-lock" show-password v-model="user.password"></el-input>
  11. </el-form-item>
  12. <el-form-item style="margin: 10px 0; text-align: right">
  13. <el-button type="primary" size="small" autocomplete="off" @click="login">登录</el-button>
  14. <el-button type="warning" size="small" autocomplete="off">注册</el-button>
  15. </el-form-item>
  16. </el-form>
  17. </div>
  18. </div>
  19. </template>
  20. <script>
  21. export default {
  22. name: "Login",
  23. data() {
  24. return {
  25. user: {},
  26. rules: {
  27. username: [
  28. { required: true, message: '请输入用户名', trigger: 'blur' },
  29. { min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }
  30. ],
  31. password: [
  32. { required: true, message: '请输入密码', trigger: 'blur' },
  33. { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
  34. ],
  35. }
  36. }
  37. },
  38. methods: {
  39. login() {
  40. this.$refs['userForm'].validate((valid) => {
  41. if (valid) { // 表单校验合法
  42. this.request.post("/user/login", this.user).then(res => {
  43. if (res.code === '200'){
  44. //存储用户信息到浏览器
  45. localStorage.setItem("user",JSON.stringify(res.data))
  46. this.$router.push("/")
  47. this.$message.success("登录成功")
  48. }else {
  49. this.$message.error(res.msg)
  50. }
  51. })
  52. }
  53. });
  54. }
  55. }
  56. </script>
  57. <style>
  58. .wrapper {
  59. height: 100vh;
  60. background-image: linear-gradient(to bottom right, #FC466B , #3F5EFB);
  61. overflow: hidden;
  62. }
  63. </style>

router/index.js

  1. {
  2. path: '/login',
  3. name: 'Login',
  4. component: () => import('../views/Login.vue')
  5. }

二、退出

Header.vue

  1. <el-dropdown-item style="font-size: 14px; padding: 5px 0">
  2. <span style="text-decoration: none" @click="logout">退出</span>
  3. </el-dropdown-item>
  4. data() {
  5. return {
  6. user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
  7. }
  8. },
  9. methods: {
  10. logout() {
  11. this.$router.push("/login")
  12. localStorage.removeItem("user")
  13. this.$message.success("退出成功")
  14. }
  15. }

三、注册

router/index.js

  1. {
  2. path: '/register',
  3. name: 'Register',
  4. component: () => import('../views/Register.vue')
  5. },

Header.vue

  1. <template>
  2. <div class="wrapper">
  3. <div style="margin: 100px auto; background-color: #fff; width: 350px; height: 400px; padding: 20px; border-radius: 10px">
  4. <div style="margin: 20px 0; text-align: center; font-size: 24px"><b>注 册</b></div>
  5. <el-form :model="user" :rules="rules" ref="userForm">
  6. <el-form-item prop="username">
  7. <el-input placeholder="请输入账号" size="medium" style="margin: 5px 0" prefix-icon="el-icon-user" v-model="user.username"></el-input>
  8. </el-form-item>
  9. <el-form-item prop="password">
  10. <el-input placeholder="请输入密码" size="medium" style="margin: 5px 0" prefix-icon="el-icon-lock" show-password v-model="user.password"></el-input>
  11. </el-form-item>
  12. <el-form-item prop="confirmPassword">
  13. <el-input placeholder="请确认密码" size="medium" style="margin: 5px 0" prefix-icon="el-icon-lock" show-password v-model="user.confirmPassword"></el-input>
  14. </el-form-item>
  15. <el-form-item style="margin: 5px 0; text-align: right">
  16. <el-button type="primary" size="small" autocomplete="off" @click="register">注册</el-button>
  17. <el-button type="warning" size="small" autocomplete="off" @click="$router.push('/login')">返回登录</el-button>
  18. </el-form-item>
  19. </el-form>
  20. </div>
  21. </div>
  22. </template>
  23. <script>
  24. export default {
  25. name: "Register",
  26. data() {
  27. return {
  28. user: {},
  29. rules: {
  30. username: [
  31. { required: true, message: '请输入用户名', trigger: 'blur' },
  32. { min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }
  33. ],
  34. password: [
  35. { required: true, message: '请输入密码', trigger: 'blur' },
  36. { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
  37. ],
  38. confirmPassword: [
  39. { required: true, message: '请输入密码', trigger: 'blur' },
  40. { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
  41. ],
  42. }
  43. }
  44. },
  45. methods: {
  46. register() {
  47. this.$refs['userForm'].validate((valid) => {
  48. if (valid) { // 表单校验合法
  49. if (this.user.password !== this.user.confirmPassword) {
  50. this.$message.error("两次输入的密码不一致")
  51. return false
  52. }
  53. this.request.post("/user/register", this.user).then(res => {
  54. if(res.code === '200') {
  55. this.$message.success("注册成功")
  56. this.$router.push("/login")
  57. } else {
  58. this.$message.error(res.msg)
  59. }
  60. })
  61. }
  62. });
  63. }
  64. }
  65. }
  66. </script>
  67. <style>
  68. .wrapper {
  69. height: 100vh;
  70. background-image: linear-gradient(to bottom right, #FC466B , #3F5EFB);
  71. overflow: hidden;
  72. }
  73. </style>

Login.vue

  1. methods: {
  2. login() {
  3. this.$refs['userForm'].validate((valid) => {
  4. if (valid) { // 表单校验合法
  5. this.request.post("/user/login", this.user).then(res => {
  6. if (res.code === '200'){
  7. //存储用户信息到浏览器
  8. localStorage.setItem("user",JSON.stringify(res.data))
  9. this.$router.push("/")
  10. this.$message.success("登录成功")
  11. }else {
  12. this.$message.error(res.msg)
  13. }
  14. })
  15. }
  16. });
  17. }
  18. }

Register.vue

  1. <template>
  2. <div class="wrapper">
  3. <div style="margin: 100px auto; background-color: #fff; width: 350px; height: 400px; padding: 20px; border-radius: 10px">
  4. <div style="margin: 20px 0; text-align: center; font-size: 24px"><b>注 册</b></div>
  5. <el-form :model="user" :rules="rules" ref="userForm">
  6. <el-form-item prop="username">
  7. <el-input placeholder="请输入账号" size="medium" style="margin: 5px 0" prefix-icon="el-icon-user" v-model="user.username"></el-input>
  8. </el-form-item>
  9. <el-form-item prop="password">
  10. <el-input placeholder="请输入密码" size="medium" style="margin: 5px 0" prefix-icon="el-icon-lock" show-password v-model="user.password"></el-input>
  11. </el-form-item>
  12. <el-form-item prop="confirmPassword">
  13. <el-input placeholder="请确认密码" size="medium" style="margin: 5px 0" prefix-icon="el-icon-lock" show-password v-model="user.confirmPassword"></el-input>
  14. </el-form-item>
  15. <el-form-item style="margin: 5px 0; text-align: right">
  16. <el-button type="primary" size="small" autocomplete="off" @click="register">注册</el-button>
  17. <el-button type="warning" size="small" autocomplete="off" @click="$router.push('/login')">返回登录</el-button>
  18. </el-form-item>
  19. </el-form>
  20. </div>
  21. </div>
  22. </template>
  23. <script>
  24. export default {
  25. name: "Register",
  26. data() {
  27. return {
  28. user: {},
  29. rules: {
  30. username: [
  31. { required: true, message: '请输入用户名', trigger: 'blur' },
  32. { min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }
  33. ],
  34. password: [
  35. { required: true, message: '请输入密码', trigger: 'blur' },
  36. { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
  37. ],
  38. confirmPassword: [
  39. { required: true, message: '请输入密码', trigger: 'blur' },
  40. { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
  41. ],
  42. }
  43. }
  44. },
  45. methods: {
  46. register() {
  47. this.$refs['userForm'].validate((valid) => {
  48. if (valid) { // 表单校验合法
  49. if (this.user.password !== this.user.confirmPassword) {
  50. this.$message.error("两次输入的密码不一致")
  51. return false
  52. }
  53. this.request.post("/user/register", this.user).then(res => {
  54. if(res.code === '200') {
  55. this.$message.success("注册成功")
  56. this.$router.push("/login")
  57. } else {
  58. this.$message.error(res.msg)
  59. }
  60. })
  61. }
  62. });
  63. }
  64. }
  65. }
  66. </script>
  67. <style>
  68. .wrapper {
  69. height: 100vh;
  70. background-image: linear-gradient(to bottom right, #FC466B , #3F5EFB);
  71. overflow: hidden;
  72. }
  73. </style>

router/index.js

  1. {
  2. path: '/register',
  3. name: 'Register',
  4. component: () => import('../views/Register.vue')
  5. }

User.vue

  1. methods: {
  2. load() {
  3. this.request.get("/user/selectPage", {
  4. params: {
  5. pageNum: this.pageNum,
  6. pageSize: this.pageSize,
  7. username: this.username,
  8. email: this.email,
  9. address: this.address,
  10. }
  11. }).then(res => {
  12. //注意data
  13. this.tableData = res.data.records
  14. this.total = res.data.total
  15. })
  16. },
  17. save() {
  18. this.request.post("/user/saveUser", this.form).then(res => {
  19. if (res.data) {
  20. this.$message.success("保存成功")
  21. this.dialogFormVisible = false
  22. this.load()
  23. } else {
  24. this.$message.error("保存失败")
  25. }
  26. })
  27. },
  28. del(id) {
  29. this.request.delete("/user/" + id).then(res => {
  30. if (res.data) {
  31. this.$message.success("删除成功")
  32. this.load()
  33. } else {
  34. this.$message.error("删除失败")
  35. }
  36. })
  37. },
  38. delBatch() {
  39. let ids = this.multipleSelection.map(v => v.id) // [{}, {}, {}] => [1,2,3]
  40. this.request.post("/user/del/batch", ids).then(res => {
  41. if (res.data) {
  42. this.$message.success("批量删除成功")
  43. this.load()
  44. } else {
  45. this.$message.error("批量删除失败")
  46. }
  47. })
  48. },

UserDto

  1. import lombok.Data;
  2. @Data
  3. public class UserDto {
  4. private String username;
  5. private String password;
  6. private String nickname;
  7. private String avatarUrl;
  8. }

Constants.java(常量类,定义Code)

  1. public interface Constants {
  2. String CODE_200 = "200"; //成功
  3. String CODE_401 = "401"; // 权限不足
  4. String CODE_400 = "400"; // 参数错误
  5. String CODE_500 = "500"; // 系统错误
  6. String CODE_600 = "600"; // 其他业务异常
  7. }

Result

  1. import lombok.AllArgsConstructor;
  2. import lombok.Data;
  3. import lombok.NoArgsConstructor;
  4. /**
  5. * 接口统一返回包装类
  6. */
  7. @Data
  8. @NoArgsConstructor
  9. @AllArgsConstructor
  10. public class Result {
  11. private String code;
  12. private String msg;
  13. private Object data;
  14. public static Result success() {
  15. return new Result(Constants.CODE_200, "", null);
  16. }
  17. public static Result success(Object data) {
  18. return new Result(Constants.CODE_200, "", data);
  19. }
  20. public static Result error(String code, String msg) {
  21. return new Result(code, msg, null);
  22. }
  23. public static Result error() {
  24. return new Result(Constants.CODE_500, "系统错误", null);
  25. }
  26. }

ServiceException.java(自定义业务异常)

  1. import lombok.Getter;
  2. /**
  3. * 自定义异常
  4. */
  5. @Getter
  6. public class ServiceException extends RuntimeException {
  7. private String code;
  8. public ServiceException(String code, String msg) {
  9. super(msg);
  10. this.code = code;
  11. }
  12. }

GlobalExceptionHandler.java

  1. import com.example.springboot.common.Result;
  2. import org.springframework.web.bind.annotation.ControllerAdvice;
  3. import org.springframework.web.bind.annotation.ExceptionHandler;
  4. import org.springframework.web.bind.annotation.ResponseBody;
  5. @ControllerAdvice
  6. public class GlobalExceptionHandler {
  7. /**
  8. * 如果抛出的的是ServiceException,则调用该方法
  9. * @param se 业务异常
  10. * @return Result
  11. */
  12. @ExceptionHandler(ServiceException.class)
  13. @ResponseBody
  14. public Result handle(ServiceException se){
  15. return Result.error(se.getCode(), se.getMessage());
  16. }
  17. }

头像,昵称功能实现:

Header.vue

  1. <div style="display: inline-block">
  2. <img :src="user.avatarUrl" referrerpolicy="no-referrer" alt=""
  3. style="width: 30px; border-radius: 50%; position: relative; top: 10px; right: 5px">
  4. <span>{{ user.nickname }}</span><i class="el-icon-arrow-down" style="margin-left: 5px"></i>
  5. </div>

UserDto

  1. @Data
  2. public class UserDto {
  3. private String username;
  4. private String password;
  5. @Alias("nickname")
  6. private String nickname;
  7. private String avatarUrl;
  8. }

四、个人信息页面显示

Person.vue

  1. <template>
  2. <el-card style="width: 500px;">
  3. <el-form label-width="80px" size="small">
  4. <el-form-item label="用户名">
  5. <el-input v-model="form.username" autocomplete="off"></el-input>
  6. </el-form-item>
  7. <el-form-item label="昵称">
  8. <el-input v-model="form.nickname" autocomplete="off"></el-input>
  9. </el-form-item>
  10. <el-form-item label="邮箱">
  11. <el-input v-model="form.email" autocomplete="off"></el-input>
  12. </el-form-item>
  13. <el-form-item label="电话">
  14. <el-input v-model="form.phone" autocomplete="off"></el-input>
  15. </el-form-item>
  16. <el-form-item label="地址">
  17. <el-input v-model="form.address" autocomplete="off"></el-input>
  18. </el-form-item>
  19. <el-form-item>
  20. <el-button type="primary" @click="save">确 定</el-button>
  21. </el-form-item>
  22. </el-form>
  23. </el-card>
  24. </template>
  25. <script>
  26. export default {
  27. name: "Person",
  28. data() {
  29. return {
  30. form: {},
  31. user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
  32. }
  33. },
  34. created() {
  35. this.request.get("user/findUserName/" + this.user.username).then(res => {
  36. if (res.code === '200') {
  37. this.form = res.data
  38. }
  39. })
  40. },
  41. methods: {
  42. save() {
  43. this.request.post("/user/saveUser", this.form).then(res => {
  44. if (res.data) {
  45. this.$message.success("保存成功")
  46. } else {
  47. this.$message.error("保存失败")
  48. }
  49. })
  50. },
  51. }
  52. }
  53. </script>
  54. <style>
  55. </style>

UserController

  1. @GetMapping("/findUserName/{username}")
  2. public Result findByName(@PathVariable String username){
  3. QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  4. queryWrapper.eq("username",username);
  5. return Result.success(userService.getOne(queryWrapper));
  6. }

用result封装方法

UserController

  1. import cn.hutool.core.collection.CollUtil;
  2. import cn.hutool.core.util.StrUtil;
  3. import cn.hutool.poi.excel.ExcelReader;
  4. import cn.hutool.poi.excel.ExcelUtil;
  5. import cn.hutool.poi.excel.ExcelWriter;
  6. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  7. import com.baomidou.mybatisplus.core.metadata.IPage;
  8. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  9. import com.example.springboot.common.Constants;
  10. import com.example.springboot.common.Result;
  11. import com.example.springboot.entity.User;
  12. import com.example.springboot.entity.dto.UserDto;
  13. import com.example.springboot.service.UserService;
  14. import org.springframework.web.bind.annotation.*;
  15. import org.springframework.web.multipart.MultipartFile;
  16. import javax.annotation.Resource;
  17. import javax.servlet.ServletOutputStream;
  18. import javax.servlet.http.HttpServletResponse;
  19. import java.io.InputStream;
  20. import java.net.URLEncoder;
  21. import java.util.List;
  22. import static com.example.springboot.common.Result.success;
  23. @RestController
  24. @RequestMapping("/user")
  25. public class UserController {
  26. @Resource
  27. private UserService userService;
  28. @GetMapping("/findUserName/{username}")
  29. public Result findByName(@PathVariable String username){
  30. QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  31. queryWrapper.eq("username",username);
  32. return Result.success(userService.getOne(queryWrapper));
  33. }
  34. //登录
  35. @PostMapping("/login")
  36. public Result login(@RequestBody UserDto userDto) {
  37. String username = userDto.getUsername();
  38. String password = userDto.getPassword();
  39. if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
  40. return Result.error(Constants.CODE_400, "系统错误");
  41. }
  42. UserDto dto = userService.login(userDto);
  43. return success(dto);
  44. }
  45. //注册
  46. @PostMapping("/register")
  47. public Result register(@RequestBody UserDto userDto) {
  48. String username = userDto.getUsername();
  49. String password = userDto.getPassword();
  50. if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
  51. return Result.error(Constants.CODE_400, "系统错误");
  52. }
  53. return success(userService.register(userDto));
  54. }
  55. //导入
  56. @PostMapping("/import")
  57. public Result imp(MultipartFile file) throws Exception {
  58. InputStream inputStream = file.getInputStream();
  59. ExcelReader reader = ExcelUtil.getReader(inputStream);
  60. //方式1(推荐) 通过javabean的方式读取Excel内的对象 但是要求表头必须是英文,跟javabean的属性要对应起来
  61. /*List<User> list = reader.readAll(User.class);*/
  62. //方式二:忽略表头的中文 直接读取表内容
  63. List<List<Object>> list = reader.read(1);
  64. List<User> users = CollUtil.newArrayList();
  65. for (List<Object> row : list) {
  66. User user = new User();
  67. user.setUsername(row.get(0).toString());
  68. user.setPassword(row.get(1).toString());
  69. user.setNickname(row.get(2).toString());
  70. user.setEmail(row.get(3).toString());
  71. user.setPhone(row.get(4).toString());
  72. user.setAddress(row.get(5).toString());
  73. user.setAvatarUrl(row.get(6).toString());
  74. users.add(user);
  75. }
  76. userService.saveBatch(users);
  77. return success(true);
  78. }
  79. //导出
  80. @GetMapping("/export")
  81. public Result export(HttpServletResponse response) throws Exception {
  82. //从数据库查询出所有数据
  83. List<User> list = userService.list();
  84. //通过工具类创建writer写出到磁盘路径
  85. /*ExcelWriter writer = ExcelUtil.getWriter(filesUploadPath+"/用户信息.xlsx");*/
  86. //在内存操作,写出到浏览器
  87. ExcelWriter writer = ExcelUtil.getWriter(true);
  88. //自定义标签别名
  89. writer.addHeaderAlias("username", "用户名");
  90. writer.addHeaderAlias("password", "密码");
  91. writer.addHeaderAlias("nickname", "昵称");
  92. writer.addHeaderAlias("email", "邮箱");
  93. writer.addHeaderAlias("phone", "电话");
  94. writer.addHeaderAlias("address", "地址");
  95. writer.addHeaderAlias("createTime", "创建时间");
  96. writer.addHeaderAlias("avatarUrl", "头像");
  97. //一次性写出list内的对象到excel。使用默认样式,强制输出标题
  98. writer.write(list, true);
  99. //响应浏览器响应的格式
  100. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
  101. String fileName = URLEncoder.encode("用户信息", "UTF-8");
  102. response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
  103. ServletOutputStream out = response.getOutputStream();
  104. writer.flush(out, true);
  105. out.close();
  106. writer.close();
  107. return Result.success();
  108. }
  109. //修改或增加
  110. @PostMapping("/saveUser")
  111. public Result saveUser(@RequestBody User user) {
  112. //新增或修改
  113. return success(userService.saveOrUpdate(user));
  114. }
  115. @GetMapping("/findAll")
  116. public Result findAll() {
  117. return success(userService.list());
  118. }
  119. @DeleteMapping("/{id}")
  120. public Result delete(@PathVariable("id") Integer id) {
  121. return success(userService.removeById(id));
  122. }
  123. //批量删除
  124. @PostMapping("/del/batch")
  125. public Result deleteBatch(@RequestBody List<Integer> ids) {
  126. return success(userService.removeBatchByIds(ids));
  127. }
  128. //分页查询
  129. //接口路径 /user/selectPage?pageNum = 1 &pageSize = 10
  130. //@RequestParam 接收
  131. //limit第一个参数 = (pageNum - 1)*pageSize
  132. //pageSize
  133. /* @GetMapping("/selectPage")
  134. public Map<String, Object> selectPage(@RequestParam String username, @RequestParam Integer pageSize, @RequestParam Integer pageNum) {
  135. pageNum = (pageNum - 1) * pageSize;
  136. username = "%" + username + "%";
  137. List<User> data = userService.selectPage(pageNum, pageSize, username);
  138. Integer total = userService.selectTotal(username);
  139. Map<String, Object> res = new HashMap<>();
  140. res.put("data", data);
  141. res.put("total", total);
  142. return res;
  143. }*/
  144. //分页查询 mybatis-plus方式
  145. @GetMapping("/selectPage")
  146. public Result selectPage(@RequestParam(defaultValue = "") String username,
  147. @RequestParam Integer pageSize,
  148. @RequestParam Integer pageNum,
  149. @RequestParam(defaultValue = "") String email,
  150. @RequestParam(defaultValue = "") String address) {
  151. IPage<User> page = new Page<>(pageNum, pageSize);
  152. QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  153. if (!"".equals(username)) {
  154. queryWrapper.like("username", username);
  155. }
  156. if (!"".equals(email)) {
  157. queryWrapper.like("email", email);
  158. }
  159. if (!"".equals(address)) {
  160. queryWrapper.like("address", address);
  161. }
  162. queryWrapper.orderByDesc("id");
  163. return success(userService.page(page, queryWrapper));
  164. }
  165. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/393427
推荐阅读
相关标签
  

闽ICP备14008679号