1.将时间精度精确到秒级
1)引入org.apache.commons.lang3.time.DateUtils包,下载网址https://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.9
或者导入下面的maven库
dateutil(引入原因:解决mysql数据库对时间类型进行四舍五入导致时间比实际值快一秒)
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
2)junit测试用例
@Test
public void test3(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = DateUtils.round(new Date(), Calendar.SECOND);//将时间精度精确到秒级
String time = sdf.format(date);
System.out.println(time);
try {
QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
String sqlInsert = "insert into orders(ordertime) values(?)";
qr.update(sqlInsert, date);
String sql = "select oid from orders where ordertime=?";
Long oid = qr.query(sql, new ScalarHandler<Long>(), time);
System.out.println(oid.intValue());
} catch (SQLException e) {
e.printStackTrace();
}
}
``