MyBatis 一對多和多對一的關係

配置和jar包

MyBatisConfig和MyBatisUtils(SqlSessionFactory工廠)配置和一對一配置一樣,詳情見一對一配置,架包也一樣


實體類

public class Classes {
	private int cid;
	private String cname;
	private String cdate;
	
	List<Student> stus;

public class Student {
	private int sid; 
	private String sname;
	private String sex;
	private int age;
	private String addr;
	private String birth; 
	private int cid;
	private String major;
	
	private Classes classes;


方法一:Mapper配置文件

mapper.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="test">
	<!-- 一對多    -->
	<resultMap type="Classes" id="Classess" >
		<id column="cid" property="cid" />	
		<collection property="stus" column="cid" select="findStudent" />
	</resultMap>
	<select id="findStudent" parameterType="int" resultType="Student">
		select * from Student where cid=#{value}
	</select>
	<select id="seeAll" resultMap="Classess">
		select * from Classes
	</select>
	
	<!-- 多對一 -->
	<resultMap type="Student" id="seeStudents">
		<id column="sid" property="sid" />
		<association property="classes" column="cid" select="seeClasses"  />
	</resultMap>
	<select id="seeClasses" resultType="Classes" parameterType="int" >
		select * from Classes where cid=#{value}
	</select>
	<select id="seeAllStudents" resultMap="seeStudents">
		select * from student
	</select>
</mapper>

在Mapper.xml配置中一對一和多對一的映射關聯用:association,一對多和多對多用collection

測試類

	//一對多
	@org.junit.Test
	public void test() throws Exception {
		SqlSession session = MyBatisUtils.getSqlSession();
		List<Classes> list = session.selectList("test.seeAll");
		for(Classes cl:list){
			System.out.println(cl);
			for(Student s:cl.getStus()){
				System.out.println(s);
			}
		}
	}
	//多對一
	@org.junit.Test
	public void test2() throws Exception {
		SqlSession session = MyBatisUtils.getSqlSession();
		List<Student> list = session.selectList("test.seeAllStudents");
		for(Student st:list){
			System.out.println(st+"____"+st.getClasses());
		}
	}


方法二:註解方式

接口類

一對多(ClassesDao)

package com.mingde.dao;

import java.util.List;

import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;

import com.mingde.po.Classes;

public interface ClassesDao {

	//查詢所有
	@Select("select * from Classes")
	//定義ResultMap
	@Results(id="ClassesResult",value={
			@Result(column="cid",property="cid"),
			@Result(column="cid",property="stus",many=@Many(select="com.mingde.dao.StudentDao.findStudents",fetchType=FetchType.LAZY))
	})
	public  List<Classes> SeeAllClasses()throws Exception;
	
	//根據id查詢
	@Select("select * from Classes where cid=#{value}")
	@ResultMap("ClassesResult")		//引入上面定義的ResultMap
	public Classes findClasses(int cid)throws Exception;
}

多對一(StudentDao)

package com.mingde.dao;

import java.util.List;

import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;

import com.mingde.po.Student;

public interface StudentDao {
	
	//根據班級cid查詢
	@Select("select * from student where cid=#{value}")
	public List<Student> findStudents(int cid)throws Exception;
	
	//查詢所有
	@Select("select * from student")
	@Results(id="findClasses",value={	//定義ResultMap
			@Result(column="sid",property="sid"),
			@Result(column="cid",property="classes",one=@One(select="com.mingde.dao.ClassesDao.findClasses",fetchType=FetchType.LAZY))
	})
	public List<Student> SeeStudents()throws Exception;
	
	//根據學生sid查詢
	@Select("select * from student where sid=#{value}")
	@ResultMap("findClasses") //引用上面的ResultMap
	public Student findStudentById(int sid)throws Exception;
	
}

注意:上面的配置是即配置一對多也配置多對一,這裏是爲了學者學習;但一般配置一對多後,就不會多對一,因爲這樣互相配置後會產生循環加載,導致內存溢出


測試類

//註解一對多
	@org.junit.Test
	public void test3() throws Exception {
		//獲取工廠,連接接口使用方法
		SqlSession session = MyBatisUtils.getSqlSession();
		ClassesDao mapper = session.getMapper(ClassesDao.class);
		//查詢所有
		List<Classes> seeAllClasses = mapper.SeeAllClasses();
		for(Classes c:seeAllClasses){
			System.out.println(c);
			for(Student st:c.getStus()){
				System.out.println(st);
			}
		}
		//根據id查詢
		Classes findClasses = mapper.findClasses(1);
		System.out.println(findClasses);
		System.out.println(findClasses.getStus());
	}
	//註解多對一
	@org.junit.Test
	public void test4() throws Exception {
		//獲取工廠,連接接口使用方法
		SqlSession session = MyBatisUtils.getSqlSession();
		StudentDao mapper = session.getMapper(StudentDao.class);
		//查詢所有
		List<Student> seeStudents = mapper.SeeStudents();
		for(Student s:seeStudents){
			System.out.println(s+"______"+s.getClasses());
		}
		//根據id查詢
		Student st = mapper.findStudentById(1126);
		System.out.println(st);
		System.out.println(st.getClasses());
	}


總結:

1、在Mapper.xml配置中一對一和多對一的映射關聯用:association,一對多和多對多用collection

2、一般配置一對多後,就不會多對一,因爲這樣互相配置後會產生循環加載,導致內存溢出

3、配置一對多或多對一的依賴關係,主要抓住配置二者的主鍵和外建,如:


//查詢所有
	@Select("select * from student")
	@Results(id="findClasses",value={	//定義ResultMap
			@Result(column="sid",property="sid"),		//主鍵,下面的是外建
			@Result(column="cid",property="classes",one=@One(select="com.mingde.dao.ClassesDao.findClasses",fetchType=FetchType.LAZY))
	})
	public List<Student> SeeStudents()throws Exception;


<!-- 一對多    -->
	<resultMap type="Classes" id="Classess" >
		<id column="cid" property="cid" />	//主鍵id
		<collection property="stus" column="cid" select="findStudent" />  //外建
	</resultMap>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章