MyBatis_多表关联查询_resultMap_单个对象_N+1方式实现

mapper

提供 StudentMapper 和 ClazzMapper, StudentMapper 查询所有学生信息, ClazzMapper 根据编号查询班级信息. 再

StudentMapper 中使用<association>设置装配:(对比resultMap_集合对象_N+1方式实现

<association>用于关联一个对象

property: 指定要关联的属性名

select: 设定要继续引用的查询, namespace+id

column: 查询时需要传递的列

Mapper包下:

ClazzMapper接口:

package com.bjsxt.mapper;

import com.bjsxt.pojo.Clazz;

public interface ClazzMapper {
	Clazz selById(int id);
}

ClazzMapper.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.bjsxt.mapper.ClazzMapper"> 
  <resultMap type="clazz" id="cmap">
  	<id property="id" column="cid"/>
  	<result property="name" column="cname"/>
  	<result property="room" column="croom"/>
  </resultMap>
  	<select id="selById" resultMap="cmap" parameterType="int">
  		select * from t_class where cid=#{0}
  	</select>
  </mapper>

StudentMapper接口:

package com.bjsxt.mapper;

import java.util.List;

import com.bjsxt.pojo.Student;

public interface StudentMapper {
	List<Student> selAll();
}

StudentMapper.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.bjsxt.mapper.StudentMapper">
	    <resultMap type="Student" id="smap">
		  	<id property="stid" column="sid"/>
		  	<result property="stname" column="name"/>
		  	<result property="stage" column="age"/>
		  	<result property="stsex" column="sex"/>
		  	<result property="stcid" column="scid"/>
		  	<!-- 用于关联一个对象 -->
		  	<association property="clazz" select="com.bjsxt.mapper.ClazzMapper.selById" column="scid"/>
	    </resultMap>
		<select id="selAll" resultMap="smap">
			select * from t_stu
		</select>
  </mapper>

 

POJO包下:

Clazz:

package com.bjsxt.pojo;

import java.io.Serializable;

public class Clazz implements Serializable{
	private int id;
	private String name;
	private String room;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getRoom() {
		return room;
	}
	public void setRoom(String room) {
		this.room = room;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((room == null) ? 0 : room.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Clazz other = (Clazz) obj;
		if (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (room == null) {
			if (other.room != null)
				return false;
		} else if (!room.equals(other.room))
			return false;
		return true;
	}
	@Override
	public String toString() {
		return "Clazz [id=" + id + ", name=" + name + ", room=" + room + "]";
	}
	public Clazz() {
		super();
	}
	
	
}

Student:

package com.bjsxt.pojo;

import java.io.Serializable;

public class Student implements Serializable{
	private int stid;
	private String stname;
	private int stage;
	private String stsex;
	private int stcid;
	private Clazz clazz;
	
	public int getStid() {
		return stid;
	}
	public void setStid(int stid) {
		this.stid = stid;
	}
	public String getStname() {
		return stname;
	}
	public void setStname(String stname) {
		this.stname = stname;
	}
	public int getStage() {
		return stage;
	}
	public void setStage(int stage) {
		this.stage = stage;
	}
	public String getStsex() {
		return stsex;
	}
	public void setStsex(String stsex) {
		this.stsex = stsex;
	}
	public int getStcid() {
		return stcid;
	}
	public void setStcid(int stcid) {
		this.stcid = stcid;
	}
	public Clazz getClazz() {
		return clazz;
	}
	public void setClazz(Clazz clazz) {
		this.clazz = clazz;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((clazz == null) ? 0 : clazz.hashCode());
		result = prime * result + stage;
		result = prime * result + stcid;
		result = prime * result + stid;
		result = prime * result + ((stname == null) ? 0 : stname.hashCode());
		result = prime * result + ((stsex == null) ? 0 : stsex.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (clazz == null) {
			if (other.clazz != null)
				return false;
		} else if (!clazz.equals(other.clazz))
			return false;
		if (stage != other.stage)
			return false;
		if (stcid != other.stcid)
			return false;
		if (stid != other.stid)
			return false;
		if (stname == null) {
			if (other.stname != null)
				return false;
		} else if (!stname.equals(other.stname))
			return false;
		if (stsex == null) {
			if (other.stsex != null)
				return false;
		} else if (!stsex.equals(other.stsex))
			return false;
		return true;
	}
	@Override
	public String toString() {
		return "Student [stid=" + stid + ", stname=" + stname + ", stage=" + stage + ", stsex=" + stsex + ", stcid="
				+ stcid + ", clazz=" + clazz + "]";
	}
	public Student() {
		super();
	}
	
	
}

 

Service包下:

StudentService接口:

package com.bjsxt.service;

import java.util.List;

import com.bjsxt.pojo.Student;

public interface StudentService {
	List<Student> selAll();
	
}

StudentServiceImp:

package com.bjsxt.service.Impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.bjsxt.mapper.StudentMapper;
import com.bjsxt.pojo.Student;
import com.bjsxt.service.StudentService;
import com.bjsxt.util.MyBatisUtil;

public class StudentServiceImpl implements StudentService{

	@Override
	public List<Student> selAll() {
		SqlSession session = MyBatisUtil.getSession();
		StudentMapper mapper = session.getMapper(StudentMapper.class);
		List<Student> list = mapper.selAll();
		session.close();
		return list;
	}

}

 

TestSel测试类:

package com.bjsxt.test;

import java.util.List;

import com.bjsxt.pojo.Student;
import com.bjsxt.service.StudentService;
import com.bjsxt.service.Impl.StudentServiceImpl;

public class TestSel {
	public static void main(String[] args) {
		StudentService ss=new StudentServiceImpl();
		List<Student> list = ss.selAll();
		for (Student student : list) {
			System.out.println(student);
		}
	}
}

运行截图:

数据库截图:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值