zoukankan      html  css  js  c++  java
  • 4.SSM整合_多表_多对多的增删改查

    多对多关系,课程和学生

    接口

    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
    
  • 相关阅读:
    HDU_2191_多重背包
    HDU_1494_dp
    POJ_1088_dfs
    所有的畅通工程[HDU1232][HDU1874][HDU1875][HDU1879]
    畅通工程[HDU1863]
    还是畅通工程[HDU1233]
    最小生成树
    Who's in the Middle[HDU1157]
    Bungee Jumping[HDU1155]
    Is It A Tree?[HDU1325][PKU1308]
  • 原文地址:https://www.cnblogs.com/tengpengfei/p/10453923.html
Copyright © 2011-2022 走看看