Mybatis_多表关联查询_resultMap_集合对象_N+1方式实现

mapper

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

在 ClazzMapper 中使用设置装配.

用于关联一个集合

property: 指定要关联的属性名

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

column: 查询时需要传递的列

 

直接上代码(准备俩张表,学生表和班级表,学生表的外键是班级表的主键  jar包和基本配置文件还是和MyBatis动态查询一样):

Mybatis_多表关联查询_resultMap_集合对象_N+1方式实现_第1张图片

俩个实体类:

1.Clazz

package com.bjsxt.pojo;

import java.io.Serializable;
import java.util.List;

public class Clazz implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = -5195063390556612327L;
	private int id;
	private String name;
	private String room;
	private List stus;
	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;
	}
	public List getStus() {
		return stus;
	}
	public void setStus(List stus) {
		this.stus = stus;
	}
	@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());
		result = prime * result + ((stus == null) ? 0 : stus.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;
		if (stus == null) {
			if (other.stus != null)
				return false;
		} else if (!stus.equals(other.stus))
			return false;
		return true;
	}
	@Override
	public String toString() {
		return "Clazz [id=" + id + ", name=" + name + ", room=" + room + ", stus=" + stus + "]";
	}
	public Clazz() {
		super();
	}
	
	
}

 

2.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;
	@Override
	public String toString() {
		return "Student [stid=" + stid + ", stname=" + stname + ", stage=" + stage + ", stsex=" + stsex + ", stcid="
				+ stcid + "]";
	}
	public Student() {
		super();
	}
	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;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		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 (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;
	}

	
	
}

 

ClazzMapper.xml



   
  
  	
  	
  	
  	
  
  	
  

ClazzMapper接口:

package com.bjsxt.mapper;

import java.util.List;

import com.bjsxt.pojo.Clazz;

public interface ClazzMapper {
	List selAll();
}

StudentMapper.xml:



  
	    
		  	
		  	
		  	
		  	
		  	
	    
		
  

StudentMapper接口:

package com.bjsxt.mapper;

import java.util.List;

import com.bjsxt.pojo.Student;

public interface StudentMapper {
	List selBystcid(int stcid);
}

 

Service接口(ClazzService 业务装配):

package com.bjsxt.service;

import java.util.List;

import com.bjsxt.pojo.Clazz;

public interface ClazzService {
	List selAll();
	
}

ServiceImpl(ClazzServiceIml):继承ClazzService 实现它的方法:

package com.bjsxt.service.Impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.bjsxt.mapper.ClazzMapper;
import com.bjsxt.pojo.Clazz;
import com.bjsxt.service.ClazzService;
import com.bjsxt.util.MyBatisUtil;

public class ClazzServiceImpl implements ClazzService{

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

}

 

测试类(TestSel):

package com.bjsxt.test;

import java.util.List;

import com.bjsxt.pojo.Clazz;
import com.bjsxt.service.ClazzService;
import com.bjsxt.service.Impl.ClazzServiceImpl;

public class TestSel {
	public static void main(String[] args) {
		ClazzService cs=new ClazzServiceImpl();
		List list = cs.selAll();
		for (Clazz clazz : list) {
			System.out.println(clazz);
		}
	}
}

运行结果截图:

Mybatis_多表关联查询_resultMap_集合对象_N+1方式实现_第2张图片

 

数据库查询截图:

Mybatis_多表关联查询_resultMap_集合对象_N+1方式实现_第3张图片

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