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

网友投稿 1825 2022-11-17

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

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

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

pahttp://ckage 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;

/**

*

Class: ArrayLongTypeHandler.java

*

Description: 存储到数据库, 将LONG数组转换成字符串;

* 从数据库获取数据, 将字符串转为LONG数组.

public class ArrayLongTypeHandler extends BaseTypeHandler {

@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;

/**

*

Class: ArrayStringTypeHandler.java

*

Description: 存储到数据库, 将基本数据数组转换成字符串;

* 从数据库获取数据, 将字符串根据','拆分,转为数组.

*

*

*/

public class ArrayStringTypeHandler extends BaseTypeHandler {

@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;

/**

*

Class: ArrayIntegerTypeHandler.java

*

Description: jsonarray 格式的字符串转换为相应的数组

*

*/

public class JsonArrayTypeHandler extends BaseTypeHandler {

@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(CallableStatehttp://ment 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;

/**

*

Filename:JsonFloatTypeHandler.java

*

Description: 将float类型数组装换成字符串

*

*/

public class JsonFloatTypeHandler extends BaseTypeHandler {

@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> {

ObjectMapper mapper = new ObjectMapper();

@Override

public Map 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 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 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 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();

}

}

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:[zz]LXC:Linux 容器工具
下一篇:即将写入MySQL源码的官方bug解决之路
相关文章

 发表评论

暂时没有评论,来抢沙发吧~