java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > mybatis typeHandler定义使用

mybatis中几种typeHandler的定义使用详解

作者:爪哇小生

本文主要介绍了mybatis中几种typeHandler的定义使用,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

1.存储到数据库, 将LONG数组转换成字符串;从数据库获取数据, 将字符串转为LONG数组

package com.winturn.utils.handler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import com.winturn.utils.CommonJsonUtil;

/**
 * <p>Class: ArrayLongTypeHandler.java</p>
 * <p>Description: 存储到数据库, 将LONG数组转换成字符串;
 *                 从数据库获取数据, 将字符串转为LONG数组.
 </p>*/
public class ArrayLongTypeHandler extends BaseTypeHandler<Object> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i,
            Object parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, CommonJsonUtil.stringify(parameter));
    }

    @Override
    public Object getNullableResult(ResultSet rs, String columnName)
            throws SQLException {
        return CommonJsonUtil.parse3(rs.getString(columnName), Object.class);
    }

    @Override
    public Object getNullableResult(ResultSet rs, int columnIndex)
            throws SQLException {
        return CommonJsonUtil.parse3(rs.getString(columnIndex), Object.class);
    }

    @Override
    public Object getNullableResult(CallableStatement cs, int columnIndex)
            throws SQLException {
        return CommonJsonUtil.parse3(cs.getString(columnIndex), Object.class);
    }
}

2.存储到数据库, 将基本数据数组转换成字符串;从数据库获取数据, 将字符串根据','拆分,转为数组.

package com.winturn.utils.handler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import com.winturn.utils.CommonJsonUtil;

/**
 * <p>Class: ArrayStringTypeHandler.java</p>
 * <p>Description: 存储到数据库, 将基本数据数组转换成字符串;
 *                 从数据库获取数据, 将字符串根据','拆分,转为数组.</p>
 *
 * 
 */
public class ArrayStringTypeHandler extends BaseTypeHandler<Object> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Object parameter,
            JdbcType jdbcType) throws SQLException {
        ps.setString(i, CommonJsonUtil.stringify(parameter));
    }

    @Override
    public Object getNullableResult(ResultSet rs, String columnName)
            throws SQLException {
        return CommonJsonUtil.parse2(rs.getString(columnName), Object.class);
    }

    @Override
    public Object getNullableResult(ResultSet rs, int columnIndex)
            throws SQLException {
        return CommonJsonUtil.parse2(rs.getString(columnIndex), Object.class);
    }

    @Override
    public Object getNullableResult(CallableStatement cs, int columnIndex)
            throws SQLException {
        return CommonJsonUtil.parse2(cs.getString(columnIndex), Object.class);
    }
}

3.jsonarray 格式的字符串转换为相应的数组

package com.winturn.utils.handler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import com.winturn.utils.CommonJsonUtil;


/**
 * <p>Class: ArrayIntegerTypeHandler.java</p>
 * <p>Description: jsonarray 格式的字符串转换为相应的数组 </p>
 * 
 */
public class JsonArrayTypeHandler extends BaseTypeHandler<Object> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i,
            Object parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, CommonJsonUtil.stringify(parameter));
    }

    @Override
    public Object getNullableResult(ResultSet rs, String columnName)
            throws SQLException {
        return CommonJsonUtil.parseJsonToArray(rs.getString(columnName), Object.class);
    }

    @Override
    public Object getNullableResult(ResultSet rs, int columnIndex)
            throws SQLException {
        return CommonJsonUtil.parseJsonToArray(rs.getString(columnIndex), Object.class);
    }

    @Override
    public Object getNullableResult(CallableStatement cs, int columnIndex)
            throws SQLException {
        return CommonJsonUtil.parseJsonToArray(cs.getString(columnIndex), Object.class);
    }
}


4.将Float类型的数组装换成字符创进行存储

package com.winturn.utils.handler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import com.winturn.utils.CommonJsonUtil;

/**
 * <p>Filename:JsonFloatTypeHandler.java</p>
 * <p>Description: 将float类型数组装换成字符串

</p>

 * 
 */
public class JsonFloatTypeHandler extends BaseTypeHandler<Object> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i,
            Object parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, CommonJsonUtil.stringifyObject(parameter));
    }

    @Override
    public Object getNullableResult(ResultSet rs, String columnName)
            throws SQLException {
        return CommonJsonUtil.parseJsonToFloat(rs.getString(columnName), Object.class);
    }

    @Override
    public Object getNullableResult(ResultSet rs, int columnIndex)
            throws SQLException {
        return CommonJsonUtil.parseJsonToFloat(rs.getString(columnIndex), Object.class);
    }

    @Override
    public Object getNullableResult(CallableStatement cs, int columnIndex)
            throws SQLException {
        return CommonJsonUtil.parseJsonToFloat(cs.getString(columnIndex), Object.class);
    }
}

5.将map装换成字符串存储到数据库,取出时将字符串装换成map

package com.winturn.utils.handler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Map;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.codehaus.jackson.map.ObjectMapper;

import com.winturn.exceptions.RolerServiceException;
import com.winturn.utils.JsonMapUtil;
/**
 * 
* @ClassName: JsonMapTypeHandler 
* @Description: 将map装换成数组存储数据库,取出时将字符串装换成map
* @author sgl
* @date 2015年12月21日 下午6:22:50
 */
public class JsonMapTypeHandler extends BaseTypeHandler<Map<String, Object>> {

    ObjectMapper mapper = new ObjectMapper();

    @Override
    public Map<String, Object> getNullableResult(ResultSet rs, String columnName) {
        try {
            String value = rs.getString(columnName);
            return mapper.readValue(value, Map.class);
        } catch (Exception e) {

        }
        return null;
    }

    @Override
    public Map<String, Object> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        try {
            String value = rs.getString(columnIndex);
            return mapper.readValue(value, Map.class);
        } catch (Exception e) {

        }
        return null;
    }

    @Override
    public Map<String, Object> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        try {
            String value = cs.getString(columnIndex);
            return mapper.readValue(value, Map.class);
        } catch (Exception e) {

        }
        return null;
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Map<String, Object> parameter, JdbcType jdbcType)
            throws SQLException {
        if (parameter == null) {
            ps.setNull(i, Types.VARCHAR);
        } else {
            try {
                ps.setString(i, JsonMapUtil.getJsonStrByMap(parameter));
            } catch (RolerServiceException e) {
                e.printStackTrace();
            }
        }

    }
}

到此这篇关于mybatis中几种typeHandler的定义使用的文章就介绍到这了,更多相关mybatis typeHandler定义使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文