mybatis plus中使用joda-time

支持Joda DateTime

例如 Pojo/Mo

package com.xxx.crud.eo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.joda.time.DateTime;

import java.io.Serializable;
import java.util.Date;

/**
 * 
 * @TableName dept
 */
@TableName(value ="dept")
@Data
public class Dept implements Serializable {
    /**
     * 主键id
     */
    @TableId(value = "id",type = IdType.ASSIGN_ID)
    private Long id;

    /**
     * 部门名称
     */
    @TableField(value = "dept_name")
    private String deptName;

    /**
     * 父类id
     */
    @TableField(value = "parent_id")
    private Long parentId;

    /**
     * 排序
     */
    @TableField(value = "sort")
    private Integer sort;

    /**
     * 负责人
     */
    @TableField(value = "charge_person")
    private String chargePerson;

    /**
     * 手机号
     */
    @TableField(value = "telephone")
    private Long telephone;

    /**
     * 邮箱
     */
    @TableField(value = "email")
    private String email;

    /**
     * 是否删除
     */
    @TableField(value = "delete_flag")
    private Boolean deleteFlag;

    /**
     * 创建时间
     */
    @TableField(value = "create_time",typeHandler = com.chenjie.crud.handler.DateTimeTypeHandler.class)
    private DateTime createTime;


    @TableField(exist = false)
    private static final long serialVersionUID = 1L;


}

 

mapper中,如下的实现必然报错

 <result property="createTime" column="create_time" jdbcType="DATE" typeHandler="com.xxx.crud.handler.DateTimeTypeHandler"/>

 


自定义实现DateTimeTypeHandler

参考此处外国友人的讨论 Mybatis Joda Time Support

参考其源码 LukeL99/joda-time-mybatis的实现

DateTimeTypeHandler

以下是是本人对DateTimeTypeHandler的实现,前人基础上稍作重构

package com.chenjie.crud.handler;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import java.sql.*;

/**
 * @author chenjie
 */
@MappedTypes(DateTime.class)
public class DateTimeTypeHandler implements TypeHandler<DateTime> {
    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, DateTime dateTime, JdbcType jdbcType)
            throws SQLException {
        if (dateTime != null) {
            preparedStatement.setTimestamp(i, new Timestamp(dateTime.getMillis()));
        } else {
            preparedStatement.setTimestamp(i, null);
        }
    }

    @Override
    public DateTime getResult(ResultSet resultSet, String s) throws SQLException {
        return toDateTime(resultSet.getTimestamp(s));
    }

    @Override
    public DateTime getResult(ResultSet resultSet, int i) throws SQLException {
        return toDateTime(resultSet.getTimestamp(i));
    }

    @Override
    public DateTime getResult(CallableStatement callableStatement, int i) throws SQLException {
        return toDateTime(callableStatement.getTimestamp(i));
    }

    private static DateTime toDateTime(Timestamp timestamp) {
        if (timestamp != null) {
            return new DateTime(timestamp.getTime(), DateTimeZone.UTC);
        } else {
            return null;
        }
    }
}

 正确使用方式

正确使用方式,在mapper xml中需要指定DateTime类型参数对应的 typeHandler

    /**
     * 创建时间
     */
    @TableField(value = "create_time",typeHandler = com.chenjie.crud.handler.DateTimeTypeHandler.class)
    private DateTime createTime;

sql

CREATE TABLE `dept` (
  `id` bigint NOT NULL COMMENT '主键id',
  `dept_name` varchar(40) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '部门名称',
  `parent_id` bigint DEFAULT NULL COMMENT '父类id',
  `sort` int DEFAULT NULL COMMENT '排序',
  `charge_person` varchar(40) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '负责人',
  `telephone` char(11) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '联系电话',
  `email` varchar(60) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '邮箱',
  `delete_flag` tinyint(1) DEFAULT NULL COMMENT '部门状态,true,false',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

 演示

 

 

 

posted @ 2022-12-07 17:38  小陈子博客  阅读(209)  评论(0编辑  收藏  举报