[英]Inserting dates into MySQL
When doing JUnit tests I have to insert values into the database. 在进行JUnit测试时,我必须将值插入数据库。 For doing this I have a file (import.sql) which contains various inserts for various tests. 为此,我有一个文件(import.sql),其中包含用于各种测试的各种插入。 It works perfect expect for dates. 它非常适合约会。 How would I have to format dates so that MySQL would be able to import them as dates. 我将如何格式化日期,以便MySQL能够将其作为日期导入。
The java entity Java实体
import java.util.Date;
@Entity
@Table(name = "TEST")
public class Test {
@Column(name = "START")
@Temporal(TemporalType.DATE)
private Date startDate;
@Column(name = "END"
@Temporal(TemporalType.DATE)
private Date endDate;
// getter & setter
}
Snippet from the import.sql 来自import.sql的代码段
INSERT INTO TEST (START, END) VALUES ('2015-07-20 22:00:00.000', '2015-07-20 23:30:00.000')
INSERT INTO TEST (START, END) VALUES ('2015-08-18 21:00:00.000', '2015-08-18 22:30:00.000')
In this case 2015-08-18 21:00:00.000
, but this doesn't work. 在这种情况下2015-08-18 21:00:00.000
,但这是行不通的。 It results in an error telling that the format isn't correct. 导致错误,提示格式不正确。
Loading from import.sql 从import.sql加载
<property name="javax.persistence.sql-load-script-source"
value="import.sql" />
How has the date be formatted? 日期如何格式化?
要匹配输入值2015-08-18 21:00:00.000
, START
字段和END
字段类型应为DATETIME(3)
。
Just use '2015-07-20 22:00:00', so stript the fraction of the seconds from the end if you are not using them any way. 只需使用“ 2015-07-20 22:00:00”即可,因此,如果您不以任何方式使用它们,请从末尾剥离几分之一秒。 Fractional second support has been added in v5.6.4, so you may have compatibility issues if you have earlier versions. v5.6.4中添加了部分辅助支持,因此,如果您使用的是较早版本,则可能存在兼容性问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.