找回密码
 立即注册
首页 业界区 业界 最强ORM让你开发效率提升百倍

最强ORM让你开发效率提升百倍

吮槌圯 2025-9-26 11:54:52
最强ORM让你开发效率提升百倍

easy-query在经过2年的迭代目前已经在查询领域可以说是无敌的存在,任何orm都不是对手,这几年的功能点简单罗列一下
[x] 动态join:查询涉及到对应的关系表就会自动添加join反之则不会讲join加入到sql中(2025年了感觉也不是什么新鲜特性了)
[x] 结构化DTO,自动根据DTO和表关系的路径自动筛选出需要的结构化属性(开发效率杀手)
[x] 结构化DTO额外配置,支持结构化DTO的返回下还能额外添加查询条件和筛选条件
[x] 隐式PARTITION BY
[X] 隐式子查询
[x] 子查询转GroupJoin全世界应该是独一份的功能,解决多对多深层关系在ORM中的子查询过多导致的性能问题真正解决了ORM在复杂查询下开发效率和性能的兼顾
框架地址 https://github.com/dromara/easy-query
文档地址 https://www.easy-query.com/easy-query-doc/
该文章demo地址 https://github.com/xuejmnet/eq-doc
刚好前几天我看到公众号有篇关于efcore的性能文章,我看了其实我一眼就知道了他的问题就是eq的子查询转GroupJoin但是正如强大的efcore也是没有实现该功能,话不多说本章节我们将入门通过公众号的demo实现大部分帖子相关的查询和功能
建模

实体关系如下:

  • 用户User:每个用户有多篇帖子和多条评论和多个点赞
  • 分类Category:帖子所属分类类目支持多个分类一个帖子或者多个帖子公用同一个分类
  • 帖子Post:每篇帖子有多个分类并可获得多个赞
  • 评论Comment:每条评论属于一个用户并关联一篇帖子 且评论支持楼中楼
  • 点赞Like:每个赞关联一篇帖子,多个点赞可以关联同一篇帖子
  • 分类帖子关联CategoryPost:帖子和分类的关联关系表
1.png

点击查看实体代码
  1. @Data
  2. @Table("t_user")
  3. @EntityProxy
  4. @EasyAlias("t_user")
  5. @EasyAssertMessage("未找到对应的用户信息")
  6. public class User implements ProxyEntityAvailable<User , UserProxy> {
  7.     @Column(primaryKey = true,comment = "用户id")
  8.     private String id;
  9.     @Column(comment = "用户姓名")
  10.     private String name;
  11.     @Column(comment = "用户手机")
  12.     private String phone;
  13.     @Column(comment = "创建时间")
  14.     private LocalDateTime createAt;
  15. }
  16. @Data
  17. @Table("t_category")
  18. @EntityProxy
  19. @EasyAlias("t_category")
  20. @EasyAssertMessage("未找到对应的类目信息")
  21. public class Category implements ProxyEntityAvailable<Category , CategoryProxy> {
  22.     @Column(primaryKey = true,comment = "类目id")
  23.     private String id;
  24.     @Column(comment = "类目姓名")
  25.     private String name;
  26.     @Column(comment = "类目排序")
  27.     private Integer sort;
  28. }
  29. @Data
  30. @Table("t_post")
  31. @EntityProxy
  32. @EasyAlias("t_post")
  33. @EasyAssertMessage("未找到对应的帖子信息")
  34. public class Post implements ProxyEntityAvailable<Post, PostProxy> {
  35.     @Column(primaryKey = true,comment = "帖子id")
  36.     private String id;
  37.     @Column(comment = "帖子标题")
  38.     private String title;
  39.     @Column(comment = "帖子内容")
  40.     private String content;
  41.     @Column(comment = "用户id")
  42.     private String userId;
  43.     @Column(comment = "发布时间")
  44.     private LocalDateTime publishAt;
  45. }
  46. @Data
  47. @Table("t_comment")
  48. @EntityProxy
  49. @EasyAlias("t_comment")
  50. @EasyAssertMessage("未找到对应的评论信息")
  51. public class Comment implements ProxyEntityAvailable<Comment , CommentProxy> {
  52.     @Column(primaryKey = true,comment = "评论id")
  53.     private String id;
  54.     @Column(comment = "父id")
  55.     private String parentId;
  56.     @Column(comment = "帖子内容")
  57.     private String content;
  58.     @Column(comment = "用户id",nullable = false)
  59.     private String userId;
  60.     @Column(comment = "帖子id",nullable = false)
  61.     private String postId;
  62.     @Column(comment = "回复时间")
  63.     private LocalDateTime createAt;
  64. }
  65. @Data
  66. @Table("t_like")
  67. @EntityProxy
  68. @EasyAlias("t_like")
  69. @EasyAssertMessage("未找到对应的点赞信息")
  70. public class Like implements ProxyEntityAvailable<Like , LikeProxy> {
  71.     @Column(primaryKey = true,comment = "评论id")
  72.     private String id;
  73.     @Column(comment = "用户id",nullable = false)
  74.     private String userId;
  75.     @Column(comment = "帖子id",nullable = false)
  76.     private String postId;
  77.     @Column(comment = "点赞时间")
  78.     private LocalDateTime createAt;
  79. }
  80. @Data
  81. @Table("t_category_post")
  82. @EntityProxy
  83. @EasyAlias("t_category_post")
  84. @EasyAssertMessage("未找到对应的类目帖子关联信息")
  85. public class CategoryPost implements ProxyEntityAvailable<CategoryPost , CategoryPostProxy> {
  86.     @Column(primaryKey = true,comment = "评论id")
  87.     private String id;
  88.     @Column(comment = "帖子id",nullable = false)
  89.     private String postId;
  90.     @Column(comment = "类目id",nullable = false)
  91.     private String categoryId;
  92. }
