广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Springboot+Mybatis实现分页加条件查询功能
  • 470
分享到

Springboot+Mybatis实现分页加条件查询功能

2024-04-02 19:04:59 470人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

本文实例为大家分享了SpringBoot+mybatis实现分页加条件查询的具体代码,供大家参考,具体内容如下 User.xml <?xml version="1.0" enc

本文实例为大家分享了SpringBoot+mybatis实现分页加条件查询的具体代码,供大家参考,具体内容如下

User.xml

<?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.shelbourne.schooldelivery.mapper.UserMapper">
<!--    用户更新-->
    <update id="update">-- 这里的id为函数名
        update user
        <set>
            <if test="username != null and username !=''">
                username=#{username},
            </if>
            <if test="nickname != null and nickname !=''">
                nickname=#{nickname},
            </if>
            <if test="email != null and email !=''">
                email=#{email},
            </if>
            <if test="phone != null and phone !=''">
                phone=#{phone},
            </if>
            <if test="address != null and address !=''">
                address=#{address}
            </if>
        </set>
        <where>
            id = #{id}
        </where>
    </update>
 
<!--    分页+条件查询-->
    <select id="selectPageWithParam" resultType="com.shelbourne.schooldelivery.entity.User">
        select * from user
        <include refid="condition"></include>
        limit #{startIdx},#{size}
    </select>
 
<!--    查询满足条件的用户总数-->
    <select id="selectTotalWithParam" resultType="java.lang.Integer">
        select count(*) from user
        <include refid="condition"></include>
    </select>
 
