mybatis-plus分頁查詢的實現(xiàn)示例
按照官方文檔進行的配置:快速開始|mybatis-plus
引入依賴:
<!-- 引入mybatisPlus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <!-- 引入mysql驅動包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.27</version> </dependency> <!-- 引入Druid依賴,阿里巴巴所提供的數(shù)據(jù)源 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.29</version> </dependency>
在application.yml配置
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8 username: root password: 123456
在啟動類上面添加@MapperScan注解,掃描mapper包
@SpringBootApplication @MapperScan("com.qiao.demo02.mapper") public class SpringbootDemo02Application { public static void main(String[] args) { SpringApplication.run(SpringbootDemo02Application.class, args); } }
新建User和UserMapper
user類
@Data public class User { @TableId private Integer userId; private String userName; private Integer userAge; private String userEmail; }
UserMapper接口
public interface UserMapper extends BaseMapper<User> { }
最重要的是繼承BaseMapper<E>接口:里面聲明了很強大的CRUD方法
public interface BaseMapper<T> extends Mapper<T> { int insert(T entity); int deleteById(Serializable id); int deleteByMap(@Param("cm") Map<String, Object> columnMap); int delete(@Param("ew") Wrapper<T> wrapper); int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList); int updateById(@Param("et") T entity); int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper); T selectById(Serializable id); List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList); List<T> selectByMap(@Param("cm") Map<String, Object> columnMap); T selectOne(@Param("ew") Wrapper<T> queryWrapper); Integer selectCount(@Param("ew") Wrapper<T> queryWrapper); List<T> selectList(@Param("ew") Wrapper<T> queryWrapper); List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper); List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper); IPage<T> selectPage(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper); IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper); }
分頁查詢
這點官方文檔講的也很詳細:https://mp.baomidou.com/guide/page.html
新建一個config包,在里面建一個MybatisPlus配置類 返回一個分頁攔截器
package com.qiao.demo02.config; @Configuration @ConditionalOnClass(value = {PaginationInterceptor.class}) public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); return paginationInterceptor; } }
這樣就能使用mybatis的分頁功能了
Junit測試
@Resource private UserMapper userMapper; @Test public void queryUserForPage(){ IPage<User> userPage = new Page<>(2, 2);//參數(shù)一是當前頁,參數(shù)二是每頁個數(shù) userPage = userMapper.selectPage(userPage, null); List<User> list = userPage.getRecords(); for(User user : list){ System.out.println(user); } }
Controller返回json串
先定義一個包裝類UserVo,用來保存分頁所需要的數(shù)據(jù)
package com.qiao.demo02.vo; @Data public class UserVo { private Integer current; private Integer size; private Long total; private List<User> userList; }
然后在控制器編寫代碼,這里省略了service層,實際開發(fā)業(yè)務代碼寫在service層,Controller只負責:接受參數(shù)、調用service層方法處理業(yè)務邏輯,返回結果
Controller類貼上了@RestController注解
@GetMapping("queryUser") public UserVo queryList(Integer current, Integer size) { /** * 這些代碼應該寫在service層 */ UserVo userVo = new UserVo(); IPage<User> page = new Page<>(current, size); userMapper.selectPage(page, null); userVo.setCurrent(current); userVo.setSize(size); userVo.setTotal(page.getTotal()); userVo.setUserList(page.getRecords()); return userVo; }
附上結果,前端直接處理json數(shù)據(jù)即可
到此這篇關于mybatis-plus分頁查詢的實現(xiàn)示例的文章就介紹到這了,更多相關mybatis-plus 分頁查詢 內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Mybatis-Plus 多表聯(lián)查分頁的實現(xiàn)代碼
- Mybatis-plus新版本分頁失效PaginationInterceptor過時的問題
- MyBatis-Plus 分頁查詢以及自定義sql分頁的實現(xiàn)
- mybatis-plus分頁傳入?yún)?shù)后sql where條件沒有l(wèi)imit分頁信息操作
- MyBatis-Plus分頁插件不生效的解決方法
- MyBatis-Plus實現(xiàn)2種分頁方法(QueryWrapper查詢分頁和SQL查詢分頁)
- MyBatis-Plus分頁時排序的實現(xiàn)方法
- 解決mybatis-plus3.4.1分頁插件PaginationInterceptor和防止全表更新與刪除插件SqlExplainInterceptor過時失效問題
- Mybatis-Plus中分頁插件PaginationInterceptor的使用
- 一文搞懂Mybatis-plus的分頁查詢操作
- MyBatis-Plus 分頁插件配置的兩種方式實現(xiàn)
相關文章
Java實現(xiàn)數(shù)據(jù)庫圖片上傳功能詳解
這篇文章主要為大家詳細介紹了如何使用Java實現(xiàn)數(shù)據(jù)庫圖片上傳功能,包含從數(shù)據(jù)庫拿圖片傳遞前端渲染,感興趣的小伙伴可以跟隨小編一起學習一下2025-03-03