返回顶部
首页 > 资讯 > 后端开发 > Python >mybatis中几种typeHandler的定义使用详解
  • 716
分享到

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

2024-04-02 19:04:59 716人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

1.存储到数据库, 将LONG数组转换成字符串;从数据库获取数据, 将字符串转为LONG数组 package com.winturn.utils.handler; import

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;


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;


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;



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;


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;

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定义使用内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: mybatis中几种typeHandler的定义使用详解

本文链接: https://lsjlt.com/news/159033.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作