本篇內容介紹了“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;
@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);
//通過注解(靜態)和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
@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);//獲取一頁的數據
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怎么實現分頁加條件查詢功能”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。