龙空技术网

Mybatis Plus框架配置数据库存取 BigDecimal数据末尾0去除

AceTime 88

前言:

此刻我们对“sql小数点前面的0不显示”大致比较注意,看官们都需要了解一些“sql小数点前面的0不显示”的相关内容。那么小编同时在网上搜集了一些有关“sql小数点前面的0不显示””的相关文章,希望你们能喜欢,我们一起来了解一下吧!

公司系统在存储金额类的float数据时,需要用到BigDecimal数据类型,但是末尾0用户一般不想看到,因此需要配置Mybatis Plus PaginationInnerInterceptor,实现插入数据库和返回数据的时候bigDecimal末尾0去除。

自定义了2个配置类:

PaginationInnerInterceptorMyBigDecimalTypeHandler

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());        };    }}

标签: #sql小数点前面的0不显示 #sql小数点后多余的0不显示 #sql小数点后多余的0不显示怎么回事