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
46
47
48
49
50
package cn.chnmuseum.party.common.mybatis;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
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转换器
*/
/* 数据库中的数据类型 */
@MappedJdbcTypes(JdbcType.DECIMAL)
/* 转化后的数据类型 */
@MappedTypes(value = BigDecimal.class)
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;
}
}