Spring mvc增删改查如何把当前时间自动写进数据库
想把系统时间自动存进SQL SERVER该怎么处理。建了dates字段,默认值写了getdate(),就是拿不到时间,把java里面的方法去掉SQL里面时间有了,但是页面会报错。
package com.san.web.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="hiuser")
public class UserInf {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
/*主键生成策略*/
@Column(length=32)
private int number;
@Column(length=32)
private String userName;
@Column(length=32)
private String age;
@Column(length=32)
private String dates;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getUserName() {
return userName;
}
public String getAge() {
return age;
}
public String getDates() {
return dates;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setAge(String age) {
this.age = age;
}
public void setDates(String dates) {
this.dates = dates;
}
}
------解决方案--------------------在基类中各个i增删改查方法中显示调用setDate(new Date())方法,同时对实体类的setDate(Date date)方法的修饰改为private禁止别人调用,通常还可以将这个时间戳设置为乐观锁