How to realize the function of paging and conditional query by Springboot+Mybatis
This article introduces the relevant knowledge of "how to achieve paging conditional query function in Springboot+Mybatis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
User.xml
-- id here is the function name update user username=# {username}, nickname=# {nickname}, email=# {email}, phone=# {phone} Address=# {address} id = # {id} select * from user limit # {startIdx} # {size} select count (*) from user 1: 1 and username like concat ("%", # {username}, "%") and email like concat ("%", # {email} "%") and address like concat ("%", # {address}, "%")
UserMapper.java
Package com.shelbourne.schooldelivery.mapper; import com.shelbourne.schooldelivery.entity.User;import org.apache.ibatis.annotations.*; import java.util.List; @ Mapperpublic interface UserMapper {/ / query all users @ Select ("select * from user") / / mybatis to provide comments. Note that semicolon List findAll () cannot be added after the SQL statement. / / New user @ Insert ("insert into user (username,password,nickname,email,phone,address)" + "values (# {username}, # {password}, # {nickname}, # {email}, # {phone}, # {address})") public Integer insert (User user); / / write SQL statement int update (User user) through annotations (static) and xml (dynamic) / / delete a single user @ Delete ("delete from user where id=# {id}") Integer deleteById (@ Param ("id") Integer id); / / finally add the @ Param parameter, whose name is the same as that in # {} above / / the number of query records @ Select ("select count (*) from user") Integer selectTotal () / / write dynamic SQL to implement paging query + conditional query / / query a page of users who meet the criteria List selectPageWithParam (Integer startIdx, Integer size, String username, String email, String address); / / query the number of all users who meet the criteria 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") / / uniformly prefix the interface. Postman background interface localhost:9090/user@RestControllerpublic class UserController {@ Autowired / / injects annotations of other classes private UserMapper userMapper; @ Autowired private UserService userService; / / query all users @ GetMapping public List findAll (String username) {return userMapper.findAll () } / / add and update @ PostMapping public Integer save (@ RequestBody User user) {/ / add RequestBody through POST request. You can convert the JSON object returned from the frontend to Java object return userService.save (user). } / / Delete request interface @ DeleteMapping ("/ {id}") public Integer delete (@ PathVariable Integer id) {/ / the "id" here must be the same as the name in DeleteMapping return userMapper.deleteById (id) } @ GetMapping ("/ page") public Map findPage (@ RequestParam Integer pageNum, @ RequestParam Integer pageSize, @ RequestParam String username, @ RequestParam String email, @ RequestParam String address) {int startIdx = (pageNum-1) * pageSize, size = pageSize; List data = userMapper.selectPageWithParam (startIdx, size, username, email, address) / / get one page of data int total = userMapper.selectTotalWithParam (username, email, address); / / Total number of queries Map res = new HashMap (); res.put ("data", data); / / tabular data res.put ("total", total); / / use return res;}} for paging
In Home.vue:
Export default {data () {return {total: 0 pageNum / number of records is 0 pageNum: 1 pageSize: from the first record by default pageSize: 10 minus / default paging size is 10 username: ", / / name of conditional query email:" / / email address of conditional query address: "", / / address of conditional query}}, created () {/ / data refresh after page rendering is completed this.flushData ()} Methods: {/ / get data 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) / / Cross-domain issues: front-end port 8080 Back-end port 9090, leading to cross-domain this.tableData = res.data this.total = res.total})} "how to implement paging conditional query function in Springboot+Mybatis" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!