溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Springboot+Mybatis怎么實(shí)現(xiàn)分頁加條件查詢功能

發(fā)布時間:2022-04-12 13:58:17 來源:億速云 閱讀:365 作者:zzz 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“Springboot+Mybatis怎么實(shí)現(xiàn)分頁加條件查詢功能”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

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為函數(shù)名
        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>
 
<!--    查詢滿足條件的用戶總數(shù)-->
    <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);
 
    //通過注解(靜態(tài))和xml里面(動態(tài))兩種方式編寫SQL語句
    int update(User user);
 
    //刪除單個用戶
    @Delete("delete from user where id=#{id}")
    Integer deleteById(@Param("id") Integer id);//最后加上@Param參數(shù),參數(shù)名和上面的#{}里面的一樣
 
    //查詢記錄條數(shù)
    @Select("select count(*) from user")
    Integer selectTotal();
 
    //編寫動態(tài)SQL實(shí)現(xiàn)分頁查詢+條件查詢
    //查詢滿足條件的某一頁用戶
    List<User> selectPageWithParam(Integer startIdx, Integer size, String username, String email, String address);
 
    //查詢滿足條件的所有用戶數(shù)
    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")  //統(tǒng)一給接口加前綴,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請求進(jìn)行新增和更新操作
    @PostMapping
    public Integer save(@RequestBody User user) {//一定要加上RequestBody,可以把前端傳回的JSON對象轉(zhuǎn)換為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);//獲取一頁的數(shù)據(jù)
        int total = userMapper.selectTotalWithParam(username, email, address);//查詢總條數(shù)
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);//表格數(shù)據(jù)
        res.put("total", total);//分頁使用
        return res;
    }
}

Home.vue中:

<script>
    export default {
        data() {
            return {
                total: 0,//記錄條數(shù)為0
                pageNum: 1,//默認(rèn)從第一條記錄開始
                pageSize: 10,//默認(rèn)分頁大小為10
                username: "",//條件查詢的姓名
                email: "",//條件查詢的郵箱
                address: "",//條件查詢的地址
            }
        },
        created() {//頁面渲染完成后的數(shù)據(jù)刷新
            this.flushData()
        },
        methods: {
            //獲取數(shù)據(jù)
            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,導(dǎo)致跨域
                    this.tableData = res.data
                    this.total = res.total
                })
            }
        }
    }
</script>

“Springboot+Mybatis怎么實(shí)現(xiàn)分頁加條件查詢功能”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI