Mybatis中typeHandler的使用

来源:互联网 发布:关于北京知实践园作文 编辑:程序博客网 时间:2024/05/17 05:08

Mybatis中typeHandler的使用


写mybatis xml文件时可先将要返回的model在头部用resultMap封装,例如:

<span style="font-size:14px;"><resultMap type="cn.com.test" id="resultMap"><result property="realName" column="real_name"/><result property="portrait" column="portrait" typeHandler="cn.com.util.ImageUrlTypeHandler"/><result property="idNumber" column="id_number"/><association property="account" column="id" javaType="cn.ccic.denglu.model.Account"><id property="id" column="id"/><result property="loginId" column="login_id"/><result property="nickName" column="nick_name"/><result property="status" column="status"/><result property="token" column="token"/><result property="lastShakeTime" column="last_shake_time"/><result property="deviceToken" column="device_token"/></association></resultMap>其中typeHandler对应工具类:</span><pre name="code" class="html">package cn.ccic.core.util;/** * 查询出数据后通过根目录重拼接 */import org.apache.ibatis.type.JdbcType;import org.apache.ibatis.type.TypeHandler;import cn.ccic.core.constant.GlobalConstant;import java.sql.*;import java.util.Calendar;import java.util.Date; public class ImageUrlTypeHandler implements TypeHandler<String> {@Overridepublic void setParameter(PreparedStatement preparedStatement, int i,String url, JdbcType jdbcType) throws SQLException {}@Overridepublic String getResult(ResultSet resultSet, String columnName)throws SQLException {String url = resultSet.getString(columnName);return url.contains("http://")?url:(GlobalConstant.UPLOAD_SERVER + url);}@Overridepublic String getResult(ResultSet resultSet, int columnIndex)throws SQLException {String url = resultSet.getString(columnIndex);return url.contains("http://")?url:(GlobalConstant.UPLOAD_SERVER + url);}@Overridepublic String getResult(CallableStatement callableStatement, int i)throws SQLException {String url = callableStatement.getString(i);return url.contains("http://")?url:(GlobalConstant.UPLOAD_SERVER + url);}}


此时查询后返回的model的portrait属性便是拼接后的数据

0 0
原创粉丝点击