zoukankan      html  css  js  c++  java
  • Springboot整合Mybatis使用TypeHandler转换特殊数据类型

    回顾一下,发现自己好久没有写文章了,今天我们来看一下Springboot整合Mybatis使用TypeHandler转换特殊数据类型怎么做

    在项目实践中,我发现,在使用postgre数据库时,有两个特殊的数据类型“UUID”和“jsonb”类型,这里我使用实体映射jar是:

    <dependency>
        <groupId>tk.mybatis</groupId>
        <artifactId>mapper-spring-boot-starter</artifactId>
        <version>2.1.5</version>
    </dependency>

    使用这个框架方便之处在于,在我们使用代码生成器的时候,不用写简单的crud,但是每每碰到“UUID”和“jsonb”类型就搞不定了。

    首先,我们来看一下 “UUID”类型,这种类型的相对而言好处理一点,就是把接收和查询的对应uuid的类型的数据映射成uuid类型,就可以成功的做查询,修改,但是“jsonb”类型就没有那么简单了。

    这里就需要用到TypeHandler,通常我们会写一个TypeHandler类。

    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 org.apache.ibatis.type.MappedTypes;
    import org.postgresql.util.PGobject;
    
    import com.slife.pdfg.common.util.JSONUtils;
    
    @MappedTypes({Object.class})
    public class JsonbTypeHandler extends BaseTypeHandler<Object> {
        private static final PGobject jsonObject = new PGobject();
    
        @Override
        public void setNonNullParameter(PreparedStatement preparedStatement, int i, Object o, JdbcType jdbcType) throws SQLException {
            jsonObject.setType("jsonb");
            jsonObject.setValue(JSONUtils.beanToJson(o));
            preparedStatement.setObject(i, jsonObject);
        }
    
        @Override
        public Object getNullableResult(ResultSet resultSet, String s) throws SQLException {
            if (null != resultSet.getString(s)) {
    
                return JSONUtils.jsonToBean(resultSet.getString(s),Object.class);
            }
            return null;
        }
    
        @Override
        public Object getNullableResult(ResultSet resultSet, int i) throws SQLException {
            if (null != resultSet.getString(i)) {
    
                return  JSONUtils.jsonToBean(resultSet.getString(i),Object.class);
            }
            return null;
        }
    
        @Override
        public Object getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
            if (null != callableStatement.getString(i)) {
                return JSONUtils.jsonToBean(callableStatement.getString(i),Object.class);
            }
            return null;
        }
    }
    

      

    这里使用它有两种方式

    第一种:直接在配置里边加配置

    mybatis: 
      mapper-locations: classpath:/mapper/**/*.xml
      type-aliases-package: com.ess.**.pojo    # model的包
      type-handlers-package: com.common.config  添加该配置时,查询对象胡数据会被转换为jsonb
    

    在配置的yml文件里边加上述配置 :type-handlers-package,包路径写上述JsonbTypeHandler的包路径地址就好,但是这种形式有一个弊端,就是会把我们所有的list类型的查询条件给转换成,jsonb对象,造成使用不方便。所以我选择了第二种方式。

    第二种方式:如下

    我们在映射实体类的时候,如果是jsonb类型在xml里边

    <resultMap id="BaseResultMap" type="com.pojo.Demo">
       <id column="guid" property="guid" jdbcType="VARCHAR" />
       <result column="dtag" property="dtag" jdbcType="OTHER" typeHandler="com.common.config.JsonbTypeHandler" /> 
    </resultMap>
    

      

    上述是实体映射的部分,接下来看一下插入如何做

    #{dtag,jdbcType=VARCHAR,typeHandler=com.common.config.JsonbTypeHandler}

    好了,写道这里,我们就解决了jsonb类型的插入和修改。

  • 相关阅读:
    centos6.8安装单机spark2.2.3
    centos6.8安装单机hadoop2.7.2
    Centos6.8系统安装mysql5.7.17
    VMware虚拟机 取消 简易安装(转)
    qt开发ROS gui界面环境配置过程总结
    QT开发ROS遇到问题:execute_process(/usr/bin/python"/home/fu/catkin_ws/build/catkin_genetated/generate_cached_setup.py)..........
    卸载ROS命令
    ROS开发过程中遇到:Could not find a package configuration file provided by "qt_build" with any of the following names: qt_buildConfig.cmake qt_build-config.cmake........
    创建ROS 工作空间时出现:程序“catkin_init_workspace”尚未安装,程序“catkin_make”尚未安装。
    qt开发ROS遇到这个问题 find_package(catkin) failed. catkin was neither found in the workspace nor in the CMAKE_PREFIX_PATH...
  • 原文地址:https://www.cnblogs.com/haoliyou/p/13743698.html
Copyright © 2011-2022 走看看