chenjiahe
2022-05-26 f3cd78cd0355259a7f08cab33df69e0ac3a6850d
提交 | 用户 | age
f3cd78 1 package com.hx.mybatis.date.handler;
C 2
3 import org.apache.ibatis.type.BaseTypeHandler;
4 import org.apache.ibatis.type.JdbcType;
5 import org.apache.ibatis.type.MappedJdbcTypes;
6 import org.apache.ibatis.type.MappedTypes;
7
8 import java.sql.*;
9 import java.time.LocalDateTime;
10
11 /**
12  * @author CJH
13  * @Date 2021-01-02
14  * // @MappedTypes注解中的类代表此转换器可以自动转换为的java对象,@MappedJdbcTypes注解中设置的是对应的jdbctype,mysql的json对象对应的jdbctype为VARCHAR。
15  */
16 @MappedTypes(value = {LocalDateTime.class})
17 @MappedJdbcTypes(value = {JdbcType.TIMESTAMP}, includeNullJdbcType = true)
18 public class GenericDateHandler extends BaseTypeHandler<Timestamp> {
19
20     public GenericDateHandler() {
21     }
22
23     public void setNonNullParameter(PreparedStatement ps, int i, Timestamp parameter, JdbcType jdbcType) throws SQLException {
24         ps.setTimestamp(i, parameter);
25     }
26
27     public Timestamp getNullableResult(ResultSet rs, String columnName) throws SQLException {
28         return rs.getTimestamp(columnName);
29     }
30
31     public Timestamp getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
32         return rs.getTimestamp(columnIndex);
33     }
34
35     public Timestamp getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
36         return cs.getTimestamp(columnIndex);
37     }
38 }