springboot+layui进行分页以及模糊查询

springboot+layui进行分页以及模糊查询

springboot+layui进行分页以及模糊查询

前端

html部分
搜索部分

<fieldset class="table-search-fieldset">
					<legend>搜索信息</legend>
					<div style="margin: 10px 10px 10px 10px">
						<form class="layui-form layui-form-pane" action="">
							<div class="layui-form-item">

								<div class="layui-inline">
									<label class="layui-form-label">用户姓名</label>
									<div class="layui-input-inline">
										<input type="text" name="userName" autocomplete="off" class="layui-input">
									</div>
								</div>
								<div class="layui-inline">
									<label class="layui-form-label">年龄</label>
									<div class="layui-input-inline">
										<input type="text" name="userAge" autocomplete="off" class="layui-input">

									</div>
								</div>
								<div class="layui-inline">
									<label class="layui-form-label">性别</label>
									<div class="layui-input-inline">
										<!-- <input type="text" name="userSex" autocomplete="off" class="layui-input"> -->
										<select name="userSex" style="width: 100px;">
											<option value="">请选择性别</option>
											<option value=""></option>
											<option value=""></option>
										</select>
									</div>
								</div>
								<div class="layui-inline">
									<button lay-submit lay-filter="data-search-btn"
										class="layui-btn layui-btn-primary"><i class="layui-icon"></i> 搜 索</button>
									<!--  -->

								</div>
							</div>
						</form>
					</div>
				</fieldset>

数据表格

<table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

js部分
数据表格渲染

