mybatis一对一查询

先准备两张数据库表用来数据演示

表1

Create Table

CREATE TABLE `tb_card` (
  `id` int NOT NULL AUTO_INCREMENT,
  `code` varchar(18) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8

 

表2

Create Table

CREATE TABLE `tb_person` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(18) DEFAULT NULL,
  `sex` varchar(18) DEFAULT NULL,
  `age` int DEFAULT NULL,
  `card_id` int DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `card_id` (`card_id`),
  CONSTRAINT `tb_person_ibfk_1` FOREIGN KEY (`card_id`) REFERENCES `tb_card` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8

数据你们自行插入(表2中 card_id有外键约束 数据插入需要注意)

第一步 创建Card、Person对象分别映射tb_card、tb_person

Card

package cn.liziy.entity;

import java.io.Serializable;
import java.util.Objects;

/**
 * @ClassName Card
 * @Author:Liziy
 * @Date 2020/8/6 15:08
 * @Description: 身份证实体类
 **/
public class Card implements Serializable {
    private static final long serialVersionUID = 5337253881741214405L;
    private Integer id;
    private String code;

    public Card() {
    }

    public Card(Integer id, String code) {
        this.id = id;
        this.code = code;
    }


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Card card = (Card) o;
        return Objects.equals(id, card.id) &&
                Objects.equals(code, card.code);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, code);
    }

    @Override
    public String toString() {
        return "Card{" +
                "id=" + id +
                ", code='" + code + '\'' +
                '}';
    }
}

Person(注意private Card card)

package cn.liziy.entity;

import java.io.Serializable;
import java.util.Objects;

/**
 * @ClassName Person
 * @Author:Liziy
 * @Date 2020/8/6 15:12
 * @Description: 身份信息实体类
 **/
public class Person implements Serializable {
    private static final long serialVersionUID = -4609455187831607128L;
    private Integer id;
    private String name;
    private String sex;
    private Integer age;
    //个人与身份证一一对应,一个人对应一个身份证
    private Card card;

    public Person(Integer id, String name, String sex, Integer age, Card card) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.card = card;
    }

    public Person() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Card getCard() {
        return card;
    }

    public void setCard(Card card) {
        this.card = card;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return Objects.equals(id, person.id) &&
                Objects.equals(name, person.name) &&
                Objects.equals(sex, person.sex) &&
                Objects.equals(age, person.age) &&
                Objects.equals(card, person.card);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, sex, age, card);
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", card=" + card +
                '}';
    }
}
第二步 mybatis编写SQL以及Dao接口的编写
CardMapper.xml
<?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="cn.liziy.dao.CardDao">
<!-- 根据id查询Card, 返回Card对象 -->
    <select id="selectCardById" parameterType="int" resultType="cn.liziy.entity.Card">
        SELECT
                id,
                code
        FROM tb_card
        WHERE id = #{id}
    </select>
</mapper>
package cn.liziy.dao;

import cn.liziy.entity.Card;

public interface CardDao {
    Card selectCardById(Integer id);
}

PersonMapper.xml

<?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="cn.liziy.dao.PersonDao">
<!-- 根据id查询Person,返回resultMap -->
    <select id="selectPersonById" parameterType="int"
            resultMap="personMap">
        SELECT
                id,
                name,
                sex,
                age,
                card_id
        FROM tb_person
        WHERE id = #{id}
    </select>
<!--  映射Person对象的resultMap  -->
    <resultMap type="cn.liziy.entity.Person" id="personMap">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="sex" column="sex"/>
        <result property="age" column="age"/>
        <!--  一对一关联映射:association      -->
        <association property="card" column="card_id"
                     select="cn.liziy.dao.CardDao.selectCardById"
                     javaType="cn.liziy.entity.Card"/>
     </resultMap>
</mapper>
package cn.liziy.dao;

import cn.liziy.entity.Person;

public interface PersonDao {

    Person selectPersonById(Integer id);

}

第三步 servic层编写

package cn.liziy.service;

import cn.liziy.entity.Person;

public interface PersonService {
    Person selectPersonById(Integer id);
}
package cn.liziy.service.impl;

import cn.liziy.dao.PersonDao;
import cn.liziy.entity.Person;
import cn.liziy.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName PersonServiceImpl
 * @Author:Liziy
 * @Date 2020/8/6 15:46
 * @Description:
 **/
@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    PersonDao personDao;

    public Person selectPersonById(Integer id) {
        return personDao.selectPersonById(id);
    }
}

第四步 Controller层编写

package cn.liziy.controller;

import cn.liziy.entity.Person;
import cn.liziy.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName PerosnController
 * @Author:Liziy
 * @Date 2020/8/6 15:48
 * @Description:
 **/
@Controller
public class PerosnController {
    @Autowired
    PersonService personService;

    @GetMapping("/person/{id}")
    public @ResponseBody
    Person person(@PathVariable Integer id){
       return personService.selectPersonById(id);
    }
}

开始测试--------------------

 

 

 

 


posted @ 2020-08-06 16:45  火星的巧克力  阅读(1212)  评论(0编辑  收藏  举报
/* 返回顶部代码 */ TOP