日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

3.SpringBoot整合Mybatis(一对多)

發布時間:2023/12/8 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 3.SpringBoot整合Mybatis(一对多) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言:

  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 } Book.java

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 } User.java

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 } UserMapper

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> UserMapper.xml

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 } UserController.java

?

三、測試結果:

?

1.數據庫查詢結果:

?

?

?

2.postman訪問結果:

?

轉載于:https://www.cnblogs.com/wx60079/p/11534570.html

總結

以上是生活随笔為你收集整理的3.SpringBoot整合Mybatis(一对多)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。