mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 01:28:24 +08:00
update docs
This commit is contained in:
parent
217b79b74a
commit
125335f544
@ -54,9 +54,37 @@ public class IDCard implements Serializable {
|
||||
|
||||
`@RelationOneToOne` 配置描述:
|
||||
|
||||
- selfField 当前实体类的属性
|
||||
- selfField 当前实体类的属性(selfField 是主键属性,且当前实体类对应的表只有 1 个主键时,可以不填写)
|
||||
- targetField 目标对象的关系实体类的属性
|
||||
|
||||
假设数据库 5 条 Account 数据,然后进行查询:
|
||||
|
||||
```java
|
||||
List<Account> accounts = accountMapper.selectAllWithRelations();
|
||||
System.out.println(accounts);
|
||||
```
|
||||
|
||||
其执行的 SQL 如下:
|
||||
|
||||
```sql
|
||||
SELECT `id`, `user_name`, `age` FROM `tb_account`
|
||||
|
||||
SELECT `account_id`, `card_no`, `content` FROM `tb_idcard`
|
||||
WHERE account_id IN (1, 2, 3, 4, 5)
|
||||
```
|
||||
|
||||
查询打印的结果如下:
|
||||
|
||||
```txt
|
||||
[
|
||||
Account{id=1, userName='孙悟空', age=18, idCard=IDCard{accountId=1, cardNo='0001', content='内容1'}},
|
||||
Account{id=2, userName='猪八戒', age=19, idCard=IDCard{accountId=2, cardNo='0002', content='内容2'}},
|
||||
Account{id=3, userName='沙和尚', age=19, idCard=IDCard{accountId=3, cardNo='0003', content='内容3'}},
|
||||
Account{id=4, userName='六耳猕猴', age=19, idCard=IDCard{accountId=4, cardNo='0004', content='内容4'}},
|
||||
Account{id=5, userName='王麻子叔叔', age=19, idCard=IDCard{accountId=5, cardNo='0005', content='内容5'}}
|
||||
]
|
||||
```
|
||||
|
||||
|
||||
## 一对多 `@RelationOneToMany`
|
||||
|
||||
@ -99,6 +127,23 @@ public class Book implements Serializable {
|
||||
- targetField 目标对象的关系实体类的属性
|
||||
|
||||
|
||||
假设数据库 5 条 Account 数据,然后进行查询:
|
||||
|
||||
```java
|
||||
List<Account> accounts = accountMapper.selectAllWithRelations();
|
||||
System.out.println(accounts);
|
||||
```
|
||||
|
||||
其执行的 SQL 如下:
|
||||
|
||||
```sql
|
||||
SELECT `id`, `user_name`, `age` FROM `tb_account`
|
||||
|
||||
SELECT `id`, `account_id`, `title`, `content` FROM `tb_book`
|
||||
WHERE account_id IN (1, 2, 3, 4, 5)
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 多对一 `@RelationManyToOne`
|
||||
|
||||
@ -208,6 +253,111 @@ public class Role implements Serializable {
|
||||
|
||||
> 注意:selfField 和 targetField 配置的是类的属性名,joinSelfColumn 和 joinTargetColumn 配置的是中间表的字段名。
|
||||
|
||||
## 父子关系查询
|
||||
|
||||
比如在一些系统中,比如菜单会有一些父子关系,例如菜单表如下:
|
||||
|
||||
```sql
|
||||
CREATE TABLE `tb_menu`
|
||||
(
|
||||
`id` INTEGER auto_increment,
|
||||
`parent_id` INTEGER,
|
||||
`name` VARCHAR(100)
|
||||
);
|
||||
```
|
||||
|
||||
Menu.java 定义如下:
|
||||
|
||||
```java
|
||||
@Table(value = "tb_menu")
|
||||
public class Menu implements Serializable {
|
||||
|
||||
private Long id;
|
||||
|
||||
private Long parentId;
|
||||
|
||||
private String name;
|
||||
|
||||
@RelationManyToOne(selfField = "parentId", targetField = "id")
|
||||
private Menu parent;
|
||||
|
||||
@RelationOneToMany(selfField = "id", targetField = "parentId")
|
||||
private List<Menu> children;
|
||||
|
||||
//getter setter
|
||||
}
|
||||
```
|
||||
|
||||
查询顶级菜单:
|
||||
|
||||
```java
|
||||
QueryWrapper qw = QueryWrapper.create();
|
||||
qw.where(MENU.PARENT_ID.eq(0));
|
||||
|
||||
List<Menu> menus = menuMapper.selectListWithRelationsByQuery(qw);
|
||||
System.out.println(JSON.toJSONString(menus));
|
||||
```
|
||||
|
||||
SQL 执行如下:
|
||||
|
||||
```sql
|
||||
SELECT `id`, `parent_id`, `name` FROM `tb_menu` WHERE `parent_id` = 0
|
||||
SELECT `id`, `parent_id`, `name` FROM `tb_menu` WHERE id = 0
|
||||
SELECT `id`, `parent_id`, `name` FROM `tb_menu` WHERE parent_id IN (1, 2, 3)
|
||||
```
|
||||
|
||||
JSON 输出内容如下:
|
||||
|
||||
```json
|
||||
[
|
||||
{
|
||||
"children": [
|
||||
{
|
||||
"id": 4,
|
||||
"name": "子菜单",
|
||||
"parentId": 1
|
||||
},
|
||||
{
|
||||
"id": 5,
|
||||
"name": "子菜单",
|
||||
"parentId": 1
|
||||
}
|
||||
],
|
||||
"id": 1,
|
||||
"name": "顶级菜单1",
|
||||
"parentId": 0
|
||||
},
|
||||
{
|
||||
"children": [],
|
||||
"id": 2,
|
||||
"name": "顶级菜单2",
|
||||
"parentId": 0
|
||||
},
|
||||
{
|
||||
"children": [
|
||||
{
|
||||
"id": 6,
|
||||
"name": "子菜单",
|
||||
"parentId": 3
|
||||
},
|
||||
{
|
||||
"id": 7,
|
||||
"name": "子菜单",
|
||||
"parentId": 3
|
||||
},
|
||||
{
|
||||
"id": 8,
|
||||
"name": "子菜单",
|
||||
"parentId": 3
|
||||
}
|
||||
],
|
||||
"id": 3,
|
||||
"name": "顶级菜单3",
|
||||
"parentId": 0
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
|
||||
|
||||
## Field Query
|
||||
|
||||
@ -31,17 +31,17 @@ public class Account implements Serializable {
|
||||
|
||||
private int age;
|
||||
|
||||
@RelationOneToOne(selfField = "id", targetField = "accountId")
|
||||
// @RelationOneToOne(selfField = "id", targetField = "accountId")
|
||||
private IDCard idCard;
|
||||
|
||||
@RelationOneToMany(selfField = "id", targetField = "accountId")
|
||||
private List<Book> books;
|
||||
|
||||
@RelationManyToMany(
|
||||
joinTable = "tb_role_mapping",
|
||||
selfField = "id", joinSelfColumn = "account_id",
|
||||
targetField = "id", joinTargetColumn = "role_id"
|
||||
)
|
||||
// @RelationManyToMany(
|
||||
// joinTable = "tb_role_mapping",
|
||||
// selfField = "id", joinSelfColumn = "account_id",
|
||||
// targetField = "id", joinTargetColumn = "role_id"
|
||||
// )
|
||||
private List<Role> roles;
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,15 @@
|
||||
package com.mybatisflex.test.relation.onetoone;
|
||||
|
||||
import com.mybatisflex.core.mybatis.FlexConfiguration;
|
||||
import com.mybatisflex.core.table.TableInfo;
|
||||
import com.mybatisflex.core.table.TableInfoFactory;
|
||||
|
||||
public class MainTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Book.class);
|
||||
tableInfo.buildResultMap(new FlexConfiguration());
|
||||
|
||||
System.out.println(tableInfo);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.mybatisflex.test.relation.onetoone;
|
||||
|
||||
import com.mybatisflex.annotation.RelationManyToOne;
|
||||
import com.mybatisflex.annotation.RelationOneToMany;
|
||||
import com.mybatisflex.annotation.Table;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
@Table(value = "tb_menu")
|
||||
public class Menu implements Serializable {
|
||||
|
||||
private Long id;
|
||||
|
||||
private Long parentId;
|
||||
|
||||
private String name;
|
||||
|
||||
@RelationManyToOne(selfField = "parentId",targetField = "id")
|
||||
private Menu parent;
|
||||
|
||||
@RelationOneToMany(selfField = "id",targetField = "parentId")
|
||||
private List<Menu> children;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getParentId() {
|
||||
return parentId;
|
||||
}
|
||||
|
||||
public void setParentId(Long parentId) {
|
||||
this.parentId = parentId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Menu getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public void setParent(Menu parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public List<Menu> getChildren() {
|
||||
return children;
|
||||
}
|
||||
|
||||
public void setChildren(List<Menu> children) {
|
||||
this.children = children;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Menu{" +
|
||||
"id=" + id +
|
||||
", parentId=" + parentId +
|
||||
", name='" + name + '\'' +
|
||||
", parent=" + parent +
|
||||
", children=" + children +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@ -15,13 +15,16 @@
|
||||
*/
|
||||
package com.mybatisflex.test.relation.onetoone;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.mybatisflex.core.MybatisFlexBootstrap;
|
||||
import com.mybatisflex.core.audit.AuditManager;
|
||||
import com.mybatisflex.core.audit.ConsoleMessageCollector;
|
||||
import com.mybatisflex.core.audit.MessageCollector;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.mybatisflex.core.relation.RelationManager;
|
||||
import com.mybatisflex.test.relation.mapper.AccountMapper;
|
||||
import com.mybatisflex.test.relation.mapper.BookMapper;
|
||||
import com.mybatisflex.test.relation.mapper.MenuMapper;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
@ -30,11 +33,14 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
import javax.sql.DataSource;
|
||||
import java.util.List;
|
||||
|
||||
import static com.mybatisflex.test.relation.onetoone.table.MenuTableDef.MENU;
|
||||
|
||||
|
||||
public class RelationsTester {
|
||||
|
||||
static AccountMapper accountMapper;
|
||||
static BookMapper bookMapper;
|
||||
static MenuMapper menuMapper;
|
||||
|
||||
@BeforeClass
|
||||
public static void init() {
|
||||
@ -48,6 +54,7 @@ public class RelationsTester {
|
||||
.setDataSource(dataSource)
|
||||
.addMapper(AccountMapper.class)
|
||||
.addMapper(BookMapper.class)
|
||||
.addMapper(MenuMapper.class)
|
||||
.start();
|
||||
|
||||
//开启审计功能
|
||||
@ -59,13 +66,14 @@ public class RelationsTester {
|
||||
|
||||
accountMapper = bootstrap.getMapper(AccountMapper.class);
|
||||
bookMapper = bootstrap.getMapper(BookMapper.class);
|
||||
menuMapper = bootstrap.getMapper(MenuMapper.class);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testOneToOne() {
|
||||
List<Account> accounts = accountMapper.selectAllWithRelations();
|
||||
System.out.println(">>>>>>1: " + accounts);
|
||||
System.out.println(JSON.toJSONString(accounts));
|
||||
}
|
||||
|
||||
|
||||
@ -85,5 +93,14 @@ public class RelationsTester {
|
||||
System.out.println(">>>>>>2: " + accounts);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMenu() {
|
||||
QueryWrapper qw = QueryWrapper.create();
|
||||
qw.where(MENU.PARENT_ID.eq(0));
|
||||
|
||||
List<Menu> menus = menuMapper.selectListWithRelationsByQuery(qw);
|
||||
System.out.println( JSON.toJSONString(menus));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -1,11 +1,17 @@
|
||||
INSERT INTO tb_account
|
||||
VALUES (1, '张三', 18),
|
||||
(2, '王麻子叔叔', 19);
|
||||
VALUES (1, '孙悟空', 18),
|
||||
(2, '猪八戒', 19),
|
||||
(3, '沙和尚', 19),
|
||||
(4, '六耳猕猴', 19),
|
||||
(5, '王麻子叔叔', 19);
|
||||
|
||||
|
||||
INSERT INTO tb_idcard
|
||||
VALUES (1,'0001', '内容1'),
|
||||
(2,'0002', '内容2');
|
||||
(2,'0002', '内容2'),
|
||||
(3,'0003', '内容3'),
|
||||
(4,'0004', '内容4'),
|
||||
(5,'0005', '内容5');
|
||||
|
||||
INSERT INTO tb_book
|
||||
VALUES (1,1,'图书1', '内容1'),
|
||||
@ -28,3 +34,19 @@ VALUES (1,1),
|
||||
(2,1),
|
||||
(2,2),
|
||||
(2,3);
|
||||
|
||||
INSERT INTO tb_menu
|
||||
VALUES (1,0,'顶级菜单1'),
|
||||
(2,0,'顶级菜单2'),
|
||||
(3,0,'顶级菜单3'),
|
||||
(4,1,'子菜单'),
|
||||
(5,1,'子菜单'),
|
||||
(6,3,'子菜单'),
|
||||
(7,3,'子菜单'),
|
||||
(8,3,'子菜单'),
|
||||
(9,4,'子菜单'),
|
||||
(10,4,'子菜单'),
|
||||
(10,5,'子菜单'),
|
||||
(10,5,'子菜单'),
|
||||
(10,9,'子菜单'),
|
||||
(10,9,'子菜单');
|
||||
@ -32,4 +32,11 @@ CREATE TABLE IF NOT EXISTS `tb_role_mapping`
|
||||
(
|
||||
`account_id` INTEGER ,
|
||||
`role_id` INTEGER
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `tb_menu`
|
||||
(
|
||||
`id` INTEGER auto_increment,
|
||||
`parent_id` INTEGER,
|
||||
`name` VARCHAR(100)
|
||||
);
|
||||
Loading…
x
Reference in New Issue
Block a user