hbase分页查询实现_MyBatis Plus 的多表联接、分页查询实现方法

本文介绍了如何使用MyBatis Plus在HBase中进行分页和多表关联查询。通过示例展示了数据库表设计、代码结构,包括mapper、service、serviceImpl和controller的实现,并提供了测试接口的json输出和sql执行情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

f022dd5adeaad8e781c135051e67c20c.png

一、引入

之前做一个学生教师学习交流平台,DAO 层使用了 MyBatisPlus,里边有一个类似百度贴吧的发帖子的功能: 1bbe9714b6661b2bf95adb8ab51ec1c2.png而如果设计表,应为

  • 帖子表 t_post

    • id

    • title 标题

    • content 内容

    • user_id 用户外键

    • xx ...

  • 用户表 t__user

    • id

    • name 帖子发起者名字

    • xx ...

示例图中红色框中的内容为 t_user 表的字段 name, 而要实现上面显示帖子,就要用到关联查询了,而且帖子很多,必须用分页查询,

那么,怎么通过 MyBatisPlus 来实现关联、分页查询呢 ?很简单,往下看。

二、需求、数据库表设计

学生教师学习交流平台项目中的两张关联表

272d6b03f74d76e3ed2ee8a1310d3f81.png

需求:显示帖子

  • 要帖子基本内容如时间、帖子内容等,即 t_question 表的内容全部要,

  • 同时还要发帖子的人名字,即 t_student 的字段 name

三、代码结构

为了写这篇文章,抽取了该 app 项目中的部分代码,彼此相互关系如下图

6058a40f60d88bdf2ac8775f89d1ce62.png

四、代码实现

代码已经放到 github 上了,若对本文的代码有疑问可以去 github 上查看详情:

https://github.com/larger5/MyBatisPlus_page_tables.git

1.实体

① Question

// import 省略

@TableName("t_question")

public class Question implements Serializable {

   private static final long serialVersionUID = 1L;

   @ApiModelProperty(value = "问答主键id")

   @TableId(value = "id", type = IdType.AUTO)

   private Integer id;

   @ApiModelProperty(value = "学生外键id")

   @TableField("student_id")

   private Integer studentId;

   @ApiModelProperty(value = "问题内容")

   private String content;

   @ApiModelProperty(value = "问题发布时间,发布的时候后台自动生成")

   private Date date;

   @ApiModelProperty(value = "问题悬赏的积分")

   private Integer value;

   // getter、setter 省略

}

② Student

// import 省略

@TableName("t_student")

public class Student implements Serializable {

   private static final long serialVersionUID = 1L;

   @ApiModelProperty(value = "学生主键id")

   @TableId(value = "id", type = IdType.AUTO)

   private Integer id;

   @ApiModelProperty(value = "学生名称")

   private String name;

   @ApiModelProperty(value = "学生密码")

   private String password;

   @ApiModelProperty(value = "学生积分数")

   private Integer points;

   @ApiModelProperty(value = "学生邮件地址")

   private String email;

   @ApiModelProperty(value = "学生手机号码")

   private String phone;

   @ApiModelProperty(value = "学生学号")

   private String num;

   @ApiModelProperty(value = "学生真实姓名")

   @TableField("true_name")

   private String trueName;

   // getter、setter 省略

}

2.mapper

① StudentMapper

// import 省略

public interface StudentMapper extends BaseMapper<Student> {

}

② QuestionMapper

// import 省略

public interface QuestionMapper extends BaseMapper<Question> {

   /**

    *

    * @param page 翻页对象,可以作为 xml 参数直接使用,传递参数 Page 即自动分页

    * @return

    */

   @Select("SELECT t_question.*,t_student.`name` FROM t_question,t_student WHERE t_question.student_id=t_student.id")

   List<QuestionStudentVO> getQuestionStudent(Pagination page);

}

3、service

① StudentService

// import 省略

public interface StudentService extends IService<Student> {

}

② QuestionService

// import 省略

public interface QuestionService extends IService<Question> {

   Page<QuestionStudentVO> getQuestionStudent(Page<QuestionStudentVO> page);

}

4、serviceImpl

① StudentServiceImpl

// import 省略

@Service

public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

}

② QuestionServiceImpl

// 省略 import

@Service

public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {

   @Override

   public Page<QuestionStudentVO> getQuestionStudent(Page<QuestionStudentVO> page) {

       return page.setRecords(this.baseMapper.getQuestionStudent(page));

   }

}

