Mybatis日期格式自动转换需要用到的两个注解说明
作者:weixin_43954217
这篇文章主要介绍了Mybatis日期格式自动转换需要用到的两个注解说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
Mybatis日期格式自动转换
public class Student {
public Integer id;
public String studentName;
@JsonFormat(pattern="yyyy-MM-dd") //从数据库读出日期格式时,进行转换的规则
@DateTimeFormat(pattern = "yyyy-MM-dd")//接受从前端传入的日期格式,映射到java类日期属性的规则
public Date birth;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
}
mybatis注解开发的时间类型处理小记
对时间类型的操作
- select操作
@Select("select date_format(createTime,'%Y-%m-%d') from tb where id = ?")
- update操作
更新为当前时间
@Update("update tb set createTime = date_format(now(),'%Y-%m-%d %H:%i:%s') where id = ?")
更新为指定时间
/**
*jdbcType = DATE时,只传入了年月日
*/
@Update("update tb set createTime = #{param, jdbcType=DATE} where id = ?")
/**
*jdbcType = TIMESTAMP,年月日+ 时分秒
*/
@Update("update tb set createTime = #{param, jdbcType=TIMESTAMP } where id = ?")
关于MySQL Date 函数的用法 参考这里
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
