闲杂小记(八)

本文介绍了一种使用MyBatis实现动态SQL的方法,并通过一个具体的例子进行说明。该例子展示了如何根据不同的条件筛选数据库中的记录,同时讨论了如何在Java中处理查询结果并释放资源。

1.增量写法:

Album getAlbum(@Param("aid") long aid, @Param("withDelete") boolean withDelete, @Param("lastTime") Date lastTime);

<select id="getAlbum" resultMap="BaseResultMap">
    SELECT
    <include refid="Base_Column_List"/>
    <if test="lastTime==null">
        ,a.is_deleted
    </if>
    FROM bp_album a
    <where>
        AND a.aid = #{aid,jdbcType=BIGINT}
        <if test="lastTime != null">
            AND a.update_time &gt;= #{lastTime, jdbcType=TIMESTAMP}
        </if>
        <if test="!withDelete">
            AND a.is_deleted = FALSE
        </if>
    </where>
</select>

2. 当某个list不被引用时,可以手动将它设为null,以便垃圾回收,释放内存。

List<Picture> pictureList = pictureMapper.getMyList(userID,lastId,limit+1,lastDate);
if(!CollectionUtils.isEmpty(pictureList)){
    for(Picture picture:pictureList){
        if(null==picture){
            continue;
        }
        RecordDTO recordDTO = getPictureRecordDTO(picture);
        dataList.add(recordDTO);
    }
    pictureList.clear();
    pictureList=null;
}