mybatis中关联查询(一对多)的使用总结

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值