Structured Object Advanced
Return DTO/VO objects of structured "trees" of any shape, obtained downward from a certain database object relationship as aggregate root, and support adding custom personalized properties at any level.
For example, User->Role->Menu, two sets of many-to-many relationships, can return a JSON data package that contains roles and corresponding menus under the user structure, and can set properties like how many corresponding menus the role has or the names of the first three menus owned by the role.
{
"user": {
"name": "XiaoMing",
"age": 12,
"roles": [
{
"name": "Administrator",
"enable": true,
"top3Menus": "User Management,Department Management,Enterprise Management",//First three menus among all menus of the role
"menuCount": 6//Number of roles
}
]
}
}1. Use Plugin to Create DTO/VO
If you're not clear how to use it, please jump to Plugin Chapter CreateStructDTO

/**
* this file automatically generated by easy-query struct dto mapping
* This file is automatically generated by easy-query structured dto mapping
* {@link com.easy.query.test.entity.blogtest.SysUser }
*
* @author xuejiaming
* @easy-query-dto schema: normal
*/
@Data
public class SysUserRoleMenuDTO {
private String id;
private String companyId;
private String name;
private Integer age;
private LocalDateTime createTime;
@Navigate(value = RelationTypeEnum.ManyToMany)
private List<InternalRoles> roles;
/**
* {@link com.easy.query.test.entity.blogtest.SysRole }
*/
@Data
public static class InternalRoles {
private String id;
private String name;
private LocalDateTime createTime;
private String top3Menus;
private Long menuCount;
}
}We add two properties top3Menus and menuCount here.
2. Use Plugin to Insert Extra Configuration Code
Enter eq_extra_auto_inclue_configure and the plugin will automatically insert the code block.

Note!!!
The target class must add
{@link com.easy.query.test.entity.blogtest.SysRole }to indicate which table the current class is structured from
/**
* this file automatically generated by easy-query struct dto mapping
* This file is automatically generated by easy-query structured dto mapping
* {@link com.easy.query.test.entity.blogtest.SysUser }
*
* @author xuejiaming
* @easy-query-dto schema: normal
*/
@Data
public class SysUserRoleMenuDTO {
private String id;
private String companyId;
private String name;
private Integer age;
private LocalDateTime createTime;
@Navigate(value = RelationTypeEnum.ManyToMany)
private List<InternalRoles> roles;
/**
* {@link com.easy.query.test.entity.blogtest.SysRole }
*/
@Data
public static class InternalRoles {
private static final ExtraAutoIncludeConfigure EXTRA_AUTO_INCLUDE_CONFIGURE = SysRoleProxy.TABLE.EXTRA_AUTO_INCLUDE_CONFIGURE()
.select(o -> Select.of(
o.menus().elements(0,2).joining(menu->menu.name(),",").as("top3Menus"),
o.menus().count().as("menuCount")
));
private String id;
private String name;
private LocalDateTime createTime;
private String top3Menus;
private Long menuCount;
}
}Write queries that support operations like where and select.
o.menus().elements(0,2).joining(menu->menu.name(),",").as("top3Menus")
This code means get the first three elements of the menus under the current role, then use the joining function to select the property name to be split and use comma to split, then assign this value to top3Menus.
o.menus().count().as("menuCount")
This code means assign the count of menus under the current role to menuCount.
This way we achieve custom properties at any level of structured objects.
3. Query
After we build the DTO, we only need one line to complete all queries and all data is queried on demand:
List<SysUserRoleMenuDTO> users = easyEntityQuery.queryable(SysUser.class)
.where(u -> {
u.username().startsWith("Jin");
})
.orderBy(u -> u.createTime().asc())
.selectAutoInclude(SysUserRoleMenuDTO.class)
.toList();4. Filtering
If we need to filter at any level, how should we implement it?
/**
* this file automatically generated by easy-query struct dto mapping
* This file is automatically generated by easy-query structured dto mapping
* {@link com.easy.query.test.entity.blogtest.SysUser }
*
* @author xuejiaming
* @easy-query-dto schema: normal
*/
@Data
public class SysUserRoleMenuDTO {
private String id;
private String companyId;
private String name;
private Integer age;
private LocalDateTime createTime;
@Navigate(value = RelationTypeEnum.ManyToMany)
private List<InternalRoles> roles;
/**
* {@link com.easy.query.test.entity.blogtest.SysRole }
*/
@Data
public static class InternalRoles {
private static final ExtraAutoIncludeConfigure EXTRA_AUTO_INCLUDE_CONFIGURE = SysRoleProxy.TABLE.EXTRA_AUTO_INCLUDE_CONFIGURE()
.where(o->o.name().like("Administrator"))
.select(o -> Select.of(
o.menus().where(menu->menu.route().startsWith("/api/app/")).elements(0,2).joining(menu->menu.name(),",").as("top3Menus"),
o.menus().where(menu->menu.route().startsWith("/api/web/")).count().as("menuCount")
));
private String id;
private String name;
private LocalDateTime createTime;
private String top3Menus;
private Long menuCount;
}
}We filter roles under the user, only filtering role names containing "Administrator", and perform extra filtering on top3Menus requiring the first three to be app menus while menuCount must be web menus.