public class UtcInstantTypeHandler extends org.apache.ibatis.type.BaseTypeHandler<Instant>
在国际化的项目中时间使用规范建议:created_at_utc
1)Java中使用`Instant`(也可以使用`OffsetDateTime`)
2)Database中使用`DATETIME(6)`
3)前后端交互使用UTC字符串`2000-01-01T00:00:00.000Z`,在前端JS代码中转换为本地时间展示`new Date("2000-01-01T00:00:00.000Z")`
4)Java把Instant转成LocalDateTime → 保存数据库:instant.atZone(ZoneOffset.UTC).toLocalDateTime();
5)读取数据库 → Java把LocalDateTime转成Instant:datetime.atZone(ZoneOffset.UTC).toInstant()
Java Instant与Mysql datetime之间的相互转换,在Mybatis的配置文件`mybatis-config.xml`中添加:
<typeHandlers>
<typeHandler handler="cn.ponfee.disjob.common.spring.UtcInstantTypeHandler" javaType="java.time.Instant" />
</typeHandlers>
InstantTypeHandler| 构造器和说明 |
|---|
UtcInstantTypeHandler() |
| 限定符和类型 | 方法和说明 |
|---|---|
Instant |
getNullableResult(CallableStatement cs,
int columnIndex) |
Instant |
getNullableResult(ResultSet rs,
int columnIndex) |
Instant |
getNullableResult(ResultSet rs,
String columnName) |
void |
setNonNullParameter(PreparedStatement ps,
int i,
Instant parameter,
org.apache.ibatis.type.JdbcType jdbcType) |
public void setNonNullParameter(PreparedStatement ps, int i, Instant parameter, org.apache.ibatis.type.JdbcType jdbcType) throws SQLException
setNonNullParameter 在类中 org.apache.ibatis.type.BaseTypeHandler<Instant>SQLExceptionpublic Instant getNullableResult(ResultSet rs, String columnName) throws SQLException
getNullableResult 在类中 org.apache.ibatis.type.BaseTypeHandler<Instant>SQLExceptionpublic Instant getNullableResult(ResultSet rs, int columnIndex) throws SQLException
getNullableResult 在类中 org.apache.ibatis.type.BaseTypeHandler<Instant>SQLExceptionpublic Instant getNullableResult(CallableStatement cs, int columnIndex) throws SQLException
getNullableResult 在类中 org.apache.ibatis.type.BaseTypeHandler<Instant>SQLExceptionCopyright © 2025. All rights reserved.