开发工具:STS

代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis/tree/c00b56dbd51a1e26ab9fd990201ad9e89a770ca9

前言:

今天探讨的是Mybatis一对多的处理关系。

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


一、添加数据表:

二、代码实现:

1.添加Book实体:

 package com.xm.pojo;
/**
* 书的实体
* @author xm
*
*/
public class Book { private int id;
private int sid;
private String name;
private double price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
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.在Student实体中添加book集合:

 package com.xm.pojo;

 import java.util.List;

 /**
* name:学生实体
* @author xxm
*
*/
public class Student {
/**
* content:主键id
*/
private int id;
/**
* content:姓名
*/
private String name; private List<Book> books; public Student() {
// TODO Auto-generated constructor stub
} public List<Book> getBooks() {
return books;
} public void setBooks(List<Book> books) {
this.books = books;
} 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;
} }

Student.java

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

 package com.xm.mapper;

 import java.util.List;

 import com.xm.pojo.Student;

 public interface StudentMapper {

     /**
* 根据id查询
* @param id
* @return
*/
public Student getById(Integer id); /**
* 查询全部
* @return
*/
public List<Student> list(); /**
* 插入
* @param student
*/
public int insert(Student student);
/**
* 主键回填的插入
* @param student
* @return
*/
public int insertToId(Student student); /**
* 根据student的id修改
* @param student
*/
public void update(Student student); /**
* 根据id删除
* @param id
*/
public void delete(Integer id); /**
* 根据id查询所有的书
* @param id
*/
public Student selectBookById(Integer id); }

