Springboot+Mybatis-Plus 用注解方式实现分页多表条件查询

在写项目时遇到需要多表查询并且由于数据量大多表查询的结果还需要分页,并且找了一下网上的资料发现很少有用注解实现的,大多采用了xml自己写select,通过查资料和自己尝试发现使用注解也可以很方便的实现。
需求:在按店铺id查询对应店铺的 评论(由对应用户发起的评论)列表时需要关联查询 用户表对应信息,并且要求评论列表能够分页查询。
  1. 项目文件结构:

  1. 新建Config、导入配置类

package com.xy.mastergoose.Config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration //使用注解@Configuration,告诉Spring Boot这是一个配置类。
public class MyBatisPlusConfig {
    //分页插件基础配置
    @Bean //添加的bean的id为方法名,等同于在xml文件里进行配置
    public MybatisPlusInterceptor paginationInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInterceptor = new PaginationInnerInterceptor(DbType.MYSQL); //此处修改为自己使用的数据库
        interceptor.addInnerInterceptor(paginationInterceptor);
        return interceptor;
    }
}
  1. 实体类数据字段:

comment:
    @TableName("t_comment")
    public class Comment {
        @TableId(type = IdType.AUTO)
        private Integer id; //评价ID(评价唯一标识)
        private String serviceStar; //服务星级(1-5)
        private String speedStar; //速度星级(1-5)
        private String productStar; //好吃水准星级(1-5)
        private Date commentTime; //评价时间
        private String commentContent; //评价内容
        private Integer userId; //用户ID(外键用于关联用户表)
        private Integer storeId; //店铺ID(外键用于关联店铺表)
    
        @TableField(exist = false) //表示阻止Spring对该属性和数据库表字段映射
        private User user;
        
        ...此处省略构造方法以及getter、setter
    }

user:
    @TableName("t_user")
    public class User {
        @TableId(type = IdType.AUTO)
        private Integer id; //用户ID(用户唯一标识)
        private String username; //用户名
        private String userPassword; //用户密码
        private Integer status; //用户身份(1、超级管理员 2、普通管理员 3、普通用户)
        private String power; //用户权限(目前未想好如何实现)
        private String phone; //用户手机号
        private String email; //用户邮箱
        private String wxCode; //微信唯一标识(用于绑定微信)
        private Integer gender; //性别(0、女 1、男)
        private String birthday; //生日
        private String headImg; //头像
    
        @TableField(exist = false) //表示阻止Spring对该属性和数据库表字段映射
        private String loginMsg; //登录返回信息

       ...此处省略构造方法以及getter、setter
    }       
  1. Contrller层实现:

    //按商铺id查询该商户的评论列表,参数storeId(商铺id)、currentPage(当前页码),一页五条、size(一页多少数据)
    @GetMapping("/commentList")
    @ApiOperation("按商铺id分页查询该商户的评论列表,参数storeId(商铺id)、currentPage(当前页码),一页五条、size(一页多少数据)")
    public IPage commentListByStoreId(int storeId,int currentPage,int size){
        Page<Comment> commentListPage = new Page<>(currentPage, size);//page对象
        IPage iPage = commentService.commentListByStoreId(commentListPage,storeId);
        return iPage;
    }
  1. Service层实现:

    //按商铺id查询该商户的评论列表
    public Page<Comment> commentListByStoreId (Page<Comment> commentListPage, int storeId) {
        return commentMapper.commentListByStoreId(commentListPage,storeId);
    }
  1. Mapper注解实现:

    //按商铺id查询该商户的评论列表
    @Select("select * from t_comment where store_id = #{storeId}")
    @Results(
            {
                    @Result(column = "id",property = "id"),
                    @Result(column = "service_star",property = "serviceStar"),
                    @Result(column = "speeds_star",property = "speedStar"),
                    @Result(column = "product_star",property = "productStar"),
                    @Result(column = "service_star",property = "serviceStar"),
                    @Result(column = "comment_time",property = "commentTime"),
                    @Result(column = "comment_content",property = "commentContent"),
                    @Result(column = "user_id",property = "userId"),
                    @Result(column = "store_id",property = "storeId"),
                    @Result(column = "user_id",property = "user",javaType = User.class,
                            one=@One(select = "com.xy.mastergoose.Mapper.UserMapper.selectById") //关联查询一条时使用@One、多条使用@Many
                    ),
            }
    )
    public Page<Comment> commentListByStoreId(Page<Comment> commentListPage,int storeId);
  1. 查询结果:

{
  "records": [
    {
      "id": 1,
      "serviceStar": "5",
      "speedStar": null,
      "productStar": "5",
      "commentTime": "2023-02-02T03:04:07.000+00:00",
      "commentContent": "真的好吃",
      "userId": 2,
      "storeId": 1,
      "user": {
        "id": 2,
        "username": "yy",
        "userPassword": "123",
        "status": 3,
        "power": "",
        "phone": "15083970929",
        "email": null,
        "wxCode": "",
        "gender": 1,
        "birthday": null,
        "headImg": "https://c28526441v.zicp.fun/u_15083970929vfbbu5dzbd1675862438954.png",
        "loginMsg": null
      }
    },
    {
      "id": 2,
      "serviceStar": "4",
      "speedStar": null,
      "productStar": "5",
      "commentTime": "2023-02-03T03:47:21.000+00:00",
      "commentContent": "可以可以",
      "userId": 2,
      "storeId": 1,
      "user": {
        "id": 2,
        "username": "yy",
        "userPassword": "123",
        "status": 3,
        "power": "",
        "phone": "15083970929",
        "email": null,
        "wxCode": "",
        "gender": 1,
        "birthday": null,
        "headImg": "https://c28526441v.zicp.fun/u_15083970929vfbbu5dzbd1675862438954.png",
        "loginMsg": null
      }
    }
  ],
  "total": 4,
  "size": 2,
  "current": 1,
  "orders": [],
  "optimizeCountSql": true,
  "hitCount": false,
  "countId": null,
  "maxLimit": null,
  "searchCount": true,
  "pages": 2
}
总结:感觉使用注解比自己写sql方便!
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Xxxy _

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值