mybatis的关联查询中的一对多的情况。
1、声明实体类
public class Pro {
private Integer id;
private Integer contractDemandId;
/**
* 附件
*/
private List<TdAttInfo> appendixData;
}
public class TdAttInfo {
/**
* pk 主键 自增
*/
private Integer id;
private Byte attachmentFromType;
}
2、配置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.xxx.xxx.xxx.xxxxProMapper">
<resultMap id="BaseResultMap" type="com.xxx.xxx.entity.TdConDemPro">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="contract_demand_id" jdbcType="INTEGER" property="contractDemandId" />
<result column="engineering_name" jdbcType="VARCHAR" property="engineeringName" />
<!-- 配置一对多的关系(图纸) -->
<collection property="drawAttInfoList" ofType="TdAttInfo"
select="getProDraws"
column="id">
</collection>
</resultMap>
<!-- 获取工程对应图纸信息 -->
<select id="getProDraws" parameterType="map" resultType="com.xxx.xxx.entity.TdAttInfo">
SELECT
atta.id id,
atta.attachment_name attachmentName,
FROM
td_attachment_info atta
WHERE
atta.attachment_from_type = 3
AND atta.column_2 = 2
AND atta.attachment_from_id = #{id}
</select>
<select id="方法" parameterType="int" resultMap="BaseResultMap">
SELECT
dpro.id,
dpro.contract_demand_id,
FROM
xxx_tab dpro
WHERE dpro.id=#{id}
</select>
3、测试代码
自验证吧