DTO Mapping Advanced
DTO Mapping Advanced
Return a DTO or VO object in the shape of an arbitrary "tree" structure obtained downwards with a database object relationship as the aggregate root, and supports custom personalized property addition at any level
Such as user->role->menu, these 2 groups of many-to-many relationships, can return a JSON data package containing roles and corresponding menus under the user structure, and can set data structure returns such as how many menus a role has or the top 3 menu names owned by the role
{
"user": {
"name": "XiaoMing",
"age": 12,
"roles": [
{
"name": "Admin",
"enable": true,
"top3Menus": "User Management,Department Management,Enterprise Management",//Top 3 of all menus in the role
"menuCount": 6//Role count
}
]
}
}1. Use plugin to create dto, vo
If you don't know 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 code blocks

Note!!!
The target class must add
{@link com.easy.query.test.entity.blogtest.SysRole }to indicate which table the current class is structured
/**
* 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 query supporting operations like where and select
o.menus().elements(0,2).joining(menu->menu.name(),",").as("top3Menus")
This code means to get the first 3 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 to assign the number of menus under the current role to menuCount
This way we can customize any level of structured objects
3. Query
After we build the dto, we only need one sentence 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. Filter
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("Admin"))
.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 users, only filtering roles whose name contains "Admin", and perform extra filtering on top3Menus requiring the first 3 to be app menus while menuCount must be web menus