Mybaties一对一关联查询详解

javaBean如下俩个表


学生表


package com.hp.bean;

/**
 * @ClassName: UserBean
 * @Description:TODO(学生实体类)
 * @author: zgx
 * @date: 2018年5月11日 下午6:32:10
 * @Copyright: 2018 www.tydic.com Inc. All rights reserved. CREATE TABLE
 *             `userinfo` ( `uid` int(11) NOT NULL AUTO_INCREMENT, `cid` int(11)
 *             DEFAULT NULL, `uname` varchar(255) COLLATE utf8_bin DEFAULT NULL,
 *             `upwd` varchar(255) COLLATE utf8_bin DEFAULT NULL, `context`
 *             varchar(500) COLLATE utf8_bin DEFAULT NULL, `sex` int(11) DEFAULT
 *             NULL, `address` varchar(255) COLLATE utf8_bin DEFAULT NULL,
 *             `emall` varchar(255) COLLATE utf8_bin DEFAULT NULL, PRIMARY KEY
 *             (`uid`), KEY `cid` (`cid`), CONSTRAINT `userinfo_ibfk_1` FOREIGN
 *             KEY (`cid`) REFERENCES `classinfo` (`c_id`) ) ENGINE=InnoDB
 *             AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
 */
public class UserBean {
	private Integer uid; // 学生id
	private ClassBean clas; // 班级id
	private String uname; // 登录用户名名
	private String upwd; // 密码
	private String context; // 学生个人介绍
	private Integer sex; // 性别 1男 0 女
	private String address; // 地址
	private String emall; // 个人邮箱

	public Integer getUid() {
		return uid;
	}

	public void setUid(Integer uid) {
		this.uid = uid;
	}

	public ClassBean getClas() {
		return clas;
	}

	public void setClas(ClassBean clas) {
		this.clas = clas;
	}

	public String getUname() {
		return uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}

	public String getUpwd() {
		return upwd;
	}

	public void setUpwd(String upwd) {
		this.upwd = upwd;
	}

	public String getContext() {
		return context;
	}

	public void setContext(String context) {
		this.context = context;
	}

	public Integer getSex() {
		return sex;
	}

	public void setSex(Integer sex) {
		this.sex = sex;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getEmall() {
		return emall;
	}

	public void setEmall(String emall) {
		this.emall = emall;
	}

	@Override
	public String toString() {
		return "UserBean [uid=" + uid + ", cid=" + clas + ", uname=" + uname
				+ ", upwd=" + upwd + ", context=" + context + ", sex=" + sex
				+ ", address=" + address + ", emall=" + emall + "]";
	}

}


class班级表


package com.hp.bean;

/**   
 * @ClassName:  ClassBean   
 * @Description:TODO(班级实例)   
 * @author: zgx
 * @date:   2018年5月11日 下午7:42:25     
 * @Copyright: 2018 www.tydic.com Inc. All rights reserved. 
 * CREATE TABLE `classinfo` (
  `c_id` int(11) NOT NULL AUTO_INCREMENT,
  `c_name` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `c_bianhao` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`c_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
 */
public class ClassBean {
	
	private Integer c_id;
	private String c_name;
	private String c_bianhao;
	public Integer getC_id() {
		return c_id;
	}
	public void setC_id(Integer c_id) {
		this.c_id = c_id;
	}
	public String getC_name() {
		return c_name;
	}
	public void setC_name(String c_name) {
		this.c_name = c_name;
	}
	public String getC_bianhao() {
		return c_bianhao;
	}
	public void setC_bianhao(String c_bianhao) {
		this.c_bianhao = c_bianhao;
	}
	@Override
	public String toString() {
		return "ClassBean [c_id=" + c_id + ", c_name=" + c_name
				+ ", c_bianhao=" + c_bianhao + "]";
	}
	

}


mapper接口如下


	//查询学生和班级
	public UserBean SelectFromUser(int id);


扫描二维码关注公众号,回复: 833693 查看本文章
<resultMap type="com.hp.bean.UserBean" id="selectMap">
<id column="uid" property="uid"/>
<result column="uname" property="uname"/>
<result column="upwd" property="upwd"/>
<result column="context" property="context"/>
<result column="sex" property="sex"/>
<result column="address" property="address"/>
<result column="emall" property="emall"/>
<association property="clas" javaType="com.hp.bean.ClassBean">
<id column="c_id" property="c_id"/>
<result column="c_name" property="c_name"/>
<result column="c_bianhao" property="c_bianhao"/>
</association>

</resultMap>


     <select id="SelectFromUser" resultMap="selectMap" parameterType="Integer">
       select * from userinfo,classinfo where userinfo.cid=classinfo.c_id and uid=#{id}
     </select>


需要写出你要查询的 主外键关联  不然查询不出来


效果图



猜你喜欢

转载自blog.csdn.net/qq_40646143/article/details/80289543