Springboot+Mybatis實現(xiàn)分頁加條件查詢功能
更新時間:2022年04月12日 09:36:20 作者:kisushotto
這篇文章主要為大家詳細介紹了Springboot+Mybatis實現(xiàn)分頁加條件查詢,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Springboot+Mybatis實現(xiàn)分頁加條件查詢的具體代碼,供大家參考,具體內(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>
?
<!-- ? ?分頁+條件查詢-->
? ? <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實現(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請求進行新增和更新操作
? ? @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);//獲取一頁的數(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,//默認從第一條記錄開始
? ? ? ? ? ? ? ? pageSize: 10,//默認分頁大小為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,導致跨域
? ? ? ? ? ? ? ? ? ? this.tableData = res.data
? ? ? ? ? ? ? ? ? ? this.total = res.total
? ? ? ? ? ? ? ? })
? ? ? ? ? ? }
? ? ? ? }
? ? }
</script>以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
java中synchronized(同步代碼塊和同步方法)詳解及區(qū)別
這篇文章主要介紹了 java中synchronized(同步代碼塊和同步方法)詳解及區(qū)別的相關資料,需要的朋友可以參考下2017-02-02
在 Spring Boot 中實現(xiàn)異常處理最佳實踐
本文介紹如何在Spring Boot中實現(xiàn)異常處理,涵蓋核心概念、實現(xiàn)方法、與先前查詢的集成、性能分析、常見問題和最佳實踐,感興趣的朋友一起看看吧2025-04-04
Java的JDBC編程使用之連接Mysql數(shù)據(jù)庫
這篇文章主要給大家介紹了關于Java的JDBC編程使用之連接Mysql數(shù)據(jù)庫的相關資料,JDBC是一種用于執(zhí)行SQL語句的Java?API,可以為多種關系數(shù)據(jù)庫提供統(tǒng)一訪問,需要的朋友可以參考下2023-12-12
SpringBoot+Shiro+Redis+Mybatis-plus 實戰(zhàn)項目及問題小結
最近也是一直在保持學習課外拓展技術,所以想自己做一個簡單小項目,于是就有了這個快速上手 Shiro 和 Redis 的小項目,說白了就是拿來練手調調 API,然后做完后拿來總結的小項目,感興趣的朋友一起看看吧2021-04-04

