需求:查询操作记录表中 operate_type=’版本更新‘ 的所有记录 中距离当前时间最近的一条记录
解决方案:
<select id="getObjectById" parameterType="hashmap" statementType="STATEMENT" >
SELECT
poi_table_name,
poi_id,
operate_type,
operate_time
FROM
gis_poi_operation_record
<where>
<if test="operate_type!=null">
operate_type = ’版本更新‘
</if>
<if test="operate_time!=null">
<![CDATA[ and operate_time < current_timestamp ]]>
</if>
</where>
order by operate_time desc limit 1
</select>
其中 标红色的 代码 是因为 在mybatis中 小于号 < 大于号> 会被识别为标签,所以解析错误 ,报错原因是:元素内容必须由格式正确的字符数据或标记组成。 所以需要加上红色部分的代码
主要就是用了 postgresql的内置函数 current_timestamp 去和 表中的时间字段 operate_time 做对比