From 4121413a7fad23fb88476aedad00af00802e81bf Mon Sep 17 00:00:00 2001 From: bjdys Date: Thu, 12 Aug 2021 14:04:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=80=E5=AF=B9=E4=B8=80=20=E4=B8=80?= =?UTF-8?q?=E5=AF=B9=E5=A4=9A=20=E5=85=B3=E7=B3=BB=E6=98=A0=E5=B0=84?= =?UTF-8?q?=E6=B3=A8=E8=A7=A3=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MAPPING.md | 137 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 MAPPING.md diff --git a/MAPPING.md b/MAPPING.md new file mode 100644 index 0000000..d2dad1a --- /dev/null +++ b/MAPPING.md @@ -0,0 +1,137 @@ +# mybatis-plus-join + +* 本页功能只能在1.2.0测试版中使用,最新版本 1.2.0.Beta1 + +* 点个Star支持一下吧 :) + +QQ群:1022221898 + +## 使用方法 + +### 安装 + +- Maven + ```xml + + com.github.yulichang + mybatis-plus-join + 1.2.0.Beta1 + + ``` +- Gradle + ``` + implementation group: 'com.github.yulichang', name: 'mybatis-plus-join', version: '1.2.0.Beta1' + ``` + 或者clone代码到本地执行 mvn install, 再引入以上依赖 +
+ 注意: mybatis plus version >= 3.4.0 +
+ +### 使用 + +* mapper继承MPJBaseMapper (必选) +* service继承MPJBaseService (可选) +* serviceImpl继承MPJBaseServiceImpl (可选) + +#### @MPJMapping注解 + +UserDO.java + +```java + +@Data +@TableName("user") +public class UserDO { + + @TableId + private Integer id; + private Integer pid;//父id + /* 其他属性略 */ + + /** + * 一对一 + */ + @TableField(exist = false) + @MPJMapping(tag = UserDO.class, thisField = "pid") + private UserDO pUser; + + /** + * 一对多 + */ + @TableField(exist = false) + @MPJMapping(tag = UserAddressDO.class, joinField = "userId") + private List addressDOList; +} +``` + +UserAddressDO.java + +```java + +@Data +@TableName("user_address") +public class UserAddressDO { + + @TableId + private Integer id; + private Integer userId; + /* 其他属性略 */ +} +``` + +使用 + +```java + +@SpringBootTest +public class MPJDeepTest { + @Resource + private UserService userService; + + @Test + void test1() { + UserDO deep = userService.getByIdDeep(1); + System.out.println(deep); + } + + @Test + void test2() { + List list = userService.listByIdsDeep(Arrays.asList(1, 4)); + list.forEach(System.out::println); + } + + @Test + void test3() { + List list = userService.listByMapDeep(new HashMap() {{ + put("id", 1); + }}); + list.forEach(System.out::println); + } + + @Test + void test4() { + UserDO one = userService.getOneDeep(Wrappers.lambdaQuery() + .eq(UserDO::getId, 1)); + System.out.println(one); + } + + @Test + void test5() { + Map deep = userService.getMapDeep(Wrappers.lambdaQuery() + .eq(UserDO::getId, 1)); + System.out.println(deep); + } +} +``` + +MPJMapping 说明: + +* MPJMapping tag 关联实体类 +* MPJMapping thisField 当前类关联对应的字段的属性名,可以不填,默认为当前类的主键 +* MPJMapping joinField 关联类对应的字段的属性名,可以不填,默认为关联类的主键 +* MPJMapping isThrowExp 一对一查询时,如果查询到多条记录是否抛出异常,true:抛出异常,false:获取列表第一条数据 +* 更多功能请看代码注释 [MPJMapping](https://gitee.com/best_handsome/mybatis-plus-join/blob/master/src/main/java/com/github/yulichang/annotation/MPJMapping.java) + + + +