美文网首页物联网Java后台知识库积累
SpringBoot+MyBaties+PostgreSQL之j

SpringBoot+MyBaties+PostgreSQL之j

作者: eoser | 来源:发表于2018-06-07 17:30 被阅读646次

    MyBaties并不支持JSON类型格式的数据存入PostgreSQL,所以在这里要进一步学习

    SpringBoot+MyBaties+PostgreSQL整合
    创建注册JSON数据类型到MyBaties

    创建一个类,继承MyBites提供的BaseTypeHandler,再通过PostgreSQL提供的PGobject 注册(可能所用名词不专业,甚至有错误,纯属个人理解)

    package com.example.mgdbandpgdb.pgdb.handler;
    
    import org.apache.ibatis.type.BaseTypeHandler;
    import org.apache.ibatis.type.JdbcType;
    import org.apache.ibatis.type.MappedTypes;
    import org.postgresql.util.PGobject;
    
    import java.sql.CallableStatement;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    //设置映射类型
    @MappedTypes(Object.class)
    public class JsonTypeHandler extends BaseTypeHandler<Object> {
        //引入PGSQL提供的工具类PGobject
        private static final PGobject jsonObject = new PGobject();
        //注册类型,绑定值
        @Override
        public void setNonNullParameter(PreparedStatement preparedStatement, int i, Object o, JdbcType jdbcType) throws SQLException {
            jsonObject.setType("json");
            jsonObject.setValue(o.toString());
            preparedStatement.setObject(i, jsonObject);
        }
    
        @Override
        public Object getNullableResult(ResultSet resultSet, String s) throws SQLException {
            return resultSet.getString(s);
        }
    
        @Override
        public Object getNullableResult(ResultSet resultSet, int i) throws SQLException {
            return resultSet.getString(i);
        }
    
        @Override
        public Object getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
            return callableStatement.getString(i);
        }
    }
    
    
    映射文件配置
      <resultMap id="BaseResultMap" type="com.example.mgdbandpgdb.pgdb.entity.Jsontable">
        <id column="id" jdbcType="INTEGER" property="id" />
        <!--重点是这一句,typeHandler是前面写的,javaType="Object",jdbcType="OTHER"-->
        <result column="json" javaType="Object" jdbcType="OTHER" typeHandler="com.example.mgdbandpgdb.pgdb.handler.JsonTypeHandler" property="json" />
        <result column="msg" jdbcType="VARCHAR" property="msg" />
      </resultMap>
    
    向PostgreSQL中写入数据
    <insert id="insert" parameterType="com.example.mgdbandpgdb.pgdb.entity.Jsontable">
        INSERT INTO jsontable (
        id,
        msg,
        json)
        VALUES (
        #{id, jdbcType=INTEGER},
        #{msg, jdbcType=VARCHAR},
        <!--重点是这一句-->
        #{json, javaType=Object,jdbcType=OTHER,typeHandler=com.example.mgdbandpgdb.pgdb.handler.JsonTypeHandler})
      </insert>
    
    从PostgreSQL查询普通数据

    当前我涉及到的和MySQL的查询没差别,所以不描述了

    从PostgreSQL的JSON列数据中查询数据

    JSON数据示例

    {"name":"AAA","sex":"women","age":22}
    

    查询的映射

    <select id="selectInJsonByAge" resultType="String">
        SELECT
          json ->> 'name'
        FROM jsontable
        WHERE json ->> 'age' = #{age}
      </select>
    

    提示:带JSON数据时普通操作可用自动生成代码工具生成,再略作修改即可,在JSON数据中查找数据要我们自己写映射文件并且在Mapper类中加方法。

    PostgreSQL在JSON数据中查数据的SQL语句写法:https://blog.csdn.net/huangwenyi1010/article/details/51224886

    相关文章

      网友评论

        本文标题:SpringBoot+MyBaties+PostgreSQL之j

        本文链接:https://www.haomeiwen.com/subject/uhzgsftx.html