mybatis-json-typehandler

A Mybatis TypeHandler for json object and json array

License

License

Categories

Categories

Kotlin Languages JSON Data MyBatis ORM
GroupId

GroupId

io.wkz.kotlin
ArtifactId

ArtifactId

mybatis-json-typehandler
Last Version

Last Version

1.0.2
Release Date

Release Date

Type

Type

jar
Description

Description

mybatis-json-typehandler
A Mybatis TypeHandler for json object and json array
Project URL

Project URL

https://github.com/wangkezun/mybatis-json-typehandler
Source Code Management

Source Code Management

https://github.com/wangkezun/mybatis-json-typehandler

Download mybatis-json-typehandler

How to add to project

<!-- https://jarcasting.com/artifacts/io.wkz.kotlin/mybatis-json-typehandler/ -->
<dependency>
    <groupId>io.wkz.kotlin</groupId>
    <artifactId>mybatis-json-typehandler</artifactId>
    <version>1.0.2</version>
</dependency>
// https://jarcasting.com/artifacts/io.wkz.kotlin/mybatis-json-typehandler/
implementation 'io.wkz.kotlin:mybatis-json-typehandler:1.0.2'
// https://jarcasting.com/artifacts/io.wkz.kotlin/mybatis-json-typehandler/
implementation ("io.wkz.kotlin:mybatis-json-typehandler:1.0.2")
'io.wkz.kotlin:mybatis-json-typehandler:jar:1.0.2'
<dependency org="io.wkz.kotlin" name="mybatis-json-typehandler" rev="1.0.2">
  <artifact name="mybatis-json-typehandler" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.wkz.kotlin', module='mybatis-json-typehandler', version='1.0.2')
)
libraryDependencies += "io.wkz.kotlin" % "mybatis-json-typehandler" % "1.0.2"
[io.wkz.kotlin/mybatis-json-typehandler "1.0.2"]

Dependencies

compile (5)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-stdlib-jdk8 jar 1.3.50
org.jetbrains.kotlin : kotlin-reflect jar 1.3.50
org.mybatis : mybatis jar 3.4.6
com.fasterxml.jackson.core : jackson-databind jar 2.9.7
com.fasterxml.jackson.module : jackson-module-kotlin jar 2.9.7

Project Modules

There are no modules declared in this project.

Mybatis-Json-TypeHandler Build Status Codecov GitHub licenseDepShield Badge


目的

开发过程中经常会遇到一对多的关系,大部分情况下这种关系都存在一张表中,通过id等关系进行关联。 但是在某些特殊情况下,单独为这种情况生成一个表并不是非常必须,而且也不会对这种关系进行查询。 此时可以考虑将数据存在一对多的一这个表中,把多的数据存成JSON结构。 但是MyBatis并没有对这种情况提供原生支持。此项目提供了一种自动序列化与反序列化的功能。 调用时仅需要在mapper中配置好即可使用。

依赖

项目依赖于MyBatis、Jackson。基于Kotlin语言。java8+

用法

  1. 引入依赖

    • maven
    <dependency>
      <groupId>io.wkz.kotlin</groupId>
      <artifactId>mybatis-json-typehandler</artifactId>
      <version>1.0.1</version>
    </dependency>
    • gradle
       compile 'io.wkz.kotlin:mybatis-json-typehandler:1.0.1'
  2. 实体类

    与正常的实体类没有任何区别

        public class Data {
            private int id;
            //数据库中序列化为JSON Object
            private JsonObject targetObject;
            // 数据库中序列化为JSON Array
            private List<JsonObject> targetList;
           //数据库中序列化为JSON Array
            private JsonObject[] tartetArray;
        }
        public class JsonObject {
            private String xxx;
            private int yyy;
        }
        data class Data(val id: Int = 0, val targetObject: JsonObject? = null, val targetList:List<JsonObject>? =null, val targetArray:Array<JsonObject>? = null)
        data class JsonObject(val xxx:String, val yyy:Int)
  3. mapper

    重点修改在#{}以及resultMap的result中。 明确定义javaType以及typeHandler即可,如下所示

     <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE
       mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
       "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
        <mapper namespace="io.wkz.kotlin.mybatis.dao.ListJsonDao">
           <resultMap id="Data" type="Data">
               <result column="targetObject" property="targetObject" javaType="JsonObject"
                       jdbcType="VARCHAR" typeHandler="io.wkz.kotlin.mybatis.JsonObjectTypeHandler"/>
               <result column="targetList" property="targetList" javaType="JsonObject"
                       jdbcType="VARCHAR" typeHandler="io.wkz.kotlin.mybatis.JsonListTypeHandler"/>
               <result column="targetArray" property="targetArray" javaType="JsonObject"
                       jdbcType="VARCHAR" typeHandler="io.wkz.kotlin.mybatis.JsonArrayTypeHandler"/>
           </resultMap>
           <insert id="add" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
               insert into tbl_name (targetObject,targetList) value
                   (#{targetObject,javaType=JsonObject,jdbcType=VARCHAR,typeHandler=io.wkz.kotlin.mybatis.JsonObjectTypeHandler},
                   #{targetList,javaType=JsonObject,jdbcType=VARCHAR,typeHandler=io.wkz.kotlin.mybatis.JsonListTypeHandler},
                   #{targetArray,javaType=JsonObject,jdbcType=VARCHAR,typeHandler=io.wkz.kotlin.mybatis.JsonArrayTypeHandler},
                )
           </insert>
           <select id="get" resultMap="Data">
               select *
               from tbl_name
               where id = #{id}
           </select>
        </mapper>

Versions

Version
1.0.2
1.0.1
1.0