开发工具:STS
代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis/tree/c00b56dbd51a1e26ab9fd990201ad9e89a770ca9
前言:
今天探讨的是Mybatis一对多的处理关系。
一个人有好多本书,每本书的主人只有一个人。当我们查询某个人拥有的所有书籍时,就涉及到了一对多的映射关系。
一、添加数据表:
二、代码实现:
1.添加Book实体:
package com.xm.pojo;
/**
* 书的实体
* @author xm
*
*/
public class Book {
private int id;
private int sid;
private String name;
private double price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
Book.java
2.在Student实体中添加book集合:
package com.xm.pojo;
import java.util.List;
/**
* name:学生实体
* @author xxm
*
*/
public class Student {
/**
* content:主键id
*/
private int id;
/**
* content:姓名
*/
private String name;
private List<Book> books;
public Student() {
// TODO Auto-generated constructor stub
}
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Student.java
3.在StudentMapper接口中定义查询方法:
package com.xm.mapper;
import java.util.List;
import com.xm.pojo.Student;
public interface StudentMapper {
/**
* 根据id查询
* @param id
* @return
*/
public Student getById(Integer id);
/**
* 查询全部
* @return
*/
public List<Student> list();
/**
* 插入
* @param student
*/
public int insert(Student student);
/**
* 主键回填的插入
* @param student
* @return
*/
public int insertToId(Student student);
/**
* 根据student的id修改
* @param student
*/
public void update(Student student);
/**
* 根据id删除
* @param id
*/
public void delete(Integer id);
/**
* 根据id查询所有的书
* @param id
*/
public Student selectBookById(Integer id);
}
StudentMapper.java
4.在mapper映射关系中,添加一对多的select和resaultMap:
注意:当多个表的字段名一样的时候,查询需要用别名。
<?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.xm.mapper.StudentMapper">
<!-- 根据id查询 -->
<select id="getById" parameterType="int" resultType="student">
select * from student where id=#{id}
</select>
<!-- 查询所有 -->
<select id="list" parameterType="int" resultType="student">
select * from student
</select>
<!-- 插入一个学生 -->
<insert id="insert" parameterType="student">
insert into student(name) values(#{name})
</insert>
<!-- 主键回填的插入 -->
<insert id="insertToId" parameterType="student" useGeneratedKeys="true" keyProperty="id">
insert into student(name) values(#{name})
</insert>
<!-- 根据id修改学生信息 -->
<update id="update" parameterType="student">
update student set name=#{name} where id=#{id}
</update>
<!-- 根据id删除学生 -->
<delete id="delete" parameterType="int">
delete from student where id=#{id}
</delete>
<resultMap type="student" id="bookMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<collection property="books" ofType="book">
<id property="id" column="bid"/>
<result property="name" column="bname"/>
<result property="price" column="price"/>
</collection>
</resultMap>
<!--根据id查询所有的书 -->
<select id="selectBookById" parameterType="int" resultMap="bookMap">
select a.*,b.id bid,b.name bname,b.price from student a,book b where a.id=b.sid and a.id=#{id};
</select>
</mapper>
StudentMapper.xml
5.在StudentController中实现查询:
package com.xm.controller;
import java.util.List;
import javax.websocket.server.PathParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xm.mapper.StudentMapper;
import com.xm.pojo.Student;
@RestController
public class StudentController {
@Autowired
private StudentMapper studentMapper;
/**
* 根据id查询学生
* @param id
* @return
*/
@GetMapping("/student/{id}")
public Student getById(@PathVariable("id") Integer id) {
Student student = studentMapper.getById(id);
return student;
}
/**
* 查询全部
* @return
*/
@GetMapping("/students")
public List<Student> list(){
List<Student> students = studentMapper.list();
return students;
}
/**
* 插入
* @param student
*/
@PostMapping("/student")
public void insert( Student student) {
studentMapper.insert(student);
}
/**
* 修改
* @param student
*/
@PutMapping("/student/{id}")
public void update(Student student,@PathVariable("id")Integer id) {
studentMapper.update(student);
}
/**
* 根据id删除
* @param id
*/
@DeleteMapping("/student/{id}")
public void delete(@PathVariable("id") Integer id) {
studentMapper.delete(id);
}
/**
* 根据id查询所有的书
*/
@GetMapping("/student/book/{id}")
public Student getBooks(@PathVariable("id") Integer id) {
Student student = studentMapper.selectBookById(id);
return student;
}
}
StudentController.java
三、测试结果:
1.数据库查询结果:
2.postman访问结果:
2018-06-20