Get the App
SLTechnology News&Howtos  ›  Database  › 

Oracle uses RowBounds in MyBatis to realize paging query function.

Shulou Source: shulou.com Published: 2022-06-01 02:20:19 09月17日 Update

Paging query in Oracle is more complex to write because of the existence of pseudo-column rownum,sql statements. Now it is very convenient to introduce a paging query by using RowBounds in MyBatis.

When you use RowBounds in MyBatis for paging query, you do not need to write offset,limit,mybatis in the sql statement. Offset,limit,mybatis will automatically splice the paging sql and add offset,limit to achieve automatic paging.

You need to pass parameters currentPage and pageSize in the foreground, which are the current page and the number of pages, respectively. The controller layer can pass the parameters to the service layer. The following is the code for the service implementation:

Package com.xyfer.service.impl;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.ibatis.session.RowBounds;import com.xyfer.dao.UserDao;import com.xyfer.service.UserService;public class UserServiceImpl implements UserService {private UserDao userDao; @ Override public Map queryUserList (String currentPage, String pageSize) {/ / query total number of data int total = userDao.queryCountUser (); / / return result set Map resultMap = new HashMap () ResultMap.put ("total", total); / / Total pages int totalpage = (total + Integer.parseInt (pageSize)-1) / Integer.parseInt (pageSize); resultMap.put ("totalpage", totalpage); / / the starting line of the data int offset = (Integer.parseInt (currentPage)-1) * Integer.parseInt (pageSize); RowBounds rowbounds = new RowBounds (offset, Integer.parseInt (pageSize)) / / user data set List userList = userDao.queryUserList (rowbounds); resultMap.put ("userList", userList); return resultMap;}}

Dao layer interface:

Package com.xyfer.dao;import java.util.List;import java.util.Map;import org.apache.ibatis.session.RowBounds;public interface UserDao {public int queryCountUser (); / / query the total number of users public List queryUserList (RowBounds rowbounds); / / query user list}

Corresponding mapper.xml file:

Select count (1) from user select * from user

Paging query data can be achieved by passing in the corresponding parameters through the postman call API.

Summary

The above is the editor to introduce to you the Oracle uses RowBounds in MyBatis to achieve paging query function, hope to help you, if you have any questions, please leave me a message, the editor will reply you in time. Thank you very much for your support to the website!

If you think this article is helpful to you, you are welcome to reprint it, please indicate the source, thank you!

Tags: Queries parameters data users interfaces statements help functions query functions complexity two code provenance foreground total number files questions results website Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn OPPO Reno NVidia Huawei Shulou Tech Info