前言:

  Mybatis一对多的处理关系:

一个人有好多本书,每本书的主人只有一个人。当我们查询某个人拥有的所有书籍时,就涉及到了一对多的映射关系。

一、添加数据表:

 CREATE TABLE `book` (
`id` int(6) NOT NULL,
`name` varchar(50) DEFAULT NULL,
`uid` int(6) DEFAULT NULL,
`price` double DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `bu_id` (`uid`),
CONSTRAINT `bu_id` FOREIGN KEY (`uid`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

二、代码实现:

1.添加Book实体:

 package com.beilin.entity;
/*
* 书的实体
* @author 北林
*
*/ public class Book { private int id;
private int uid;
private String name;
private double price; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public int getUid() {
return uid;
} public void setUid(int uid) {
this.uid = uid;
} 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.在User实体中添加book集合:

 public class User {
/**
* name:学生实体
*/ //主键id
private int id;
//姓名
private String name;
//年龄
private int age;
//添加book集合
private List<Book> books; // Get和 Set方法
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;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public List<Book> getBooks() {
return books;
} public void setBooks(List<Book> books) {
this.books = books;
}
}

User.java

3.在UserMapper接口中定义查询方法:

 package com.beilin.mapper;

 import com.beilin.entity.User;

 import java.util.List;

 public interface UserMapper {

      //插入
public void insert(User user); //根据id删除
public void delete(Integer id); //根据user的id修改
public void update(User user); //根据id查询
public User getById(Integer id); //查询全部
public List<User> list(); /**
* 根据id查询所有的书
* @param id
*/
public User selectBookById(Integer id); }

UserMapper

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.beilin.mapper.UserMapper"> <!-- 插入一个user -->
<insert id="insert" parameterType="user" useGeneratedKeys="true" keyProperty="id">
insert into user(name,age) values(#{name},#{age})
</insert> <!-- 根据id删除user -->
<delete id="delete" parameterType="int">
delete from user where id=#{id}
</delete> <!-- 根据id修改user信息 -->
<update id="update" parameterType="user">
update user set name=#{name},age=#{age} where id=#{id}
</update> <!-- 根据id查询 -->
<select id="getById" parameterType="int" resultType="user">
select * from user where id=#{id}
</select> <!-- 查询所有 -->
<select id="list" parameterType="int" resultType="user">
select * from user
</select> <resultMap id="bookMap" type="user">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<collection property="books" ofType="book">
<id property="id" column="bid"/>
<result property="name" column="bookName"/>
<result property="price" column="price"/>
</collection>
</resultMap> <!--根据id查询所有的书 -->
<select id="selectBookById" parameterType="int" resultMap="bookMap">
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};
</select> </mapper>

UserMapper.xml

5.在UserController中实现查询:

 package com.beilin.controller;

 import com.beilin.entity.Book;
import com.beilin.entity.User;
import com.beilin.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import java.util.List; @RestController
public class UserController { @Autowired
private UserMapper userMapper; //插入user
@RequestMapping("/user")
public void insert( User user) {
userMapper.insert(user);
} //根据id删除
@RequestMapping("/user1/{id}")
public void delete(@PathVariable("id") Integer id) {
userMapper.delete(id);
}
//修改
@RequestMapping("/user2/{id}")
public void update(User user,@PathVariable("id") Integer id) {
userMapper.update(user);
} //根据id查询user
@RequestMapping("/user3/{id}")
public User getById(@PathVariable("id") Integer id) {
User user = userMapper.getById(id);
return user;
} //查询全部
@RequestMapping("/users")
public List<User> list(){
List<User> users = userMapper.list();
return users;
} /**
* 根据id查询所有的书
*/
@GetMapping("/user/book/{id}")
public User getBooks(@PathVariable("id") Integer id){
User user = userMapper.selectBookById(id);
return user;
}
}

UserController.java

三、测试结果:

1.数据库查询结果:

2.postman访问结果:

3.SpringBoot整合Mybatis(一对多)的更多相关文章

  1. SpringBoot整合Mybatis之项目结构、数据源

    已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...

  2. SpringBoot整合Mybatis【非注解版】

    接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 ​ 选择Spring Initializr,配置JDK版本 ​ 输入项目名 ​ 选择构建web项目所需的state ...

  3. SpringBoot整合Mybatis注解版---update出现org.apache.ibatis.binding.BindingException: Parameter 'XXX' not found. Available parameters are [arg1, arg0, param1, param2]

    SpringBoot整合Mybatis注解版---update时出现的问题 问题描述: 1.sql建表语句 DROP TABLE IF EXISTS `department`; CREATE TABL ...

  4. springboot学习随笔(四):Springboot整合mybatis(含generator自动生成代码)

    这章我们将通过springboot整合mybatis来操作数据库 以下内容分为两部分,一部分主要介绍generator自动生成代码,生成model.dao层接口.dao接口对应的sql配置文件 第一部 ...

  5. springboot整合mybatis出现的一些问题

    springboot整合mybatis非常非常的简单,简直简单到发指.但是也有一些坑,这里我会详细的指出会遇到什么问题,并且这些配置的作用 整合mybatis,无疑需要mapper文件,实体类,dao ...

  6. springBoot整合mybatis、jsp 或 HTML

    springBoot整合mybatis.jsp Spring Boot的主要优点: 1:  为所有Spring开发者更快的入门: 2:  开箱即用,提供各种默认配置来简化项目配置: 3:  内嵌式容器 ...

  7. SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)

    1.概念:SpringBoot 整合 MyBatis 2.背景 SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少数据层操作,所有的开发 ...

  8. SpringBoot整合Mybatis完整详细版二:注册、登录、拦截器配置

    接着上个章节来,上章节搭建好框架,并且测试也在页面取到数据.接下来实现web端,实现前后端交互,在前台进行注册登录以及后端拦截器配置.实现简单的未登录拦截跳转到登录页面 上一节传送门:SpringBo ...

  9. SpringBoot整合Mybatis完整详细版

    记得刚接触SpringBoot时,大吃一惊,世界上居然还有这么省事的框架,立马感叹:SpringBoot是世界上最好的框架.哈哈! 当初跟着教程练习搭建了一个框架,传送门:spring boot + ...

随机推荐

  1. dell 9代cpu新机器安装centos7.7 bios 配置

    1.步骤如下,按f2或f12选择进入bios,每一步配置的内容如图所示,U盘写镜像,引导U盘启动,安装.(电源管理自启动那几个步骤可以不做)

  2. BOSCH汽车工程手册————自适应巡航速度控制ACC

    驾驶员通过自动速度控制器操纵键,将汽车行驶速度控制在预设的期望速度上. ACC系统则在自动速度控制的基础上检测本车到前面行驶汽车的距离以及相对速度,以及其他车道上的信息. 利用这些数据就能控制两车之间 ...

  3. CondenseNet: An Efficient DenseNet using Learned Group Convolutions

    1. 摘要 作者提出了一个前所未有高效的新奇网络结构,称之为 CondenseNet,该结构结合了密集连接性和可学习的分组卷积模块. 密集连接性有利于网络中的特征复用,而可学习的分组卷积模块则可以移除 ...

  4. 设置ubuntu14.04命令行启动

    编辑文件"/etc/default/grub",  把 GRUB_CMDLINE_LINUX_DEFAULT="quiet"  改成GRUB_CMDLINE_L ...

  5. nodejs之express中间件cookie-parser使用

    知识点: * .domain的使用,.aaa.com的域名都共享这个cookie信息 * res.cookie(,domain:'.aaa.com'}); * .获取所有cookie,设置cookie ...

  6. linux(centOS7)的基本操作(四) 定时任务——crontab

    概述 对于Java开发人员,定时任务并不陌生,无非是让系统在特定时间执行特定的命令或程序.例如spring提供的@Scheduled注解.OpenSymphony提供的quartz框架,都可以实现定时 ...

  7. Redis 5.0

    1.概述默认端口号:6379 redis是一种nosql数据库,他的数据是保存在内存中,同时redis可以定时把内存数据同步到磁盘,即可以将数据持久化,并且他比memcached支持更多的数据结构(s ...

  8. April.Util更新之权限

    目录 前言 权限 中间层 小结 前言 在之前已经提到过,公用类库Util已经开源,目的一是为了简化开发的工作量,毕竟有些常规的功能类库重复率还是挺高的,二是为了一起探讨学习软件开发,用的人越多问题也就 ...

  9. JS实现网页选取截屏 保存+打印 功能(转)

    源码地址: 1.1 确定截图选取范围 用户在开始截图后,需要在页面上选取一个截图范围,并且可以直观的看到,类似如下效果: image 我们的选取范围就是鼠标开始按下的那个点到鼠标拖动然后松开的那个点之 ...

  10. flaskdebug模式

    #从flask这个包中导入Flask这个类 #Flask这个类是项目的核心,以后很多操作都是基于这个类的对象 #注册url.注册蓝图等都是基于这个类的对象 from flask import Flas ...