1. 程式人生 > >Mybatis傳值為空需要配置JdbcType來解決嗎?(XML檔案不需要配置JdbcType)

Mybatis傳值為空需要配置JdbcType來解決嗎?(XML檔案不需要配置JdbcType)

1,解決思路,配置自定義的語言驅動,重寫自己的Paramethander

 

 1 package cn.com.servyou.gxdqy.tool.xmlhelper;
 2 
 3 import org.apache.ibatis.executor.parameter.ParameterHandler;
 4 import org.apache.ibatis.mapping.BoundSql;
 5 import org.apache.ibatis.mapping.MappedStatement;
 6 import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver;
7 8 /** 9 * @author : hao 10 * @project : daieweb 11 * @description : 自定義的 jdbcType 語言驅動 12 * @time : 2018/10/12 16:00 13 */ 14 public class JdbcTypeLanguageDriver extends XMLLanguageDriver { 15 public JdbcTypeLanguageDriver() { 16 } 17 18 @Override 19 public ParameterHandler createParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
20 return new JdbcTypeDefaultParameterHandler(mappedStatement, parameterObject, boundSql); 21 } 22 }

 

 

  1 package cn.com.servyou.gxdqy.tool.xmlhelper;
  2 
  3 import org.apache.ibatis.executor.ErrorContext;
  4 import org.apache.ibatis.mapping.BoundSql;
  5 import
org.apache.ibatis.mapping.MappedStatement; 6 import org.apache.ibatis.mapping.ParameterMapping; 7 import org.apache.ibatis.mapping.ParameterMode; 8 import org.apache.ibatis.reflection.MetaObject; 9 import org.apache.ibatis.scripting.defaults.DefaultParameterHandler; 10 import org.apache.ibatis.session.Configuration; 11 import org.apache.ibatis.type.JdbcType; 12 import org.apache.ibatis.type.TypeException; 13 import org.apache.ibatis.type.TypeHandler; 14 import org.apache.ibatis.type.TypeHandlerRegistry; 15 16 import java.math.BigDecimal; 17 import java.math.BigInteger; 18 import java.sql.PreparedStatement; 19 import java.sql.SQLException; 20 import java.util.Date; 21 import java.util.HashMap; 22 import java.util.List; 23 import java.util.Map; 24 25 /** 26 * @author : hao 27 * @project : daieweb 28 * @description : 引數處理器 負責把值為空且jdbcType為空的 初始化 jdbcType 29 * @time : 2018/10/12 16:01 30 */ 31 public class JdbcTypeDefaultParameterHandler extends DefaultParameterHandler { 32 33 private final TypeHandlerRegistry typeHandlerRegistry; 34 private final MappedStatement mappedStatement; 35 private final Object parameterObject; 36 private BoundSql boundSql; 37 private Configuration configuration; 38 public static Map<Class<?>, JdbcType> typeMap = new HashMap(); 39 40 static { 41 42 //設定預設的型別轉換,參考 TypeHandlerRegistry 43 register(Boolean.class, JdbcType.BOOLEAN); 44 register(boolean.class, JdbcType.BOOLEAN); 45 46 register(Byte.class, JdbcType.TINYINT); 47 register(byte.class, JdbcType.TINYINT); 48 49 register(Short.class, JdbcType.SMALLINT); 50 register(short.class, JdbcType.SMALLINT); 51 52 register(Integer.class, JdbcType.INTEGER); 53 register(int.class, JdbcType.INTEGER); 54 55 register(Long.class, JdbcType.BIGINT); 56 register(long.class, JdbcType.BIGINT); 57 58 register(Float.class, JdbcType.FLOAT); 59 register(float.class, JdbcType.FLOAT); 60 61 register(Double.class, JdbcType.DOUBLE); 62 register(double.class, JdbcType.DOUBLE); 63 64 register(String.class, JdbcType.VARCHAR); 65 66 register(BigDecimal.class, JdbcType.DECIMAL); 67 register(BigInteger.class, JdbcType.DECIMAL); 68 69 register(Byte[].class, JdbcType.BLOB); 70 register(byte[].class, JdbcType.BLOB); 71 72 register(Date.class, JdbcType.DATE); 73 register(java.sql.Date.class, JdbcType.DATE); 74 register(java.sql.Time.class, JdbcType.TIME); 75 register(java.sql.Timestamp.class, JdbcType.TIMESTAMP); 76 77 register(Character.class, JdbcType.CHAR); 78 register(char.class, JdbcType.CHAR); 79 } 80 81 public JdbcTypeDefaultParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) { 82 super(mappedStatement, parameterObject, boundSql); 83 84 this.mappedStatement = mappedStatement; 85 this.configuration = mappedStatement.getConfiguration(); 86 this.typeHandlerRegistry = mappedStatement.getConfiguration().getTypeHandlerRegistry(); 87 this.parameterObject = parameterObject; 88 this.boundSql = boundSql; 89 } 90 91 /** 92 * 重寫 方法 設定預設值 93 * @param ps 94 */ 95 @Override 96 public void setParameters(PreparedStatement ps) { 97 ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId()); 98 List<ParameterMapping> parameterMappings = boundSql.getParameterMappings(); 99 if (parameterMappings != null) { 100 for (int i = 0; i < parameterMappings.size(); i++) { 101 ParameterMapping parameterMapping = parameterMappings.get(i); 102 if (parameterMapping.getMode() != ParameterMode.OUT) { 103 Object value; 104 String propertyName = parameterMapping.getProperty(); 105 if (boundSql.hasAdditionalParameter(propertyName)) { // issue #448 ask first for additional params 106 value = boundSql.getAdditionalParameter(propertyName); 107 } else if (parameterObject == null) { 108 value = null; 109 } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) { 110 value = parameterObject; 111 } else { 112 MetaObject metaObject = configuration.newMetaObject(parameterObject); 113 value = metaObject.getValue(propertyName); 114 } 115 TypeHandler typeHandler = parameterMapping.getTypeHandler(); 116 JdbcType jdbcType = parameterMapping.getJdbcType(); 117 if (value == null && jdbcType == null) { 118 if (parameterMapping.getJavaType() != null && typeMap.containsKey(parameterMapping.getJavaType())) { 119 jdbcType = typeMap.get(parameterMapping.getJavaType()); 120 } else { 121 jdbcType = configuration.getJdbcTypeForNull(); 122 } 123 } 124 try { 125 typeHandler.setParameter(ps, i + 1, value, jdbcType); 126 } catch (TypeException e) { 127 throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e); 128 } catch (SQLException e) { 129 throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e); 130 } 131 } 132 } 133 } 134 } 135 136 public static void register(String type, String jdbcType) { 137 try { 138 typeMap.put(Class.forName(type), JdbcType.valueOf(jdbcType)); 139 } catch (ClassNotFoundException e) { 140 throw new RuntimeException("配置 typeMaps 時出錯!", e); 141 } 142 } 143 144 public static void register(Class<?> type, JdbcType jdbcType) { 145 typeMap.put(type, jdbcType); 146 } 147 }