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

mapper

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

在 ClazzMapper 中使用<collection>设置装配.

<collection>用于关联一个集合

property: 指定要关联的属性名

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

column: 查询时需要传递的列

 

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

俩个实体类:

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<Student> 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<Student> getStus() {
		return stus;
	}
	public void setStus(List<Student> 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

<?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"/>
  	<collection property="stus" select="com.bjsxt.mapper.StudentMapper.selBystcid" column="cid"></collection>
  </resultMap>
  	<select id="selAll" resultMap="cmap">
  		select * from t_class
  	</select>
  </mapper>

ClazzMapper接口:

package com.bjsxt.mapper;

import java.util.List;

import com.bjsxt.pojo.Clazz;

public interface ClazzMapper {
	List<Clazz> 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"/>
	    </resultMap>
		<select id="selBystcid" resultMap="smap" parameterType="int">
			select * from t_stu where scid=#{0}
		</select>
  </mapper>

StudentMapper接口:

package com.bjsxt.mapper;

import java.util.List;

import com.bjsxt.pojo.Student;

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

 

Service接口(ClazzService 业务装配):

package com.bjsxt.service;

import java.util.List;

import com.bjsxt.pojo.Clazz;

public interface ClazzService {
	List<Clazz> 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<Clazz> selAll() {
		SqlSession session = MyBatisUtil.getSession();
		ClazzMapper mapper = session.getMapper(ClazzMapper.class);
		List<Clazz> 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<Clazz> list = cs.selAll();
		for (Clazz clazz : list) {
			System.out.println(clazz);
		}
	}
}

运行结果截图:

 

数据库查询截图:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值