<!--    查询条件-->
    <sql id="condition">
        <where>
            1=1
            <if test="username != null and username != ''">
                and username like concat("%",#{username},"%")
            </if>
            <if test="email != null and email != ''">
                and email like concat("%",#{email},"%")
            </if>
            <if test="address != null and address != ''">
                and address like concat("%",#{address},"%")
            </if>
        </where>
    </sql>
</mapper>

UserMapper.java

package com.shelbourne.schooldelivery.mapper;
 
import com.shelbourne.schooldelivery.entity.User;
import org.apache.ibatis.annotations.*;
 
import java.util.List;
 
@Mapper
public interface UserMapper {
 
    //查询所有用户
    @Select("select * from user")
    //mybatis提供注解,注意SQL语句后不能加分号
    List<User> findAll();
 
    //新增用户
    @Insert("insert into user(username,passWord,nickname,email,phone,address)" +
            "values(#{username},#{password},#{nickname},#{email},#{phone},#{address})")
    public Integer insert(User user);
 
    //通过注解(静态)和xml里面(动态)两种方式编写SQL语句
    int update(User user);
 
    //删除单个用户
    @Delete("delete from user where id=#{id}")
    Integer deleteById(@Param("id") Integer id);//最后加上@Param参数,参数名和上面的#{}里面的一样
 
    //查询记录条数
    @Select("select count(*) from user")
    Integer selectTotal();
 
    //编写动态SQL实现分页查询+条件查询
    //查询满足条件的某一页用户
    List<User> selectPageWithParam(Integer startIdx, Integer size, String username, String email, String address);
 
    //查询满足条件的所有用户数
    int selectTotalWithParam(String username, String email, String address);
}

UserController.java

package com.shelbourne.schooldelivery.controller;
 
import com.shelbourne.schooldelivery.entity.User;
import com.shelbourne.schooldelivery.mapper.UserMapper;
import com.shelbourne.schooldelivery.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.WEB.bind.annotation.*;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@RequestMapping("/user")  //统一给接口加前缀,postman后台接口localhost:9090/user
@RestController
public class UserController {
 
    @Autowired  //注入其他类的注解
    private UserMapper userMapper;
 
    @Autowired
    private UserService userService;
 
    //查询所有用户
    @GetMapping
    public List<User> findAll(String username) {
        return userMapper.findAll();
    }
 
    //通过POST请求进行新增和更新操作
    @PostMapping
    public Integer save(@RequestBody User user) {//一定要加上RequestBody,可以把前端传回的JSON对象转换为Java对象
        return userService.save(user);
    }
 
    //删除请求接口
    @DeleteMapping("/{id}")
    public Integer delete(@PathVariable Integer id) {//这里的“id”必须和DeleteMapping里面的名字一样
        return userMapper.deleteById(id);
    }
 
    @GetMapping("/page")
    public Map<String, Object> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String username,
                                        @RequestParam String email, @RequestParam String address) {
        int startIdx = (pageNum - 1) * pageSize, size = pageSize;
        List<User> data = userMapper.selectPageWithParam(startIdx, size, username, email, address);//获取一页的数据
        int total = userMapper.selectTotalWithParam(username, email, address);//查询总条数
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);//表格数据
        res.put("total", total);//分页使用
        return res;
    }
}

Home.Vue中:

<script>
    export default {
        data() {
            return {
                total: 0,//记录条数为0
                pageNum: 1,//默认从第一条记录开始
                pageSize: 10,//默认分页大小为10
                username: "",//条件查询的姓名
                email: "",//条件查询的邮箱
                address: "",//条件查询的地址
            }
        },
        created() {//页面渲染完成后的数据刷新
            this.flushData()
        },
        methods: {
            //获取数据
            flushData() {
                fetch("http://localhost:9090/user/page?pageNum=" +
                    this.pageNum + "&pageSize=" + this.pageSize + "&username=" +
                    this.username + "&email=" + this.email + "&address=" + this.address).then(res => res.json()).then(res => {
                    // console.log(res)
                    //跨域问题:前端端口8080,后端端口9090,导致跨域
                    this.tableData = res.data
                    this.total = res.total
                })
            }
        }
    }
</script>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: Springboot+Mybatis实现分页加条件查询功能

本文链接: https://www.lsjlt.com/news/145664.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Springboot+Mybatis实现分页加条件查询功能
    本文实例为大家分享了Springboot+Mybatis实现分页加条件查询的具体代码,供大家参考,具体内容如下 User.xml <xml version="1.0" enco...
    99+
    2022-11-13
  • Springboot+Mybatis怎么实现分页加条件查询功能
    本篇内容介绍了“Springboot+Mybatis怎么实现分页加条件查询功能”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!User.xml...
    99+
    2023-06-30
  • springboot +mybatis 使用PageHelper实现分页并带条件模糊查询功能
    完整案例: SpringBoot + laypage分页 + 模糊查询 完整案例 下面在通过实例代码介绍下springboot +mybatis 使用PageHelper实现分页并带...
    99+
    2022-11-13
  • SpringBoot分页查询功能的实现方法
    目录前言:首先是手动实现分页查询:接下来是关联前端分页和后台数据:总结前言: 学习了SpringBoot分页查询的两种写法,一种是手动实现,另一种是使用框架实现。现在我将具体的实现流...
    99+
    2022-11-13
  • Mybatis实现联表查询并且分页功能
    今天同学突然问我这个怎么搞。 然后自己搞了一下发现这个玩意有坑。。就记录一下 0. 表结构 person表 cat表 一个person有多个cat 实体类就这么写 1. 实体类 ...
    99+
    2022-11-11
  • oracle+mybatis-plus+springboot怎么实现分页查询
    本篇内容主要讲解“oracle+mybatis-plus+springboot怎么实现分页查询”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“oracle+mybatis-plus+springb...
    99+
    2023-06-20
  • mybatis plus实现条件查询
    目录一、wapper介绍二、常用的条件方法1. gt 表示 >2. le 表示 <=3. lt&nb...
    99+
    2022-11-13
  • oracle+mybatis-plus+springboot实现分页查询的实例
    今天蠢了一上午才弄出这玩意,话不多说上代码! 1、建一个配置类 package com.sie.demo.config; import com.baomidou.mybati...
    99+
    2022-11-12
  • Oracle使用MyBatis中RowBounds实现分页查询功能
    Oracle中分页查询因为存在伪列rownum,sql语句写起来较为复杂,现在介绍一种通过使用MyBatis中的RowBounds进行分页查询,非常方便。 使用MyBatis中的RowBounds进行分页查...
    99+
    2022-10-18
  • 如何使用mybatis-plus实现分页查询功能
    今天就跟大家聊聊有关使用mybatis-plus如何实现分页查询功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。 引入依赖...
    99+
    2022-11-13
  • Mybatis实现分页查询
    一. 简单分页查询——limit 使用select查询时,如果结果集数据量较大,一个页面难以处理,就会采用分页查询。 分页查询,就是从结果集中拿出指定的第n页到第m页的数据来显示。 // limit分页公式 // currentP...
    99+
    2023-09-12
    mybatis java mysql
  • mybatis-plus 多表关联条件分页查询
    此处以一对多,条件分页查询为例: 一.表结构: 主表 CREATE TABLE `t_user` ( `id` bigint NOT NULL AUTO_INCREMENT, `user_name` varchar(255) DEFAU...
    99+
    2023-08-20
    mybatis mysql java
  • SpringBoot如何整合PageHelper实现分页查询功能
    这篇文章主要介绍了SpringBoot如何整合PageHelper实现分页查询功能,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。使用方法导入依赖在中央仓库sonatype中搜...
    99+
    2023-06-29
  • SpringBoot整合PageHelper实现分页查询功能详解
    前言 本文介绍的是MyBatis 分页插件 PageHelper,如果你也在用 MyBatis,建议尝试该分页插件,这一定是最方便使用的分页插件。分页插件支持任何复杂的单表、多表分页...
    99+
    2022-11-13
  • SSH如何实现条件查询和分页查询
    这篇文章将为大家详细讲解有关SSH如何实现条件查询和分页查询,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。1、QueryHelper和PageResultQueryHel...
    99+
    2022-10-19
  • Mybatis-Plus实现分页查询
    Mybatis-Plus实现分页查询 文章目录 Mybatis-Plus实现分页查询1.1技术概述1.2技术详述1.2.1配置分页插件1.2.2自定义分页1、如果你的mapper没有继承BaseMapper2、如果你的mapper继...
    99+
    2023-08-17
    mybatis java spring boot
  • 如何在springboot中使用mybatis-plus实现一个多表分页查询功能
    这篇文章将为大家详细讲解有关如何在springboot中使用mybatis-plus实现一个多表分页查询功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。新建一个springboot工程需要...
    99+
    2023-06-07
  • Java实现分页查询功能
    分页查询 分页查询将数据库中庞大的数据分段显示,每页显示用户自定义的行数,提高用户体验度,最主要的是如果一次性从服务器磁盘中读出全部数据到内存,有内存溢出的风险 真假分页 假分页: ...
    99+
    2022-11-13
  • MyBatis-Plus 条件查询器的实现
    目录一、常用注解1.1 @TableName1.2 @TableId1.3 @TableField1.4 @TableLogic二、条件构造器Wrapper2.1 QueryWrap...
    99+
    2022-11-13
  • mybatis实现多条件模糊查询
    EmployeeMapper.xml SELECT id,NAME,gender,idcard,nationid FROM employee WHERE 1=1 ...
    99+
    2023-09-01
    sql 数据库 mysql
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作