The typeHandler interface implements JSONObject in FastJson

The pits that typeHandler cannot scan: You can see what this brother wrote, http://blog.csdn.net/goldenfish1919/article/details/52806659 The solution uses reflection. There is another way to solve it. mybatis.tk seems to be fine. My version may be old. After setting, there is no response. The mapper configuration file has been scanned, but the typeHandler has no response. Well, let's see how to customize the typeHandler interface of Jsonobject

It is recommended that you use the new version of mybatis jar package or mybatis.tk.

This is to facilitate the use of EL to directly access the value, so the type to be saved and retrieved must be of the JsonObject type. But mybatis does not have this format by default, not Zichi.

Implement the interface:

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);
		
	}

}

For the problems that Mapper can't scan, please read the first one.

Directly use the method to configure in the Mapper file:

<result column="json" property="json" jdbcType="VARCHAR" typeHandler="com.cnm.filter.FastJsonTypeHandler"/>

The second is to add the variable after the used variable

#{json,typeHandler=com.cnm.filter.FastJsonTypeHandler}

General configuration method: (old jar has bugs)

<typeHandlers>
		<typeHandler handler="com.cnm.filter.FastJsonTypeHandler" javaType="com.alibaba.fastjson.JSONObject" jdbcType="VARCHAR"/>
	</typeHandlers>

After the configuration file is modified, it can be used directly. This method is suitable for the new version of mybatis. The old one has problems and cannot be scanned. Re-registration is required using reflection.

Mybatis often comes with its own integrated TypeHeader

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());  

BUG problem location

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325442242&siteId=291194637