mybatis mapping between understanding

1, two-table query association mapping:

1.1, StudentMapper.xml relevant code

  <resultMap id="list" type="com.aibaiyang.idemo.dto.StudentCourseDTO">
  </resultMap>

 

  <select id="findByName" parameterType="com.aibaiyang.idemo.entity.Student" resultMap="list">
        SELECT
            s.id id,s.name `name`,c.`name` course_name
        FROM
            student s
        INNER JOIN course c ON s.course_id = c.id WHERE 1 = 1
        <if test="name != null and !&quot;&quot;.equals(name.trim())">
          and s.name like CONCAT('%',#{name},'%')
        </if>
        <if test="courseId != null">
          and s.course_id = #{courseId}
        </if>
  </select>

 

1.2, application.yml profile settings underscore turn hump

mybatis:
  configuration:
    map-underscore-to-camel-case: true

 

1.3, StudentCourseDTO categories:

package com.aibaiyang.idemo.dto;

import lombok.Data;

/**
 * @Author zhong guo
 * @Date 2019/10/6 12:38
 * @description
 **/
@Data
public class StudentCourseDTO {

    private Integer id;

    private String name;

    private String courseName;

}

 

2, main table associated with the child table:

2.1, StudentMapper.xml relevant code

  <resultMap id="BaseResultMap" type="com.aibaiyang.idemo.dto.StudentCourseOutput" >
    <id column="s_id" property="id" jdbcType="INTEGER" />
    <result column="s_name" property="name" jdbcType="VARCHAR" />
    <collection property="courses" resultMap="CourseMapper.BaseResultMap" />
  </resultMap>

  <select id="findAll" resultMap="BaseResultMap">
        SELECT
            s.id s_id,
            s.`name` s_name,
            c.id c_id,
            c.code,
            c.`name` c_name
        FROM
            student s
        INNER JOIN course c ON s.course_id = c.id
  </select>

 

2.2, CourseMapper.xml relevant code:

<?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" >
<mapper namespace="CourseMapper" >
  <resultMap id="BaseResultMap" type="com.aibaiyang.idemo.entity.Course" >
    <id column="c_id" property="id" jdbcType="INTEGER" />
    <result column="code" property="code" jdbcType="VARCHAR" />
    <result column="c_name" property="name" jdbcType="VARCHAR" />
  </resultMap>
</mapper>

 

2.3、StudentCourseOutput 类:

package com.aibaiyang.idemo.dto;

import com.aibaiyang.idemo.entity.Course;
import lombok.Data;

import java.util.List;

/**
 * @Author zhong guo
 * @Date 2019/10/6 15:37
 * @description
 **/
@Data
public class StudentCourseOutput {

    private Integer id;

    private String name;

    private List<Course> courses;

}

 

其中 <collection property="courses" resultMap="CourseMapper.BaseResultMap" /> 的

property="courses"对应 private List<Course> courses;

resultMap = "CourseMapper.BaseResultMap" corresponding to BaseResultMap under CourseMapper namespace;

 

3, Project address: idemo-the mybatis

 

Guess you like

Origin www.cnblogs.com/aibaiyang/p/11628436.html