代码编写2(多表联合查询)

代码编写2(多表联合查询)

接口

package edu.hqu.mybatis.dao;

import edu.hqu.mybatis.pojo.Student;

import java.util.List;

public interface StudentMapper {
    List<Student> getAllStudents();
    List<Student> getStudents2();

}

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="edu.hqu.mybatis.dao.StudentMapper">
    <!--只进行一次查询,联表查询-->
    <resultMap id="studentResultMap" type="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <association property="teacher" javaType="Teacher"><!--teacher为学生表中的属性-->
            <result property="id" column="tid"/>
            <result property="name" column="tname"/>
        </association>
    </resultMap>
    <select id="getAllStudents" resultMap="studentResultMap">
            select s.id as sid,s.name as sname,s.tid as stid,t.id as tid,t.name as tname
            from t_student s,t_teacher t where s.tid=t.id;
    </select>


    <!--先查询出所有学生,根据tid查询老师,两次查询-->
    <select id="getStudents2" resultMap="studentTeacher">
        select  * from t_student
    </select>
    <resultMap id="studentTeacher" type="Student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <association property="teacher" column="tid" javaType="Teacher" select="getTeacher">
            <result property="id" column="id"/>
            <result property="name" column="name"/>
        </association>
    </resultMap>
    <select id="getTeacher" resultType="Teacher">
        select * from t_teacher where id=#{tid}
    </select>
</mapper>

测试类

package edu.hqu.mybatis.dao;

import edu.hqu.mybatis.pojo.Student;
import edu.hqu.mybatis.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class StudentMapperTest {
    @Test
    public void testgetAllStudents(){
        SqlSession sqlSession = MybatisUtil.getSqlSession();//获取一个sqlSession
        try{ StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            List<Student> allStudents = mapper.getAllStudents();
            for (Student allStudent : allStudents) {
                System.out.println(allStudent);
            }}
        catch(Exception e)
        {e.printStackTrace();}
        finally {
            sqlSession.close();
        }
    }
    @Test
    public void testgetStudents2(){
        SqlSession sqlSession = MybatisUtil.getSqlSession();//获取一个sqlSession
        try{ StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            List<Student> allStudents = mapper.getStudents2();
            for (Student allStudent : allStudents) {
                System.out.println(allStudent);
            }}
        catch(Exception e)
        {e.printStackTrace();}
        finally {
            sqlSession.close();
        }
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值