StudentMapper.java

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.xm.mapper.StudentMapper"> <!-- 根据id查询 -->
<select id="getById" parameterType="int" resultType="student">
select * from student where id=#{id}
</select>
<!-- 查询所有 -->
<select id="list" parameterType="int" resultType="student">
select * from student
</select> <!-- 插入一个学生 -->
<insert id="insert" parameterType="student">
insert into student(name) values(#{name})
</insert>
<!-- 主键回填的插入 -->
<insert id="insertToId" parameterType="student" useGeneratedKeys="true" keyProperty="id">
insert into student(name) values(#{name})
</insert> <!-- 根据id修改学生信息 -->
<update id="update" parameterType="student">
update student set name=#{name} where id=#{id}
</update> <!-- 根据id删除学生 -->
<delete id="delete" parameterType="int">
delete from student where id=#{id}
</delete> <resultMap type="student" id="bookMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<collection property="books" ofType="book">
<id property="id" column="bid"/>
<result property="name" column="bname"/>
<result property="price" column="price"/>
</collection>
</resultMap>
<!--根据id查询所有的书 -->
<select id="selectBookById" parameterType="int" resultMap="bookMap">
select a.*,b.id bid,b.name bname,b.price from student a,book b where a.id=b.sid and a.id=#{id};
</select>
</mapper>

StudentMapper.xml

5.在StudentController中实现查询:

 package com.xm.controller;

 import java.util.List;

 import javax.websocket.server.PathParam;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController; import com.xm.mapper.StudentMapper;
import com.xm.pojo.Student; @RestController
public class StudentController {
@Autowired
private StudentMapper studentMapper; /**
* 根据id查询学生
* @param id
* @return
*/
@GetMapping("/student/{id}")
public Student getById(@PathVariable("id") Integer id) { Student student = studentMapper.getById(id);
return student; } /**
* 查询全部
* @return
*/
@GetMapping("/students")
public List<Student> list(){
List<Student> students = studentMapper.list();
return students;
} /**
* 插入
* @param student
*/
@PostMapping("/student")
public void insert( Student student) {
studentMapper.insert(student);
} /**
* 修改
* @param student
*/
@PutMapping("/student/{id}")
public void update(Student student,@PathVariable("id")Integer id) {
studentMapper.update(student);
} /**
* 根据id删除
* @param id
*/
@DeleteMapping("/student/{id}")
public void delete(@PathVariable("id") Integer id) {
studentMapper.delete(id);
} /**
* 根据id查询所有的书
*/
@GetMapping("/student/book/{id}")
public Student getBooks(@PathVariable("id") Integer id) {
Student student = studentMapper.selectBookById(id);
return student;
} }

StudentController.java

三、测试结果:

1.数据库查询结果:

2.postman访问结果:

                                                                    2018-06-20

4、SpringBoot+Mybatis整合------一对多的更多相关文章

  1. SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)

    SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...

  2. SpringBoot+Mybatis整合入门(一)

    SpringBoot+Mybatis 四步整合 第一步 添加依赖 springBoot+Mybatis相关依赖 <!--springBoot相关--> <parent> < ...

  3. SpringBoot+Mybatis整合实例

    前言 大家都知道springboot有几大特点:能创建独立的Spring应用程序:能嵌入Tomcat,无需部署WAR文件:简化Maven配置:自动配置Spring等等.这里整合mybatis,创建一个 ...

  4. 2、SpringBoot+Mybatis整合------一对一

    开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis01/tree/93398da60c647573645917b2 ...

  5. springboot/Mybatis整合

    正题 本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 springboot: 1.5.6 jdk:1.8.0_161 maven:3.3.9 额外功能 PageHelper ...

  6. springboot+mybatis整合(单元测试,异常处理,日志管理,AOP)

    我用的事IDEA,jdk版本是1.7.新建项目的时候这个地方的选择需要注意一下,springboot版本是1.5的,否则不支持1.7的jdk pom.xml <dependency> &l ...

  7. 9、SpringBoot+Mybatis整合------动态sql

    开发工具:STS 前言: mybatis框架中最具特色的便是sql语句中的自定义,而动态sql的使用又使整个框架更加灵活. 动态sql中的语法: where标签 if标签 trim标签 set标签 s ...

  8. 5、SpringBoot+Mybatis整合------多对多

    开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis/tree/3baea10a3a1104bda815c20695 ...

  9. 3、SpringBoot+Mybatis整合------主键回填

    开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis01/tree/d68efe51774fc4d96e5c6870 ...

随机推荐

  1. SSM-@Transactional 注释不生效

    1.在applicationConext.xml 中配置事务注解驱动 <!-- 事务注解驱动 --> <tx:annotation-driven /> <!-- 配置事务 ...

  2. tinkphp3.2.3 关于事务处理。

    自己做一个测试,关于事务处理的. 在对多表进行操作的时候 基本上都离不开事务. 有的操作,是要由上一操作后,产的值(如主表里插入后,要获取插入的主键ID值,返回给下面处理表用.)带到后面的表处理当中去 ...

  3. 跟我一起用python画你所想吧!

    0.库的引入 要想画图,我们先倒入两个库. import numpy as np import matplotlib.pyplot as plt 注:以下代码全都基于导入这两个库的前提下编写的. 1. ...

  4. 使用python将元组转换成列表,并替换其中元素

    aa = (1, 2, 3, 4, 5, 6) b = [(x == 5 and 8 or x) for x in aa] z = map(lambda x: 8 if x == 5 else x, ...

  5. 如何修改FlashFXP默认编辑工具使用记事本打开

    FlashFXP如果不设置默认编辑工具,那么当你打开html文档的时候,默认会用word打开,很不方便,其实简单的设置下,可以默认以任何工具打开.具体设置方法如下: 选项>>关联文件. 然 ...

  6. phpstorm 配置 webserver ,配置根目录

    原文链接    http://blog.csdn.net/pony_maggie/article/details/52367093 phpstorm自带了一个web server,我们可以直接在IDE ...

  7. C#用委托实现异步,异步与多线程的异同

    异步与多线程的区别(转) 一.异步和多线程有什么区别?其实,异步是目的,而多线程是实现这个目的的方法.异步是说,A发起一个操作后(一般都是比较耗时的操作,如果不耗时的操作就没有必要异步了),可以继续自 ...

  8. Redis入门--(一)简介NoSQL

    1.什么是NoSql? 2.为什么需要NoSQL? 互联网经历了1.0和2.0的发展: web1.0 是早期新浪,雅虎等只能浏览,不能交互: 传统关系型数据库在应付web2.0这种动态网站的时候力不从 ...

  9. 账户密码提示 jq简单事件

    $(".username").focus(function(){ if($(this).val()=="请输入用户名"){ $(this).val(" ...

  10. python面试题——框架和其他(132题)

    一.框架对比 (1)django.flask.tornado框架的比较? Django:简单的说Django是一个大而全的Web框架,内置了很多组件,ORM.admin.Form. ModelForm ...