NullValueHandler.java 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package cn.iselab.mooctest.user.config;
  2. import org.apache.ibatis.type.JdbcType;
  3. import org.apache.ibatis.type.TypeHandler;
  4. import java.sql.*;
  5. /**
  6. * @author sean
  7. * @date 2018-03-15.
  8. */
  9. public class NullValueHandler implements TypeHandler<String> {
  10. @Override
  11. public String getResult(ResultSet rs, String columnName) throws SQLException {
  12. return rs.getString(columnName);
  13. }
  14. @Override
  15. public String getResult(ResultSet rs, int columnIndex) throws SQLException {
  16. return rs.getString(columnIndex);
  17. }
  18. @Override
  19. public void setParameter(PreparedStatement preparedStatement, int i, String s, JdbcType jdbcType) throws SQLException {
  20. if (s == null &&
  21. jdbcType == JdbcType.VARCHAR
  22. // || jdbcType == JdbcType.TIMESTAMP || jdbcType == JdbcType.BIGINT || jdbcType == JdbcType.TINYINT || jdbcType == JdbcType.INTEGER)
  23. ) {
  24. preparedStatement.setString(i, "");
  25. } else {
  26. preparedStatement.setString(i, s);
  27. }
  28. }
  29. @Override
  30. public String getResult(CallableStatement callableStatement, int i) throws SQLException {
  31. return callableStatement.getString(i);
  32. }
  33. }