1.存储到数据库, 将long数组转换成字符串;从数据库获取数据, 将字符串转为long数组
package com.w增肥秘方inturn.utils.handler;import java.sql.callablestatement;import java.sql.preparedstatement;import java.sql.resultt;import java.sql.sqlexception;import org.apache.ibatis.type.batypehandler;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 batypehandler<object> { @override public void tnonnullparameter(preparedstatement ps, int i, object parameter, jdbctype jdbctype) throws sqlexception { ps.tstring(i, commonjsonutil.stringify(parameter)); } @override public object getnullableresult(resultt rs, string columnname) throws sqlexception { return commonjsonutil.par3(rs.getstring(columnname), obje服装学校ct.class); } @override public object getnullableresult(resultt rs, int columnindex) throws sqlexception { return commonjsonutil.par3(rs.getstring(columnindex), object.class); } @override public object getnullableresult(callablestatement cs, int columnindex) throws sqlexception { return commonjsonutil.par3(cs.getstring(columnindex), object.class); }}
2.存储到数据库, 将基本数据数组转换成字符串;从数据库获取数据, 将字符串根据’,’拆分,转为数组.
package com.winturn.utils.handler;import java.sql.callablestatement;import java.sql.preparedstatement;import java.sql.resultt;import java.sql.sqlexception;import org.apache.ibatis.type.batypehandler;import org.apache.ibatis.type.jdbctype;import com.winturn.utils.commonjsonutil;/** * <p>class: arraystringtypehandler.java</p> * <p>description: 存储到数据库, 将基本数据数组转换成字符串; * 从数据库获取数据, 将字符串根据','拆分,转为数组.</p> * * */public class arraystringtypehandler extends batypehandler<object> { @override public void tnonnullparameter(preparedstatement ps, int i, object parameter, jdbctype jdbctype) throws sqlexception { ps.tstring(i, commonjsonutil.stringify(parameter)); } @override public object getnullableresult(resultt rs, string columnname) throws sqlexception { return commonjsonutil.par2(rs.getstring(columnname), object.class); } @override public object getnullableresult(resultt rs, int columnindex) throws sqlexception { return commonjsonutil.par2(rs.getstring(columnindex), object.class); } @override public object getnullableresult(callablestatement cs, int columnindex) throws sqlexceptio柴静调查n { return commonjsonutil.par2(cs.getstring(columnindex), object.class); }}
3.jsonarray 格式的字符串转换为相应的数组
package com.winturn.utils.handler;import java.sql.callablestatement;import java.sql.preparedstatement;import java.sql.resultt;import java.sql.sqlexception;import org.apache.ibatis.type.batypehandler;import org.apache.ibatis.type.jdbctype;import com.winturn.utils.commonjsonutil;/** * <p>class: arrayintegertypehandler.java</p> * <p>description: jsonarray 格式的字符串转换为相应的数组 </p> * */public class jsonarraytypehandler extends batypehandler<object> { @override public void tnonnullparameter(preparedstatement ps, int i, object parameter, jdbctype jdbctype) throws sqlexception { ps.tstring(i, commonjsonutil.stringify(parameter)); } @override public object getnullableresult(resultt rs, string columnname) throws sqlexception { return commonjsonutil.parjsontoarray(rs.getstring(columnname), object.class); } @override public object getnullableresult(resultt rs, int columnindex) throws sqlexception { return commonjsonutil.parjsontoarray(rs.getstring(columnindex), object.class); } @override public object getnullableresult(callablestatement cs, int columnindex) throws sqlexception { return commonjsonutil.parjsontoarray(cs.getstring(columnindex), object.class); }}
4.将float类型的数组装换成字符创进行存储
package com.winturn.utils.handler;import java.sql.callablestatement;import java.sql.preparedstatement;import java.sql.resultt;import java.sql.sqlexception;import org.apache.ibatis.type.batypehandler;import org.apache.ibatis.type.jdbctype;import com.winturn.utils.commonjsonutil;/** * <p>filename:jsonfloattypehandler.java</p> * <p>description: 将float类型数组装换成字符串</p> * */public class jsonfloattypehandler extends batypehandler<行书书法作品欣赏;object> { @override public void tnonnullparameter(preparedstatement ps, int i, object parameter, jdbctype jdbctype) throws sqlexception { ps.tstring(i, commonjsonutil.stringifyobject(parameter)); } @override public object getnullableresult(resultt rs, string columnname) throws sqlexception { return commonjsonutil.parjsontofloat(rs.getstring(columnname), object.class); } @override public object getnullableresult(resultt rs, int columnindex) throws sqlexception { return commonjsonutil.parjsontofloat(rs.getstring(columnindex), object.class); } @override public object getnullableresult(callablestatement cs, int columnindex) throws sqlexception { return commonjsonutil.parjsontofloat(cs.getstring(columnindex), object.class); }}
5.将map装换成字符串存储到数据库,取出时将字符串装换成map
package com.winturn.utils.handler;import java.sql.callablestatement;import java.sql.preparedst九妹歌词atement;import java.sql.resultt;import java.sql.sqlexception;import java.sql.types;import java.util.map;import org.apache.ibatis.type.batypehandler;import org.apache.ibatis.type.jdbctype;import org.codehaus.jackson.map.objectmapper;import com.winturn.exceptions.rolerrviceexception;import com.winturn.utils.jsonmaputil;/** * * @classname: jsonmaptypehandler * @description: 将map装换成数组存储数据库,取出时将字符串装换成map* @author sgl* @date 2015年12月21日 下午6:22:50 */public class jsonmaptypehandler extends batypehandler<map<string, object>> { objectmapper mapper = new objectmapper(); @override public map<string, object> getnullableresult(resultt 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(resultt 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 tnonnullparameter(preparedstatement ps, int i, map<string, object> parameter, jdbctype jdbctype) throws sqlexception { if (parameter == null) { ps.tnull(i, types.varchar); } el { try { ps.tstring(i, jsonmaputil.getjsonstrbymap(parameter)); } catch (rolerrviceexception e) { e.printstacktrace(); } } }}
到此这篇关于mybatis中几种typehandler的定义使用的文章就介绍到这了,更多相关mybatis typehandler定义使用内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-04 00:23:26,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/b57e42035ec8e56076b5dee031052b87.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:mybatis中几种typeHandler的定义使用详解.doc
本文 PDF 下载地址:mybatis中几种typeHandler的定义使用详解.pdf
留言与评论(共有 0 条评论) |