mybatis联表查询一对多与多对一


t_student 表(学生表)

 

t_grade(年级表):



首先来看看一对多

Grade类:

import java.util.List;

public class Grade {
    private int gid;
    private String gname;
    private List<Student> students;

get....set(大家都懂省略感觉这样看着更加清晰)

Student:

public class Student {
    private int id;
    private String name;
    private int gradeId;
get...set....
获取session的工具类:
import java.io.IOException;
import java.io.InputStream;

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


public class FactoryUtil {
    private static SqlSessionFactory sessionFactory=null;  
    static{//单例
        if(sessionFactory==null){
            String url="mybatis-config.xml";
            InputStream is=null;
            try {
                is = Resources.getResourceAsStream(url);
                sessionFactory=new SqlSessionFactoryBuilder().build(is);
                
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }    
        }   
    }
    public static SqlSession getSession(){
       return sessionFactory.openSession();
       
    }
    
}

GradeMapper接口:

public interface GradeMapper {
    public Grade findGrade(Integer gid);
}

StudentMapper接口:

public interface StudentMapper {
    public Student findStudent(Integer gradeId);
}

GradeMapper.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.czy.mappers.GradeMapper">
	<resultMap type="Grade" id="grade">
		<id property="gid" column="gid"/>
		<result property="gname" column="gname"/>
		<collection property="students" ofType="Student">
			<id property="id" column="id"/>
			<result property="name" column="name"/>
			<result property="gradeId" column="gradeId"/>
		</collection>
	</resultMap>
	<select id="findGrade" parameterType="Integer" resultMap="grade">
		select *from t_grade t1,t_student t2 where t1.gid=t2.gradeId and t1.gid=#{gid}
	</select>
</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="com.czy.mappers.StudentMapper">
	<resultMap type="Student" id="student">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="gradeId" column="gradeId"/>
	</resultMap>
	<select id="findStudent" parameterType="Integer" resultMap="student">
		select *from t_student where gradeId=#{gradeId}
	</select>
	
</mapper>
main:
public class Tmain {
   public static void main(String[] args) {
       SqlSession session=FactoryUtil.getSession();
       GradeMapper mapper=session.getMapper(GradeMapper.class);
       Grade grade=mapper.findGrade(1);
       System.out.println(grade.toString());
       ArrayList<Student> list=(ArrayList<Student>) grade.getStudents();
       for(Student one:list){
           System.out.println(one.toString());
       }
       
   }
   
}

运行结果:


上面写的是一对多,也就是通过年级(一)来查找学生(多),下面将在上面的基础上加上多对一,也就是通过学生来查询年级

Student类参照上文,不做修改

Grade类:添加一个Grade对象,后面set 、get方法加上

public class Student {
    private int id;
    private String name;
    private int gradeId;
    private Grade grade;

GradeMapper接口:

public interface GradeMapper {
    public Grade findGrade(Integer gid);//通过gid来找Grade
    public Grade toStudentfindGrade(Integer gid);//通过gid来查找grade,但是这个是用StudentMapper发起查找,是先调用StudentMapper中的findStudentId方法然后将gradeId传过来做参数
}

StudentMapper:

public interface StudentMapper {
    
    public Student findStudentid(Integer id);//通过id来查找student
}

GradeMapper.xml

    

<mapper namespace="com.czy.mappers.GradeMapper">
	<resultMap type="Grade" id="grade">
		<id property="gid" column="gid"/>
		<result property="gname" column="gname"/>
		<collection property="students" ofType="Student">
			<id property="id" column="id"/>
			<result property="name" column="name"/>
			<result property="gradeId" column="gradeId"/>
		</collection>
	</resultMap>
	
	<select id="findGrade" parameterType="Integer" resultMap="grade">
		select *from t_grade t1,t_student t2 where t1.gid=t2.gradeId and t1.gid=#{gid}
	</select>
	<select id="toStudentfindGrade" parameterType="Integer" resultMap="grade">
		select *from t_grade where gid=#{gid} 
	</select>
</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="com.czy.mappers.StudentMapper">
	<resultMap type="Student" id="student">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="gradeId" column="gradeId"/>
		<association property="grade" select="com.czy.mappers.GradeMapper.toStudentfindGrade" column="gid"/>
	</resultMap>
	<select id="findStudent" parameterType="Integer" resultMap="student">
		select *from t_student  where gradeId=#{gradeId} 
	</select>
	<select id="findStudentid" parameterType="Integer" resultMap="student">
		select *from t_student t1,t_grade t2 where t1.gradeId=t2.gid and t1.id=#{id}
	</select>
	
</mapper>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值