實體類
@Data public class ListImageTextVO { private String id; private Integer itype; private String title;private List<String> picUrls; //list集合 }
xml文件(必須寫resultMap)
這里列舉兩種寫法:
第一種:
<collection property="picUrls" column="id" select="findPics" />
其中,column必填,為傳遞參數,在select指定的方法中需要用到,select指定的方法的寫法應該為:mapper內的地址+方法名
我這里是因為指定的方法在同一個mapper的xml文件中,所以簡寫了;
這里更完整的寫法為:
<collection property="picUrls" javaType="ArrayList" column="id" ofType="java.lang.String" select="com.ruiheng.admin.mapper.ImageTextMapper.addPics" />
<resultMap id="ListImageTextVO" type="com.ruiheng.admin.vo.imagetext.ListImageTextVO"> <id column="id" property="id" jdbcType="VARCHAR"/> <result column="itype" property="itype" jdbcType="INTEGER"/> <result column="title" property="title" jdbcType="VARCHAR"/> <collection property="picUrls" column="id" select="findPics" /> </resultMap> <select id="findList" resultMap="ListImageTextVO" > SELECT it.* FROM t_image_text it
</select> <resultMap id="picUrls" type="String" > <result column="pic_url" property="picUrl"/> </resultMap> <select id="findPics" resultMap="picUrls"> select pic_url from t_third_pic where third_id=#{id} </select>
第二種:
<collection property="picUrls" resultMap="picUrls" />
<resultMap id="ListImageTextVO" type="com.ruiheng.admin.vo.imagetext.ListImageTextVO"> <id column="id" property="id" jdbcType="VARCHAR"/> <result column="itype" property="itype" jdbcType="INTEGER"/> <result column="title" property="title" jdbcType="VARCHAR"/> <collection property="picUrls" resultMap="picUrls" /> </resultMap> <select id="findList" resultMap="ListImageTextVO" > SELECT it.* ,tp.pic_url FROM t_image_text it
JOIN t_third_pic tp ON tp.third_id = it.id
</select> <resultMap id="picUrls" type="String" > <result column="pic_url" property="picUrl"/> </resultMap>
通過觀察上面兩種寫法,很明顯是第二種更簡潔一些,不用單獨寫sql語句,直接在一個地方寫就好了;當然,這是因為我查詢的集合只包含簡單的String 類型參數,如果這里是復雜的對象,
相對而言,第一種寫法更靈活,思路清晰一些;所以具體情況應該具體對待