自定义TypeHandler
继承BaseTypeHandler指定具体的泛型
@MappedTypes({Date.class})
@MappedJdbcTypes({JdbcType.DATE})
public class DateTimeWithTImeZoneTypeHandler extends BaseTypeHandler<Date> {Log log = LogFactory.getLog(DateTimeWithTImeZoneTypeHandler.class);/*** 用于定义在Mybatis设置参数时该如何把Java类型的参数转换为对应的数据库类型* @param ps 当前的PreparedStatement对象* @param i 当前参数的位置* @param parameter 当前参数的Java对象* @param jdbcType 当前参数的数据库类型*/@Overridepublic void setNonNullParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType) throws SQLException {ps.setTimestamp(i, new Timestamp(parameter.getTime()));}/*** 用于在Mybatis获取数据结果集时如何把数据库类型转换为对应的Java类型** @param rs 当前的结果集* @param columnName 当前的字段名称* @return 转换后的Java对象* @throws SQLException the SQL exception*/@Overridepublic Date getNullableResult(ResultSet rs, String columnName) throws SQLException {String ret = rs.getString(columnName);try {SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S X");return dateFormat.parse(ret);} catch (ParseException e) {log.fatal(e.getMessage());return null;}}/*** 用于在Mybatis通过字段位置获取字段数据时把数据库类型转换为对应的Java类型* @param rs 当前的结果集* @param columnIndex 当前字段的位置* @return 转换后的Java对象*/@Overridepublic Date getNullableResult(ResultSet rs, int columnIndex) throws SQLException {String ret = rs.getString(columnIndex);try {SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S X");return dateFormat.parse(ret);} catch (ParseException e) {log.fatal(e.getMessage());return null;}}/*** 用于Mybatis在调用存储过程后把数据库类型的数据转换为对应的Java类型* @param cs 当前的CallableStatement执行后的CallableStatement* @param columnIndex 当前输出参数的位置* @return* @throws SQLException*/@Overridepublic Date getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {String ret = cs.getString(columnIndex);try {SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S X");return dateFormat.parse(ret);} catch (ParseException e) {log.fatal(e.getMessage());return null;}}
}
给需要处理的字段加上自定义typeHandler
@EqualsAndHashCode()
@Data
@TableName(value = "TEST_DATE", autoResultMap = true)
public class TestDate {@TableField(value = "NEW_TIME",typeHandler = DateTimeWithTImeZoneTypeHandler.class)private Date newTime;@TableField(value = "OLD_TIME",typeHandler = DateTimeWithTImeZoneTypeHandler.class)private Date oldTime;@TableId(value = "ID", type = IdType.ASSIGN_UUID)private String id;
}
mapper层代码
@Mapper
public interface ITestDateMapper extends BaseMapper<TestDate> {
}
Controller触发请求
@RestController
@RequestMapping("/type")
public class TypeHandleController {@Resourceprivate ITestDateMapper iTestDateMapper;@GetMapping(value = "/task1")public Result<Object> task1() {//新增TestDate testDate = new TestDate();testDate.setNewTime(new Date());testDate.setOldTime(new Date());iTestDateMapper.insert(testDate);//查询LambdaQueryWrapper<TestDate> queryWrapper = new LambdaQueryWrapper<>();List<TestDate> testDates = iTestDateMapper.selectList(queryWrapper);return Result.success(testDates);}
}
responseBody
{"code": 0,"msg": "success","data": [{"newTime": "2024-03-03T10:35:26.704+00:00","oldTime": "2024-03-03T10:35:26.704+00:00","id": "fe380a2e53307239b169197bb594f9b2"}, {"newTime": "2024-03-03T10:36:27.777+00:00","oldTime": "2024-03-03T10:36:27.777+00:00","id": "05069ec3462d4bb6ae260669dcb629a1"}, {"newTime": "2024-03-03T10:37:14.973+00:00","oldTime": "2024-03-03T10:37:14.973+00:00","id": "a05a98ebe436c5811b052ec524ac5be5"}, {"newTime": "2024-03-03T10:40:58.744+00:00","oldTime": "2024-03-03T10:40:58.744+00:00","id": "465d5c1573a4623ca518eb1f918d15c1"}, {"newTime": "2024-03-03T10:42:42.604+00:00","oldTime": "2024-03-03T10:42:42.604+00:00","id": "afcf6802b752f38b87b6eb56011f0d4d"}]
}
省略mybatisplus注入配置类和application.yml数据库配置