欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

多对多映射

程序员文章站 2022-04-24 11:32:29
...
<?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">
<!-- com.mybatis.mappers.StudentMapper是我们定义接口的全限定名字 这样就可以使用接口调用映射的SQL语句了 这个名字一定要和接口对应上 -->
<mapper namespace="com.mybatis.mappers.Many2ManyMapper">
	<insert id="insertStudent" parameterType="Student">
	   insert into student values(#{id},#{name},#{gender},#{major},#{grade})
	</insert>
	<insert id="insertCourse" parameterType="Course">
	   insert into course values(#{id},#{courseCode},#{courseName})
	</insert>
	<select id="selectStudentById" parameterType="int" resultType="Student">
		select * from student where id=#{id}
	</select>
	<select id="selectCourseById" parameterType="int" resultType="Course">
		select id,course_code as courseCode,course_name  courseName from course where id=#{id}
	</select>
	<insert id="studentSelectCourse">
        <!-- param1代表方法中第一个参数 以此类推 -->
            insert into
                student_course(id,student_id,course_id)
            values
                (#{param1},#{param2.id},#{param3.id})
	</insert>
      <!-- 如果有特殊符号的话 需要用 <![CDATA[ 特殊符号 ]]>  例如 < & 等等 -->
        <select id="getStudentByIdOnCondition" parameterType="int" resultType="Student">
             <![CDATA[
             select *
             from student
             where id <  #{id}
             ]]>
        </select>
     <!--  这里使用了嵌套结果ResultMap的方式进行级联查询,当然也可以使用嵌套查询select -->
     <!-- 映射一个基本的Student查询结果 -->
        <resultMap id="StudentResult" type="Student">
            <id property="id" column="id"/>
            <result property="name" column="name"/>
            <result property="gender" column="gender"/>
            <result property="major" column="major"/>
            <result property="grade" column="grade"/>
        </resultMap>
        <!-- 继承上面那个基本的映射,再扩展出级联查询 -->
        <resultMap id="StudentResultWithCourses" type="Student" extends="StudentResult">
            <collection property="courses" resultMap="CourseResult"></collection>
        </resultMap>
        <!-- 这里特别要是的是column="cid" 这是和select语句中的 c.id as cid对应的 一定一定一定要对应起来 -->
        <resultMap id="CourseResult" type="Course">
            <id property="id" column="cid"/>
            <result property="courseCode" column="course_code"/>
            <result property="courseName" column="course_name"/>
        </resultMap>
        <!--  
            注意:查询语句的中的c.id as cid这个地方,避免名字相同出现查询结果不正确的情况
            同时在id="CourseResult"的resultMap中也有与这里对应的设置要特别特别注意
        -->
        <select id="getStudentByIdWithCourses" parameterType="int" resultMap="StudentResultWithCourses">
            select s.id,s.name,s.gender,s.major,s.grade,c.id as cid,c.course_code,c.course_name,sc.id,sc.student_id,sc.course_id
            from student s,course c,student_course sc
            where  
            s.id=#{id}
            and
            s.id=sc.student_id  
            and  
            sc.course_id=c.id
        </select>

</mapper>

pojo

public class Student {
            private Integer id;
            private String name; // 姓名
            private String gender; // 性别
            private String major; // 专业
            private String grade; // 年级
            private List<Course> courses;// 所选的课程
		public Student(Integer id, String name, String gender,String major, String grade, List<Course> courses) {
				super();
				this.id = id;
				this.name = name;
				this.gender = gender;
				this.major = major;
				this.grade = grade;
				this.courses = courses;
			}
		public Student(Integer id, String name, String gender, String major,
				String grade) {
			super();
			this.id = id;
			this.name = name;
			this.gender = gender;
			this.major = major;
			this.grade = grade;
		}
		public Student() {
			super();
		}
		public Integer getId() {
			return id;
		}
		public void setId(Integer id) {
			this.id = id;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getGender() {
			return gender;
		}
		public void setGender(String gender) {
			this.gender = gender;
		}
		public String getMajor() {
			return major;
		}
		public void setMajor(String major) {
			this.major = major;
		}
		public String getGrade() {
			return grade;
		}
		public void setGrade(String grade) {
			this.grade = grade;
		}
		public List<Course> getCourses() {
			return courses;
		}
		public void setCourses(List<Course> courses) {
			this.courses = courses;
		}
		@Override
		public String toString() {
			return "Student [id=" + id + ", name=" + name + ", gender="
					+ gender + ", major=" + major + ", grade=" + grade
					+ ", courses=" + courses + "]";
		}
            

}