MyBatis学习笔记——一对一,多对一

来源:互联网 发布:java泛型t 编辑:程序博客网 时间:2024/05/27 21:49

1.一对一的关联关系的查询

比如Student中插入了一个Address的对象,t_student中的addressId和t_address关联,StudentMapper:

public class Student {    private Integer id;    private String name;    private Integer age;    private Address address;    public Address getAddress() {        return address;    }    public void setAddress(Address address) {        this.address = address;    }    public Student(){    }    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;    }    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 Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }    @Override    public String toString() {        return "Student [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + "]";    }}

AddressMapper:

public interface AddressMapper {    public Student findById(Integer id);}

StudentMapper.xml:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.java1234.mapper.StudentMapper">    <!-- 自定义一个用于返回List数据类型resultMap -->    <resultMap type="Student" id="StudentResult">        <id property="id" column="id"/>        <result property="name" column="name"/>        <result property="age" column="age"/>        <!-- 一对一的关联映射关系的查询 ,用<association>标签指定,column="addressId"中的id是t_address的id关联的外键,select标签中表示是调用AddressMapper中的findById(通过ID查找address)的方法,-->        <association property="address" column="addressId" select="com.java1234.mapper.AddressMapper.findById"></association>    </resultMap>        <select id="findStudentAddress" parameterType="Integer" resultMap="StudentResult">        select * from tb_student s,tb_address a where s.addressId=a.id and s.id=#{id}    </select></mapper> 

AddressMap.xml:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.java1234.mapper.AddressMapper">    <resultMap type="Address" id="AddressResult">        <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 tb_address where id=#{id}    </select></mapper>

2.多对一关联映射关系的查询

在Student类中加一个Grade类的对象类grade,在StudentMapper.xml中,自定义的ResultMap中增加一个属性:

<!-- 在多方建立对一方的引用 -->        <association property="grade" column="gradeId" select="com.java1234.mapper.GradeMapper.findById"></association>

即可

3.一对多关联关系的查询

在一方创建一个集合:

private List<Student> students;

GradeMapper.xml中用collection指定students集合:

<resultMap type="Grade" id="GradeResult">        <result property="id" column="id"/>        <result property="gradeName" column="gradeName"/>        <collection property="students" column="id" select="com.java1234.mapper.StudentMapper.findStudentsByGradeId"></collection>    </resultMap>    <select id="findById" parameterType="Integer" resultMap="GradeResult">        select * from tb_grade where id=#{id}    </select>

这里注意如果同时在一方建立了多方的集合以及在多方建立了一方的引用,即2、3情况集合在了一起,那么注意他们的toString方法只能在其中 的一个中引用,否则将会陷入死循环,内存溢出的错误StackOverflowerError,(即如果Student类中重写toString方法时包含了Grade的属性,那在重写Grade的toString方法时不能包含Student的那个students,反之一样),详见Eclispe,MyBatis_HelloWorld项目

阅读全文
0 0
原创粉丝点击