layui.use(['form', 'table'], function() {
				var $ = layui.jquery,
					form = layui.form,
					table = layui.table;
				console.log("执行到了表格")

				table.render({
					elem: '#currentTableId',
					headers: {
						"Authorization": localStorage.token
					},
					url: 'http://127.0.0.1:8081/user/lists',
					method: 'get',
					xhrFields: {
					                    withCredentials: true
					                },
					page: true,
					response: {
						statusCode:200, //规定成功的状态码,默认:0
					},
					parseData: function(res) { //res 即为原始返回的数据
					console.log(res);
					
					if(res.code != 200){
						return {
							"code": res.code, //解析接口状态
							"msg": res.msg, //解析提示文本
							
						};
					}else{
						return {
							"code": res.code, //解析接口状态
							"msg": res.msg, //解析提示文本
							"count": res.data.total, //解析数据长度
							"data": res.data.list //解析数据列表
						};
					}
						
					},
					toolbar: '#toolbarDemo',
					defaultToolbar: ['filter', 'exports', 'print', {
						title: '提示',
						layEvent: 'LAYTABLE_TIPS',
						icon: 'layui-icon-tips'
					}],

					cols: [
						[{
								type: "checkbox",
								width: 50
							},
							{
								field: 'userId',
								width: 80,
								title: 'ID',
								sort: true
							},
							{
								field: 'userName',
								width: 80,
								title: '用户名'
							},
							{
								field: 'userPwd',
								width: 80,
								title: '密码'
							},
							{
								field: 'userAge',
								width: 80,
								title: '年龄'
							},
							{
								field: 'userSex',
								title: '性别',
								minWidth: 150
							},
							{
								field: 'createTime',
								title: '创建时间',
								minWidth: 150
							},
							{
								field: 'updateTime',
								title: '更新时间',
								minWidth: 150
							},
							{
								title: '操作',
								minWidth: 150,
								toolbar: '#currentTableBar',
								align: "center"
							}
						]
					],
					limits: [10, 15, 20, 25, 50, 100],
					limit: 15,
					page: true,
					skin: 'line'
				});

数据表格模糊搜索重载

form.on('submit(data-search-btn)', function(data) {
					var result = JSON.stringify(data.field);
					var user = {};
					user.userName = data.field.userName
					user.userAge = data.field.userAge;
					user.userSex = data.field.userSex;

				
					console.log("执行到了重载")
					//执行搜索重载
					table.reload('currentTableId', {
						url: "http://localhost:8081/user/lists",
						method: "get",
						page: {
							curr: 1    //重新以第一页开始渲染
						},
						where: user
					}, 'data');
					return false;
				});

后端

插件

 <!-- mybatis的分页助手-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.10</version>
            <exclusions>    <!--排除与mybatis-plus的冲突否则报错-->
                <exclusion>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis</artifactId>
                </exclusion>
            </exclusions>

        </dependency>

Mapper层

@Repository
@Mapper
public interface UserMapper extends BaseMapper<User> {

    List<User> selectAll(User user);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zzuli.mapper.UserMapper">
    <sql id="Base_Column_List">
    user_id, user_name, user_pwd, user_sex, user_age, create_time, update_time,phone,email
    </sql>
    <select id="selectAll" resultType="com.zzuli.entity.User">
        select <include refid="Base_Column_List"/> from user
        <where>
            <if test="userName != null and userName != ''">
                user_name like '%${userName}%'
            </if>
            <if test="userAge != null and userAge != 0">
                and user_age  = #{userAge}
            </if>
            <if test="userSex != null and userSex != ''">
                and user_sex  = #{userSex}
            </if>
        </where>
    </select>


</mapper>

service层

 @Override
    public List<User> selectAll(User user) {
        return userMapper.selectAll(user);
    }

    public Map<String, Object> selectAll(Integer pageNum, Integer pageSize, User user){
        //通过PageHelper对象设定分页参数
        PageHelper.startPage(pageNum, pageSize);
        //当前页面的数据
        List<User> list = userMapper.selectAll(user);
        //创建PageInfo对象,并将当前页面数据传入,计算分页相关数据
        PageInfo<User> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        Map<String, Object> map = new HashMap<>();
        //本页数据
        map.put("list", list);
        //总数据条目
        map.put("total", total);
        return map;

    }

Controller层

//    分页以及模糊查询
    @RequiresPermissions("sys:user:list")
    @GetMapping("/lists")
    public Result lists (int page,
                          int limit,
                         User user) {
            System.out.println(user);
            Map<String, Object> data = userService.selectAll(page, limit, user);

            return Result.succ(200, "查询成功", data);
    }

Result结果封装类

package com.zzuli.common.lang;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result implements Serializable {       //成功主要看数据
    private int code;  //200为正常                   //失败主要看信息
    private int count;
    private String msg;
    private Object data;

//    工具类
    public static Result succ (int code, String msg, Object data,Integer count){
        Result r = new Result();
        r.setCode(code);
        r.setMsg(msg);
        r.setData(data);
        r.setCount(count);
        return r;
    }
    public static Result succ (int code, String msg, Object data){
        Result r = new Result();
        r.setCode(code);
        r.setMsg(msg);
        r.setData(data);
        return r;
    }
//    再封装一层
    public static Result succ(Object data){
        return succ(200,"操作成功!",data);
    }
    public static Result succ(Object data,Integer count){
        return succ(200,"操作成功!",data,count);
    }

    public static Result fail (String msg){
        return fail(msg,null);
    }

    public static Result fail (String msg, Object data){
        return fail(400,msg,data);
    }

    public static Result fail (int code, String msg, Object data){
        Result r = new Result();
        r.setCode(code);
        r.setMsg(msg);
        r.setData(data);
        return r;
    }
}

效果展示

分页
模糊查询

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
使用Spring BootLayui实现分页,可以按照以下步骤进行操作: 1. 添加依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.13</version> </dependency> ``` 这个依赖是用来实现分页的,它是基于MyBatis的分页插件,可以帮助我们快速实现分页功能。 2. 编写Controller 在Controller中定义一个方法,用于查询数据并返回分页结果。可以使用PageHelper.startPage()方法来开启分页功能,并将查询结果包装成PageInfo对象返回。例如: ```java @GetMapping("/list") public PageInfo<User> list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) { PageHelper.startPage(pageNum, pageSize); List<User> userList = userService.list(); return new PageInfo<>(userList); } ``` 3. 编写前端页面 使用Layui的表格组件来展示分页数据。在html文件中引入Layui的相关文件,并创建一个表格: ```html <table class="layui-table" lay-data="{url: '/user/list', page:true, limit:10}"> <thead> <tr> <th>ID</th> <th>姓名</th> <th>年龄</th> <th>性别</th> </tr> </thead> <tbody> <tr> <td>{{d.id}}</td> <td>{{d.name}}</td> <td>{{d.age}}</td> <td>{{d.gender}}</td> </tr> </tbody> </table> ``` 这里使用Layui的数据表格组件,通过设置url属性来指定数据接口的地址,page属性来开启分页功能,limit属性来设置每页显示的数据量。在表格中使用{{d.xxx}}的形式来渲染数据。 4. 启动应用程序 现在可以启动Spring Boot应用程序并访问前端页面,就能看到分页数据了。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韭菜盖饭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值