博主首页

mybatis类型转换。TypeHandler案例

配置

mybatis-plus:
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: false
  global-config:
    db-config:
      column-underline: true
      db-type: mysql
    refresh: false
  type-handlers-package: com.peony.common.typehandlers

 

自定义类型转换类

package com.peony.common.typehandlers;

import com.peony.common.util.Slog;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

/**
 * @projectName: recorder
 * @package: com.peony.common.typehandlers
 * @className: MyDateTypeHandler
 * @author: zy
 * @description:
 * @date: 2023-09-06 11:32
 */
public class MyDateTypeHandler extends BaseTypeHandler<LocalDateTime> {

    /**
     * 将java对象转为数据库对象进行存储
     * @param preparedStatement
     * @param i
     * @param localDateTime
     * @param jdbcType
     * @throws SQLException
     */
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, LocalDateTime localDateTime, JdbcType jdbcType) throws SQLException {
        // 将 LocalDateTime 转换为 ZonedDateTime
        try{
            if(localDateTime!=null){
                ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
                // 将 ZonedDateTime 转换为 Date
                Date date = (Date) Date.from(zonedDateTime.toInstant());
                preparedStatement.setDate(i,date);
            }
        }catch (Exception e){
            Slog.error(e.getMessage(),e);
        }
    }


    /**
     * 将数据库类型转为java类型
     * @param resultSet
     * @param s
     * @return
     * @throws SQLException
     */
    @Override
    public LocalDateTime getNullableResult(ResultSet resultSet, String s) throws SQLException {
        Timestamp timestamp = resultSet.getTimestamp(s);
        if(timestamp!=null){
            java.util.Date date1 = new java.util.Date(timestamp.getTime());
            return LocalDateTime.ofInstant(date1.toInstant(), ZoneId.systemDefault());
        }

        return null;
    }

    @Override
    public LocalDateTime getNullableResult(ResultSet resultSet, int i) throws SQLException {
        Timestamp timestamp = resultSet.getTimestamp(i);
        if(timestamp!=null){
            java.util.Date date1 = new java.util.Date(timestamp.getTime());
            return LocalDateTime.ofInstant(date1.toInstant(), ZoneId.systemDefault());
        }
        return null;
    }

    @Override
    public LocalDateTime getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        Timestamp timestamp = callableStatement.getTimestamp(i);
        if(timestamp!=null){
            java.util.Date date1 = new java.util.Date(timestamp.getTime());
            return LocalDateTime.ofInstant(date1.toInstant(), ZoneId.systemDefault());
        }
        return null;
    }
}

对应类型的字段就会走转换代码

private java.time.LocalDateTime workBeginDate;
posted @ 2023-09-06 14:32  笑~笑  阅读(95)  评论(0编辑  收藏  举报