建議你們用新版的mybatis jar包或者mybatis.tk這個。java
實現接口:sql
package com.cnm.filter; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; 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 com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; @MappedTypes({JSONObject.class}) @MappedJdbcTypes({JdbcType.VARCHAR}) public class FastJsonTypeHandler implements TypeHandler<JSONObject>{ @Override public JSONObject getResult(ResultSet rs, String columnName) throws SQLException { String string = rs.getString(columnName); JSONObject json = JSONObject.parseObject(string); return json; } @Override public JSONObject getResult(ResultSet rs, int columnIndex) throws SQLException { String string = rs.getString(columnIndex); JSONObject json = JSONObject.parseObject(string); return json; } //@param cs 當前的CallableStatement執行後的CallableStatement ![輸入圖片說明](https://static.oschina.net/uploads/img/201709/05153912_SK1d.png "在這裏輸入圖片標題") public JSONObject getResult(CallableStatement cs, int columnIndex) throws SQLException { String string = cs.getString(columnIndex); JSONObject json = JSONObject.parseObject(string); return json; } /** * 用於定義在Mybatis設置參數時該如何把Java類型的參數轉換爲對應的數據庫類型 * @param ps 當前的PreparedStatement對象 * @param i 當前參數的位置 * @param parameter 當前參數的Java對象 * @param jdbcType 當前參數的數據庫類型 * @throws SQLException */ public void setParameter(PreparedStatement ps, int i, JSONObject parameter, JdbcType jdbcType) throws SQLException { if(parameter == null){ ps.setString(i, null); return; } String json = JSON.toJSONString(parameter); ps.setString(i, json); } }
Mapper掃描不到的問題就看最前面寫的吧。數據庫
直接使用方法 在Mapper文件中配置:apache
<result column="json" property="json" jdbcType="VARCHAR" typeHandler="com.cnm.filter.FastJsonTypeHandler"/>
第二種是在使用的變量後面加json
#{json,typeHandler=com.cnm.filter.FastJsonTypeHandler}
常規配置方法:(舊jar 有bug)mybatis
<typeHandlers> <typeHandler handler="com.cnm.filter.FastJsonTypeHandler" javaType="com.alibaba.fastjson.JSONObject" jdbcType="VARCHAR"/> </typeHandlers>
配置文件修改後,就能夠直接使用,此方法適合新版mybatis,舊的有問題,掃描不上。須要使用反射來進行從新註冊。app
Mybatis 經常使用自帶集成TypeHeadleride
register(Boolean.class, new BooleanTypeHandler()); register(boolean.class, new BooleanTypeHandler()); register(Byte.class, new ByteTypeHandler()); register(byte.class, new ByteTypeHandler()); register(Short.class, new ShortTypeHandler()); register(short.class, new ShortTypeHandler()); register(Integer.class, new IntegerTypeHandler()); register(int.class, new IntegerTypeHandler()); register(Long.class, new LongTypeHandler()); register(long.class, new LongTypeHandler()); register(Float.class, new FloatTypeHandler()); register(float.class, new FloatTypeHandler()); register(Double.class, new DoubleTypeHandler()); register(double.class, new DoubleTypeHandler()); register(String.class, new StringTypeHandler()); register(String.class, JdbcType.CHAR, new StringTypeHandler()); register(String.class, JdbcType.CLOB, new ClobTypeHandler()); register(String.class, JdbcType.VARCHAR, new StringTypeHandler()); register(String.class, JdbcType.LONGVARCHAR, new ClobTypeHandler()); register(String.class, JdbcType.NVARCHAR, new NStringTypeHandler()); register(String.class, JdbcType.NCHAR, new NStringTypeHandler()); register(String.class, JdbcType.NCLOB, new NClobTypeHandler()); register(Object.class, JdbcType.ARRAY, new ArrayTypeHandler()); register(BigInteger.class, new BigIntegerTypeHandler()); register(BigDecimal.class, new BigDecimalTypeHandler()); register(Byte[].class, new ByteObjectArrayTypeHandler()); register(Byte[].class, JdbcType.BLOB, new BlobByteObjectArrayTypeHandler()); register(Byte[].class, JdbcType.LONGVARBINARY, new BlobByteObjectArrayTypeHandler()); register(byte[].class, new ByteArrayTypeHandler()); register(byte[].class, JdbcType.BLOB, new BlobTypeHandler()); register(byte[].class, JdbcType.LONGVARBINARY, new BlobTypeHandler()); register(Object.class, UNKNOWN_TYPE_HANDLER); register(Object.class, JdbcType.OTHER, UNKNOWN_TYPE_HANDLER); register(Date.class, new DateTypeHandler()); register(Date.class, JdbcType.DATE, new DateOnlyTypeHandler()); register(Date.class, JdbcType.TIME, new TimeOnlyTypeHandler()); register(java.sql.Date.class, new SqlDateTypeHandler()); register(java.sql.Time.class, new SqlTimeTypeHandler()); register(java.sql.Timestamp.class, new SqlTimestampTypeHandler()); register(Character.class, new CharacterTypeHandler()); register(char.class, new CharacterTypeHandler());