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(一对多)相关推荐
- SpringBoot整合Mybatis(高级)
SpringBoot整合Mybatis(高级) 文章目录 SpringBoot整合Mybatis(高级) 前言 基础环境配置 增删改查 ResultMap 复杂查询 多对一 一对多 动态SQL if ...
- SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例(转)...
SpringBoot整合mybatis.shiro.redis实现基于数据库的细粒度动态权限管理系统实例 shiro 目录(?)[+] 前言 表结构 maven配置 配置Druid 配置mybatis ...
- mybatis plugins_[MyBatis] SpringBoot 整合Mybatis
现在基本上搭建一个简单的工程都是三剑客 springboot+mybatis+redis 之前整合Mybatis 都是按照SSM来,所以,这一次带来SpringBoot+MyBatis 的快速整合 p ...
- SpringBoot整合Mybatis超详细流程
SpringBoot整合Mybatis超详细流程 文章目录 SpringBoot整合Mybatis超详细流程 前言 详细流程 0.引入Mybatis 1.创建数据 2.创建程序目录 3.理解后台访问流 ...
- 3、SpringBoot整合MyBatis注解版及配置文件版
目录 1.配置pom.xml 2.配置application.yml 3.配置DruidConfig关联yml的配置文件spring.datasource 4.创建数据库及数据库表结构 5.创建对应的 ...
- (一)SpringBoot 整合 MyBatis
一.工具 IDE:idea.DB:mysql 二.创建SpringBoot工程 在Idea中使用SpringInitializr模板创建SpringBoot工程,依赖选择如下: 这里也可以不选JDBC ...
- SpringBoot整合mybatis进行快速开发
SpringBoot整合mybatis进行数据库操作 1.环境的搭建 pom.xml <!--核心模块,包括自动配置支持.日志和YAML --> <dependencies>& ...
- springboot 整合mybatis实现curd
springboot 整合mybatis pom文件 mvc 架构 application.properties 扩展配置,druid配置类 项目地址: https://github.com/seve ...
- springboot整合mysql5.7_详解SpringBoot整合MyBatis详细教程
1. 导入依赖 首先新建一个springboot项目,勾选组件时勾选Spring Web.JDBC API.MySQL Driver 然后导入以下整合依赖 org.mybatis.spring.boo ...
最新文章
- 判断jQuery库是否被正确引入
- 双一流博士导师整理:最新的计算机视觉学习路线(含时间分配建议)
- OSChina 周六乱弹 —— 老用户与狗
- 20岁以后的男人应该知道的一些事,看一看吧
- CI框架取消index.php
- 物联卡查询流量_物联卡流量查询_python_API文档_开发指南_物联网无线连接服务 - 阿里云...
- 【初学】部署架构相关的一些知识
- 一个比较全介绍UltraGrid的博客
- 数据结构严蔚敏 栈基本操作 C语言实现
- 为什么经转速环PI之后的输出量是电流(基于MTPA分析,内含代码)
- Android - 警告:it is always overridden by the value specified in the Gradle build script
- 第一次OllyDbg逆向记录(分析思路和注意点其他文章)
- 访达前往文件夹_MacOS实用技巧之Finder(访达)的使用
- 计算机网络数据爆分片MTU,踢走绊脚石,MTU解析与常见问题汇总-上篇
- 既是老师又是师兄的临别箴言
- 汉诺塔模拟器java
- 苹果xsmax怎么开机_苹果XSMAX进水不开机苹果售后维修
- ffmpeg php 水平翻转,FFmpeg 视频画面旋转的命令详解及旋转失败的解决方法
- 用 Matlab 实现 GS 算法设计计算全息图
- Camera AF和FF
热门文章
- 开源项目智慧教室:考试作弊系统、动态点名等功能
- STM32的IO口有幺蛾子(bug)
- mysql 瘦身_Mysql瘦身方法_MySQL
- 知名食品品牌全案咨询公司之塔望整体介绍
- 后门触发器之频域角度——Rethinking the Backdoor Attacks’ Triggers A Frequency Perspective
- Android 文件外/内部存储的获取各种存储目录路径
- 靶机11 Empire Lupin One
- CleanMyMac X2022最新mac超强清理系统工具
- np.c_ 对比 np.r_
- 18935 贪吃的小Q