开发工具:STS
代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis/tree/c00b56dbd51a1e26ab9fd990201ad9e89a770ca9
前言:
今天探讨的是Mybatis一对多的处理关系。
一个人有好多本书,每本书的主人只有一个人。当我们查询某个人拥有的所有书籍时,就涉及到了一对多的映射关系。
一、添加数据表:
二、代码实现:
1.添加Book实体:
1 package com.xm.pojo; 2 /** 3 * 书的实体 4 * @author xm 5 * 6 */ 7 public class Book { 8 9 private int id; 10 private int sid; 11 private String name; 12 private double price; 13 public int getId() { 14 return id; 15 } 16 public void setId(int id) { 17 this.id = id; 18 } 19 public int getSid() { 20 return sid; 21 } 22 public void setSid(int sid) { 23 this.sid = sid; 24 } 25 public String getName() { 26 return name; 27 } 28 public void setName(String name) { 29 this.name = name; 30 } 31 public double getPrice() { 32 return price; 33 } 34 public void setPrice(double price) { 35 this.price = price; 36 } 37 38 39 40 }
2.在Student实体中添加book集合:
1 package com.xm.pojo; 2 3 import java.util.List; 4 5 /** 6 * name:学生实体 7 * @author xxm 8 * 9 */ 10 public class Student { 11 /** 12 * content:主键id 13 */ 14 private int id; 15 /** 16 * content:姓名 17 */ 18 private String name; 19 20 private List<Book> books; 21 22 public Student() { 23 // TODO Auto-generated constructor stub 24 } 25 26 27 public List<Book> getBooks() { 28 return books; 29 } 30 31 32 public void setBooks(List<Book> books) { 33 this.books = books; 34 } 35 36 37 public int getId() { 38 return id; 39 } 40 public void setId(int id) { 41 this.id = id; 42 } 43 public String getName() { 44 return name; 45 } 46 public void setName(String name) { 47 this.name = name; 48 } 49 50 51 52 }
3.在StudentMapper接口中定义查询方法:
1 package com.xm.mapper; 2 3 import java.util.List; 4 5 import com.xm.pojo.Student; 6 7 public interface StudentMapper { 8 9 /** 10 * 根据id查询 11 * @param id 12 * @return 13 */ 14 public Student getById(Integer id); 15 16 /** 17 * 查询全部 18 * @return 19 */ 20 public List<Student> list(); 21 22 /** 23 * 插入 24 * @param student 25 */ 26 public int insert(Student student); 27 /** 28 * 主键回填的插入 29 * @param student 30 * @return 31 */ 32 public int insertToId(Student student); 33 34 /** 35 * 根据student的id修改 36 * @param student 37 */ 38 public void update(Student student); 39 40 /** 41 * 根据id删除 42 * @param id 43 */ 44 public void delete(Integer id); 45 46 /** 47 * 根据id查询所有的书 48 * @param id 49 */ 50 public Student selectBookById(Integer id); 51 52 }
4.在mapper映射关系中,添加一对多的select和resaultMap:
注意:当多个表的字段名一样的时候,查询需要用别名。
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <mapper namespace="com.xm.mapper.StudentMapper"> 4 5 <!-- 根据id查询 --> 6 <select id="getById" parameterType="int" resultType="student"> 7 select * from student where id=#{id} 8 </select> 9 <!-- 查询所有 --> 10 <select id="list" parameterType="int" resultType="student"> 11 select * from student 12 </select> 13 14 <!-- 插入一个学生 --> 15 <insert id="insert" parameterType="student"> 16 insert into student(name) values(#{name}) 17 </insert> 18 <!-- 主键回填的插入 --> 19 <insert id="insertToId" parameterType="student" useGeneratedKeys="true" keyProperty="id"> 20 insert into student(name) values(#{name}) 21 </insert> 22 23 <!-- 根据id修改学生信息 --> 24 <update id="update" parameterType="student"> 25 update student set name=#{name} where id=#{id} 26 </update> 27 28 <!-- 根据id删除学生 --> 29 <delete id="delete" parameterType="int"> 30 delete from student where id=#{id} 31 </delete> 32 33 <resultMap type="student" id="bookMap"> 34 <id property="id" column="id"/> 35 <result property="name" column="name"/> 36 <collection property="books" ofType="book"> 37 <id property="id" column="bid"/> 38 <result property="name" column="bname"/> 39 <result property="price" column="price"/> 40 </collection> 41 </resultMap> 42 <!--根据id查询所有的书 --> 43 <select id="selectBookById" parameterType="int" resultMap="bookMap"> 44 select a.*,b.id bid,b.name bname,b.price from student a,book b where a.id=b.sid and a.id=#{id}; 45 </select> 46 </mapper>
5.在StudentController中实现查询:
1 package com.xm.controller; 2 3 import java.util.List; 4 5 import javax.websocket.server.PathParam; 6 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.web.bind.annotation.DeleteMapping; 9 import org.springframework.web.bind.annotation.GetMapping; 10 import org.springframework.web.bind.annotation.PathVariable; 11 import org.springframework.web.bind.annotation.PostMapping; 12 import org.springframework.web.bind.annotation.PutMapping; 13 import org.springframework.web.bind.annotation.RestController; 14 15 import com.xm.mapper.StudentMapper; 16 import com.xm.pojo.Student; 17 18 @RestController 19 public class StudentController { 20 @Autowired 21 private StudentMapper studentMapper; 22 23 /** 24 * 根据id查询学生 25 * @param id 26 * @return 27 */ 28 @GetMapping("/student/{id}") 29 public Student getById(@PathVariable("id") Integer id) { 30 31 Student student = studentMapper.getById(id); 32 return student; 33 34 } 35 36 /** 37 * 查询全部 38 * @return 39 */ 40 @GetMapping("/students") 41 public List<Student> list(){ 42 List<Student> students = studentMapper.list(); 43 return students; 44 } 45 46 /** 47 * 插入 48 * @param student 49 */ 50 @PostMapping("/student") 51 public void insert( Student student) { 52 studentMapper.insert(student); 53 } 54 55 /** 56 * 修改 57 * @param student 58 */ 59 @PutMapping("/student/{id}") 60 public void update(Student student,@PathVariable("id")Integer id) { 61 studentMapper.update(student); 62 } 63 64 /** 65 * 根据id删除 66 * @param id 67 */ 68 @DeleteMapping("/student/{id}") 69 public void delete(@PathVariable("id") Integer id) { 70 studentMapper.delete(id); 71 } 72 73 /** 74 * 根据id查询所有的书 75 */ 76 @GetMapping("/student/book/{id}") 77 public Student getBooks(@PathVariable("id") Integer id) { 78 Student student = studentMapper.selectBookById(id); 79 return student; 80 } 81 82 }
三、测试结果:
1.数据库查询结果:
2.postman访问结果:
2018-06-20