最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

如何使用mybatis-plus實(shí)現(xiàn)分頁查詢功能

 更新時(shí)間:2022年06月21日 10:28:40   作者:GoLang.fmt  
最近在研究mybatis,然后就去找簡(jiǎn)化mybatis開發(fā)的工具,發(fā)現(xiàn)就有通用Mapper和mybatis-plus兩個(gè)比較好的可是使用,可是經(jīng)過對(duì)比發(fā)現(xiàn)還是mybatis-plus比較好,下面這篇文章主要給大家介紹了關(guān)于如何使用mybatis-plus實(shí)現(xiàn)分頁查詢功能的相關(guān)資料,需要的朋友可以參考下

今天就跟大家聊聊有關(guān)使用mybatis-plus如何實(shí)現(xiàn)分頁查詢功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

引入依賴:

<!-- 引入mybatisPlus -->
    <dependency>
   <groupId>com.baomidou</groupId>
   <artifactId>mybatis-plus-boot-starter</artifactId>
   <version>3.2.0</version>
  </dependency>
  <!-- 引入mysql驅(qū)動(dòng)包 -->
  <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&#63;useUnicode=true&characterEncoding=UTF-8
 username: root
 password: 123456

在啟動(dòng)類上面添加@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接口:里面聲明了很強(qiáng)大的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<&#63; 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<&#63; 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);
}

分頁查詢

這點(diǎn)官方文檔講的也很詳細(xì):https://mp.baomidou.com/guide/page.html

新建一個(gè)config包,在里面建一個(gè)MybatisPlus配置類 返回一個(gè)分頁攔截器

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測(cè)試

@Resource
 private UserMapper userMapper;
 @Test
 public void queryUserForPage(){
  IPage<User> userPage = new Page<>(2, 2);//參數(shù)一是當(dāng)前頁,參數(shù)二是每頁個(gè)數(shù)
  userPage = userMapper.selectPage(userPage, null);
  List<User> list = userPage.getRecords();
  for(User user : list){
   System.out.println(user);
  }
 }

Controller返回json串

先定義一個(gè)包裝類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層,實(shí)際開發(fā)業(yè)務(wù)代碼寫在service層,Controller只負(fù)責(zé):接受參數(shù)、調(diào)用service層方法處理業(yè)務(wù)邏輯,返回結(jié)果

Controller類貼上了@RestController注解

 @GetMapping("queryUser")
 public UserVo queryList(Integer current, Integer size) {
  /**
   * 這些代碼應(yīng)該寫在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;
 }

附上結(jié)果,前端直接處理json數(shù)據(jù)即可

看完上述內(nèi)容,你們對(duì)使用mybatis-plus如何實(shí)現(xiàn)分頁查詢功能有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注小編,感謝大家的支持。

總結(jié)

到此這篇關(guān)于如何使用mybatis-plus實(shí)現(xiàn)分頁查詢功能的文章就介紹到這了,更多相關(guān)mybatis-plus分頁查詢功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

洛南县| 扎囊县| 宜阳县| 泸西县| 长顺县| 当涂县| 富川| 镇康县| 安乡县| 山东| 章丘市| 牟定县| 视频| 瑞金市| 共和县| 黔西县| 万山特区| 南靖县| 阿拉尔市| 门头沟区| 久治县| 大厂| 图木舒克市| 中山市| 安化县| 射阳县| 金华市| 时尚| 房产| 辉县市| 大同市| 贡觉县| 梁平县| 三门峡市| 乌拉特中旗| 舞阳县| 河西区| 南雄市| 九寨沟县| 潢川县| 临猗县|