天天看點

Mybatis Plus架構配置資料庫存取 BigDecimal資料末尾0去除

作者:AceTime

公司系統在存儲金額類的float資料時,需要用到BigDecimal資料類型,但是末尾0使用者一般不想看到,是以需要配置Mybatis Plus PaginationInnerInterceptor,實作插入資料庫和傳回資料的時候bigDecimal末尾0去除。

自定義了2個配置類:

  • PaginationInnerInterceptor
  • MyBigDecimalTypeHandler
package com.demo.config.mybatis;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

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

/**
 * @Author: pengweijian
 * @Date: 2023/1/12 10:43
 * @Description: 自定義BigDecimal類型Handler
 */
public class MyBigDecimalTypeHandler 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 {
        BigDecimal result = rs.getBigDecimal(columnName);
        if(result != null) {
            //去除小數點後面尾部多餘的0
            result = result.stripTrailingZeros();
        }
        return result;
    }

    @Override
    public BigDecimal getNullableResult(ResultSet rs, int columnIndex)
            throws SQLException {
        BigDecimal result = rs.getBigDecimal(columnIndex);
        if(result != null) {
            //去除小數點後面尾部多餘的0
            result = result.stripTrailingZeros();
        }
        return result;
    }

    @Override
    public BigDecimal getNullableResult(CallableStatement cs, int columnIndex)
            throws SQLException {
        BigDecimal result = cs.getBigDecimal(columnIndex);
        if(result != null) {
            //去除小數點後面尾部多餘的0
            result = result.stripTrailingZeros();
        }
        return result;
    }
}           
package com.demo.config.mybatis;

import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.math.BigDecimal;

/**
 * mybatis-plus 配置
 *
 * @author pengweijian
 */
@Configuration
@MapperScan("com.demo.**.**.mapper.**")
public class MybatisPlusConfiguration {

    /**
     * 分頁插件
     *
     * @return
     */
    @Bean
    public PaginationInnerInterceptor paginationInnerInterceptor() {
        return new PaginationInnerInterceptor();
    }

    @Bean
    public ConfigurationCustomizer  typeHandlerRegistry() {
        return configuration -> {
            //代碼增強,實作插入資料庫和傳回資料的時候bigDecimal末尾0去除
            TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
            typeHandlerRegistry.register(BigDecimal.class, new MyBigDecimalTypeHandler());
            typeHandlerRegistry.register(JdbcType.NUMERIC, new MyBigDecimalTypeHandler());
            typeHandlerRegistry.register(JdbcType.DECIMAL, new MyBigDecimalTypeHandler());
        };
    }

}


           

繼續閱讀