mybatis中关联查询(一对多)的使用总结
之前介绍了一对一的使用总结,现在大致介绍一下一对多的使用。
在mybatis中一对多可以通过标签来完成,下面来看代码!
(这里是对关联查询的使用介绍,如果想进行深入了解,需自己查看相关书籍)
1.实体类
public class UserRoleList {
private Long id;
private String username;
private String password;
private String email;
private List<Role> roleList;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public List<Role> getRoleList() {
return roleList;
}
public void setRoleList(List<Role> roleList) {
this.roleList = roleList;
}
}
2.sql映射接口
UserRoleList selectByIdMap(Long id);
3.sql文件
<resultMap type="com.hbcfc.yusys.cmis.customer.mapper.domain.UserRoleList" id="userRoleListMap">
<id property="id" column="id" />
<result property="username" column="username" />
<result property="password" column="password" />
<result property="email" column="email" />
<collection ofType="com.hbcfc.yusys.cmis.customer.mapper.domain.Role" property="roleList">
<id property="id" column="role_id" />
<result property="roleName" column="role_name" />
<result property="roleId" column="roleid" />
</collection>
</resultMap>
<select id="selectByIdMap4" resultMap="userRoleListMap">
select
u.id,u.username,u.password,u.email,r.id role_id,r.rolename role_name,r.role_id roleid
from t_user u,t_role r where u.role=r.role_id and u.id=#{id}
</select>