欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MyBatis一对一关系的查询

程序员文章站 2022-04-21 23:51:16
...

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 + "]";
	}
	
	
}

    代表团实体

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

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;

	public Delegation() {
		super();
	}

	public Delegation(Integer delegationId, String delegationName) {
		super();
		this.delegationId = delegationId;
		this.delegationName = delegationName;
	}

	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;
	}

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

2、查询所有的用户

<?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.UserMapper">

 <resultMap type="com.qf.meeting.pojo.User" id="UserResultMap">
 	<id property="userId" column="userId"/>
 	<result property="userLoginName" column="userLoginName"/>
 	<result property="userTel" column="userTel"/>
 	<result property="userPwd" column="userPwd"/>
 	<result property="photo" column="photo"/>
 	<result property="userName" column="userName"/>
 	<association property="delegation" column="delegationId" javaType="com.qf.meeting.pojo.Delegation" autoMapping="true">
 	<id property="delegationId" column="delegationId"/>
 	</association>
 </resultMap>
 
 <select id="findAllUser" resultMap="UserResultMap">
 	select * from t_user u ,t_delegation d where u.delegationId = d.delegationId;
 </select>
 </mapper>

  查询结果

[{"userId":1,"userLoginName":"**","userTel":"*********","userPwd":"123456","photo":"***.jpg","userName":"****","delegation":{"delegationId":1,"delegationName":"电力局代表团"}},
{"userId":2,"userLoginName":"**","userTel":"*********","userPwd":"123456","photo":"***.jpg","userName":"***","delegation":{"delegationId":1,"delegationName":"电力局代表团"}}]