iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Springboot+Mybatis怎么实现分页加条件查询功能
  • 206
分享到

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

2023-06-30 01:06:00 206人浏览 泡泡鱼
摘要

本篇内容介绍了“SpringBoot+mybatis怎么实现分页加条件查询功能”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!User.xml

本篇内容介绍了“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; @Mapperpublic 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@RestControllerpublic 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>

“Springboot+Mybatis怎么实现分页加条件查询功能”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!

--结束END--

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

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

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

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

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

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

  • 微信公众号

  • 商务合作