2.SpringBoot整合Mybatis(一对一)

前言:

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

准备工具:IntelliJ IDEA    Navicat for MySQL   Postman

 

一、建立数据库:

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

1 CREATE TABLE `id_card` (
2   `cardId` bigint(18) NOT NULL,
3   `uid` int(5) DEFAULT NULL,
4   `begin_time` date DEFAULT NULL,
5   `end_time` date DEFAULT NULL,
6   PRIMARY KEY (`cardId`),
7   KEY `fksu_id` (`uid`),
8   CONSTRAINT `fksu_id` FOREIGN KEY (`uid`) REFERENCES `user` (`id`)
9 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
id_card

 

 

 二、代码实现

1.添加身份证实体:

 1 package com.beilin.entity;
 2 import org.springframework.format.annotation.DateTimeFormat;
 3 import java.util.Date;
 4 
 5 /**
 6    * 身份证实体
 7   * @author 北林
 8    *
 9    */
10 
11 public class IDCard {
12 
13     private int uid;
14     private long cardId;
15 
16     @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
17     private Date beginTime;
18     @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
19     private Date endTime;
20     private User user;
21 
22 
23     public User getUser() {
24         return user;
25     }
26     public void setUser(User user) {
27         this.user = user;
28     }
29     public int getUid() {
30         return uid;
31     }
32     public void setUid(int uid) {
33         this.uid = uid;
34     }
35     public long getCardId() {
36         return cardId;
37     }
38     public void setCardId(long cardId) {
39         this.cardId = cardId;
40     }
41     public Date getBeginTime() {
42         return beginTime;
43     }
44     public void setBeginTime(Date beginTime) {
45         this.beginTime = beginTime;
46     }
47     public Date getEndTime() {
48         return endTime;
49     }
50     public void setEndTime(Date endTime) {
51         this.endTime = endTime;
52     }
53 
54 
55 }
IDCard.java

在这里需要注意的是:由于身份证上的日期类型是Data 我在Postman反反复复试了多次,如果要修改的话,总是失败,在此就用:String类型--->Data

为此,在IDCard同级目录下创建:

 1 package com.beilin.entity;
 2 
 3 import org.springframework.format.annotation.DateTimeFormat;
 4 
 5 import java.util.Date;
 6 
 7 public class IDCardRequest {
 8 
 9     private int uid;
10     private long cardId;
11 
12     private String beginTime;
13     private String endTime;
14 
15 
16     public int getUid() {
17         return uid;
18     }
19 
20     public void setUid(int uid) {
21         this.uid = uid;
22     }
23 
24     public long getCardId() {
25         return cardId;
26     }
27 
28     public void setCardId(long cardId) {
29         this.cardId = cardId;
30     }
31 
32     public String getBeginTime() {
33         return beginTime;
34     }
35 
36     public void setBeginTime(String beginTime) {
37         this.beginTime = beginTime;
38     }
39 
40     public String getEndTime() {
41         return endTime;
42     }
43 
44     public void setEndTime(String endTime) {
45         this.endTime = endTime;
46     }
47 }
IDCardRequest.java

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

(本来简单的增删改查不打算写的,再写也是为了自己更熟练,这里主要看后面的那两方法)

 1 package com.beilin.mapper;
 2 import com.beilin.entity.IDCard;
 3 import java.util.List;
 4 
 5 /**
 6    * Idcard的数据操作层接口类
 7   * @author 北林
 8    *
 9    */
10 
11 public interface IDCardMapper {
12 
13     //
14     public void insert(IDCard iDCard);
15 
16     //
17     public void delete(Integer uid);
18 
19     //
20     public void update(IDCard iDCard);
21 
22     //
23     public List<IDCard> getByUid(Integer uid);
24 
25     /**
26      * 获取包括user信息的身份证列表
27      */
28     public List<IDCard>  getListIdOfUser();
29 
30 }
IDCardMapper.java

3.添加mapper映射:

 

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
 3 <mapper namespace="com.beilin.mapper.IDCardMapper">
 4 
 5     <!--  user表与身份证表的一对一映射-->
 6     <resultMap type="iDCard" id="IDCardOfUserMap">
 7         <id property="cardId" column="cardId"/>
 8         <result property="beginTime" column="begin_time"/>
 9         <result property="endTime" column="end_time"/>
10         <association property="user" javaType="user">
11             <id property="id" column="id"/>
12             <result property="name" column="name"/>
13             <result property="age" column="age"/>
14         </association>
15     </resultMap>
16 
17 
18     <!--插入id_card表信息-->
19     <insert id="insert" parameterType="iDCard">
20         insert into id_card values(#{cardId},#{uid},#{beginTime},#{endTime})
21     </insert>
22 
23     <!--根据uid删除id_card表信息-->
24     <delete id="delete" parameterType="int">
25         delete from id_card where uid=#{uid}
26     </delete>
27 
28     <!--根据uid修改id_card表信息-->
29     <update id="update" parameterType="iDCard">
30         update id_card set cardId=#{cardId},begin_time=#{beginTime},end_time=#{endTime} where uid=#{uid}
31     </update>
32 
33     <!--根据uid查询id_card信息-->
34     <select id="getByUid" parameterType="int" resultType="iDCard">
35         select * from id_card where uid=#{uid}
36     </select>
37 
38 
39     <!-- 查出带有uer信息的id_card列表 -->
40     <select id="getListIdOfUser" resultMap="IDCardOfUserMap">
41         select * from id_card a,user b where a.uid=b.id;
42     </select>
43 
44 </mapper>
IDCardMapper.xml

 

4.添加controller:

 1 package com.beilin.controller;
 2 
 3 import com.beilin.entity.IDCard;
 4 import com.beilin.entity.IDCardRequest;
 5 import com.beilin.mapper.IDCardMapper;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.web.bind.annotation.*;
 8 
 9 import java.text.SimpleDateFormat;
10 import java.util.Date;
11 import java.util.List;
12 
13 @RestController
14 public class IDCardController {
15     @Autowired
16     private IDCardMapper iDCardMapper;
17 
18     //插入IDCard信息
19     //注意这里使用的是json格式
20     @PostMapping("/insert")
21     public void insert(@RequestBody IDCard iDCard){
22         iDCardMapper.insert(iDCard);
23     }
24 
25     //根据uid删除
26     @RequestMapping("/delete/{uid}")
27     public  void delete(@PathVariable("uid") Integer uid){
28         iDCardMapper.delete(uid);
29     }
30 
31     @RequestMapping("/update/{uid}")
32     public void update(IDCardRequest idCardRequest, @PathVariable("uid") Integer uid){
33         IDCard idCard = new IDCard();
34         idCard.setCardId(idCardRequest.getCardId());
35         idCard.setUid(idCardRequest.getUid());
36         try{
37             idCard.setBeginTime(this.strToDate(idCardRequest.getBeginTime()));
38             idCard.setEndTime(this.strToDate(idCardRequest.getEndTime()));
39         }catch (Exception e){
40             e.printStackTrace();
41         }
42         iDCardMapper.update(idCard);
43     }
44 
45     /**
46      *  根据uid查询
47      * @return
48      */
49     @RequestMapping("/select/{uid}")
50     public List<IDCard> getByUid(@PathVariable("uid") Integer uid) {
51         List<IDCard> iDCards = iDCardMapper.getByUid(uid);
52         return iDCards;
53     }
54 
55     /**
56      * 查出带有user信息的身份证列表
57      * @return
58      */
59     @GetMapping("/IDCardsOfUser")
60     public List<IDCard> listOfUser(){
61         List<IDCard>  iDCards=  iDCardMapper.getListIdOfUser();
62         return iDCards;
63     }
64 
65     private Date strToDate(String strDate) throws Exception {
66         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
67         return simpleDateFormat.parse(strDate);
68     }
69 }
IDCardController.java

 

三、测试:

(当然,最开始建表里面是什么都没有的,可以根据上面的增删改方法自行练习,这里不再赘述)

1.数据表信息:

2.Postman运行结果: 

 

 后面还有一部分没有截出来

 

最后,边学边写也是非常耗时间的,往往一个小问题都能卡个一两天,写博客也是当事后日记来写,以便加深记忆,如果有什么错误的地方,也请大家指出!

转载于:https://www.cnblogs.com/wx60079/p/11525649.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值