mybatis之级联关系(一对一对多)

Posted blue星空

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis之级联关系(一对一对多)相关的知识,希望对你有一定的参考价值。

0. 表结构

 

 

 

 

1. 准备工作

    1.1 配置文件等信息,请参考  myBatis之入门示例

    1.2 entity

  1.2.1 TPersonInfo.java

package com.blueStarWei.entity;

public class TPersonInfo {
    
    private Integer id;
    private String name;
    private Integer age;
    private Address address;
    
    //setter & getter
    
    @Override
    public String toString() {
        return "TPersonInfo [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + "]";
    }
    
}

       1.2.2 Address.java

package com.blueStarWei.entity;

public class Address {
    
    private int id;
    private String country;
    private String city;
    
    //setter & getter
    
    @Override
    public String toString() {
        return "Address [country=" + country + ", city=" + city + "]";
    }
    
}

 

2 一对一关系(Maper)

    2.1 方法一:

        2.1.1 PersonAddressMapper.java

package com.blueStarWei.mappers;

import java.util.List;

import com.blueStarWei.entity.TPersonInfo;

public interface PersonAddressMapper {
    
    List<TPersonInfo> findAllWithAddress();
}

         2.1.2 PersonAddressMapper.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="com.blueStarWei.mappers.PersonAddressMapper">
    
    <select id="findAllWithAddress" resultMap="personResult">
        SELECT t1.*,t2.* FROM t_person_info t1 JOIN t_address t2 ON t2.id = t1.addressid 
    </select>
    
    <resultMap type="TPersonInfo" id="personResult">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
        <association property="address" resultMap="addressResult"/>
    </resultMap>
    
    <resultMap type="Address" id="addressResult">
        <id property="id" column="id"/>
        <result property="country" column="country"/>
        <result property="city" column="city"/>
    </resultMap>

</mapper> 

 

    2.2 方法二【推荐】

        2.2.1 AddressMapper.java

package com.blueStarWei.mappers;

import com.blueStarWei.entity.Address;

public interface AddressMapper {

    public Address findById(Integer id);
}

        2.2.2 AddressMapper.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="com.blueStarWei.mappers.AddressMapper">
    
    <select id="findById" parameterType="Integer" resultMap="addressResult">
        SELECT * FROM t_address t where t.id = #{id}
    </select>
    
    <resultMap type="Address" id="addressResult">
        <id property="id" column="id"/>
        <result property="country" column="country"/>
        <result property="city" column="city"/>
    </resultMap>

</mapper> 

        2.2.3 PersonMapper.java

package com.blueStarWei.mappers;

import java.util.List;

import com.blueStarWei.entity.TPersonInfo;

public interface PersonMapper {

    TPersonInfo findById(Integer id);
    
    List<TPersonInfo> findAll();
}

        2.2.4 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="com.blueStarWei.mappers.PersonMapper">
    
    <select id="findById" parameterType="Integer" resultMap="personResult">
        select * from t_person_info a where a.id = #{id}
    </select>
    
    <select id="findAll" resultMap="personResult">
        select * from t_person_info
    </select>
    
    <resultMap type="TPersonInfo" id="personResult">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
        <association property="address" column="addressid" 
            select="com.blueStarWei.mappers.AddressMapper.findById"/>
    </resultMap>
    
</mapper> 

 

    2.3. 总结:

        方法一是使用级联的方式查找出需要的所有信息,然后将结果返回到对应的entity类中,方法二是通过外键关联的方式返回结果。方法二在开发过程中被推荐时间,因为其具有代码高复用性。

 

3 一对多关系

    3.1 Family.java

package com.blueStarWei.entity;

import java.util.List;

public class Family {

    private Integer id;
    private String familyCode;
    private List<TPersonInfo> persons;
    
    //setter & getter

    @Override
    public String toString() {
        return "Family [id=" + id + ", familyCode=" + familyCode + ", persons=" + persons + "]";
    }
    
}

    3.2 FamilyMapper.java

package com.blueStarWei.mappers;

import com.blueStarWei.entity.Family;

public interface FamilyMapper {

    Family findById(Integer id);
    
}

    3.3 FamilyMapper.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="com.blueStarWei.mappers.FamilyMapper">
    
    <select id="findById" parameterType="Integer" resultMap="familyResult">
        SELECT * FROM t_family t where t.id = #{id}
    </select>
    
    <resultMap type="Family" id="familyResult">
        <id property="id" column="id"/>
        <result property="familyCode" column="familyCode"/>
        <collection property="persons" column="id" select="com.blueStarWei.mappers.PersonMapper.findByFamilyId"/>
    </resultMap>

</mapper> 

    3.4 PersonMapper.java

package com.blueStarWei.mappers;

import java.util.List;

import com.blueStarWei.entity.TPersonInfo;

public interface PersonMapper {

    List<TPersonInfo> findByFamilyId(Integer familyId);
}

    3.5 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="com.blueStarWei.mappers.PersonMapper">
    
    <select id="findByFamilyId" parameterType="Integer" resultMap="personResult">
        select * from t_person_info a where a.familyId = #{familyId}
    </select>
    
    <resultMap type="TPersonInfo" id="personResult">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
        <association property="address" column="addressid" 
            select="com.blueStarWei.mappers.AddressMapper.findById"/>
    </resultMap>
    
</mapper> 

 

以上是关于mybatis之级联关系(一对一对多)的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis之级联小结

Mybatis一对一,一对多,多对多代码

mybatis 查询一对多 集合中没有值为啥

Mybatis 级联查询 (一对多 )

MyBatis框架关联映射

java,mybatis 一对多级联查询,怎么给多的一方添加条件啊???