<?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.ccy.dao.TeacherMapper">
<!-- 第一种-->
<select id="getTeacherList" resultType="teacher">
SELECT
*
FROM
teacher
</select>
<select id="getTeacherById" 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 =#{tid}
</select>
<resultMap id="TeacherStudent" type="teacher">
<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"/>
<!-- <association property="teacher" javaType="teacher">-->
<!-- <result property="name" column="tname" />-->
<!-- </association>-->
</collection>
</resultMap>
<!-- 第二种-->
<select id="getTeacherById2" resultMap="TeacherStudent2">
select * from teacher where id=#{tid}
</select>
<resultMap id="TeacherStudent2" type="teacher">
<result column="id" property="id"/>
<collection property="students" column="id" ofType="student" select="getStudent" javaType="ArrayList">
</collection>
</resultMap>
<select id="getStudent" resultType="student">
select * from student where tid=#{tid}
</select>
</mapper>
mybatis一对多查询
最新推荐文章于 2023-03-24 09:22:58 发布