采用技术:vue+springboot+mybatis-plus
基本环境配置就不说了,直接上核心代码
前端查询框
当我们一开始加载页面的时候搜索的条件是空的此时直接向后端传输空值即可
前端传输格式
getCount() {
//根据token来进行获取数据
const token = window.localStorage.getItem("Authorization");
// "http://localhost/yueyou-admin/user/search"
//通过get方法获取数据
this.$http.get("http://localhost:8084/user/search", {
// headers: {
// "Authorization": token
// },
params: {
"startDate": this.date[0],//开始时间
"endDate": this.date[1],//结束时间
"sex": this.sex,//性别
"telephone": this.telephone,//手机号
"nikeName": this.nikeName,//用户昵称
"pageNum": this.currenPage,//页码
"pageSize": this.pageSize//每页大小
}
//下面部分是打印数据
}).then((response) => {
let result = response.data;
if (result.data.list.length > 0) {
//存在数据
this.tableData = result.data.list;
this.currenPage = result.data.pageNum;
this.total = result.data.total;
this.pages = result.data.pages;
} else {
this.tableData = [];
this.currenPage = 0;
this.total = 0;
this.pages = 0;
}
}).catch((error) => {
this.$message({
message: '网络错误!',
type: 'warning'
});
});
根据自己的需要进行跨域处理和token校验在这里我就不展示了
为了能更好的展示数据接下来我将用json格式来显示返回的数据
在网站地址输入传输的url地址和信息,第一次加载界面的时候只需要页码和每页大小
返回数据
那我们加入其他查询条件会如何呢
可以看到输入条件存在时才进行该条件的查询不存在就不查询,接下来我们来看看后端是如何实现的
后端代码
controller层就是简单的接收数据在这我也不展示了,直接到service层的业务处理部分
package com.leach.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leach.dao.UserMapper;
import com.leach.domain.User;
import com.leach.domain.UserSearch;
import com.leach.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public PageInfo<User> getUser(UserSearch userSearch) throws ParseException {
//分页
PageHelper.startPage(userSearch.getPageNum(),userSearch.getPageSize());
//条件构造器
QueryWrapper queryWrapper = new QueryWrapper();
//Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2005-06-09"); 将String转为事件类型
//创建一个map
int index = 0;
HashMap<String,Object> map = new HashMap<>();
//判断条件不为空就放入map
if(!userSearch.getSex().equals("")){
map.put("sex",userSearch.getSex());
index++;
}
if(!userSearch.getTelephone().equals("")){
map.put("telephone",userSearch.getTelephone());
index++;
}
if(!userSearch.getNikeName().equals("")){
map.put("nike_name",userSearch.getNikeName());
index++;
}
//当map有值就放到条件构造器中
if(index>0){
queryWrapper.allEq(map);
}
/*获取在这期间生日的user*/
if(!userSearch.getStartDate().equals("")&&!userSearch.getEndDate().equals("")){
queryWrapper.between("birthday",new SimpleDateFormat("yyyy-MM-dd").parse(userSearch.getStartDate()),
new SimpleDateFormat("yyyy-MM-dd").parse(userSearch.getEndDate()));
}
ArrayList<User> list = (ArrayList<User>) userMapper.selectList(queryWrapper);
PageInfo<User> pageInfo = new PageInfo<>(list);
return pageInfo;
}
}
由于本人前端不是很擅长不知道如何将传递的值设置为null有解答的可以评论教育我哈哈哈哈哈
个人感觉mybatis-plus可以帮助减少很多代码但是跟sql语句又更加揉在一起了,在大型项目中可能会导致维护难度增加所以使用需考虑考虑