前言:
Mybatis一对多的处理关系:
一个人有好多本书,每本书的主人只有一个人。当我们查询某个人拥有的所有书籍时,就涉及到了一对多的映射关系。
一、添加数据表:
1 CREATE TABLE `book` ( 2 `id` int(6) NOT NULL, 3 `name` varchar(50) DEFAULT NULL, 4 `uid` int(6) DEFAULT NULL, 5 `price` double DEFAULT NULL, 6 PRIMARY KEY (`id`), 7 KEY `bu_id` (`uid`), 8 CONSTRAINT `bu_id` FOREIGN KEY (`uid`) REFERENCES `user` (`id`) 9 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
二、代码实现:
1.添加Book实体:
1 package com.beilin.entity; 2 /* 3 * 书的实体 4 * @author 北林 5 * 6 */ 7 8 public class Book { 9 10 private int id; 11 private int uid; 12 private String name; 13 private double price; 14 15 public int getId() { 16 return id; 17 } 18 19 public void setId(int id) { 20 this.id = id; 21 } 22 23 public int getUid() { 24 return uid; 25 } 26 27 public void setUid(int uid) { 28 this.uid = uid; 29 } 30 31 public String getName() { 32 return name; 33 } 34 35 public void setName(String name) { 36 this.name = name; 37 } 38 39 public double getPrice() { 40 return price; 41 } 42 43 public void setPrice(double price) { 44 this.price = price; 45 } 46 }
2.在User实体中添加book集合:
1 public class User { 2 /** 3 * name:学生实体 4 */ 5 6 //主键id 7 private int id; 8 //姓名 9 private String name; 10 //年龄 11 private int age; 12 //添加book集合 13 private List<Book> books; 14 15 // Get和 Set方法 16 public int getId() { 17 return id; 18 } 19 20 public void setId(int id) { 21 this.id = id; 22 } 23 24 public String getName() { 25 return name; 26 } 27 28 public void setName(String name) { 29 this.name = name; 30 } 31 32 public int getAge() { 33 return age; 34 } 35 36 public void setAge(int age) { 37 this.age = age; 38 } 39 40 public List<Book> getBooks() { 41 return books; 42 } 43 44 public void setBooks(List<Book> books) { 45 this.books = books; 46 } 47 }
3.在UserMapper接口中定义查询方法:
1 package com.beilin.mapper; 2 3 import com.beilin.entity.User; 4 5 import java.util.List; 6 7 public interface UserMapper { 8 9 //插入 10 public void insert(User user); 11 12 //根据id删除 13 public void delete(Integer id); 14 15 //根据user的id修改 16 public void update(User user); 17 18 //根据id查询 19 public User getById(Integer id); 20 21 //查询全部 22 public List<User> list(); 23 24 /** 25 * 根据id查询所有的书 26 * @param id 27 */ 28 public User selectBookById(Integer id); 29 30 31 }
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.beilin.mapper.UserMapper"> 4 5 <!-- 插入一个user --> 6 <insert id="insert" parameterType="user" useGeneratedKeys="true" keyProperty="id"> 7 insert into user(name,age) values(#{name},#{age}) 8 </insert> 9 10 <!-- 根据id删除user --> 11 <delete id="delete" parameterType="int"> 12 delete from user where id=#{id} 13 </delete> 14 15 <!-- 根据id修改user信息 --> 16 <update id="update" parameterType="user"> 17 update user set name=#{name},age=#{age} where id=#{id} 18 </update> 19 20 <!-- 根据id查询 --> 21 <select id="getById" parameterType="int" resultType="user"> 22 select * from user where id=#{id} 23 </select> 24 25 <!-- 查询所有 --> 26 <select id="list" parameterType="int" resultType="user"> 27 select * from user 28 </select> 29 30 31 <resultMap id="bookMap" type="user"> 32 <id property="id" column="id"/> 33 <result property="name" column="name"/> 34 <result property="age" column="age"/> 35 <collection property="books" ofType="book"> 36 <id property="id" column="bid"/> 37 <result property="name" column="bookName"/> 38 <result property="price" column="price"/> 39 </collection> 40 </resultMap> 41 42 <!--根据id查询所有的书 --> 43 <select id="selectBookById" parameterType="int" resultMap="bookMap"> 44 select a.*,b.id bid,b.name as "bookName" ,b.price from user a,book b where a.id=b.uid and a.id=#{id}; 45 </select> 46 47 </mapper>
5.在UserController中实现查询:
1 package com.beilin.controller; 2 3 import com.beilin.entity.Book; 4 import com.beilin.entity.User; 5 import com.beilin.mapper.UserMapper; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.web.bind.annotation.*; 8 9 import java.util.List; 10 11 @RestController 12 public class UserController { 13 14 @Autowired 15 private UserMapper userMapper; 16 17 //插入user 18 @RequestMapping("/user") 19 public void insert( User user) { 20 userMapper.insert(user); 21 } 22 23 //根据id删除 24 @RequestMapping("/user1/{id}") 25 public void delete(@PathVariable("id") Integer id) { 26 userMapper.delete(id); 27 } 28 //修改 29 @RequestMapping("/user2/{id}") 30 public void update(User user,@PathVariable("id") Integer id) { 31 userMapper.update(user); 32 } 33 34 //根据id查询user 35 @RequestMapping("/user3/{id}") 36 public User getById(@PathVariable("id") Integer id) { 37 User user = userMapper.getById(id); 38 return user; 39 } 40 41 //查询全部 42 @RequestMapping("/users") 43 public List<User> list(){ 44 List<User> users = userMapper.list(); 45 return users; 46 } 47 48 /** 49 * 根据id查询所有的书 50 */ 51 @GetMapping("/user/book/{id}") 52 public User getBooks(@PathVariable("id") Integer id){ 53 User user = userMapper.selectBookById(id); 54 return user; 55 } 56 }
三、测试结果:
1.数据库查询结果:
2.postman访问结果: