Mybatis-Plus一对多

作者: adm 分类: java,mybatis 发布时间: 2021-08-21

Mybatis-Plus一对多及分页

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" >
<!-- 这里填写对应的Dao文件所在的路径 -->
<mapper namespace="net.yinzhong.export.mapper.CustomsUnmixedMapper">
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="net.yinzhong.export.entity.CustomsUnmixedEntity">
        <id column="id" property="id" />


        <collection property="customsUnmixedExtend" ofType="net.yinzhong.export.entity.CustomsUnmixedExtendEntity" column="id" select="getCustomsUnmixedExtendList">
            <result column="unmixed_id" property="unmixedId" />
            <result column="gnum" property="gnum" />
        </collection>
    </resultMap>
    <select id="getCustomsUnmixedByTid" resultMap="BaseResultMap">
        select *
        from yz_customs_unmixed
        where tid=#{tid}
    </select>
    <select id="getCustomsUnmixedExtendList" resultType="net.yinzhong.export.entity.CustomsUnmixedExtendEntity">
        select * FROM yz_customs_unmixed_extend WHERE unmixedId=#{unmixedId}
    </select>
</mapper>

 

  1. xml上面的映射resultMap中使用collection标签来完成一对多。
  2. collection标签中property属性的值跟CustomsUnmixed实体类中的List属性对应,查出的多条图片的数据会映射到CustomsUnmixed这个属性中。
  3. collection标签中column属性值为CustomsUnmixed中的id,要根据这个id去CustomsUnmixedExtend表中查询

Mapper

List<CustomsUnmixedEntity> getCustomsUnmixedByTid(String tid);
List<CustomsUnmixedExtendEntity> getCustomsUnmixedExtendList(Long unmixedId);

Server

public List<CustomsUnmixedEntity> getCustomsUnmixedByTid(String tid) {
    return customsUnmixedMapper.getCustomsUnmixedByTid(tid);
}

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!