我们希望用户可以自己控制是否要模糊查询

用户可以自由的选择字段去查询。

如上图,我在前端页面准备了

  • 多选框:决定是否模糊查询。(True or False)
  • 下拉选择框:决定要查询关键词的所属字段
  • 输入框:决定关键词
  • 按钮:发起请求

肯定要传参数的,所有这个接口请求方式设置为Post。考虑到方便接收参数,我们使用post的param方式传参(不了解这个的可以看我之前的笔记 https://www.cnblogs.com/mllt/p/17990445/project202401-14)

export function user_search(data){
return postWithParams("/users/search",data)
}

现在我们先编写按钮事件 userSearch()

const userSearch=()=>{
//配置参数
let data={
"like":checked.value,//是否模糊查询
"key":ziduan_select.value,//字段名称
"word":keyword.value//关键词 }
//发送请求
user_search(data).then((res)=>{
if(res["code"]===1){
message.success(res["msg"]);
dataSource.value=res["data"]
}else {
message.error(res["msg"]);
}
})
}

接下来去编写后端接口

Controller:

    // 条件查询
@PostMapping("/search")
public ResponseEntity<?> searchUsers(@RequestParam boolean like,@RequestParam String key,@RequestParam String word) {
List<Users> users = usersService.searchUsers(like, key, word);
return ResponseEntity.success(users);
}

Service

    List<Users> searchUsers(boolean like, String key, String word);

ServiceImpl

 @Override
public List<Users> searchUsers(boolean like, String key, String word) {
if(like){
return usersMapper.searchUsersLike(key,word);
}else{
return usersMapper.searchUsersNoLike(key,word);
}
}

mapper

    //自定义字段模糊查询
@Select("select * from users where `${key}` LIKE CONCAT('%', #{word}, '%')")
List<Users> searchUsersLike(@Param("key") String key, @Param("word") String word);
@Select("select * from users where `${key}` = #{word}")
List<Users> searchUsersNoLike(String key, String word);

完毕,就是如此简单。

我把前端的这个组件的完整代码贴出来

<script setup>
import {PlusSquareOutlined,SearchOutlined} from "@ant-design/icons-vue";
import {onMounted, reactive, ref} from "vue";
import {user_delete_by_id, user_get_list, user_search, user_update} from "../../../apis/UserApi.js";
import { cloneDeep } from 'lodash-es';
import {message} from "ant-design-vue"; onMounted(()=>{
user_get_list().then((res)=>{
dataSource.value=res.data
//对得到的用户数据进行处理,以显示在表格中
})
})
const columns = [
{
title: '操作',
dataIndex: 'operation',
width: '20%',
},
{
title: '用户ID',
dataIndex: 'userId',
width: '10%',
},
{
title: '昵称',
dataIndex: 'userNickname',
width: '10%',
},
{
title: '账号',
dataIndex: 'userAccount',
width: '10%',
},
{
title: '身份',
dataIndex: 'userIdentity',
width: '10%',
}, {
title: '权限',
dataIndex: 'remarks',
width: '10%',
},
{
title: '注册时间',
dataIndex: 'userRegTime',
width: '25%',
}, ];
const dataSource = ref([]);
const editableData = reactive({});
const edit = key => {
editableData[key] = cloneDeep(dataSource.value.filter(item => key === item.userId)[0]);
};
const save = key => {
Object.assign(dataSource.value.filter(item => key === item.userId)[0], editableData[key]);
delete editableData[key];
// 找到要更新的用户的索引
const userIndex = dataSource.value.findIndex(item => key === item.userId)
// 调用更新用户的函数
const updatedUser = dataSource.value[userIndex];
console.log(updatedUser)
user_update(updatedUser)
.then(response => {
// 处理响应,例如显示成功消息
// console.log('User updated successfully', response);
message.success(response["msg"])
})
.catch(error => {
// 处理错误,例如显示错误消息
// console.error('Failed to update user', error);
message.error(error)
}); };
const cancel = key => {
delete editableData[key];
};
const formatDate= (dateTimeStr)=> {
if (dateTimeStr === '' || dateTimeStr==null) {
return ''; // 返回空字符串或者其他占位符
}
// 使用 Date 对象解析日期时间字符串
const date = new Date(dateTimeStr);
// 检查日期对象是否有效
if (isNaN(date.getTime())) {
return ''; // 返回空字符串或者其他占位符
}
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
} const checked = ref(true);
const keyword=ref("")
const ziduan_select=ref("user_id")
const ziduan=ref([{
value: 'user_id',
label: '用户ID',
},
{
value: 'user_nickname',
label: '昵称',
},
{
value: 'user_account',
label: '用户账号',
},])
const onDelete = key => {
let uid = key
dataSource.value = dataSource.value.filter(item => item.userId !== key);
user_delete_by_id(uid).then((res)=>{
if(res["code"]===1){
message.success(res["msg"]);
}else {
message.error(res["msg"]);
}
})
}; const userSearch=()=>{
//配置参数
let data={
"like":checked.value,//是否模糊查询
"key":ziduan_select.value,//字段名称
"word":keyword.value//关键词 }
//发送请求
user_search(data).then((res)=>{
if(res["code"]===1){
message.success(res["msg"]);
dataSource.value=res["data"]
}else {
message.error(res["msg"]);
}
})
}
</script> <template>
<br>
<div class="mytable">
<a-breadcrumb>
<a-breadcrumb-item><a href="/">主页</a></a-breadcrumb-item>
<a-breadcrumb-item><a href="/user">用户管理</a></a-breadcrumb-item>
</a-breadcrumb>
<br>
<hr>
<br>
<a-space>
<a-checkbox v-model:checked="checked">模糊查询</a-checkbox> <a-select
v-model:value="ziduan_select"
style="width: 100px"
:options="ziduan"
></a-select> <a-input v-model:value="keyword" placeholder="请输入关键词"/> <a-button type="primary" @click="userSearch">
<template #icon>
<SearchOutlined />
</template>
搜索
</a-button> <a-button type="primary" >
<template #icon>
<PlusSquareOutlined />
</template>
新增用户
</a-button>
</a-space> </div> <br>
<a-table class="mytable" :columns="columns" :data-source="dataSource" bordered>
<template #bodyCell="{ column, text, record }">
<template v-if="['userNickname', 'userIdentity', 'userStatus','remarks'].includes(column.dataIndex)">
<div>
<a-input
v-if="editableData[record.userId]"
v-model:value="editableData[record.userId][column.dataIndex]"
style="margin: -5px 0"
/>
<template v-else>
{{ text }}
</template>
</div>
</template>
<template v-else-if="column.dataIndex==='userRegTime'">
{{formatDate(text)}}
</template>
<template v-else-if="column.dataIndex === 'operation'">
<div class="editable-row-operations">
<span v-if="editableData[record.userId]">
<a-typography-link @click="save(record.userId)" style="margin: 14px">保存</a-typography-link> <a-popconfirm style="margin: 14px" title="放弃更改?" ok-text="放弃" cancel-text="取消" @confirm="cancel(record.userId)">
<a>取消</a>
</a-popconfirm>
</span>
<span v-else>
<a @click="edit(record.userId)">编辑</a>
</span>
</div> <a-popconfirm
v-if="dataSource.length"
title="确认删除?"
ok-text="确定" cancel-text="取消"
@confirm="onDelete(record.userId)"
>
<a>删除</a>
</a-popconfirm>
</template>
</template>
</a-table>
</template> <style scoped>
.mytable{
margin: 0 auto;max-width: 960px;width: 90%; border-radius:5px;box-shadow: 0 5px 15px 2px rgba(0,0,0,0.2);padding: 1em;box-sizing: border-box;background: rgba(255,255,255,0.9) }
</style>

2024年1月Java项目开发指南16:用户自由选择字段查询、是否模糊查询的更多相关文章

  1. java 实现用户自由选择字段实现导出EXCEL表格

    package com.thinkgem.jeesite.common.utils.excel; import java.io.File; import java.io.OutputStream; i ...

  2. 转:Java项目开发规范参考

    Java项目开发规范参考 - KevinLee的博客 - 博客频道 - CSDN.NEThttp://blog.csdn.net/u011383131/article/details/51227860 ...

  3. IDEA 学习笔记之 Java项目开发深入学习(2)

    Java项目开发深入学习(2): 查找变量被用到的地方 编译当前文件 增加变量watch 注意:我使用了keymap (eclipse模板),所以很多快捷键和eclipse一样. F5单步调试进入函数 ...

  4. IDEA 学习笔记之 Java项目开发深入学习(1)

    Java项目开发深入学习(1): 定义编译输出路径: 继承以上工程配置 重新定义新的项目编译路径 添加source目录:点击添加,再点击移除: 编译项目: 常用快捷键总结: Ctrl+Space 代码 ...

  5. IDEA 学习笔记之 Java项目开发

    Java项目开发: 新建模块: 添加JDK: 导入本地Jars: 从远程Maven仓库下载: 创建package: 新建类/接口/枚举等: 字体太小,改字体: Duplicate Scheme 修改编 ...

  6. 《Maven在Java项目开发中的应用》论文笔记(十七)

    标题:Maven在Java项目开发中的应用 一.基本信息 时间:2019 来源:山西农业大学 关键词:Maven:Java Web:仓库:开发人员:极限编程; 二.研究内容 1.Maven 基本原理概 ...

  7. 收藏基本Java项目开发的书

    一.Java项目开发全程实录 第1章 进销存管理系统(Swing+SQL Server2000实现) 第2章企业内部通信系统(Swing+JavaDB实现) 第3章 企业人事管理系统( Swing+H ...

  8. Java项目开发中实现分页的三种方式一篇包会

    前言   Java项目开发中经常要用到分页功能,现在普遍使用SpringBoot进行快速开发,而数据层主要整合SpringDataJPA和MyBatis两种框架,这两种框架都提供了相应的分页工具,使用 ...

  9. 详细介绍idea实现javaweb项目登入注册(华东交通大学教务处信息管理系统)、模糊查询

    详细介绍idea实现javaweb项目登入注册(华东交通大学教务处信息管理系统).模糊查询 1,创建数据库,我的用户名:root 密码:root,数据库名称:lianwei,表名:login 2,效果 ...

  10. Java项目开发

    项目开发整体构建: MVC+DAO设计模式 用面向对象的方式理解和使用数据库,一个数据库对应一个java项目 数据库--项目 表--类 字段--属性 表中的一条数据--类的一个对象 M:模型层 Jav ...

随机推荐

  1. yaml.load与yaml.dump的用法

    import yaml #向yaml文件中写 with open("E:\个人\ rename.yaml", 'w') as f: project = {'在远方':"1 ...

  2. 精彩回顾|【2023 ACDU 中国行·深圳站】数据库主题交流活动成功举办!

    6月30日下午,[ACDU 中国行·深圳站]在深圳回酒店圆满落下帷幕.本次活动由中国数据库联盟(ACDU)联合墨天轮社区主办,围绕「数据库前沿技术揭秘及应用」这一主题,七位数据库行业的领军人物从数据库 ...

  3. vue前端开发仿钉图系列(2)左侧图层列表的开发详解

    项目开发前还是特别说明一下组件库的重要性,谢谢饿了么团队分享的element组件库,大大节省了页面的开发成本.左侧图层列表核心功能有1.根据图层类型展示点线面2.开关控制右侧地图上点线面的展示和隐藏3 ...

  4. iOSwkwebView 打开 TXT/PDF 文件乱码的问题

    最近做资料文件下载下来并查看的时候,用 WKWebView 打开office 类型的文件的时候是没问题的,但是打开测试人员上传的一个 TXT/PDF 文件就出现了乱码问题,经过查看,应该是文件的编码问 ...

  5. .Net Core 的 using 作用

    // using 的使用 // 1. 引用命名空间 using namespace // 2. 自动释放资源 执行结束自动调用 IDispose 接口释放资源 // using (var contex ...

  6. is特性

    is是特性在动态路由的时候使用 ,在挂载点 component 使用,用来判断哪个组件显示 :

  7. mongo对文档中数组进行过滤的三种方法

    前言 在mongo中数据类型有很多种,常见的包括: 数据类型 例子 描述 String { "x" : "foot" } 字符串.存储数据常用的数据类型.在 M ...

  8. KubeSphere 社区双周报|2024.09.27-10.10

    KubeSphere 社区双周报主要整理展示新增的贡献者名单和证书.新增的讲师证书以及两周内提交过 commit 的贡献者,并对近期重要的 PR 进行解析,同时还包含了线上/线下活动和布道推广等一系列 ...

  9. 基于 KubeSphere 的 AI 平台开发实践

    概述 本文基于 "KubeSphere & Friends 2021 Meetup 北京站" 分享主要内容整理而来,详细内容建议观看视频,本文有一定删减. 作者:胡涛(Da ...

  10. Mysql导出文本文件

    使用mysqldump命令导出文本文件 mysqldump -u root -pPassword -T 目标目录 dbname [tables] [option]; 其中: Password 参数表示 ...