条件查询和数据查询

news/2024/10/23 9:21:26/

一、后端

1.controller层

package com.like.controller;import com.like.common.CommonDto;
import com.like.entity.User;
import com.like.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
@RequestMapping("/user")
public class UserController {@Resourceprivate UserService userService;@GetMapping("/getList")public CommonDto getList(){CommonDto commonDto = new CommonDto();List<User> userList = userService.getList();commonDto.setContent(userList);return commonDto;}
}

2.service层

package com.like.service;import com.baomidou.mybatisplus.extension.service.IService;
import com.like.entity.User;
import java.util.List;public interface UserService extends IService<User> {List<User> getList();}

QueryWrapper是Mybatis-plus 3.0.7版本之前推出的查询工具类。和LambdaQueryWrapper的使用方法不同,QueryWrapper需要使用SQL语句来构建查询条件 

package com.like.service.impl;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.like.entity.User;
import com.like.mapper.UserMapper;
import com.like.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {@Resourceprivate UserMapper userMapper;@Overridepublic List<User> getList() {QueryWrapper<User> queryWrapper = new QueryWrapper<>();List<User> selectList = userMapper.selectList(queryWrapper);return selectList;}
}

二、前端

1.启动时展示数据

首先编写axios的get请求,注意methods和created和data这些是同级,笔者就是犯了这个小错误,导致debug了一万年,希望大家引以为戒

methods: {handleEdit(index, row) {console.log(index, row);},handleDelete(index, row) {console.log(index, row);},handleSizeChange(val) {console.log(`每页 ${val} 条`);},handleCurrentChange(val) {console.log(`当前页: ${val}`);},getUserList(){this.axios.get("http://localhost:3333/user/getList").then((resp)=>{console.log(resp,'resp');});}},created() {//Vue生命周期函数,此处的目的是页面打开,就调用函数,将数据库中的数据显示出来this.getUserList();}

这样,启动前端工程后,立刻向后端发送了一个get请求,浏览器响应数据如下

显然,我们需要的数据就在data的content中,此时只需让data中的tableData=resp.data.content

   getUserList(){this.axios.get("http://localhost:3333/user/getList").then((resp)=>{console.log(resp,'resp');this.tableData = resp.data.content;});}

这时候,我们的数据库的数据就全部显示在页面上了

 2.条件查询

现在我们需要的效果是,点击查询,触发条件查询,即根据姓名查询用户信息

给getUserList方法加上参数name

   getUserList(){this.axios.get("http://localhost:3333/user/getList",{params:{name:this.query.name  //参数让其等于输入框输入的name}}).then((resp)=>{console.log(resp,'resp');this.tableData = resp.data.content;});}

按钮处添加点击事件,点击触发该方法

<el-button type="primary" @click="getUserList">查询</el-button>

点击查询按钮时候,我们可以看到前端有响应,并且network中的get请求带有参数,这样我们就可以编写后端代码了

后端接口代码逻辑完善

 @Overridepublic List<User> getList(User user) {LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();//判断前端是否传来有参数,如果有参数,就使用这个构造条件if (ObjectUtils.isNotEmpty(user.getName())) {lambdaQueryWrapper.like(User::getName,user.getName());}List<User> userList = userMapper.selectList(lambdaQueryWrapper);return userList;}

3.输入框增加可清除效果

<el-input v-model="query.name" style="width:200px" placeholder="请输入用户姓名" clearable></el-input>


http://www.ppmy.cn/news/1138548.html

相关文章

【C语言】模拟实现strstr

strstr这个库函数看到这个名字大概率猜不到这是什么函数&#xff0c; 但经过学习就可以很好的认识到这个函数 目录 介绍&#xff1a;模拟实现&#xff1a;思路&#xff1a;代码实现&#xff1a; 介绍&#xff1a; 可以看到此函数是用来寻找一个字符串中是否含有另一个字符串 代…

视频号规则改动,不再支持拍单,传统无货源模式已行不通!

视频号小店批量铺货行不通了&#xff0c;大家好我是派大星&#xff0c;这两天视频号发布了一个公告&#xff0c; 核心信息呢就是10月7号&#xff0c;视频号小店&#xff0c;将无法直接查看消费者的详细下单信息&#xff0c;只能通过电子面单的形式&#xff0c;打单发货。每个店…

CTFHUB - SSRF

目录 SSRF漏洞 攻击对象 攻击形式 产生漏洞的函数 file_get_contents() fsockopen() curl_exec() 提高危害 利用的伪协议 file dict gopher 内网访问 伪协议读取文件 端口扫描 POST请求 总结 上传文件 总结 FastCGI协议 CGI和FastCGI的区别 FastCGI协议 …

《protobuf》基础语法3

文章目录 默认值更新规则保留字段未知字段 默认值 在反序列化时&#xff0c;若被反序列化的二进制序列中不包含某个字段&#xff0c;则在反序列化时&#xff0c;就会设置对应默认值。不同的类型默认值不同&#xff1a; 类型默认值字符串“”布尔型false数值类型0枚举型0设置了…

找不到msvcp140.dll是什么意思?三个快速解决msvcp140.dll丢失问题的方法

msvcp140.dll 丢失意味着您的计算机上缺少Microsoft Visual C 2015 Redistributable中的一个动态链接库文件。msvcp140.dll是该软件包中的一个组件&#xff0c;许多应用程序和游戏都需要这个动态链接库文件才能正常运行。当您尝试运行需要 msvcp140.dll 的应用程序或游戏时&…

leetcode做题笔记164. 最大间距

给定一个无序的数组 nums&#xff0c;返回 数组在排序之后&#xff0c;相邻元素之间最大的差值 。如果数组元素个数小于 2&#xff0c;则返回 0 。 您必须编写一个在「线性时间」内运行并使用「线性额外空间」的算法。 示例 1: 输入: nums [3,6,9,1] 输出: 3 解释: 排序后的…

为什么MySQL索引选择B+树而不使用B树?

为什么mysql索引选择B树而不使用B树&#xff1f; 1. 关于mysql查询效率&#xff1a; 2. 关于分块读取&#xff1a; 3. 关于数据格式存储&#xff1a; 4. 关于合适的数据结构&#xff1a;哈希表&#xff0c;树 哈希表: 分析&#xff1a; 哈希表是散列表&#xff0c;存储在其中的…

ld.lld的unknown file type错误

报错 在一次编译aosp源码中&#xff0c;出现以下错误&#xff1a; FAILED: //external/pdfium:libpdfium link libpdfium.so [arm] Outputs: out/soong/.intermediates/external/pdfium/libpdfium/android_arm_armv8-a_shared/unstripped/libpdfium.so Error: exited with cod…