多对一 一对多

多对一:

package it.ytu.pojo;

import lombok.Data;

@Data
public class Student {
	private int id;
	private String name;
	private Teacher teacher;
	
}

package it.ytu.pojo;

import lombok.Data;

@Data
public class Teacher {
	private int id;
	private String name;
}


package it.ytu.dao;

import java.util.List;

import it.ytu.pojo.Student;

public interface StudentMapper {
	public List<Student> getStudents();
	public List<Student> getStudent();
}

<?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="it.ytu.dao.StudentMapper">


  <select id="getStudents" resultMap="StudentTeacher">
  	select * from student
  </select>
  <resultMap type="student" id="StudentTeacher">
  	<result property="id" column="id"/>
  	<result property="name" column="name"/>
  	<association property="teacher" column="tid" javaType="teacher" select="getTeacher"/>
  </resultMap>
  <select id="getTeacher" resultType="teacher">
  	select * from teacher where id=#{tid}
  </select>
  
  <select id="getStudent" resultMap="StudentTeacher2">
  	select s.id sid,s.name sname,t.name tname ,t.id tid
  	from student s,teacher t 
  	where s.tid=t.id
  </select>
  <resultMap type="student" id="StudentTeacher2">
  	<result property="id" column="sid"/>
  	<result property="name" column="sname"/>
  	<association property="teacher" javaType="teacher">
  		<result property="id" column="tid"/>
  		<result property="name" column="tname"/>
  	</association>
  </resultMap>
  
</mapper>
 <mappers>
  	<mapper class="it.ytu.dao.TeacherMapper"/>
  	<mapper class="it.ytu.dao.StudentMapper"/>
  </mappers>
package mytest;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import it.ytu.dao.StudentMapper;
import it.ytu.dao.TeacherMapper;
import it.ytu.pojo.Student;
import it.ytu.pojo.Teacher;
import it.ytu.utils.MybatisUtils;

public class MyTest {
	public static void main(String[] args) {
		SqlSession sqlSession=MybatisUtils.getSqlSession();
		TeacherMapper mapper=sqlSession.getMapper(TeacherMapper.class);
		Teacher teacher=mapper.getTeacher(1);
		System.out.println(teacher);
		sqlSession.close();
	}
	@Test
	public void test() {
		SqlSession sqlSession=MybatisUtils.getSqlSession();
		StudentMapper mapper=sqlSession.getMapper(StudentMapper.class);
		List<Student> students=mapper.getStudents();
		for (Student student : students) {
			System.out.println(student);
		}
		
		sqlSession.close();
	}
	@Test
	public void test1() {
		SqlSession sqlSession=MybatisUtils.getSqlSession();
		StudentMapper mapper=sqlSession.getMapper(StudentMapper.class);
		List<Student> students=mapper.getStudent();
		for (Student student : students) {
			System.out.println(student);
		}
		
		sqlSession.close();
	}
}

一对多:

package it.ytu.pojo;

import lombok.Data;

@Data
public class Student {
	private int id;
	private String name;
	private int tid;
}

package it.ytu.pojo;

import java.util.List;

import lombok.Data;

@Data
public class Teacher {
	private int id;
	private String name;
	private List<Student> students;
}

package it.ytu.dao;

import org.apache.ibatis.annotations.Param;

import it.ytu.pojo.Teacher;

public interface TeacherMapper {
	Teacher getTeacher(@Param("id")int id);
	Teacher getTeacher2(@Param("id")int id);
}

<?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="it.ytu.dao.TeacherMapper">
  <select id="getTeacher" resultMap="TeacherStudent">
    select s.id sid, s.name sname , t.name tname, t.id tid 
	from student s,teacher t 
	where s.tid = t.id and t.id=#{id}
  </select>
  <resultMap type="teacher" id="TeacherStudent">
    <result property="id" column="tid"/>
    <result property="name" column="tname"/>
    <collection property="students" ofType="student">
    	<result property="id" column="sid"/>
    	<result property="name" column="sname"/>
    	<result property="tid" column="tid"/>
    </collection>
  </resultMap>
  
  <select id="getTeacher2" resultMap="TeacherStudent2">
	select * from teacher where id = #{id}
  </select>
  <resultMap id="TeacherStudent2" type="Teacher">
    <collection property="students" javaType="ArrayList" ofType="Student" column="id" select="getStudentByTeacherId"/>
  </resultMap>
  <select id="getStudentByTeacherId" resultType="Student">
	select * from student where tid = #{id}
  </select>
</mapper>
package mytest;


import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import it.ytu.dao.TeacherMapper;
import it.ytu.pojo.Teacher;
import it.ytu.utils.MybatisUtils;

public class MyTest {
	@Test
	public void test() {
		SqlSession sqlSession=MybatisUtils.getSqlSession();
		TeacherMapper mapper=sqlSession.getMapper(TeacherMapper.class);
		
		Teacher teacher=mapper.getTeacher(1);
		System.out.println(teacher);
		
		sqlSession.close();
	}
	@Test
	public void test2() {
		SqlSession sqlSession=MybatisUtils.getSqlSession();
		TeacherMapper mapper=sqlSession.getMapper(TeacherMapper.class);
		
		Teacher teacher=mapper.getTeacher2(1);
		System.out.println(teacher);
		
		sqlSession.close();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值