Mybatis 注解关联 (一对多、多对一)

Mybatis中的一对多、多对一关联关系可以通过xml文档配置,也可以通过annotation注解配置:

以student(多)和school(一)为例:

config.xml:

  <mappers>  
    <mapper class="com.dw.dao.SchoolMapper"/>
    <mapper class="com.dw.dao.StudentMapper"/>  
  </mappers>  
SchoolMapper.java:

package com.dw.dao;

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

import com.dw.domain.School;

public interface SchoolMapper {
   /**单表查询*/
	@Select("select * from t_school as sc where sc.sc_id=#{id}")
	@Results({
		@Result(column="sc_id",property="id"),
		@Result(column="sc_name",property="name")
	})
	public School selectSchoolById(Integer id);
	
	
	/**一对多 多表查询*/
	@Select("select * from t_school as sc where sc.sc_id=#{id}")
	@Results({
		@Result(column="sc_id",property="id"),
		@Result(column="sc_name",property="name"),
		@Result(column="sc_id",property="students",many=@Many(select="com.dw.dao.StudentMapper.selectStudentBySchoolId",fetchType=FetchType.LAZY))
	})
	public School selectSchoolById2(Integer id);
}
StudentMapper.java:

package com.dw.dao;

import java.util.List;

import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import com.dw.domain.School;
import com.dw.domain.Student;

public interface StudentMapper {
   /**单表查询*/
	@Select("select * from t_student as s where s.s_id=#{id}")
	@Results({
		@Result(column="s_id",property="id"),
		@Result(column="s_name",property="name"),
		@Result(column="s_NO",property="NO")
	})
	public Student selectStudentById(Integer id);
	
	
	/**多对一  多表查询*/
	@Select("select * from t_student as s where s.s_id=#{id}")
	@Results({
		@Result(column="s_id",property="id"),
		@Result(column="s_name",property="name"),
		@Result(column="s_NO",property="NO"),
		
		@Result(column="sc_id",property="school",javaType=School.class,
		one=@One(select="com.dw.dao.SchoolMapper.selectSchoolById"))
	})
	public Student selectStudentById2(Integer id);
	
	
	/**通过学校id查询学生*/
	@Select("select s.s_id,s.s_name,s.s_NO from t_student as s where s.sc_id=#{id}")
	@Results({
		@Result(column="s_id",property="id"),
		@Result(column="s_name",property="name"),
		@Result(column="s_NO",property="NO")
	})
	public List<Student> selectStudentBySchoolId(Integer id);
}
测试:

/**school单表查询*/
  @Test
  public void selectSchool4() {
	  myInit();
	  School school = schoolMapper.selectSchoolById(1);
	  System.out.println(school);
	  destory();
  }
  
  /**student单表查询*/
  @Test
  public void selectStudent4() {
	  myInit();
	  Student student = studentMapper.selectStudentById(1);
	  System.out.println(student);
  }
  
  /**多对一 多表查询*/
  @Test
  public void selectStudent5() {
	  myInit();
	  Student student = studentMapper.selectStudentById2(1);
	  System.out.println(student);
	  destory();
  }
  
  /**一对多 多表查询*/
  @Test
  public void selectSchool5() {
	  myInit();
	  School school = schoolMapper.selectSchoolById2(1);
	  System.out.println(school);
	  destory();
  }
  @SuppressWarnings("unused")
private void myInit() {
	  try {
			in = Resources.getResourceAsStream("config.xml");
			sessionFactory = new SqlSessionFactoryBuilder().build(in);
			session = sessionFactory.openSession();
//			userDao = session.getMapper(UserDao.class);
//			schoolDao = session.getMapper(SchoolDao.class);
//			studentDao = session.getMapper(StudentDao.class);
			schoolMapper = session.getMapper(SchoolMapper.class);
			studentMapper = session.getMapper(StudentMapper.class);
	} catch (Exception e) {
		e.printStackTrace();
	}
  }
  
  @SuppressWarnings("unused")
private void destory() {
	  if(session !=null) {
		  session.close();
	  }
	  if(in != null) {
		  try {
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	  }
  }
School.java:

package com.dw.domain;

import java.util.List;

public class School {
	private Integer id;
	private String name;
	private List<Student> students;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<Student> getStudents() {
		return students;
	}

	public void setStudents(List<Student> students) {
		this.students = students;
	}

	@Override
	public String toString() {
		return "School [id=" + id + ", name=" + name + ", students=" + students + "]";
	}

}
Student.java:

package com.dw.domain;

public class Student {
	private Integer id;
	private String name;
	private String NO;
	private School school;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNO() {
		return NO;
	}

	public void setNO(String nO) {
		NO = nO;
	}

	public School getSchool() {
		return school;
	}

	public void setSchool(School school) {
		this.school = school;
	}

	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", NO=" + NO + ", school=" + school + "]";
	}

	
}












  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柏油

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值