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
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0