开发工具: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. Appium自动化中截图的问题

    在用Appium做UI自动化过程中,大家会发现测试报告很重要,而在测试报告中截图很重要. 因为很多公司都是用Jenkins作为持续集成工具,所以要让执行自动化测试的人看明白自动化在跑什么,哪里失败了, ...

  2. js监听dom元素内容变化

    $("#divid").bind('DOMNodeInserted', function(e) { alert('element now contains: ' + $(e.tar ...

  3. 错误 chamfermatching.cpp:969:30: error: the compiler can assume that the address of ‘annotate_img’

    修改 ./build/modules/contrib/CMakeFiles/opencv_contrib.dir/flags.make文件,删掉-Werror=address,然后重新make

  4. (转) awk学习

     awk学习  原文:http://blog.chinaunix.net/uid-23302288-id-3785105.html http://www.zsythink.net/archives/t ...

  5. python 字符串转字节数组

    场景: java加解密和python加解密互转的时候,因一些非显示字符无法确认两者是否一致,故需要打出他们的十六进制字节数组进行比较 1.python代码实现 str='123';print str. ...

  6. C# 多线程之线程同步

    多线程间应尽量避免同步问题,最好不要线程间共享数据.如果必须要共享数据,就需要使用同步技术,确保一次只有一个线程访问和改变共享状态. 一::lock语句 lock语句事设置锁定和接触锁定的一种简单方法 ...

  7. Python异常处理及元类

    一.异常处理 异常是错误发生的信号,一旦程序出错就会产生一个异常,如果该异常没有被应用程序处理,那么该异常就会跑出来,程序的执行也随之终止,也就是说异常就是一个事件,该事件会在程序执行过程中发生,影响 ...

  8. 在 eclipse 中调出其内置的浏览器

    两种方法: 1.点击工具栏中的浏览器图标,就会在主面板中出现浏览器: 跳出一个blank页面,如下: 第二种方法:点击Window——Show view——Other 输入 "browser ...

  9. Java—包装类、Date和SimpleDateFormat、Calendar类

    包装类 基本数据类型不能调用方法,功能简单,为了让基本数据类型也具备对象的特性,Java为每个基本数据类型提供了一个包装类,这样就可以像操作对象那样来操作基本数据类型. 基本类型和包装类之间的对应关系 ...

  10. 如何领域驱动设计?-实践感悟&总结分享

    主要是在开发过程中,个人对于领域驱动设计的实践感悟和总结:也是对新进开发人员的培训资料:希望对关注DDD的童鞋有所帮助. 概述 领域驱动不是纯粹的技术问题,领域建模(建立数据表只是一部分)是领域专家( ...