`

MyBatis3一对一关系映射

阅读更多
1. MyBatis3关系映射,一对一关系

创建t_address表
create table t_address (
id int(11) NOT NULL AUTO_INCREMENT,
sheng varchar(20) DEFAULT NULL,
shi varchar(20) DEFAULT NULL,
qu varchar(20) DEFAULT NULL,
PRIMARY KEY(id)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

插入数据
insert into t_address(id, sheng, shi, qu) values(1, '北京市', '北京市', '东城区');
insert into t_address(id, sheng, shi, qu) values(2, '河北省', '石家庄市', '新华区');

在t_student表增加addressId关联字段
alter table t_student add addressId INT(11);

如果使用外键(当前情况不需要):
alter table t_student add FOREIGN KEY fk_student_address(addressId) REFERENCES t_address(id);
alter table t_student drop foreign key t_student_ibfk_1;
alter table t_student drop index fk_student_address;


2. 使用mybatis查询一对一关系关联表的方式

1. 不推荐
<resultMap type="Student" id="StudentAddressResult">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="age" column="age"/>
    <result property="address.id" column="addressId"/>
    <result property="address.sheng" column="sheng"/>
    <result property="address.shi" column="shi"/>
    <result property="address.qu" column="qu"/>
</resultMap>

2. 不推荐
<resultMap type="Address" id="AddressResult">
    <result property="id" column="id"/>
    <result property="sheng" column="sheng"/>
    <result property="shi" column="shi"/>
    <result property="qu" column="qu"/>
</resultMap>
<resultMap type="Student" id="StudentAddressResult">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="age" column="age"/>
    <association property="address" resultMap="AddressResult"/>
</resultMap>

3. 不推荐
<resultMap type="Student" id="StudentAddressResult">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="age" column="age"/>
    <association property="address" javaType="Address">
        <result property="id" column="id"/>
        <result property="sheng" column="sheng"/>
        <result property="shi" column="shi"/>
        <result property="qu" column="qu"/>
    </association>
</resultMap>

4. 推荐(column对应的是sql表中的外键)
StudentMapper.xml
<resultMap type="Student" id="StudentAddressResult">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="age" column="age"/>
    <association property="address" column="addressId" select="com.andrew.mappers.AddressMapper.findById"></association>
</resultMap>
AddressMapper.xml
<resultMap type="Address" id="AddressResult">
    <result property="id" column="id"/>
    <result property="sheng" column="sheng"/>
    <result property="shi" column="shi"/>
    <result property="qu" column="qu"/>
</resultMap>
<select id="findById" parameterType="Integer" resultType="Address">
    select * from t_address where id=#{id}
</select>


3. 使用mybatis查询关联表

package com.andrew.model;
public class Address {
    private Integer id;
    private String sheng;
    private String shi;
    private String qu;
    @Override
    public String toString() {
        return "Address [id=" + id + ", sheng=" + sheng + ", shi=" + shi
                + ", qu=" + qu + "]";
    }
    // getter and setter
}


package com.andrew.model;
public class Student {
    private Integer id;
    private String name;
    private Integer age;
    private Address address;
    public Student() {
        super();
    }
    public Student(String name, Integer age) {
        super();
        this.name = name;
        this.age = age;
    }
    public Student(Integer id, String name, Integer age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", age=" + age
                + ", address=" + address + "]";
    }
    // getter and setter
}


package com.andrew.mappers;
import com.andrew.model.Address;
public interface AddressMapper {
    public Address findById(Integer id);
}


<?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.andrew.mappers.AddressMapper">
    <resultMap type="Address" id="AddressResult">
        <result property="id" column="id"/>
        <result property="sheng" column="sheng"/>
        <result property="shi" column="shi"/>
        <result property="qu" column="qu"/>
    </resultMap>
    <select id="findById" parameterType="Integer" resultType="Address">
        select * from t_address where id=#{id}
    </select>
</mapper>


package com.andrew.mappers;
import java.util.List;
import com.andrew.model.Student;
public interface StudentMapper {
    public int add(Student student);
    public int update(Student student);
    public int delete(Integer id);
    public Student findById(Integer id);
    public List<Student> find();
    public Student findStudentWithAddress(Integer id);
}


<?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.andrew.mappers.StudentMapper">
    <resultMap type="Student" id="StudentResult">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
    </resultMap>
    <resultMap type="Student" id="StudentAddressResult">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
        <association property="address" column="addressId" select="com.andrew.mappers.AddressMapper.findById"></association>
    </resultMap>
    <insert id="add" parameterType="Student">
        insert into t_student values(null, #{name}, #{age})
    </insert>
    <update id="update" parameterType="Student">
        update t_student set name=#{name},age=#{age} where id=#{id}
    </update>
    <delete id="delete" parameterType="Integer">
        delete from t_student where id=#{id}
    </delete>
    <select id="findById" parameterType="Integer" resultType="Student">
        select * from t_student where id=#{id}
    </select>
    <select id="find" resultMap="StudentAddressResult">
        select * from t_student
    </select>
    <select id="findStudentWithAddress" resultMap="StudentAddressResult" parameterType="Integer">
        select * from t_student t1,t_address t2 where t1.addressId=t2.id and t1.id=#{id}
    </select>
</mapper>


package com.andrew.service;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.andrew.mappers.StudentMapper;
import com.andrew.model.Student;
import com.andrew.util.SqlSessionFactoryUtil;
public class StudentAddressTest {
    private static Logger logger=Logger.getLogger(StudentTest.class);
    private SqlSession sqlSession=null;
    private StudentMapper studentMapper=null;
    @Before
    public void setUp() throws Exception {
        sqlSession = SqlSessionFactoryUtil.openSession();
        studentMapper = sqlSession.getMapper(StudentMapper.class);
    }
    @After
    public void tearDown() throws Exception {
        sqlSession.close();
    }
    @Test
    public void testFindStudentWithAddress() {
        logger.info("查询学生和地址");
        Student student = studentMapper.findStudentWithAddress(2);
        System.out.println(student);
    }
}
运行结果:
Student [id=2, name=李四, age=20, address=Address [id=2, sheng=河北省, shi=石家庄市, qu=新华区]]
分享到:
评论

相关推荐

    Java MyBatis 关系映射 一对一关系

    本话题主要关注MyBatis中的一对一关系映射,这是一种常见的数据库关联关系,用于描述一个实体与另一个实体之间的对应关系。 一对一关系映射在数据库设计中意味着一个表中的记录唯一对应另一个表中的记录。例如,...

    Springboot中mybatis表关联映射关系(一对一)

    Springboot 中 MyBatis 表关联映射关系(一对一) 在 Springboot 中,MyBatis 提供了强大的表关联映射关系机制,可以实现一对一、多对一、多对多等各种关联关系。在本文中,我们将详细介绍 Springboot 中 MyBatis ...

    myBatis一对一和一对多

    ### 一对一映射(One-to-One) 一对一关系通常发生在两个实体之间,例如一个用户对应一个唯一地址。在MyBatis中,可以通过`&lt;association&gt;`标签来配置一对一的关系。 1. **配置XML映射文件**: 在Mapper XML文件中...

    MyBatis一对多映射

    3. 映射文件:在MyBatis的XML映射文件中,通过`&lt;association&gt;`标签定义一对多关系。`&lt;collection&gt;`子标签用于指定集合属性,`property`表示Java类中的字段名,`select`属性则是用来查询从表数据的SQL语句。 例如: ...

    Mybatis课程实验报告及源码,实验名称:Mybatis完成一对一关系映射(可不用修改直接使用)

    “Mybatis课程实验报告及源码-实验名称:Mybatis完成一对一关系映射(可不用修改直接使用).zip” 是一份关于Mybatis框架实验的报告,附带了完整的源代码和示例项目。以下是对这份实验报告的详细描述: 这份实验...

    MyBatis一对一查询的代码

    这样,当执行查询时,MyBatis会自动处理这种复杂的一对一关系,将查询结果组装成完整的`User`对象,其中包含了`UserDetail`对象。 总结一下,MyBatis中的一对一查询主要通过`resultMap`、`id`、`result`以及`...

    MyBatis框架的学习(五)——一对一关联映射和一对多关联映射

    在本篇关于MyBatis框架的学习中,我们将深入探讨一对一关联映射和一对多关联映射的概念、配置以及在实际开发中的应用。MyBatis是一个优秀的持久层框架,它允许开发者将SQL语句直接写在XML配置文件或者注解中,从而...

    MyBatis的一对一关联映射

    一对一关联映射是指两个实体类之间存在一对一的关系,即每个实体实例对应数据库中的一条记录,且这条记录在另一张表中也有唯一对应的记录。在 MyBatis 中,我们可以通过 XML 映射文件或者注解来配置这种关联关系。 ...

    mybatis关联映射源码

    在MyBatis中,关联映射是处理对象关系映射(ORM)的重要部分,用于描述实体类之间的关联关系,如一对一(OneToOne)、一对多(OneToMany)和多对多(ManyToMany)。下面我们将深入探讨这些关联映射的实现和原理。 ...

    Mybatis关联映射Demo

    1. **一对一(OneToOne)映射**:这种映射通常用于表示两个实体之间一对一的关系,例如一个用户只有一个地址。在Mybatis中,可以通过`&lt;association&gt;`标签来定义一对一关系,包括内嵌方式和外键方式。 2. **一对多...

    MyBatis高级映射(一对多查询)

    在MyBatis的映射文件中,我们需要为一对一和一对多关系创建对应的XML元素。对于一对多关系,通常使用`&lt;association&gt;`或`&lt;collection&gt;`标签来定义。例如,假设我们有一个`Department`类和一个`Employee`类,一个部门...

    SpringBoot中mybatis表关联映射关系(一对多嵌套:结果方式)

    SpringBoot 中 MyBatis 表关联映射关系(一对多嵌套:结果方式) 在 SpringBoot 中,MyBatis 是一个非常流行的持久层框架,它提供了强大的持久化功能,能够将 Java 对象与数据库表进行映射。在实际开发中,我们经常...

    mybatis一对一,一对多

    在测试中,你可以创建类似 `mybatis0408` 这样的例子,模拟不同的一对一和一对多关系,通过运行 SQL 并观察返回的结果,以加深对这两个标签的理解。 总之,掌握 MyBatis 的 `association` 和 `collection` 使用,...

    mybatis实现一对一关联映射实例代码

    MyBatis 实现一对一关联映射实例代码 MyBatis 是一个基于 Java 的持久层框架,它提供了对数据库的访问和操作。在实际项目开发中,经常存在一对一的关系,如一个人对应一张身份证信息,这就是一对一的关系。下面我们...

    MyBatis关联映射代码

    "关联映射"是MyBatis中的一个重要概念,用于处理数据库中表之间的关联关系,比如一对一、一对多、多对一和多对多的关系。下面将详细介绍MyBatis的关联映射以及如何在代码中实现。 关联映射是MyBatis通过XML配置文件...

    实现Mybatis框架中一对多关联映射的查询操作。

    在处理复杂的数据库关联关系时,比如一对一、一对多、多对一、多对多等,Mybatis提供了灵活的映射机制。本篇将详细讲解如何在Mybatis中实现一对多关联映射的查询操作。 首先,我们要明确一对多关联映射的基本概念。...

    MyBatis注解配置映射器:一对多关系的实现

    本文将深入探讨如何使用MyBatis的注解配置来实现一对多关系映射,以此来提高开发效率并减少代码冗余。 首先,我们需要理解一对多关系的概念。在数据库设计中,一对多关系表示一个表中的记录可以对应另一个表中的多...

    Mybatis实现一对一,一对多数据插入数据实验报告.docx

    Mybatis,一个优秀的持久层框架,提供了方便的方式来处理复杂的数据关系,包括一对一和一对多的关系。这篇实验报告将详细阐述如何在Mybatis中实现这两种关系的数据插入。 1. **一对一关系** - **定义**:一对一...

    MyBatis高级映射(一对一查询)

    1. 映射文件配置:在MyBatis的Mapper XML文件中,我们可以使用`&lt;resultMap&gt;`标签定义一个结果映射,然后使用`&lt;association&gt;`标签来指定一对一的关系。`&lt;association&gt;`标签的`property`属性用于指定Java对象的字段名...

    MyBatis_关系映射之一对多-src.zip

    在这个名为"MyBatis_关系映射之一对多-src.zip"的压缩包中,我们很显然会探讨MyBatis中的一对多关系映射这一核心概念。 在数据库设计中,一对多关系是常见的实体关系类型,意味着一个父表(或父实体)可以与多个子...

Global site tag (gtag.js) - Google Analytics