文章目录
前后端传输Date类型数据
在前端显示如 “2022-04-14 00:00:00”的Json数据格式,后端编写方法有如下几种:
JsonFormat注解
- 在实体类的变量声明加上JsonFormat
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date time;
- 在实体类的get方法上加入JsonFormat和JsonFileld
@JsonFormat(
pattern = "yyyy-MM-dd HH:mm:ss"
)
@JSONField(
format = "yyyy-MM-dd HH:mm:ss"
)
public Date getTime() {
return time;
}
多参数查询
Controller层
/**
* 方法传入对象,根据对象查询
* @param emsLoginLog
* @return json
*/
@RequestMapping("/findAllLog")
@ResponseBody
public String findAllLog(EmsLoginLog emsLoginLog) throws ParseException {
List<EmsLoginLog> emsLoginLogList= emsLoginLogService.findAllLog(emsLoginLog);
return JsonMapper.getInstance().toJson(emsLoginLogList);
}
Service 和Dao层
//Service 和Dao 添加一个查询语句即可
List<EmsLoginLog> findAllLog(EmsLoginLog emsLoginLog);
Mybatis查询语句
根据对象有无进行查询,可多参数查询,不需要写多个查询语句。
<select id="findAllLog" parameterType="EmsLoginLog" resultMap="EmsLoginLogMap">
select info_id, user_name, ipaddr, login_type, status, login_time from ems_logininfor
<where>
<if test="ipaddr != null and ipaddr != ''">
AND ipaddr = #{ipaddr}
</if>
<if test="status != null and status != ''">
AND status = #{status}
</if>
<if test="userName != null and userName != ''">
AND user_name = #{userName}
</if>
<if test="startTime !=null">
and login_time >= #{startTime}
</if>
<if test="endTime != null">
and login_time <= #{endTime}
</if>
</where>
order by info_id desc
</select>
测试
使用软件 postman。
测试链接:
http://ip:port/findAllLog?endTime=2022-04-14 00:00:00&startTime=2022-04-14 10:00:00
返回结果如下:
Postgresql与Mysql 转换Date语法
Postgresql
- 双:: 转换Date
<if test="endTime != null">
and login_time <= #{endTime}::Date
</if>
- to_date
<if test="endTime != null">
and login_time <= to_date(#{endTime},'yyyy-MM-dd HH:mm:ss')
</if>
Mysql
- date_format(#{endTime},‘%y%m%d’)
<if test="endTime != null '">
and date_format(login_time,'%y%m%d') <= date_format(#endTime},'%y%m%d')
</if>
此次多重查询就到此结束,如有不正,欢迎大家评论指正。