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> 
复制代码

 

posted @   blue星空  阅读(369)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示