2、SpringBoot+Mybatis整合------一对一
开发工具: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整合------一对一的更多相关文章
- SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)
SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...
- SpringBoot+Mybatis整合入门(一)
SpringBoot+Mybatis 四步整合 第一步 添加依赖 springBoot+Mybatis相关依赖 <!--springBoot相关--> <parent> < ...
- SpringBoot+Mybatis整合实例
前言 大家都知道springboot有几大特点:能创建独立的Spring应用程序:能嵌入Tomcat,无需部署WAR文件:简化Maven配置:自动配置Spring等等.这里整合mybatis,创建一个 ...
- springboot/Mybatis整合
正题 本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 springboot: 1.5.6 jdk:1.8.0_161 maven:3.3.9 额外功能 PageHelper ...
- springboot+mybatis整合(单元测试,异常处理,日志管理,AOP)
我用的事IDEA,jdk版本是1.7.新建项目的时候这个地方的选择需要注意一下,springboot版本是1.5的,否则不支持1.7的jdk pom.xml <dependency> &l ...
- 9、SpringBoot+Mybatis整合------动态sql
开发工具:STS 前言: mybatis框架中最具特色的便是sql语句中的自定义,而动态sql的使用又使整个框架更加灵活. 动态sql中的语法: where标签 if标签 trim标签 set标签 s ...
- 5、SpringBoot+Mybatis整合------多对多
开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis/tree/3baea10a3a1104bda815c20695 ...
- 3、SpringBoot+Mybatis整合------主键回填
开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis01/tree/d68efe51774fc4d96e5c6870 ...
- 1、SpringBoot+Mybatis整合------简单CRUD的实现
编译工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis01/commit/b757cd9bfa4e2de551b2e9 ...
随机推荐
- 安装Python + Selenium
1.Python下载与安装 先去Python官网下载安装包:http://www.python.org/ 下载后按步骤安装(最好不要安装到系统盘) 安装好后将安装路径(Python和Scripts) ...
- Murano Weekly Meeting 2016.07.05
Meeting time: 2016.July.05 1:00~2:00 Chairperson: Kirill Zaitsev, from Mirantis Meeting summary: 1. ...
- Murano Weekly Meeting 2015.08.25
Meeting time: 2015.August.25th 1:00~2:00 Chairperson: Serg Melikyan, PTL from Mirantis Meeting summ ...
- ApplicationContextAware的作用
ApplicationContextAware其实我们看到---Aware就知道是干嘛用的了,就是属性注入的, 但是这个ApplicationContextAware的不同地方在于,实现了这个接口的b ...
- hduoj 2546饭卡
饭卡 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...
- Windows Store 应用中获取程序集版本号的方法
本文为个人博客备份文章,原文地址: http://validvoid.net/windows-store-app-get-assembly-version/ WinRT 中对反射做了很多限制,假设 W ...
- oracle 空值排序,排在最前面或者最后面
1,排在最前面用order by name nulls first; eg:select t.name,t.code from table t where t.code!='1' order by n ...
- Xtrareport 多栏报表
首先看下布局designer 细节: 分组一定要用到GroupHeather 设置好有 右边会出现 接下来是代码部分 Form1中代码 using DevExpress.XtraReports.UI; ...
- 【转】js弹出框、对话框、提示框、弹窗总结
js弹出框.对话框.提示框.弹窗总结 一.js的三种最常见的对话框 //====================== JS最常用三种弹出对话框 ======================== //弹 ...
- js获取日期:前天、昨天、今天、明天、后天
前天,昨天,今天,明天,后天 <html> <head> <meta http-equiv="Content-Type" content=" ...