SSM-Mybatis(3)

Complex sql query

Environment setup

CREATE TABLE `teacher` (
  `id` INT(10) NOT NULL,
  `name` VARCHAR(30) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8

INSERT INTO teacher(id, name) VALUES (1,'秦老师'); 

CREATE TABLE `student` (
  `id` INT(10) NOT NULL,
  `name` VARCHAR(30) DEFAULT NULL,
  `tid` INT(10) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fktid` (`tid`),
  CONSTRAINT `fktid` FOREIGN KEY (`tid`) REFERENCES `teacher` (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `student` (`id`, `name`, `tid`) VALUES (1, '小明', 1); 
INSERT INTO `student` (`id`, `name`, `tid`) VALUES (2, '小红', 1); 
INSERT INTO `student` (`id`, `name`, `tid`) VALUES (3, '小张', 1); 
INSERT INTO `student` (`id`, `name`, `tid`) VALUES (4, '小李', 1); 
INSERT INTO `student` (`id`, `name`, `tid`) VALUES (5, '小王', 1);

Test environment setup

  1. New entity class Teacher Student
  2. Establish Mapper interface
  3. Resume Mapper.xml
  4. Binding the registered Mapper interface in the core configuration file
  5. Test whether the query can be successful
    Insert picture description here
package com.kuang.pojo;

public class Student {
    
    
    private int id;
    private String name;
    //学生需要关联一个老师!
    private Teacher teacher;
}
package com.kuang.pojo;

public class Teacher {
    
    
    private int id;
    private String name;
    //一个老师有多个学生
    private List<Student> students;
}

Many-to-one instance

public interface StudentMapper {
    
    
    //查询所有的学生信息,以及对应的老师的信息
    public List<Student> getStudent();
}

Follow query nesting processing

<mapper namespace="com.kuang.dao.StudentMapper">
    <!--
        1.查询所有的学生信息
        2.根据查询出来的学生的id,寻找对应的老师
    -->
    <select id="getStudent" resultMap="StudentTeacher">
        select  s.id,s.name,t.name from student s,teacher t where s.tid = t.id;
    </select>
    <resultMap id="StudentTeacher" type="Student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <!--复杂的属性,我们需要单独处理
            对象:association
            集合:collection
        -->
        <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
    </resultMap>
    <select id="getTeacher" resultType="Teacher">
        select * from mybatis.teacher where id = #{id}
    </select>
    
</mapper>

Insert picture description here

Nested processing according to results

<!--方式二:-->
    <select id="getStudent" resultMap="StudentTeacher">
        select  s.id sid,s.name sname,t.name tname
        from student s,teacher t
        where s.tid = t.id;
    </select>
    <resultMap id="StudentTeacher" type="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <association property="teacher" column="tname" javaType="Teacher">
            <result property="name" column="tname"/>
        </association>
    </resultMap>

One to many

Nested queries according to results


public interface TeacherMapper {
    
    
    //获取老师
    List<Teacher> getTeacher();
}
<mapper namespace="com.kuang.dao.TeacherMapper">
    <!--按照查询嵌套处理-->
    <select id="getTeacher" resultMap="TeacherStudent">
        select s.id sid,s.name sname,t.name tname,t.id tid
        from mybatis.student s,mybatis.teacher t
        where s.id = 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"/>
            <result property="tid" column="tid"/>
        </collection>
    </resultMap>
</mapper>

Follow query nesting processing

<mapper namespace="com.kuang.dao.TeacherMapper">


    <select id="getTeacher" resultMap="TeacherStudent">
        select * from mybatis.teacher where id = #{tid}
    </select>
    <resultMap id="TeacherStudent" type="Teacher">
        <collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacher" column="id"/>
    </resultMap>
    <select id="getStudentByTeacherId" resultType="Student">
        select * from mybatis.student where tid = #{tid}
    </select>
</mapper>

summary

  1. Association-association [many to one]
  2. Collection-collection[-to-many]
  3. javaType & ofType
    • javaType is used to specify
    • ofType is used to specify the pojo type mapped to the List or collection, the type in the generic

Dynamic SQL

Dynamic SQL is to generate different SQL statements according to different conditions

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG WHERE state = ‘ACTIVE’
  <if test="title != null">
    AND title like #{title}
  </if>
  <if test="author != null and author.name != null">
    AND author_name like #{author.name}
  </if>
</select>

foreach
Insert picture description here

Guess you like

Origin blog.csdn.net/m0_46656833/article/details/112989176