欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-03 19:41:33
1.存储到数据库, 将long数组转换成字符串;从数据库获取数据, 将字符串转为long数组package com.winturn.utils.handler;import java.sql.call...

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

相关标签: mybatis typeHandler