赞
踩
- import com.alibaba.fastjson.JSONObject;
- import org.apache.ibatis.type.BaseTypeHandler;
- import org.apache.ibatis.type.JdbcType;
- import org.apache.ibatis.type.MappedJdbcTypes;
- import org.apache.ibatis.type.MappedTypes;
- import java.sql.CallableStatement;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
- @MappedTypes(JSONObject.class)
- @MappedJdbcTypes(JdbcType.VARCHAR)
- public class MySqlJsonHandler extends BaseTypeHandler<JSONObject>{
- /**
- * 设置非空参数
- * @param ps
- * @param i
- * @param parameter
- * @param jdbcType
- * @throws SQLException
- */
- @Override
- public void setNonNullParameter(PreparedStatement ps, int i, JSONObject parameter, JdbcType jdbcType) throws SQLException {
- ps.setString(i,String.valueOf(parameter.toJSONString()));
- }
- /**
- * 根据列名,获取可以为空的结果
- * @param rs
- * @param columnName
- * @return
- * @throws SQLException
- */
- @Override
- public JSONObject getNullableResult(ResultSet rs, String columnName) throws SQLException {
- String sqlJson = rs.getString(columnName);
- if (null != sqlJson) {
- return JSONObject.parseObject(sqlJson);
- }
- return null;
- }
- /**
- * 根据列索引,获取可以为内控的接口
- * @param rs
- * @param columnIndex
- * @return
- * @throws SQLException
- */
- @Override
- public JSONObject getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
- String sqlJson = rs.getString(columnIndex);
- if (null != sqlJson) {
- return JSONObject.parseObject(sqlJson);
- }
- return null;
- }
- /**
- *
- * @param cs
- * @param columnIndex
- * @return
- * @throws SQLException
- */
- @Override
- public JSONObject getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
- String sqlJson = cs.getNString(columnIndex);
- if (null != sqlJson) {
- return JSONObject.parseObject(sqlJson);
- }
- return null;
- }
- }
- mybatis:
- mapper-locations: classpath:mapper/*.xml
- type-aliases-package: com.xxx.xxx.entity
- type-handlers-package: com.xxx.xxx.handler #增加此项配置
@TableField(typeHandler=MySqlJsonHandler.class)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。