MyBatis一对多查询

1、用户实体

package com.qf.meeting.pojo;
import java.io.Serializable;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
 
public class User implements Serializable{
	private final static Logger LOG = LogManager.getLogger(User.class);
	private Integer userId;
	private String userLoginName;
	private String userTel;
	private String userPwd;
	private String photo;
	private String userName;
	private Delegation delegation;
	public User() {
		super();
	}
	public User(Integer userId, String userLoginName, String userTel, String userPwd, String photo, String userName,
			Delegation delegation) {
		super();
		this.userId = userId;
		this.userLoginName = userLoginName;
		this.userTel = userTel;
		this.userPwd = userPwd;
		this.photo = photo;
		this.userName = userName;
		this.delegation = delegation;
	}
	public Integer getUserId() {
		return userId;
	}
	public void setUserId(Integer userId) {
		this.userId = userId;
	}
	public String getUserLoginName() {
		return userLoginName;
	}
	public void setUserLoginName(String userLoginName) {
		this.userLoginName = userLoginName;
	}
	public String getUserTel() {
		return userTel;
	}
	public void setUserTel(String userTel) {
		this.userTel = userTel;
	}
	public String getUserPwd() {
		return userPwd;
	}
	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}
	public String getPhoto() {
		return photo;
	}
	public void setPhoto(String photo) {
		this.photo = photo;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public Delegation getDelegation() {
		return delegation;
	}
	public void setDelegation(Delegation delegation) {
		this.delegation = delegation;
	}
	@Override
	public String toString() {
		return "User [userId=" + userId + ", userLoginName=" + userLoginName + ", userTel=" + userTel + ", userPwd="
				+ userPwd + ", photo=" + photo + ", userName=" + userName + ", delegation=" + delegation + "]";
	}
	
	
}

2、代表团实体

package com.qf.meeting.pojo;
import java.io.Serializable;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
public class Delegation implements Serializable{
	private final static Logger LOG = LogManager.getLogger(Delegation.class);

	private Integer delegationId;
	
	private String delegationName;
	
	private List<User> users;

	public Delegation() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Delegation(Integer delegationId, String delegationName, List<User> users) {
		super();
		this.delegationId = delegationId;
		this.delegationName = delegationName;
		this.users = users;
	}

	public Integer getDelegationId() {
		return delegationId;
	}

	public void setDelegationId(Integer delegationId) {
		this.delegationId = delegationId;
	}

	public String getDelegationName() {
		return delegationName;
	}

	public void setDelegationName(String delegationName) {
		this.delegationName = delegationName;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	@Override
	public String toString() {
		return "Delegation [delegationId=" + delegationId + ", delegationName=" + delegationName + ", users=" + users
				+ "]";
	}

}

3、xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qf.meeting.mapper.DelegationMapper">
	<resultMap type="com.qf.meeting.pojo.Delegation" id="delegationMap">
		<id property="delegationId" column="delegationId"/>
		<result property="delegationName" column="delegationIdName"/>
		<collection property="users" column="uId"  autoMapping="true" ofType="com.qf.meeting.pojo.User">
			<id property="userId" column="userId"/>
			<result property="userName" column="userName"/>
			<result property="userTel" column="userTel"/>
			<result property="userLoginName" column="userLoginName"/>
			<result property="userLoginName" column="userLoginName"/>
		</collection>
	</resultMap>

	<select id="fingAll" resultMap="delegationMap">
		select * from t_delegation d,t_user u where d.delegationId = u.delegationId;
	</select>
</mapper>

4、查询结果

[{"delegationId":1,"delegationName":null,"users":[{"userId":1,"userLoginName":"zz","userTel":"********","userPwd":"123456","photo":"zz.jpg","userName":"zzz","delegation":null,"staffs":null},{"userId":2,"userLoginName":"zz","userTel":"***********","userPwd":"123456","photo":"zz.jpg","userName":"zz","delegation":null,"staffs":null}]}]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值