复制代码
帖子相关查询

帖子分页

对Post表进行分页按publishAt倒序进行排序按title进行搜索
首先我们定一个公用类
  1. @Data
  2. public class PageRequest {
  3.     private Integer pageIndex=1;
  4.     private Integer pageSize=5;
  5. }
复制代码
定义请求参数
  1. @Data
  2. public class PostPageRequest extends PageRequest {
  3.     private String title;
  4. }
复制代码
分页动态条件
  1.     @PostMapping("/page")
  2.     public EasyPageResult<Post> page(@RequestBody PostPageRequest request) {
  3.         return easyEntityQuery.queryable(Post.class)
  4.                 .where(t_post -> {
  5. //                    if(EasyStringUtil.isNotBlank(request.getTitle())){
  6. //                        t_post.title().contains(request.getTitle());
  7. //                    }
  8.                     t_post.title().contains(EasyStringUtil.isNotBlank(request.getTitle()),request.getTitle());
  9.                 })
  10.                 .orderBy(t_post -> t_post.publishAt().desc())
  11.                 .toPageResult(request.getPageIndex(),request.getPageSize());
  12.     }
复制代码
这边提供了两种方式实现动态查询,当title不为空的时候加入表达式筛选,执行我们来看看实际情况

  • 使用if函数包裹表达式断言,支持任意java表达式
  • 使用断言函数第一个参数重载,默认第一个参数为true才会执行断言操作
  • 使用where重载第一个参数为true执行当前where
请求参数
  1. {"pageIndex":1,"pageSize":5,"title":"电影"}
复制代码
  1. ==> Preparing: SELECT COUNT(*) FROM `t_post` WHERE `title` LIKE CONCAT('%',?,'%')
  2. ==> Parameters: 电影(String)
  3. ==> Preparing: SELECT `id`,`title`,`content`,`user_id`,`publish_at` FROM `t_post` WHERE `title` LIKE CONCAT('%',?,'%') ORDER BY `publish_at` DESC LIMIT 3
  4. ==> Parameters: 电影(String)
复制代码
container还是like!!!
> 细心地朋友会发现我们使用了contains函数而不是like函数,因为当传入的查询条件本身带有%时那么like会让%变成通配符,而contains会将%视为被查询的一部分,这是需要用户注意的,具体使用contains还是like应该有用户自行决断

推荐写法
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

3 天前

举报

喜欢鼓捣这些软件,现在用得少,谢谢分享!
您需要登录后才可以回帖 登录 | 立即注册