DecimalHandler.java 1.28 KB
Newer Older
licc's avatar
licc committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
package cn.wisenergy.common.utils.mybatis;

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

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

/**
 * MyBatisDecimal转换器
 * Created by wyy on 2017/2/21.
 */
public class DecimalHandler implements TypeHandler<BigDecimal> {
    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, BigDecimal bigDecimal, JdbcType jdbcType) throws SQLException {
        preparedStatement.setBigDecimal(i, bigDecimal);
    }

    @Override
    public BigDecimal getResult(ResultSet resultSet, String s) throws SQLException {
        return transfer(resultSet.getString(s));
    }

    @Override
    public BigDecimal getResult(ResultSet resultSet, int i) throws SQLException {
        return transfer(resultSet.getString(i));
    }

    @Override
    public BigDecimal getResult(CallableStatement callableStatement, int i) throws SQLException {
        return transfer(callableStatement.getString(i));
    }

    private BigDecimal transfer(String val) {
        if (val != null) {
            val = val.replace(",", "");
            return new BigDecimal(val);
        }
        return null;
    }

}