Mybatis多对一、多对一关联查询

1,新建 实体class,与student,同时创建数据库


student:

CREATE TABLE`t_student` (

  `id` int(5) NOT NULL AUTO_INCREMENT,

  `sname` varchar(10) DEFAULT NULL,

  `cid` int(5) NOT NULL DEFAULT '0',

  PRIMARY KEY (`id`,`cid`)

)

clazz:

CREATE TABLE `t_classes`(

  `id` int(5) NOT NULL AUTO_INCREMENT,

  `cname` varchar(10) DEFAULT NULL,

  PRIMARY KEY (`id`)

)


2.实体类对象:






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="clazz">
	<select id="getAllClazzs" resultMap="cMap">
		select c.id cid,c.cname,s.id,s.sname from t_classes c left join t_student s on
		s.cid=c.id
	</select>

	<resultMap type="cn.gzsxt.pojo.Clazz" id="cMap">
		<id property="id" column="cid" />
		<result property="cname" column="cname" />
		<collection property="students" javaType="java.util.List"
			ofType="cn.gzsxt.pojo.Student">
			<id property="id" column="id" />
			<result property="sname" column="sname" />
		</collection>

	</resultMap>

</mapper>

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="student"> 
	<select id="getALlStudents" resultMap="sMap">
		select s.id,s.sname,c.id cid,c.cname from t_student s,t_classes c where s.cid=c.id
	</select>
	
	<resultMap type="cn.gzsxt.pojo.Student" id="sMap">
		<id property="id" column="id"/>
		<result property="sname" column="sname"/>
		
		<association property="clazz" javaType="cn.gzsxt.pojo.Clazz">
			<id property="id" column="cid"/>
			<result property="cname" column="cname"/>
		</association>
	</resultMap>



	<select id="getStudentByClazzId" resultType="cn.gzsxt.pojo.Student">
		select * from t_student where cid=#{cid}
	</select>

</mapper>

Text:

package cn.gzsxt.text;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import cn.gzsxt.pojo.Clazz;
import cn.gzsxt.pojo.Student;


public class TextConnection {

	private SqlSession sqlSession;
	

	/**
	 * 测试多对一
	 */
	@Test
	public void textMany2One(){
		try {
			sqlSession = getSqlSession();
			List<Student> sList = sqlSession.selectList("getALlStudents");
			for (Student student : sList) {
				System.out.println(student);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			closeSession(sqlSession);
		}
		
	}
	
	/**
	 * 测试一对多
	 */
	@Test
	public void textOnt2many(){
		try {
			sqlSession = getSqlSession();
			List<Clazz> cList = sqlSession.selectList("getAllClazzs");
		for (Clazz clazz : cList) {
			System.out.println(clazz);
		}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			closeSession(sqlSession);
		}
	}
	
	/**
	 * 测试一对多中n+1的问题
	 * 区别在于 首次查出所有的班级信息,然后根据班级的id依次去查询学生的信息,在延迟加载策略中比较有用
	 */
	@Test
	public void textOne2many2(){
		try {
			sqlSession = getSqlSession();
			List<Clazz> cList = sqlSession.selectList("clazz1.getAllClazzs1");
			for (Clazz clazz : cList) {
				System.out.println(clazz);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			closeSession(sqlSession);
		}
	}
	
	
	/**
	 * 关闭session
	 * 
	 * @param session
	 */
	private void closeSession(SqlSession sqlSession) {
		if (null != sqlSession) {
			sqlSession.close();
		}
	}

	/**
	 * 获取SqlSession
	 * 
	 * @return
	 * @throws IOException
	 */
	private SqlSession getSqlSession() throws IOException {
		SqlSessionFactory sessionFactory;
		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
		// 2,读取配置文件
		InputStream inputStream = Resources
				.getResourceAsStream("mybatis.cfg.xml");
		sessionFactory = builder.build(inputStream);
		// 3,根据会话工厂获取会话
		SqlSession session = sessionFactory.openSession();
		return session;
	}

}

目录结构:



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值