postgreSQL + mybatis + springboot
1 数据库存储类型
postgreSQL数据库
表中时间字段:
名称 | 类型 |
---|---|
time_low_limit | timestamp |
time_up_limit | timestamp |
因为时间在数据库表中存储的是timestamp类型,因此在java的model类中,与数据库对应的字段类型采用的Date类型。
2 model 类
@Table(name = "class_info")
public class ClassInfo {
……
/**
* 时间上限
*/
@Column(name = "time_up_limit")
private Date timeUpLimit;
/**
* 时间下限
*/
@Column(name = "time_low_limit")
private Date timeLowLimit;
……
}
往表内插入数据时,传入时间为时间戳格式的Long型,故而转换成了Date类型。
classInfo.setTimeUpLimit(new Date(reqDTO.getTimeUpLimit()));
classInfo.setTimeLowLimit(new Date(reqDTO.getTimeLowLimit()));
classInfoService.save(caseInfo);
3 通过时间段进行筛选查询
此时传入的时间为字符串类型的时间戳数字,同样最终需要转换成Date类型
// 字符串先转Long类型,Long类型再转Date类型
queryCondition.setTimeUpLimit(new Date(Long.parseLong(value))); // 这里value为字符串类型的时间戳数字
4 mapper.xml文件中关于时间筛查的条件查询sql
<select id="listByCondition" resultMap="BaseResultMap">
select * from class_info
where 1=1
<if test="timeUpLimit != null">
and #{timeUpLimit} > time_up_limit
</if>
<if test="timeLowLimit != null">
and time_low_limit > #{timeLowLimit}
</if>
<choose>
<when test="orderField != null">
order by #{orderField}
</when>
<otherwise>
order by update_time
</otherwise>
</choose>
<choose>
<when test="orderRule != null">
#{orderRule}
</when>
<otherwise>
desc
</otherwise>
</choose>
</select>