广告
返回顶部
首页 > 资讯 > 精选 >如何在springboot中使用mybatis-plus实现一个多表分页查询功能
  • 535
分享到

如何在springboot中使用mybatis-plus实现一个多表分页查询功能

2023-06-07 23:06:39 535人浏览 泡泡鱼
摘要

这篇文章将为大家详细讲解有关如何在SpringBoot中使用mybatis-plus实现一个多表分页查询功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。新建一个springboot工程需要

这篇文章将为大家详细讲解有关如何在SpringBoot中使用mybatis-plus实现一个多表分页查询功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

新建一个springboot工程

需要导入mybatis和mybatis-plus的依赖文件

<dependency>      <groupId>com.baomidou</groupId>      <artifactId>mybatis-plus-boot-starter</artifactId>      <version>3.1.1</version>    </dependency>     <dependency>      <groupId>org.mybatis.spring.boot</groupId>      <artifactId>mybatis-spring-boot-starter</artifactId>      <version>2.0.1</version>    </dependency>

application.yml配置文件

server: port: 8080spring: datasource:  url: jdbc:mysql://localhost:3306/mybatisplus?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=UTC  username: root  passWord数据库密码mybatis: mapper-locations: classpath*:mapper  @Bean  public PaginationInterceptor paginationInterceptor(){    return new PaginationInterceptor();  }}

controller类

@RestController@RequestMapping("/user")public class UserController {  @Autowired  UserService userService;    @RequestMapping("/findAll")  public Result<IPage<User>> findAll(@RequestBody Page<User> page){     return userService.pages(page);  }    @RequestMapping("/selectAll")  public Result<IPage<User>> selectAll(@RequestBody Page<User> page){    return userService.pageList(page);  }}

service类

public interface UserService extends IService<User> {  Result<IPage<User>> pages(Page<User> page);  Result<IPage<User>> pageList(Page<User> page);}

service实现类

@Servicepublic class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {  @Autowired  UserMapper userMapper;  @Override  public Result<IPage<User>> pages(Page<User> page) {    IPage<User> userIPage = userMapper.Pages(page);    return Result.getSuccess("分页查询成功",userIPage);  }  @Override  public Result<IPage<User>> pageList(Page<User> page) {    IPage<User> userIPage = userMapper.pageList(page);    return Result.getSuccess("分页查询成功",userIPage);  }}

mapper接口

注意!!: 如果入参是有多个,需要加注解指定参数名才能在xml中取值

@Mapper@Repositorypublic interface UserMapper extends BaseMapper<User> {  IPage<User> Pages(@Param("page") Page<User> page);  IPage<User> pageList(@Param("page") Page<User> page);}

xml文件

一对一关联

 <!-- 一对一 通用查询映射结果 -->  <resultMap id="BaseResultMap1" type="com.tuanzi.user.entity.User">    <result column="id" property="id" />    <result column="name" property="name" />    <result column="age" property="age" />    <result column="email" property="email" />    <!--assocication  一对一关联查询        可以指定联合的JavaBean对象        property="work"指定哪个属性是联合的对象        javaType:指定这个属性对象的类型      -->    <association property="work" javaType="com.tuanzi.user.entity.Work">      <result column="id" property="id" />      <result column="position" property="position" />      <result column="user_id" property="userId" />    </association>  </resultMap>

一对多关联

<!-- 一对多 通用查询映射结果 -->  <resultMap id="BaseResultMap2" type="com.tuanzi.user.entity.User">    <result column="id" property="id" />    <result column="name" property="name" />    <result column="age" property="age" />    <result column="email" property="email" />    <!--collection定义关联结合类型的属性的封装规则property="workList"指定哪个属性是联合的对象ofType:指定集合里面元素的类型-->    <collection property="workList" ofType="com.tuanzi.user.entity.Work">      <result column="id" property="id" />      <result column="position" property="position" />      <result column="user_id" property="userId" />    </collection>  </resultMap>

sql语句:

<select id="Pages" resultMap="BaseResultMap1">    select a.id id,a.name name,a.age age,a.email email,b.position position,b.user_id user_id from user a LEFT JOIN work b on a.id=b.user_id  </select>  <select id="pageList" resultMap="BaseResultMap2">    select a.id id,a.name name,a.age age,a.email email,b.position position,b.user_id user_id from user a LEFT JOIN work b on a.id=b.user_id  </select>

这样就基本完成了!我这里省略了实体类

我们运行一下,用postman测试一下结果
这里我们需要传2个参数,当然我们也可以不用传,因为mybatis-plus有默认值
来看下mybatis-plus的page源码

如何在springboot中使用mybatis-plus实现一个多表分页查询功能

效果图:

如何在springboot中使用mybatis-plus实现一个多表分页查询功能

如何在springboot中使用mybatis-plus实现一个多表分页查询功能

关于如何在springboot中使用mybatis-plus实现一个多表分页查询功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

--结束END--

本文标题: 如何在springboot中使用mybatis-plus实现一个多表分页查询功能

本文链接: https://www.lsjlt.com/news/250426.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作