Mybatis自定义TypeHandler

Mybatis是一个优秀的持久层框架,它提供了丰富的功能来简化数据库操作。在Mybatis中,TypeHandler是用于处理Java对象与数据库字段之间的类型转换的组件。自定义TypeHandler可以帮助我们处理一些特殊的数据类型或者自定义的数据类型。

自定义TypeHandler需要实现org.apache.ibatis.type.TypeHandler接口,并重写其中的方法。主要包括以下几个方法:

  1. setParameter:用于将Java对象转换为JDBC参数进行设置。
  2. getResult:用于将数据库字段的值转换为Java对象进行返回。
  3. getResult(CallableStatement cs, int columnIndex):用于将数据库字段的值转换为Java对象进行返回,支持存储过程。
  4. getResult(ResultSet rs, String columnName):用于将数据库字段的值转换为Java对象进行返回,通过字段名获取值。
  5. getResult(ResultSet rs, int columnIndex):用于将数据库字段的值转换为Java对象进行返回,通过字段索引获取值。

下面是一个自定义TypeHandler的示例代码:

public class MyCustomTypeHandler implements TypeHandler<MyCustomType> {

    @Override
    public void setParameter(PreparedStatement ps, int i, MyCustomType parameter, JdbcType jdbcType) throws SQLException {
        // 将Java对象转换为JDBC参数进行设置
        ps.setString(i, parameter.toString());
    }

    @Override
    public MyCustomType getResult(ResultSet rs, String columnName) throws SQLException {
        // 将数据库字段的值转换为Java对象进行返回
        String value = rs.getString(columnName);
        return new MyCustomType(value);
    }

    @Override
    public MyCustomType getResult(ResultSet rs, int columnIndex) throws SQLException {
        // 将数据库字段的值转换为Java对象进行返回
        String value = rs.getString(columnIndex);
        return new MyCustomType(value);
    }

    @Override
    public MyCustomType getResult(CallableStatement cs, int columnIndex) throws SQLException {
        // 将数据库字段的值转换为Java对象进行返回,支持存储过程
        String value = cs.getString(columnIndex);
        return new MyCustomType(value);
    }
}

在Mybatis的配置文件中,我们需要注册自定义的TypeHandler。可以通过在标签中添加子标签来进行配置,示例如下:

<typeHandlers>
    <typeHandler handler="com.example.MyCustomTypeHandler"/>
</typeHandlers>

这样,Mybatis就会在需要处理MyCustomType类型的字段时,使用我们自定义的TypeHandler进行类型转换。

如果项目是使用SpringBoot开发,也可以直接在类配置器的获取sqlSessionFactory实例的方法中进行注入。

image-20240501113408143

其实Mybatis默认提供了一些基本类型的类型转换器,我们可以参照这些类型转换器去自定义。

在这个包目录下:org.apache.ibatis.type

image-20240501113021260

我们随便看一个实现类:BigDecimalTypeHandler

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.apache.ibatis.type;

import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BigDecimalTypeHandler extends BaseTypeHandler<BigDecimal> {
    public BigDecimalTypeHandler() {
    }

    public void setNonNullParameter(PreparedStatement ps, int i, BigDecimal parameter, JdbcType jdbcType) throws SQLException {
        ps.setBigDecimal(i, parameter);
    }

    public BigDecimal getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return rs.getBigDecimal(columnName);
    }

    public BigDecimal getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return rs.getBigDecimal(columnIndex);
    }

    public BigDecimal getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return cs.getBigDecimal(columnIndex);
    }
}

很简单,就是继承了BaseTypeHandler接口,然后实现其中的抽象方法就可以了。

我们可以自己实现这个BigDecimal的类型转换器,比如我这里自定义了一个BigDecimal类型转换器,默认将查询的结果小数点后面的0去掉。

package com.au.sa.storeRebate.common.handler;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author xiaohuihui
 * @creteTime 2024-05-01 10:53
 * @description
 */
@Component
public class BigDecimalTypeHandler extends BaseTypeHandler<BigDecimal> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, BigDecimal parameter, JdbcType jdbcType) throws SQLException {
        ps.setBigDecimal(i, parameter);
    }

    @Override
    public BigDecimal getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return rs.getBigDecimal(columnName).stripTrailingZeros();
    }

    @Override
    public BigDecimal getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return rs.getBigDecimal(columnIndex).stripTrailingZeros();
    }

    @Override
    public BigDecimal getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return cs.getBigDecimal(columnIndex).stripTrailingZeros();
    }
}

然后在Mybatis数据源的配置类中,注入Handler的包扫描路径就可以了。

@Primary
    @Bean("sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("master") DataSource dataSource, @Qualifier("mybatisPlusInterceptor") MybatisPlusInterceptor mybatisPlusInterceptor) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
        LOGGER.info("数据库连接池信息:{}", ((HikariDataSource)dataSource).getIdleTimeout());
        GlobalConfig globalConfig=new GlobalConfig();
        globalConfig.setMetaObjectHandler(metaObjectHandler);
        sqlSessionFactory.setGlobalConfig(globalConfig);
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setMapUnderscoreToCamelCase(true);
        if (Objects.equals(isShowSql, true)) {
            configuration.setLogImpl(StdOutImpl.class);
        }
        configuration.setCacheEnabled(false);
        configuration.addInterceptor(mybatisPlusInterceptor);
        sqlSessionFactory.setConfiguration(configuration);
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
        sqlSessionFactory.setTypeHandlersPackage("com.au.sa.storeRebate.common.handler");
        return sqlSessionFactory.getObject();
    }

就是这一行代码:

sqlSessionFactory.setTypeHandlersPackage("com.au.sa.storeRebate.common.handler");

也可以采用这种形式:

    @Resource
    private BigDecimalTypeHandler bigDecimalTypeHandler;


    sqlSessionFactory.setTypeHandlers(bigDecimalTypeHandler);

实现的效果:

image-20240501114048387

返回的结果自动将小数点后面的0去除了。

相关推荐

  1. MybatisTypeHandler使用

    2024-05-02 12:28:06       40 阅读
  2. mybatisTypeHandler解读

    2024-05-02 12:28:06       49 阅读
  3. mybatis执行定义sql

    2024-05-02 12:28:06       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-02 12:28:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-02 12:28:06       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-02 12:28:06       82 阅读
  4. Python语言-面向对象

    2024-05-02 12:28:06       91 阅读

热门阅读

  1. 【算法作业】最少分割回文字符串,开设分公司

    2024-05-02 12:28:06       33 阅读
  2. 孤独的城市公路(增加开始结束页面)

    2024-05-02 12:28:06       28 阅读
  3. 前后端分离系统的权限设计

    2024-05-02 12:28:06       31 阅读
  4. JVM

    JVM

    2024-05-02 12:28:06      32 阅读
  5. JVM对象锁的升级过程

    2024-05-02 12:28:06       35 阅读
  6. 体现记录表(wst_cash_draws)

    2024-05-02 12:28:06       27 阅读
  7. 附加功率效率 PAE 测试方法

    2024-05-02 12:28:06       29 阅读
  8. git常用命令大全

    2024-05-02 12:28:06       34 阅读
  9. 7-45 最小支撑树

    2024-05-02 12:28:06       33 阅读
  10. PyTorch与深度学习:探索人工智能的新前沿

    2024-05-02 12:28:06       37 阅读