Mybatis中collection、association标签使用
数据库环境搭建:
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');
pojo
Student
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author Zhujintao 2021/1/19 20:47
* @desc Student.
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("学生实体")
public class Student {
@ApiModelProperty("学生ID")
private Integer id;
@ApiModelProperty("学生姓名")
private String name;
@ApiModelProperty("教师ID")
private Integer tid;
}
Teacher
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
/**
* @author Zhujintao 2021/1/19 20:47
* @desc Teacher.
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("教师实体")
public class Teacher {
@ApiModelProperty("教师ID")
private Integer id;
@ApiModelProperty("教师姓名")
private String name;
}
controller
StudentController
import com.adrian.common.R;
import com.adrian.pojo.Student;
import com.adrian.service.StudentService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author Zhujintao 2021/1/19 20:52
* @desc StudentController.
*/
@Api(tags = "学生管理")
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("getStudentList")
public R getStudentList(@RequestParam(value = "tid") Integer tid){
List<Student> studentList = this.studentService.getStudentList(tid);
return R.ok().data("data",studentList);
}
@GetMapping("getStudentByNameAndTid")
public R getStudentByNameAndTid(@RequestParam(value = "name") String name , @RequestParam(value = "tid") String tid) {
List<Student> studentList = this.studentService.getStudentByNameAndTid(name,tid);
return R.ok().data("data",studentList);
}
}
TeacherController
import com.adrian.common.R;
import com.adrian.pojo.Teacher;
import com.adrian.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Zhujintao 2021/1/19 20:52
* @desc TeacherController.
*/
@Api(tags = "教师管理")
@RestController
@RequestMapping("/teacher")
public class TeacherController {
@Autowired
private TeacherService teacherService;
@ApiOperation("根据ID获取Teacher")
@GetMapping("getTeacher")
public R getTeacher(@RequestParam(value = "id") Integer id){
Teacher teacher = this.teacherService.getTeacher(id);
return R.ok().data("data",teacher);
}
@ApiOperation("根据ID获取Teacher和对应学生")
@GetMapping("getTeacherAndStudent")
public R getTeacherAndStudent(@RequestParam(value = "id") Integer id){
Teacher teacher = this.teacherService.getTeacherAndStudent(id);
return R.ok().data("data",teacher);
}
}
service接口
StudentService
import com.adrian.pojo.Teacher;
import com.adrian.pojo.Student;
import java.util.List;
/**
* @author Zhujintao 2021/1/19 20:52
* @desc StudentService.
*/
public interface StudentService {
List<Student> getStudentList(Integer tid);
List<Student> getStudentByNameAndTid(String name, String tid);
}
TeacherService
import com.adrian.pojo.Teacher;
/**
* @author Zhujintao 2021/1/19 20:53
* @desc TeacherService.
*/
public interface TeacherService {
Teacher getTeacher(Integer id);
Teacher getTeacherAndStudent(Integer id);
}
service实现类
StudentServiceImpl
import com.adrian.dao.StudentDao;
import com.adrian.pojo.Student;
import com.adrian.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Zhujintao 2021/1/19 20:53
* @desc StudentServiceImpl.
*/
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Override
public List<Student> getStudentList(Integer tid) {
return this.studentDao.getStudentList(tid);
}
@Override
public List<Student> getStudentByNameAndTid(String name, String tid) {
Map<String,Object> map = new HashMap<>();
map.put("name",name);
map.put("tid",tid);
return this.studentDao.getStudentByNameAndTid(map);
}
}
TeacherServiceImpl
import com.adrian.dao.TeacherDao;
import com.adrian.pojo.Teacher;
import com.adrian.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author Zhujintao 2021/1/19 20:53
* @desc TeacherServiceImpl.
*/
@Service
public class TeacherServiceImpl implements TeacherService {
@Autowired
private TeacherDao teacherDao;
@Override
public Teacher getTeacher(Integer id) {
return this.teacherDao.getTeacher(id);
}
@Override
public Teacher getTeacherAndStudent(Integer id) {
return this.teacherDao.getTeacherAndStudent(id);
}
}
Dao接口
import com.adrian.pojo.Student;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
import java.util.Map;
/**
* @author Zhujintao 2021/1/19 20:54
* @desc StudentDao.
*/
@Mapper
public interface StudentDao {
List<Student> getStudentList(Integer tid);
List<Student> getStudentByNameAndTid(Map<String, Object> map);
}
import com.adrian.pojo.Teacher;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* @author Zhujintao 2021/1/19 20:54
* @desc TeacherDao.
*/
@Mapper
public interface TeacherDao {
Teacher getTeacher(Integer id);
Teacher getTeacherAndStudent(Integer id);
}
Mapper.xml
<?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="com.adrian.dao.StudentDao">
<resultMap id="baseResultMap" type="com.adrian.pojo.Student">
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="name" jdbcType="VARCHAR" property="name"/>
<result column="tid" jdbcType="INTEGER" property="tid"/>
</resultMap>
<select id="getStudents" resultMap="StudentTeacher">
select * from student;
</select>
<resultMap id="StudentTeacher" type="com.adrian.vo.StudentVo" extends="baseResultMap">
<association property="teacher" javaType="com.adrian.pojo.Teacher"
select="com.adrian.dao.TeacherDao.getTeacher"
column="{id=tid}"/>
</resultMap>
</mapper>
<?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="com.adrian.dao.TeacherDao">
<resultMap id="BaseResultMap" type="com.adrian.pojo.Teacher">
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="name" jdbcType="VARCHAR" property="name"/>
</resultMap>
<select id="getTeacherAndStudent" resultMap="TeacherStudent">
select * from teacher where id = #{id}
</select>
<resultMap id="TeacherStudent" type="com.adrian.vo.TeacherVo" extends="BaseResultMap">
<collection property="studentList" javaType="java.util.ArrayList" ofType="com.adrian.pojo.Student"
select="com.adrian.dao.StudentDao.getStudentList"
column="{tid = id}"/>
</resultMap>
</mapper>