zoukankan      html  css  js  c++  java
  • mybatis 返回null 转 空字符串

    package com.neusoft.mid.msf.issaweb.config;

    import org.apache.ibatis.executor.result.ResultMapException;
    import org.apache.ibatis.type.BaseTypeHandler;
    import org.apache.ibatis.type.JdbcType;
    import org.apache.ibatis.type.MappedJdbcTypes;
    import org.apache.ibatis.type.MappedTypes;

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

    /**

    • @Name: CustomStringTypeHandler

    • @Desc: 自定义mybatis处理类,将null返回为空串(‘’)

    • @Author: zhou.wang

    • @Date: 2021-05-20 13:41
      */
      @MappedTypes({String.class})
      @MappedJdbcTypes(JdbcType.VARCHAR)
      public class CustomStringTypeHandler extends BaseTypeHandler {

      @Override
      public String getResult(ResultSet rs, String columnName) {
      String result;
      try {
      result = getNullableResult(rs, columnName);
      } catch (Exception e) {
      throw new ResultMapException("Error attempting to get column '" + columnName + "' from result set. Cause: " + e, e);
      }
      return result;
      }

      @Override
      public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType)
      throws SQLException {
      ps.setString(i, parameter);
      }

      @Override
      public String getNullableResult(ResultSet rs, String columnName)
      throws SQLException {
      return rs.getString(columnName) == null? "" : rs.getString(columnName);
      }

      @Override
      public String getNullableResult(ResultSet rs, int columnIndex)
      throws SQLException {
      return rs.getString(columnIndex) == null? "" : rs.getString(columnIndex);
      }

      @Override
      public String getNullableResult(CallableStatement cs, int columnIndex)
      throws SQLException {
      return cs.getString(columnIndex) == null? "" : cs.getString(columnIndex);
      }
      }

  • 相关阅读:
    POJ_2104_K-th Number_主席树
    BZOJ_1014_[JSOI2008]火星人prefix_splay+hash
    BZOJ_1861_[Zjoi2006]Book 书架_splay
    BZOJ_2242_[SDOI2011]计算器_快速幂+扩展GCD+BSGS
    BZOJ_3239_Discrete Logging_BSGS
    BZOJ_1269&&1507_[AHOI2006]文本编辑器editor&&[NOI2003]Editor
    BZOJ_1552_[Cerc2007]robotic sort_splay
    BZOJ_1500_[NOI2005]维修数列_splay
    BZOJ_1251_序列终结者
    吴裕雄--天生自然ORACLE数据库学习笔记:优化SQL语句
  • 原文地址:https://www.cnblogs.com/adai-study-1030/p/14789511.html
Copyright © 2011-2022 走看看