mybatis-plus分页查询的实现示例
程序员文章站
2022-06-19 12:46:14
按照官方文档进行的配置:引入依赖: com.baomidou<...
按照官方文档进行的配置:
引入依赖:
<!-- 引入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依赖,阿里巴巴所提供的数据源 --> <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); }
分页查询
这点官方文档讲的也很详细:
新建一个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);//参数一是当前页,参数二是每页个数 userpage = usermapper.selectpage(userpage, null); list<user> list = userpage.getrecords(); for(user user : list){ system.out.println(user); } }
controller返回json串
先定义一个包装类uservo,用来保存分页所需要的数据
package com.qiao.demo02.vo; @data public class uservo { private integer current; private integer size; private long total; private list<user> userlist; }
然后在控制器编写代码,这里省略了service层,实际开发业务代码写在service层,controller只负责:接受参数、调用service层方法处理业务逻辑,返回结果
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数据即可
到此这篇关于mybatis-plus分页查询的实现示例的文章就介绍到这了,更多相关mybatis-plus 分页查询 内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!