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
    
  • 相关阅读:
    【数学】三分法
    【数学】【背包】【NOIP2018】P5020 货币系统
    【数学】【CF27E】 Number With The Given Amount Of Divisors
    【单调队列】【P3957】 跳房子
    【极值问题】【CF33C】 Wonderful Randomized Sum
    【DP】【CF31E】 TV Game
    【神仙题】【CF28D】 Don't fear, DravDe is kind
    【线段树】【CF19D】 Points
    【字符串】KMP字符串匹配
    【二维树状数组】【CF10D】 LCIS
  • 原文地址:https://www.cnblogs.com/tengpengfei/p/10453923.html
Copyright © 2011-2022 走看看