Skip to content

基于 Mybatis 的拓展插件功能

xml
<resultMap id="BaseResultMap" type="Do对象路径">
	<id column="id" jdbcType="BIGINT" property="id" />
	<result column="ext_obj" jdbcType="VARCHAT" property="extObj" 
	// javaType = 嵌套对象的包路径, TypeHandler 映射处理方法
	 javaType="com.waitingresult.ExtObj" typeHandler="com.waitingresult.mybatis.ExtObjHandler" />
</resultMap>
Java
public class ExtObjHandler  implements TypeHandler<ExtObj> {
	
	@Override  
	public void setParameter(PreparedStatement ps, int i, ExtObj parameter, JdbcType jdbcType) throws SQLException {  
	    ps.setString(i, JSON.toJSONString(parameter));  
	}  
	  
	@Override  
	public ExtObj getResult(ResultSet rs, String columnName) throws SQLException {  
	    String configType = rs.getString("ext_obj");  
	   // Json反序列化
	    return versionConfig;  
	}  
	  
	@Override  
	public ExtObj getResult(ResultSet rs, int columnIndex) throws SQLException {  
	    String configType = rs.getString("ext_obj");  
	     // Json反序列化
	    return versionConfig;    
	    }  
	  
	@Override  
	public ExtObj getResult(CallableStatement cs, int columnIndex) throws SQLException {  
	    String extObjJsonStr = cs.getString("ext_obj");  
	     // Json反序列化
	    return versionConfig;    
	    }
}

waitingresult.com