重温MyBatis(四、Mapper的关联映射之:一对一关联映射)

33 篇文章 0 订阅
13 篇文章 0 订阅

一、Idcard类

1-1:

package com.springmvc.pojo;

/**
 * @author YuusukeUchiha
 * @date 2020/8/1 19:40
 * @ide IntelliJ IDEA
 */
public class Idcard {

    private int id;
    private String cno;

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

    public int getId() {
        return id;
    }

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

    public String getCno() {
        return cno;
    }

    public void setCno(String cno) {
        this.cno = cno;
    }
}

1-2:

package com.springmvc.mapper;

import com.springmvc.pojo.Idcard;

/**
 * @author YuusukeUchiha
 * @date 2020/7/23 22:54
 * @ide IntelliJ IDEA
 */
public interface IdcardMapper {

    Idcard findIdcardById(int id);                          //  根据编号查询用户信息
}

1-3:

<?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.springmvc.mapper.IdcardMapper">

    <!--    根据id查询身份证信息-->
    <select id="findIdcardById" parameterType="int" resultType="Idcard">
        select * from idcard where id = #{id}
    </select>



</mapper>

1-4:

//    @Test
    public void testFindIdcardById() {

        // 获得UserInfoMapper接口的代理对象
        IdcardMapper idcardMapper = sqlSession.getMapper(IdcardMapper.class);
        // 直接调用接口的方法,查询编号为1的UserInfo对象
        Idcard idcard = idcardMapper.findIdcardById(1);
        // 打印输出结果
        System.out.println(idcard .toString());
    }

二、Person类

2-1:

package com.springmvc.pojo;

/**
 * @author YuusukeUchiha
 * @date 2020/8/1 19:41
 * @ide IntelliJ IDEA
 */
public class Person {

    private int id;
    private String name;
    private int age;
    private String sex;

    // 关联属性
    private Idcard idcard;

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", idcard=" + idcard +
                '}';
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getSex() {
        return sex;
    }

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

    public Idcard getIdcard() {
        return idcard;
    }

    public void setIdcard(Idcard idcard) {
        this.idcard = idcard;
    }
}

2-2:

package com.springmvc.mapper;

import com.springmvc.pojo.Person;

/**
 * @author YuusukeUchiha
 * @date 2020/7/23 22:54
 * @ide IntelliJ IDEA
 */
public interface PersonMapper {

    Person findPersonById(int id);          //  根据编号查询用户信息
}

2-3:

<?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.springmvc.mapper.PersonMapper">

    <!--    根据id查询身份证信息-->
<!--    <select id="findPersonById" parameterType="int" resultType="Person">-->
    <select id="findPersonById" parameterType="int" resultMap="personMap">
        select * from person where id = #{id}
    </select>
    <resultMap id="personMap" type="Person">
    <id property="id" column="id" />
    <result property="name" column="name" />
    <result property="age" column="age" />
    <result property="sex" column="sex"/>
    <!--    一对一关联映射 -->
    <association property="idcard" column="cid"
                 select="com.springmvc.mapper.IdcardMapper.findIdcardById"
                 javaType="Idcard"
    />
</resultMap>



</mapper>

2-4:

 @Test
    public void testFindPersonById() {

        // 获得UserInfoMapper接口的代理对象
        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
        // 直接调用接口的方法,查询编号为1的UserInfo对象
        Person person = personMapper.findPersonById(1);
        // 打印输出结果
        System.out.println(person .toString());
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值