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

mapper

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

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

用于关联一个对象

property: 指定要关联的属性名

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

column: 查询时需要传递的列

MyBatis_多表关联查询_resultMap_单个对象_N+1方式实现_第1张图片

Mapper包下:

ClazzMapper接口:

package com.bjsxt.mapper;

import com.bjsxt.pojo.Clazz;

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

ClazzMapper.xml:



   
  
  	
  	
  	
  
  	
  

StudentMapper接口:

package com.bjsxt.mapper;

import java.util.List;

import com.bjsxt.pojo.Student;

public interface StudentMapper {
	List selAll();
}

StudentMapper.xml:



  
	    
		  	
		  	
		  	
		  	
		  	
		  	
		  	
	    
		
  

 

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 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 selAll() {
		SqlSession session = MyBatisUtil.getSession();
		StudentMapper mapper = session.getMapper(StudentMapper.class);
		List 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 list = ss.selAll();
		for (Student student : list) {
			System.out.println(student);
		}
	}
}

运行截图:

MyBatis_多表关联查询_resultMap_单个对象_N+1方式实现_第2张图片

数据库截图:

MyBatis_多表关联查询_resultMap_单个对象_N+1方式实现_第3张图片

 

 

你可能感兴趣的:(数据库学习,不忘初心,方得始终)