Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

关于SpringDataJPA和MapStruct的使用

我使用SpringDataJpa进行多表关联,其中几个实体类的对应情况如下:

User:
public class User extends BaseEntity  {
 private String username;
 private String password;
 @ManyToOne ( cascade = CascadeType.ALL, fetch = FetchType.LAZY )
 @JoinColumn ( name = "dept_id" )
 private Department depts;
 @ManyToOne ( cascade = CascadeType.ALL, fetch = FetchType.LAZY )
 @JoinColumn ( name = "role_id" )
 private Role role;
}
Role :
public class Role extends BaseEntity {
 private String role;
 private String roleName;
 @OneToMany (mappedBy = "roles",cascade=CascadeType.ALL,fetch=FetchType.LAZY)
 private Set<User> users;
 @ManyToMany (mappedBy = "roles",cascade=CascadeType.ALL,fetch=FetchType.EAGER)
 @JoinTable(name = "t_role_route",joinColumns=@JoinColumn(name="role_id",referencedColumnName = "id"),inverseJoinColumns = @JoinColumn(name = "route_id"))
 @JsonIgnore
 private Set<Route> routes;
 @ManyToOne ( cascade = CascadeType.ALL, fetch = FetchType.LAZY )
 @JoinColumn ( name = "dept_id" )
 private Department depts;
}
Route:
public class Route extends BaseEntity {
 private String route;
 private String routeName;
 @ManyToMany(mappedBy = "routes")
 private Set<Role> roles;
}
Department:
public class Department extends BaseEntity {
 private String deptName;
 private String dept;
 @OneToMany (mappedBy = "depts",cascade= CascadeType.ALL,fetch= FetchType.LAZY)
 private Set<User> users;
 @OneToMany (mappedBy = "depts",cascade=CascadeType.ALL,fetch=FetchType.LAZY)
 private Set<Role> roles;
}

现在我在登录后想返回这么一个对象:
UserVo:

private String username;
private String password;
private List<String> routes;
private List<String> routeNames;
private String deptName;
private String dept;
private Collection<? extends GrantedAuthority> authorities;

其中routesrouteNames是role中routes的属性,deptNamedept是user中dept的属性,现在我想在用户登陆的时候根据username 从数据库查询到user以及关联的信息,并且使用mapStruct进行转换成userVO,请问这个该怎么转化呢?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...