敏捷工具
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.3 KiB

2 years ago
  1. package com.whn.hellospring.handler;
  2. import org.apache.ibatis.type.BaseTypeHandler;
  3. import org.apache.ibatis.type.JdbcType;
  4. import org.joda.money.CurrencyUnit;
  5. import org.joda.money.Money;
  6. import java.sql.CallableStatement;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. /**
  11. * 在Money与Long之间转换的TypeHandler,处理CNY和人民币
  12. */
  13. public class MoneyTypeHandler extends BaseTypeHandler<Money> {
  14. @Override
  15. public void setNonNullParameter(PreparedStatement preparedStatement, int i, Money money, JdbcType jdbcType) throws SQLException {
  16. preparedStatement.setLong(i,money.getAmountMinorLong()); //存储为Long类型,精确到分
  17. }
  18. @Override
  19. public Money getNullableResult(ResultSet resultSet, String s) throws SQLException {
  20. return parseMoney(resultSet.getLong(s));
  21. }
  22. @Override
  23. public Money getNullableResult(ResultSet resultSet, int i) throws SQLException {
  24. return parseMoney(resultSet.getLong(i));
  25. }
  26. @Override
  27. public Money getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
  28. return parseMoney(callableStatement.getLong(i));
  29. }
  30. /**
  31. * 将Long值转换为Money
  32. */
  33. private Money parseMoney(Long value){
  34. return Money.ofMinor(CurrencyUnit.of("CNY"),value);
  35. }
  36. }