欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2022年04月12日 09:36:20   作者:kisushotto  
這篇文章主要為大家詳細(xì)介紹了Springboot+Mybatis實(shí)現(xiàn)分頁(yè)加條件查詢,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Springboot+Mybatis實(shí)現(xiàn)分頁(yè)加條件查詢的具體代碼,供大家參考,具體內(nèi)容如下

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>
?
<!-- ? ?分頁(yè)+條件查詢-->
? ? <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語(yǔ)句后不能加分號(hào)
? ? 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);
?
? ? //通過(guò)注解(靜態(tài))和xml里面(動(dòng)態(tài))兩種方式編寫SQL語(yǔ)句
? ? int update(User user);
?
? ? //刪除單個(gè)用戶
? ? @Delete("delete from user where id=#{id}")
? ? Integer deleteById(@Param("id") Integer id);//最后加上@Param參數(shù),參數(shù)名和上面的#{}里面的一樣
?
? ? //查詢記錄條數(shù)
? ? @Select("select count(*) from user")
? ? Integer selectTotal();
?
? ? //編寫動(dòng)態(tài)SQL實(shí)現(xiàn)分頁(yè)查詢+條件查詢
? ? //查詢滿足條件的某一頁(yè)用戶
? ? 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后臺(tái)接口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();
? ? }
?
? ? //通過(guò)POST請(qǐng)求進(jìn)行新增和更新操作
? ? @PostMapping
? ? public Integer save(@RequestBody User user) {//一定要加上RequestBody,可以把前端傳回的JSON對(duì)象轉(zhuǎn)換為Java對(duì)象
? ? ? ? return userService.save(user);
? ? }
?
? ? //刪除請(qǐng)求接口
? ? @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);//獲取一頁(yè)的數(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);//分頁(yè)使用
? ? ? ? return res;
? ? }
}

Home.vue中:

<script>
? ? export default {
? ? ? ? data() {
? ? ? ? ? ? return {
? ? ? ? ? ? ? ? total: 0,//記錄條數(shù)為0
? ? ? ? ? ? ? ? pageNum: 1,//默認(rèn)從第一條記錄開(kāi)始
? ? ? ? ? ? ? ? pageSize: 10,//默認(rèn)分頁(yè)大小為10
? ? ? ? ? ? ? ? username: "",//條件查詢的姓名
? ? ? ? ? ? ? ? email: "",//條件查詢的郵箱
? ? ? ? ? ? ? ? address: "",//條件查詢的地址
? ? ? ? ? ? }
? ? ? ? },
? ? ? ? created() {//頁(yè)面渲染完成后的數(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)
? ? ? ? ? ? ? ? ? ? //跨域問(wèn)題:前端端口8080,后端端口9090,導(dǎo)致跨域
? ? ? ? ? ? ? ? ? ? this.tableData = res.data
? ? ? ? ? ? ? ? ? ? this.total = res.total
? ? ? ? ? ? ? ? })
? ? ? ? ? ? }
? ? ? ? }
? ? }
</script>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • IDEA創(chuàng)建springboot + mybatis項(xiàng)目全過(guò)程(步驟詳解)

    IDEA創(chuàng)建springboot + mybatis項(xiàng)目全過(guò)程(步驟詳解)

    這篇文章主要介紹了IDEA創(chuàng)建springboot + mybatis項(xiàng)目全過(guò)程及步驟詳解,本文通圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • java實(shí)現(xiàn)LFU算法的示例代碼

    java實(shí)現(xiàn)LFU算法的示例代碼

    LFU(Least Frequently Used)算法根據(jù)數(shù)據(jù)的歷史訪問(wèn)頻率來(lái)淘汰數(shù)據(jù),其核心思想是“如果數(shù)據(jù)過(guò)去被訪問(wèn)多次,那么將來(lái)被訪問(wèn)的頻率也更高”,本文為大家整理了Java實(shí)現(xiàn)LFU算法的示例代碼,需要的可以參考下
    2023-11-11
  • MyBatis中如何查詢某個(gè)時(shí)間段內(nèi)的數(shù)據(jù)

    MyBatis中如何查詢某個(gè)時(shí)間段內(nèi)的數(shù)據(jù)

    這篇文章主要介紹了MyBatis中如何查詢某個(gè)時(shí)間段內(nèi)的數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • IDEA設(shè)置JVM可分配內(nèi)存大小和其他參數(shù)的教程

    IDEA設(shè)置JVM可分配內(nèi)存大小和其他參數(shù)的教程

    這篇文章主要介紹了IDEA設(shè)置JVM可分配內(nèi)存大小和其他參數(shù)的教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • 詳解Java代碼常見(jiàn)優(yōu)化方案

    詳解Java代碼常見(jiàn)優(yōu)化方案

    這篇文章主要介紹了Java代碼常見(jiàn)優(yōu)化方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 詳述IntelliJ IDEA 中自動(dòng)生成 serialVersionUID 的方法(圖文)

    詳述IntelliJ IDEA 中自動(dòng)生成 serialVersionUID 的方法(圖文)

    本篇文章主要介紹了詳述IntelliJ IDEA 中自動(dòng)生成 serialVersionUID 的方法(圖文),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-11-11
  • SpringBoot+Mybatis plus+React實(shí)現(xiàn)條件選擇切換搜索實(shí)踐

    SpringBoot+Mybatis plus+React實(shí)現(xiàn)條件選擇切換搜索實(shí)踐

    本文主要介紹了SpringBoot+Mybatis plus+React實(shí)現(xiàn)條件選擇切換搜索實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Java異常處理方法匯總

    Java異常處理方法匯總

    這篇文章主要介紹了Java異常處理方法匯總,我們?cè)谲浖_(kāi)發(fā)的過(guò)程中,任何語(yǔ)言的開(kāi)發(fā)過(guò)程中都離不開(kāi)異常處理。下面下小編加來(lái)給大家分享各種異常處理,希望對(duì)大家有所幫助,需要的朋友可以參考一下
    2021-12-12
  • java實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器類實(shí)例

    java實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器類實(shí)例

    這篇文章主要介紹了java實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器類,涉及java針對(duì)鍵盤監(jiān)聽(tīng)及數(shù)字運(yùn)算的處理技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • Java字節(jié)流和字符流總結(jié)IO流!

    Java字節(jié)流和字符流總結(jié)IO流!

    下面小編就為大家?guī)?lái)一篇Java IO流字節(jié)流和字符流的實(shí)例講解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2021-07-07

最新評(píng)論