开发工具:STS

代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis01/tree/93398da60c647573645917b27bd549da2f9f0f15

前言:

上一篇blog里面介绍了整合springboot和mybatis的项目的建立,以及单表的简单的增删改查。这里是上一篇blog的地址:https://www.cnblogs.com/TimerHotel/p/springboot_matatis_01.html。今天我们来介绍一对一的关系该怎么处理。

一、建立数据库:

每个学生对应一张身份证,每张身份证上有身份证号cardId、开始日期、结束日期。并且建立与student表的外键关系

二、代码实现

1.添加身份证实体:

 package com.xm.pojo;

 import java.util.Date;
/**
* 身份证实体
* @author xm
*
*/
public class IDCard { private int sid;
private long cardId;
private Date beginTime;
private Date endTime;
private Student student; public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public long getCardId() {
return cardId;
}
public void setCardId(long cardId) {
this.cardId = cardId;
}
public Date getBeginTime() {
return beginTime;
}
public void setBeginTime(Date beginTime) {
this.beginTime = beginTime;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
} }

IDCard.java

2.添加数据操作接口mapper:

 package com.xm.mapper;

 import java.util.List;

 import com.xm.pojo.IDCard;

 /**
* Idcard的数据操作层接口类
* @author xm
*
*/
public interface IDCardMapper { /**
* 获取包括学生信息的身份证列表
* @return
*/
public List<IDCard> getListIdOfStudent(); }

IDCardMapper.java

3.添加mapper映射:

 <?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.IDCardMapper"> <!-- 学生表与身份证表的一对一映射-->
<resultMap type="iDCard" id="IDCardOfStudentMap">
<id property="cardId" column="cardId"/>
<result property="beginTime" column="begin_time"/>
<result property="endTime" column="end_time"/>
<association property="student" javaType="student">
<id property="id" column="id"/>
<result property="name" column="name"/>
</association>
</resultMap> <!-- 查出带有学生信息的身份证列表 -->
<select id="getListIdOfStudent" resultMap="IDCardOfStudentMap">
select * from id_card a,student b where a.sid=b.id;
</select> </mapper>

IDCardMapper.xml

4.添加controller

 package com.xm.controller;

 import java.util.List;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import com.xm.mapper.IDCardMapper;
import com.xm.pojo.IDCard; /**
* 身份证信息的控制类
* @author xm
*
*/
@RestController
public class IDCardController {
@Autowired
private IDCardMapper iDCardMapper;
/**
* 查出带有学生信息的身份证列表
* @return
*/
@GetMapping("/IDCardsOfStudent")
public List<IDCard> listOfStudent(){
List<IDCard> iDCards= iDCardMapper.getListIdOfStudent();
return iDCards;
} }

IDCardController.java

三、测试:

1.数据表信息:

2.运行结果:

                                                                             

                                                                          2018-06-19

2、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. springboot/Mybatis整合

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

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

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

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

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

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

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

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

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

  9. 1、SpringBoot+Mybatis整合------简单CRUD的实现

    编译工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis01/commit/b757cd9bfa4e2de551b2e9 ...

随机推荐

  1. select获取到option的value和text方法

    function getSelectval(id){ var selId = document.getElementById(id); //获取select的id var seleIndex =sel ...

  2. Keepalived & Lvs集群搭建实验

    实验拓扑图: 实验原理: Keepalived 是基于 LVS ,并与 LVS 高度融合的 LVS和keepalived的关系:lvs起的是负载均衡功能,而keepalived则是高可用(热 备)的支 ...

  3. 如何解读IL代码

    如何解读IL代码 关于IL代码,我有将从三个方面去揭开它神秘的面纱.IL代码是什么?我们为什么要去读懂IL代码?我们如何去读懂IL代码?这三个问题的解答,将是我解读IL代码的整体思路. IL代码是什么 ...

  4. mysql 使用 unix 方式显示日期和时间

    1.UNIX中文为时间戳.该方式显示从1970年1月1日开始经过的秒数. 2.函数 UNIX_TIMESTAMP() 返回时间戳格式的时间, FROM_UNIXTIME() 将时间戳格式的时间转换为普 ...

  5. iOS-swift-枚举和结构体

    1.枚举 使用关键字 enum 创 建枚举. 枚举默认起始值为 0,可以自定义起始值. 在枚举中可以定义方法,和类中定义的一样. 使用关键字 rawValue 访问当前枚举的值. enum Rank: ...

  6. Linux访问https报错

    今天用Linux(CentOS)拉Git仓库时,报了个错unable to access 'https://github.com/Wind4/vlmcsd.git/': SSL connect err ...

  7. springboot从入门到精通(三)

    再开始第三节之前,先补充一下第二节里出现的小问题,就是springboot的application.properties,我在文件中添加了server.port=9090这个参数,但是启动项目后并未生 ...

  8. CSS知识点梳理

  9. 从零开始的全栈工程师——JS面向对象(初篇)

    面向对象编程 面向对象编程是用抽象方式创建基于现实世界模型的一种编程模式.它使用先前建立的范例,包括模块化,多态和封装几种技术.今天,许多流行的编程语言(如Java,JavaScript,C#,C+ ...

  10. 处理移动端自适应布局的方法- calc()与vw

    在处理移动端自适应布局时,目前前端最流行的方法应该就是使用媒体查询,来设置HTML的字体大小,然后用rem为单位对Dom的宽高进行设置,这个方法的优势在于兼容性方面很好,劣势则在于当前市场上不同的机型 ...