多對多關系,課程和學生
接口
public interface CourseMapper {
/**
* 獲取所有課程
* @return
* @throws Exception
*/
public List<Course> getAll() throws Exception;
}
映射文件
<mapper namespace="com.demo2.mapper.CourseMapper">
<select id="getAll" resultType="Course">
select t1.c_id id, t1.c_name name, t1.c_credit credit
from t_course t1
</select>
</mapper>
實體類
public class Course {
private Integer id;
private String name;
private Double credit;
private List<Student> students;
//省略getter和setter
多
接口
public interface StudentMapper {
/**
* 查詢所有學生的選課情況
* @return
* @throws Exception
*/
public List<Student> getStuCou() throws Exception;
/**
* 刪除指定id用戶的某門課(根據課程id)的選課情況
* @param StudentCourseLink
* @throws Exception
* 入參為多個參數時,可以使用Map,model,或者@param
*/
public void delStuCouById(@Param("s_id") String s_id, @Param("c_id") String c_id) throws Exception;
/**
* 添加選課
* @param sc
* @throws Exception
*/
public void addCou(Stu_Cou sc) throws Exception;
/**
* 根據ID獲取指定的學生以及選課情況
* @param id
* @return
* @throws Exception
*/
public Student getStuById(String id) throws Exception;
}
映射文件
<mapper namespace="com.demo2.mapper.StudentMapper">
<!-- 多對多,多表查詢 -->
<!-- 查詢有兩種方式, -->
<!--方式一嵌套結果: 就是把所有的字段都映射,一條SQL連表查詢, <collection>標簽映射-->
<resultMap id="stuCouMap" type="Student">
<id column="s_id" property="id"/>
<result column="s_name" property="name"/>
<result column="s_sex" property="sex"/>
<result column="s_age" property="age"/>
<!-- 多對多關聯 -->
<collection property="courses" ofType="Course">
<id column="c_id" property="id"/>
<result column="c_name" property="name"/>
<result column="c_credit" property="credit"/>
</collection>
</resultMap>
<!-- 查詢所有學生及他們的選擇課程的信息,因為返回的結果集所有字段使用result標簽映射實體屬性,所以直接使用*查詢所有 -->
<select id="getStuCou" resultMap="stuCouMap">
select t1.*, t2.*
from t_student t1,
t_course t2,
t_stu_cou t3
where t1.s_id = t3.sc_s_id
and t2.c_id = t3.sc_c_id
</select>
<!--方式二嵌套查詢: 就是把所有的字段都映射,兩條SQL,單獨查詢, <collection>標簽映射-->
<resultMap id="stuCouMap2" type="Student">
<id column="s_id" property="id"/>
<result column="s_name" property="name"/>
<result column="s_sex" property="sex"/>
<result column="s_age" property="age"/>
<!-- 多對多關聯,該column屬性值為id為getStuByIdSelect標簽查詢的結果中的id -->
<collection column="s_id" property="courses" javaType="ArrayList" ofType="Course" select="getCourse">
</collection>
</resultMap>
<!-- 注意此處查詢的課程的字段返回的字段沒有使用result標簽映射實體屬性,所以要使用別名來映射 -->
<select id="getCourse" parameterType="int" resultType="Course">
select t1.c_id id, t1.c_name name, t1.c_credit credit
from t_course t1
left join t_stu_cou t2
on t1.c_id = t2.sc_c_id
where t2.sc_s_id = #{id}
</select>
<!-- 根據學生ID獲取學生信息以及選課信息,此處返回的結果集使用了result標簽映射實體屬性,所以可以直接使用*查詢所有字段 -->
<select id="getStuById" parameterType="String" resultMap="stuCouMap2">
select *
from t_student
where s_id = #{id}
</select>
<!-- 根據學生ID刪除學生的選課信息,入參為多個參數時,可以使用Map,model,或者@param -->
<delete id="delStuCouById">
delete from t_stu_cou
where sc_s_id = #{s_id}
and sc_c_id = #{c_id}
</delete>
<insert id="addCou" parameterType="Stu_Cou">
insert into t_stu_cou(sc_s_id, sc_c_id, createtime) value(#{stu.id}, #{cou.id}, #{createtime})
</insert>
</mapper>
實體類
public class Student {
private Integer id;
private String name;
private String sex;
private Integer age;
private List<Course> courses;
//省略getter和setter
中間表
實體類
public class Stu_Cou {
private Student stu;
private Course cou;
private Date createtime;
//省略getter和setter