5、controller

// 省略 import

@RestController

@RequestMapping("/common")

@EnableSwagger2

public class CommonController {

   @Autowired

   QuestionService questionService;

   @Autowired

   StudentService studentService;

   @GetMapping("/getAllQuestionByPage/{page}/{size}")

   public Map<String, Object> getAllQuestionByPage(@PathVariable Integer page, @PathVariable Integer size) {

       Map<String, Object> map = new HashMap<>();

       Page<Question> questionPage = questionService.selectPage(new Page<>(page, size));

       if (questionPage.getRecords().size() == 0) {

           map.put("code", 400);

       } else {

           map.put("code", 200);

           map.put("data", questionPage);

       }

       return map;

   }

   @GetMapping("/getAllQuestionWithStudentByPage/{page}/{size}")

   public Map<String, Object> getAllQuestionWithStudentByPage(@PathVariable Integer page, @PathVariable Integer size) {

       Map<String, Object> map = new HashMap<>();

       Page<QuestionStudentVO> questionStudent = questionService.getQuestionStudent(new Page<>(page, size));

       if (questionStudent.getRecords().size() == 0) {

           map.put("code", 400);

       } else {

           map.put("code", 200);

           map.put("data", questionStudent);

       }

       return map;

   }

}

6、MyBatisPlus 配置

// 省略 import

@EnableTransactionManagement

@Configuration

@MapperScan("com.cun.app.mapper")

public class MybatisPlusConfig {

   /**

    * 分页插件

    */

   @Bean

   public PaginationInterceptor paginationInterceptor() {

       return new PaginationInterceptor();

   }

   /**

    * 打印 sql

    */

   @Bean

   public PerformanceInterceptor performanceInterceptor() {

       PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();

       //格式化sql语句

       Properties properties = new Properties();

       properties.setProperty("format", "true");

       performanceInterceptor.setProperties(properties);

       return performanceInterceptor;

   }

}

7、关联查询 VO 对象

// import 省略

public class QuestionStudentVO implements Serializable {

   @ApiModelProperty(value = "问答主键id")

   @TableId(value = "id", type = IdType.AUTO)

   private Integer id;

   @ApiModelProperty(value = "学生外键id")

   @TableField("student_id")

   private Integer studentId;

   private String name;

   @ApiModelProperty(value = "问题内容")

   private String content;

   @ApiModelProperty(value = "问题发布时间,发布的时候后台自动生成")

   private Date date;

   @ApiModelProperty(value = "问题悬赏的积分")

   private Integer value;

   // getter、setter 省略

五、测试接口

a643cfbf833e71cff19fa5decedbf5ea.png

1、没有关联的分页查询接口

http://localhost/common/getAllQuestionByPage/1/2

① json 输出

{

 "code": 200,

 "data": {

   "total": 10,

   "size": 2,

   "current": 1,

   "records": [

     {

       "id": 1,

       "studentId": 3,

       "content": "唐代,渝州城里,有一个性格开朗、乐观的小伙子,名叫景天。",

       "date": 1534497561000,

       "value": 5

     },

     {

       "id": 2,

       "studentId": 1,

       "content": "雪见从小父母双亡,由爷爷唐坤抚养成人。",

       "date": 1533201716000,

       "value": 20

     }

   ],

   "pages": 5

 }

}

② sql 执行

dd91218e12831ca8e9d0765532afd586.png

2、多表关联、分页查询接口

http://localhost/common/getAllQuestionWithStudentByPage/1/2

① json 输出

{

 "code": 200,

 "data": {

   "total": 10,

   "size": 2,

   "current": 1,

   "records": [

     {

       "id": 1,

       "studentId": 3,

       "name": "vv",

       "content": "唐代,渝州城里,有一个性格开朗、乐观的小伙子,名叫景天。",

       "date": 1534497561000,

       "value": 5

     },

     {

       "id": 2,

       "studentId": 1,

       "name": "cun",

       "content": "雪见从小父母双亡,由爷爷唐坤抚养成人。",

       "date": 1533201716000,

       "value": 20

     }

   ],

   "pages": 5

 }

}

② sql 执行

ebc5d3f74615524048d917e638a5e686.png

六、其他

写本文的原因:

① MyBatisPulse 的参考资料较少

② 网上有做法不合时宜的文章(自定义page类、配置版)

③ 官方文档使用的是配置版的,笔者采用注解版的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值