Springboot+Mybatis實(shí)現(xiàn)分頁(yè)加條件查詢功能
本文實(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ò)程及步驟詳解,本文通圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07MyBatis中如何查詢某個(gè)時(shí)間段內(nèi)的數(shù)據(jù)
這篇文章主要介紹了MyBatis中如何查詢某個(gè)時(shí)間段內(nèi)的數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08IDEA設(shè)置JVM可分配內(nèi)存大小和其他參數(shù)的教程
這篇文章主要介紹了IDEA設(shè)置JVM可分配內(nèi)存大小和其他參數(shù)的教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01詳述IntelliJ IDEA 中自動(dòng)生成 serialVersionUID 的方法(圖文)
本篇文章主要介紹了詳述IntelliJ IDEA 中自動(dòng)生成 serialVersionUID 的方法(圖文),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-11-11SpringBoot+Mybatis plus+React實(shí)現(xiàn)條件選擇切換搜索實(shí)踐
本文主要介紹了SpringBoot+Mybatis plus+React實(shí)現(xiàn)條件選擇切換搜索實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09java實(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