diff --git a/src/main/java/neatlogic/framework/dao/config/mybatis-config.xml b/src/main/java/neatlogic/framework/dao/config/mybatis-config.xml index 974bd597625fce9df296fdf9a2e6afedc544ca5c..5d94ad0241df6c80d6f4f544c177923861e5e049 100644 --- a/src/main/java/neatlogic/framework/dao/config/mybatis-config.xml +++ b/src/main/java/neatlogic/framework/dao/config/mybatis-config.xml @@ -26,6 +26,7 @@ limitations under the License. + diff --git a/src/main/java/neatlogic/framework/dao/plugin/LocalDateTimeTypeHandler.java b/src/main/java/neatlogic/framework/dao/plugin/LocalDateTimeTypeHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..b7467d85b8e7ee56fb336bdebaf90c5c917b00a0 --- /dev/null +++ b/src/main/java/neatlogic/framework/dao/plugin/LocalDateTimeTypeHandler.java @@ -0,0 +1,42 @@ +package neatlogic.framework.dao.plugin; + +import org.apache.ibatis.type.BaseTypeHandler; +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.MappedTypes; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +@MappedTypes(LocalDateTime.class) +public class LocalDateTimeTypeHandler extends BaseTypeHandler { + + private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + @Override + public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException { + ps.setObject(i, LocalDateTime.parse(parameter, formatter)); + } + + @Override + public String getNullableResult(ResultSet rs, String columnName) throws SQLException { + LocalDateTime localDateTime = rs.getObject(columnName, LocalDateTime.class); + return localDateTime != null ? localDateTime.format(formatter) : null; + } + + @Override + public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException { + LocalDateTime localDateTime = rs.getObject(columnIndex, LocalDateTime.class); + return localDateTime != null ? localDateTime.format(formatter) : null; + } + + @Override + public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { + LocalDateTime localDateTime = cs.getObject(columnIndex, LocalDateTime.class); + return localDateTime != null ? localDateTime.format(formatter) : null; + } + +}