├── .gitignore
├── LICENSE
├── README.md
├── build.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
├── main
└── java
│ └── com
│ └── github
│ └── jneat
│ └── mybatis
│ ├── JsonNodeValue.java
│ ├── JsonNodeValueTypeHandler.java
│ ├── ReaderWriter.java
│ ├── TreeNodeLazyWrapper.java
│ └── TreeNodeTypeHandler.java
└── test
├── java
└── com
│ └── github
│ └── jneat
│ └── mybatis
│ ├── JsonEntity.java
│ ├── JsonHandlersTestApi.java
│ ├── JsonMapper.java
│ ├── JsonNodeValueTest.java
│ └── PostgresqlTest.java
└── resources
├── com
└── github
│ └── jneat
│ └── mybatis
│ └── JsonMapper.xml
└── postgresql.sql
/.gitignore:
--------------------------------------------------------------------------------
1 | build
2 |
3 | .gradle
4 | .nb-gradle-properties
5 | .nb-gradle
6 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2018 by rumatoest at github.com
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # JSON support for Mybatis 3.x using Jackson 2.6.x
2 |
3 | Provide support for JSON like field types in any Database.
4 | Note, that I developed this handler with PostgreSql in mind,
5 | but it looks like it can be used with any other database even without JSON support.
6 |
7 | You should be able to use any Jackson version compatible with API version >= 2.6.0.
8 |
9 | [](https://jitpack.io/#jneat/mybatis-jackson)
10 | [API javadoc](https://jitpack.io/com/github/jneat/mybatis-jackson/-SNAPSHOT/javadoc/)
11 |
12 | ## How does it work
13 | Because JDBC does not support JSON types, it transfer JSON to/from database as a string.
14 | It serialize JSON to string on save and deserialize from string on read.
15 | This feature means that we are really do not care if our DB can support JSON or not.
16 |
17 | There are 2 handlers available:
18 |
19 | * __TreeNodeTypeHandler__ - work only with ArrayNode and ObjectNode only via TreeNode interface
20 | * __JsonNodeValueTypeHandler__ - value wrapper around JsonNode
21 |
22 | ### Lazy reading
23 | Type handlers return lazy wrapper instead of already parsed Node representation.
24 | It is waiting for you to call any of its methods - only then it will read JSON into structure.
25 | But this approach may lead to **unexpected runtime exceptions** in a case if your database will return
26 | invalid JSON string.
27 |
28 | ### Result is always not null despite what stored into DB
29 | I just want to avoid some complexity by relying on MissingNode and not nullable results.
30 |
31 | Just look here.
32 | ```java
33 | class Dto {
34 | JsonNodeValue value1;
35 | TreeNode value2;
36 | // ... Some getters and setters
37 | }
38 |
39 | // Note that in DB value1 and value2 columns are null or empty strings
40 | Dto row = dtoMapper.get();
41 |
42 | // But actually you have JsonNodeValue wrapper and it is not null
43 | assert row.getValue1().isNotPresent();
44 |
45 | // Eventually you will find that you are dealing with MissingNode
46 | assert row.getValue1().get().isMissingNode();
47 | assert row.getValue2().isMissingNode();
48 |
49 | ```
50 |
51 | ## Add to your project
52 | You can add this artifact to your project using [JitPack](https://jitpack.io/#jneat/mybatis-jackson).
53 | All versions list, instructions for gradle, maven, ivy etc. can be found by link above.
54 |
55 | To get latest commit use -SNAPSHOT instead version number.
56 |
57 | ## Configure
58 | In result map configuration you should use:
59 |
60 | * `javaType="com.fasterxml.jackson.core.TreeNode"`
61 | * `javaType="com.fasterxml.jackson.databind.JsonNode"`
62 |
63 | You should not configure anything if you want to use TreeNode types as arguments in your mapper
64 | functions, but keep in mind that handler only expect objects of type ArrayNode or ObjectNode
65 | for TreeNodeTypeHandler. And JsonNode for JsonNodeValueTypeHandler.
66 |
67 |
68 | ### Mybatis config
69 | ```xml
70 |
71 |
72 |
73 |
74 |
75 | ```
76 |
77 | Or you can use package search
78 |
79 | ```xml
80 |
81 |
82 |
83 |
84 | ```
85 |
86 | ### Mybatis via Spring
87 | ```xml
88 |
89 |
90 |
91 |
92 | ```
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'java'
2 | apply plugin: 'maven-publish'
3 |
4 | group = 'com.github.jneat'
5 | version = '0.5.1'
6 |
7 | sourceCompatibility = 1.8
8 | targetCompatibility = 1.8
9 |
10 | repositories {
11 | mavenCentral()
12 | }
13 |
14 | dependencies {
15 | runtime 'com.fasterxml.jackson.core:jackson-core:2.6.0'
16 | runtime 'com.fasterxml.jackson.core:jackson-databind:2.6.0'
17 | runtime 'org.mybatis:mybatis:3.3.0'
18 |
19 | testCompile 'com.fasterxml.jackson.core:jackson-core:2.6.0'
20 | testCompile 'com.fasterxml.jackson.core:jackson-databind:2.6.0'
21 | testCompile 'org.mybatis:mybatis:3.3.0'
22 |
23 | testCompile 'junit:junit:4.12'
24 | testCompile 'org.assertj:assertj-core:3.2.0'
25 |
26 | testCompile 'org.postgresql:postgresql:9.4-1206-jdbc42'
27 | }
28 |
29 | //Include runtime for compilation
30 | sourceSets.main.compileClasspath += configurations.runtime
31 | javadoc.classpath += configurations.runtime
32 |
33 | buildscript {
34 | repositories {
35 | mavenCentral()
36 | }
37 | dependencies {
38 | classpath 'org.mybatis:mybatis:3.3.0'
39 | }
40 | }
41 |
42 | task sourcesJar(type: Jar, dependsOn: classes) {
43 | classifier = 'sources'
44 | from sourceSets.main.allSource
45 | }
46 |
47 | task javadocJar(type: Jar, dependsOn: javadoc) {
48 | classifier = 'javadoc'
49 | from javadoc.destinationDir
50 | }
51 |
52 | artifacts {
53 | archives sourcesJar
54 | archives javadocJar
55 | }
56 |
57 | publishing {
58 | publications {
59 | mavenJar(MavenPublication) {
60 | from components.java
61 | artifact sourcesJar {
62 | classifier "sources"
63 | }
64 | artifact javadocJar {
65 | classifier "javadoc"
66 | }
67 | }
68 | }
69 | }
70 |
71 | // Tasks
72 | [compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
73 |
74 | gradle.projectsEvaluated {
75 | tasks.withType(JavaCompile) {
76 | options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
77 | }
78 | }
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jneat/mybatis-jackson/4db41f61c6b838ef5c3f7c67dd6598dfd70f7784/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue Feb 13 16:05:22 MSK 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.2.1-bin.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | rootProject.name = 'mybatis-jackson'
2 |
--------------------------------------------------------------------------------
/src/main/java/com/github/jneat/mybatis/JsonNodeValue.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2016 Vladislav Zablotsky
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package com.github.jneat.mybatis;
25 |
26 | import com.fasterxml.jackson.databind.JsonNode;
27 | import com.fasterxml.jackson.databind.node.MissingNode;
28 |
29 | import java.io.IOException;
30 | import java.io.ObjectOutputStream;
31 | import java.io.Serializable;
32 |
33 | /**
34 | * Value container that transfer JSON from/into DB.
35 | * Main feature of this container is lazy initializing while reading values from DB.
36 | * It will build JsonNode object only at first call of {@link JsonNodeValue#get()}
37 | * and sometimes {@link JsonNodeValue#isEmpty()}.
38 | */
39 | public class JsonNodeValue implements Serializable {
40 |
41 | private static final long serialVersionUID = 745861884668365334L;
42 |
43 | /**
44 | * Value container without any content.
45 | * You will not be able to call get() methods on this object.
46 | */
47 | public static JsonNodeValue EMPTY = new JsonNodeValue();
48 |
49 | private String source;
50 |
51 | private boolean dbSource;
52 |
53 | private transient JsonNode value;
54 |
55 | private JsonNodeValue() {
56 | this.source = null;
57 | this.value = null;
58 | }
59 |
60 | private JsonNodeValue(String source) {
61 | if (source != null) {
62 | source = source.trim();
63 | this.source = source.isEmpty() ? null : source;
64 | } else {
65 | this.source = null;
66 | }
67 | this.value = null;
68 | }
69 |
70 | private JsonNodeValue(JsonNode value) {
71 | this.value = value;
72 | this.source = null;
73 | }
74 |
75 | /**
76 | * Build value container from JsonNode object.
77 | * In this case {@link JsonNodeValue#get()} will never throw any exception.
78 | *
79 | * @param node JSON node or null
80 | */
81 | public static JsonNodeValue from(JsonNode node) {
82 | return node == null ? EMPTY : new JsonNodeValue(node);
83 | }
84 |
85 | /**
86 | * Build value container from JSON string.
87 | * NOTE if input is not valid JSON than exception in {@link JsonNodeValue#get()} will be thrown.
88 | *
89 | * @param json JSON string or null
90 | */
91 | public static JsonNodeValue from(String json) {
92 | if (json == null || json.isEmpty()) {
93 | return EMPTY;
94 | }
95 | json = json.trim();
96 | return json.isEmpty() ? EMPTY : new JsonNodeValue(json);
97 | }
98 |
99 | static JsonNodeValue fromDb(String json) {
100 | JsonNodeValue v = from(json);
101 | if (v.isPresent()) {
102 | v.dbSource = true;
103 | }
104 | return v;
105 | }
106 |
107 | /**
108 | * Test input value and return not null - value from input or empty object.
109 | */
110 | public static JsonNodeValue orEmpty(JsonNodeValue node) {
111 | return node == null || node.isNotPresent() ? EMPTY : node;
112 | }
113 |
114 | /**
115 | * Check if nested value is present (not null or empty JSON string).
116 | */
117 | public boolean isPresent() {
118 | return value != null || (source != null && !source.isEmpty());
119 | }
120 |
121 | /**
122 | * Opposite to {@link JsonNodeValue#isPresent()}.
123 | */
124 | public boolean isNotPresent() {
125 | return !isPresent();
126 | }
127 |
128 | /**
129 | * Return true if value is not present or if underlying JSON is empty object, array or null.
130 | * WARNING this method can throw same exceptions as {@link JsonNodeValue#get()} in a case if
131 | * source is invalid JSON string.
132 | */
133 | public boolean isEmpty() {
134 | if (!isPresent()) {
135 | return true;
136 | }
137 |
138 | JsonNode n = get();
139 |
140 | if ((n.isObject() || n.isArray()) && n.size() == 0) {
141 | return true;
142 | }
143 |
144 | if (n.isNull()) {
145 | return true;
146 | }
147 |
148 | return false;
149 | }
150 |
151 | /**
152 | * Opposite to {@link JsonNodeValue#isEmpty()}.
153 | */
154 | public boolean isNotEmpty() {
155 | return !isEmpty();
156 | }
157 |
158 | /**
159 | * Return COPY of JSON node value (will parse node from string at first call).
160 | * WARNING if object constructed with invalid JSON string, exception will be thrown.
161 | *
162 | * @return Copy of valid JsonNode or MissingNode if no data.
163 | * @throws RuntimeException On JSON parsing errors.
164 | */
165 | public JsonNode get() throws RuntimeException {
166 | if (!isPresent()) {
167 | return MissingNode.getInstance();
168 | }
169 |
170 | if (value == null) {
171 | synchronized (this) {
172 | if (value == null) {
173 | try {
174 | value = ReaderWriter.readTree(source);
175 | } catch (Exception ex) {
176 | throw new RuntimeException("Can not parse JSON string. " + ex.getMessage(), ex);
177 | }
178 | }
179 | }
180 | }
181 | return value.deepCopy();
182 | }
183 |
184 | /**
185 | * Same as {@link JsonNodeValue#get()}.
186 | * Created for compatibility with frameworks that works with object properties,
187 | * thus require get* methods.
188 | */
189 | public JsonNode getValue() {
190 | return get();
191 | }
192 |
193 | boolean hasDbSource() {
194 | return this.dbSource && this.source != null;
195 | }
196 |
197 | String getSource() {
198 | return this.source;
199 | }
200 |
201 | private void writeObject(ObjectOutputStream oos) throws IOException {
202 | if (this.source == null && this.value != null) {
203 | this.source = ReaderWriter.write(this.value);
204 | }
205 | oos.defaultWriteObject();
206 | }
207 | }
208 |
--------------------------------------------------------------------------------
/src/main/java/com/github/jneat/mybatis/JsonNodeValueTypeHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2016 Vladislav Zablotsky
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package com.github.jneat.mybatis;
25 |
26 | import org.apache.ibatis.executor.result.ResultMapException;
27 | import org.apache.ibatis.type.BaseTypeHandler;
28 | import org.apache.ibatis.type.JdbcType;
29 | import org.apache.ibatis.type.MappedTypes;
30 |
31 | import java.io.IOException;
32 | import java.sql.CallableStatement;
33 | import java.sql.PreparedStatement;
34 | import java.sql.ResultSet;
35 | import java.sql.SQLException;
36 |
37 | /**
38 | * Map JSON string as value container with JsonNode.
39 | * Should always return not null value.
40 | * Use JSON string representation as intermediate data format.
41 | *
42 | * @see JsonNodeValue
43 | */
44 | @MappedTypes({JsonNodeValue.class})
45 | public class JsonNodeValueTypeHandler extends BaseTypeHandler {
46 |
47 | @Override
48 | public void setNonNullParameter(PreparedStatement ps, int i, JsonNodeValue parameter, JdbcType jdbcType) throws SQLException {
49 | if (parameter.isPresent()) {
50 | String json;
51 | if (parameter.hasDbSource()) {
52 | json = parameter.getSource();
53 | } else {
54 | try {
55 | json = ReaderWriter.write(parameter.get());
56 | } catch (IOException ex) {
57 | throw new RuntimeException(ex.getMessage(), ex);
58 | }
59 | }
60 | ps.setString(i, json);
61 | } else {
62 | ps.setString(i, null);
63 | }
64 | }
65 |
66 | @Override
67 | public JsonNodeValue getNullableResult(ResultSet rs, String columnName) throws SQLException {
68 | String jsonSource = rs.getString(columnName);
69 | return JsonNodeValue.fromDb(jsonSource);
70 | }
71 |
72 | @Override
73 | public JsonNodeValue getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
74 | String jsonSource = rs.getString(columnIndex);
75 | return JsonNodeValue.fromDb(jsonSource);
76 | }
77 |
78 | @Override
79 | public JsonNodeValue getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
80 | String jsonSource = cs.getString(columnIndex);
81 | return JsonNodeValue.fromDb(jsonSource);
82 | }
83 |
84 | /*
85 | Override BaseTypeHandler in such way that result will never be null
86 | */
87 | @Override
88 | public JsonNodeValue getResult(ResultSet rs, String columnName) throws SQLException {
89 | try {
90 | return getNullableResult(rs, columnName);
91 | } catch (Exception e) {
92 | throw new ResultMapException("Error attempting to get column '" + columnName + "' from result set. Cause: " + e, e);
93 | }
94 | }
95 |
96 | @Override
97 | public JsonNodeValue getResult(ResultSet rs, int columnIndex) throws SQLException {
98 | try {
99 | return getNullableResult(rs, columnIndex);
100 | } catch (Exception e) {
101 | throw new ResultMapException("Error attempting to get column #" + columnIndex + " from result set. Cause: " + e, e);
102 | }
103 | }
104 |
105 | @Override
106 | public JsonNodeValue getResult(CallableStatement cs, int columnIndex) throws SQLException {
107 | try {
108 | return getNullableResult(cs, columnIndex);
109 | } catch (Exception e) {
110 | throw new ResultMapException("Error attempting to get column #" + columnIndex + " from callable statement. Cause: " + e, e);
111 | }
112 | }
113 | }
114 |
--------------------------------------------------------------------------------
/src/main/java/com/github/jneat/mybatis/ReaderWriter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2016 Vladislav Zablotsky
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package com.github.jneat.mybatis;
25 |
26 | import com.fasterxml.jackson.core.JsonParser;
27 | import com.fasterxml.jackson.core.JsonProcessingException;
28 | import com.fasterxml.jackson.core.TreeNode;
29 | import com.fasterxml.jackson.databind.JsonNode;
30 | import com.fasterxml.jackson.databind.ObjectMapper;
31 | import com.fasterxml.jackson.databind.ObjectReader;
32 | import com.fasterxml.jackson.databind.ObjectWriter;
33 |
34 | import java.io.IOException;
35 |
36 | final class ReaderWriter {
37 |
38 | private static final ObjectReader READER;
39 |
40 | private static final ObjectWriter WRITER;
41 |
42 | static {
43 | ObjectMapper mapper = new ObjectMapper();
44 | WRITER = mapper.writer();
45 |
46 | mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
47 | mapper.configure(JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS, true);
48 | mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
49 | READER = mapper.reader();
50 |
51 | }
52 |
53 | static JsonNode readTree(String json) throws IOException {
54 | return READER.readTree(json);
55 | }
56 |
57 | static String write(TreeNode tree) throws JsonProcessingException {
58 | return WRITER.writeValueAsString(tree);
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/main/java/com/github/jneat/mybatis/TreeNodeLazyWrapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2016 Vladislav Zablotsky
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package com.github.jneat.mybatis;
25 |
26 | import com.fasterxml.jackson.core.JsonParser;
27 | import com.fasterxml.jackson.core.JsonPointer;
28 | import com.fasterxml.jackson.core.JsonToken;
29 | import com.fasterxml.jackson.core.ObjectCodec;
30 | import com.fasterxml.jackson.core.TreeNode;
31 | import com.fasterxml.jackson.databind.JsonNode;
32 |
33 | import java.io.IOException;
34 | import java.io.Serializable;
35 | import java.util.Iterator;
36 |
37 | /**
38 | * Lazy JSON node wrapper, that will create generate real TreeNode after first call to it's methods.
39 | * Note, that in a case if input JSON string is invalid it may throw runtime exception from any method.
40 | */
41 | public class TreeNodeLazyWrapper implements TreeNode, Serializable {
42 |
43 | private static final long serialVersionUID = -5553988352322235606L;
44 |
45 | private final String json;
46 |
47 | private JsonNode node;
48 |
49 | TreeNodeLazyWrapper(String json) {
50 | this.json = json;
51 | }
52 |
53 | /**
54 | * This will return source JSON string passed as argument into constructor.
55 | */
56 | public String getJsonSource() {
57 | return this.json;
58 | }
59 |
60 | private JsonNode tree() {
61 | if (this.node == null) {
62 | synchronized (this) {
63 | if (this.node == null) {
64 | try {
65 | node = ReaderWriter.readTree(json);
66 | } catch (IOException ex) {
67 | throw new RuntimeException(ex.getMessage(), ex);
68 | }
69 | }
70 | }
71 | }
72 | return this.node;
73 | }
74 |
75 | @Override
76 | public JsonToken asToken() {
77 | return tree().asToken();
78 | }
79 |
80 | @Override
81 | public JsonParser.NumberType numberType() {
82 | return tree().numberType();
83 | }
84 |
85 | @Override
86 | public int size() {
87 | return tree().size();
88 | }
89 |
90 | @Override
91 | public boolean isValueNode() {
92 | return tree().isValueNode();
93 | }
94 |
95 | @Override
96 | public boolean isContainerNode() {
97 | return tree().isContainerNode();
98 | }
99 |
100 | @Override
101 | public boolean isMissingNode() {
102 | return tree().isMissingNode();
103 | }
104 |
105 | @Override
106 | public boolean isArray() {
107 | return tree().isArray();
108 | }
109 |
110 | @Override
111 | public boolean isObject() {
112 | return tree().isObject();
113 | }
114 |
115 | @Override
116 | public TreeNode get(String string) {
117 | return tree().get(string);
118 | }
119 |
120 | @Override
121 | public TreeNode get(int i) {
122 | return tree().get(i);
123 | }
124 |
125 | @Override
126 | public TreeNode path(String string) {
127 | return tree().path(string);
128 | }
129 |
130 | @Override
131 | public TreeNode path(int i) {
132 | return tree().path(i);
133 | }
134 |
135 | @Override
136 | public Iterator fieldNames() {
137 | return tree().fieldNames();
138 | }
139 |
140 | @Override
141 | public TreeNode at(JsonPointer jp) {
142 | return tree().at(jp);
143 | }
144 |
145 | @Override
146 | public TreeNode at(String string) throws IllegalArgumentException {
147 | return tree().at(string);
148 | }
149 |
150 | @Override
151 | public JsonParser traverse() {
152 | return tree().traverse();
153 | }
154 |
155 | @Override
156 | public JsonParser traverse(ObjectCodec oc) {
157 | return tree().traverse(oc);
158 | }
159 |
160 | @Override
161 | public String toString() {
162 | return tree().toString();
163 | }
164 |
165 | @Override
166 | public int hashCode() {
167 | return tree().hashCode();
168 | }
169 |
170 | @Override
171 | public boolean equals(Object o) {
172 | return tree().equals(o);
173 | }
174 | }
175 |
--------------------------------------------------------------------------------
/src/main/java/com/github/jneat/mybatis/TreeNodeTypeHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2016 Vladislav Zablotsky
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package com.github.jneat.mybatis;
25 |
26 | import com.fasterxml.jackson.core.TreeNode;
27 | import com.fasterxml.jackson.databind.JsonNode;
28 | import com.fasterxml.jackson.databind.node.ArrayNode;
29 | import com.fasterxml.jackson.databind.node.MissingNode;
30 | import com.fasterxml.jackson.databind.node.ObjectNode;
31 | import org.apache.ibatis.executor.result.ResultMapException;
32 | import org.apache.ibatis.type.BaseTypeHandler;
33 | import org.apache.ibatis.type.JdbcType;
34 | import org.apache.ibatis.type.MappedTypes;
35 |
36 | import java.io.IOException;
37 | import java.sql.CallableStatement;
38 | import java.sql.PreparedStatement;
39 | import java.sql.ResultSet;
40 | import java.sql.SQLException;
41 |
42 | /**
43 | * Map JSON column string as TreeNode.
44 | * Return MissingNode instead of null.
45 | * Use JSON string representation as intermediate data format.
46 | *
47 | * @see TreeNode
48 | */
49 | @MappedTypes({JsonNode.class, TreeNode.class, ArrayNode.class, ObjectNode.class})
50 | public class TreeNodeTypeHandler extends BaseTypeHandler {
51 |
52 | @Override
53 | public void setNonNullParameter(PreparedStatement ps, int i, TreeNode parameter, JdbcType jdbcType) throws SQLException {
54 | try {
55 | ps.setString(i, ReaderWriter.write(parameter));
56 | } catch (IOException ex) {
57 | throw new RuntimeException(ex.getMessage(), ex);
58 | }
59 | }
60 |
61 | @Override
62 | public TreeNode getNullableResult(ResultSet rs, String columnName) throws SQLException {
63 | String jsonSource = rs.getString(columnName);
64 | return fromString(jsonSource);
65 | }
66 |
67 | @Override
68 | public TreeNode getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
69 | String jsonSource = rs.getString(columnIndex);
70 | return fromString(jsonSource);
71 | }
72 |
73 | @Override
74 | public TreeNode getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
75 | String jsonSource = cs.getString(columnIndex);
76 | return fromString(jsonSource);
77 | }
78 |
79 | private TreeNode fromString(String source) {
80 | if (source == null || source.isEmpty()) {
81 | // This is where we replace null result with empty node
82 | return MissingNode.getInstance();
83 | } else {
84 | // I really hope that source will be valid JSON string (^_^)
85 | return new TreeNodeLazyWrapper(source);
86 | }
87 | }
88 |
89 | /*
90 | Override BaseTypeHandler in such way that result will never be null
91 | */
92 | @Override
93 | public TreeNode getResult(ResultSet rs, String columnName) throws SQLException {
94 | try {
95 | return getNullableResult(rs, columnName);
96 | } catch (Exception e) {
97 | throw new ResultMapException("Error attempting to get column '" + columnName + "' from result set. Cause: " + e, e);
98 | }
99 | }
100 |
101 | @Override
102 | public TreeNode getResult(ResultSet rs, int columnIndex) throws SQLException {
103 | try {
104 | return getNullableResult(rs, columnIndex);
105 | } catch (Exception e) {
106 | throw new ResultMapException("Error attempting to get column #" + columnIndex + " from result set. Cause: " + e, e);
107 | }
108 | }
109 |
110 | @Override
111 | public TreeNode getResult(CallableStatement cs, int columnIndex) throws SQLException {
112 | try {
113 | return getNullableResult(cs, columnIndex);
114 | } catch (Exception e) {
115 | throw new ResultMapException("Error attempting to get column #" + columnIndex + " from callable statement. Cause: " + e, e);
116 | }
117 | }
118 | }
119 |
--------------------------------------------------------------------------------
/src/test/java/com/github/jneat/mybatis/JsonEntity.java:
--------------------------------------------------------------------------------
1 | package com.github.jneat.mybatis;
2 |
3 | import com.fasterxml.jackson.core.TreeNode;
4 |
5 | import java.io.Serializable;
6 |
7 | public class JsonEntity implements Serializable {
8 |
9 | private static final long serialVersionUID = 2361613838967425855L;
10 |
11 | private long id;
12 |
13 | private TreeNode jsonArray;
14 |
15 | private TreeNode jsonObject;
16 |
17 | private JsonNodeValue nodeArray = JsonNodeValue.EMPTY;
18 |
19 | private JsonNodeValue nodeObject = JsonNodeValue.EMPTY;
20 |
21 | public JsonEntity() {
22 | }
23 |
24 | public JsonEntity(long id, TreeNode jsonArray, TreeNode jsonObject, JsonNodeValue nodeArray, JsonNodeValue nodeObject) {
25 | this.id = id;
26 | this.jsonArray = jsonArray;
27 | this.jsonObject = jsonObject;
28 | this.nodeArray = nodeArray;
29 | this.nodeObject = nodeObject;
30 | }
31 |
32 | public long getId() {
33 | return id;
34 | }
35 |
36 | public void setId(long id) {
37 | this.id = id;
38 | }
39 |
40 | public TreeNode getJsonArray() {
41 | return jsonArray;
42 | }
43 |
44 | public void setJsonArray(TreeNode jsonArray) {
45 | this.jsonArray = jsonArray;
46 | }
47 |
48 | public TreeNode getJsonObject() {
49 | return jsonObject;
50 | }
51 |
52 | public void setJsonObject(TreeNode jsonObject) {
53 | this.jsonObject = jsonObject;
54 | }
55 |
56 | public JsonNodeValue getNodeArray() {
57 | return nodeArray;
58 | }
59 |
60 | public void setNodeArray(JsonNodeValue nodeArray) {
61 | this.nodeArray = JsonNodeValue.orEmpty(nodeArray);
62 | }
63 |
64 | public JsonNodeValue getNodeObject() {
65 | return nodeObject;
66 | }
67 |
68 | public void setNodeObject(JsonNodeValue nodeObject) {
69 | this.nodeObject = JsonNodeValue.orEmpty(nodeObject);
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/src/test/java/com/github/jneat/mybatis/JsonHandlersTestApi.java:
--------------------------------------------------------------------------------
1 | package com.github.jneat.mybatis;
2 |
3 | import org.apache.ibatis.mapping.Environment;
4 | import org.apache.ibatis.session.Configuration;
5 | import org.apache.ibatis.session.SqlSessionFactory;
6 | import org.apache.ibatis.session.SqlSessionFactoryBuilder;
7 | import org.apache.ibatis.transaction.TransactionFactory;
8 | import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
9 | import org.assertj.core.api.Assertions;
10 |
11 | import java.io.IOException;
12 | import java.io.InputStream;
13 | import java.io.StringWriter;
14 | import java.sql.Connection;
15 | import java.sql.SQLException;
16 | import java.sql.Statement;
17 | import javax.sql.DataSource;
18 |
19 | public abstract class JsonHandlersTestApi {
20 |
21 | protected static String getResourceAsString(String resource) throws IOException {
22 |
23 | // System.out.println(JsonHandlersTestApi.class.getResource(resource).getFile());
24 | //
25 | // Assertions.assertThat(JsonHandlersTestApi.class.getResource(resource).getFile())
26 | // .isEqualTo("");
27 | String query;
28 | try (final InputStream is = JsonHandlersTestApi.class.getResourceAsStream(resource)) {
29 | // try (final InputStream is = JsonHandlersTestApi.class.getResourceAsStream(resource)) {
30 |
31 | Assertions.assertThat(is).isNotNull();
32 |
33 | StringWriter stringWriter = new StringWriter();
34 | int b;
35 | while ((b = is.read()) != -1) {
36 | stringWriter.write(b);
37 | }
38 | query = stringWriter.toString();
39 | }
40 | return query;
41 | }
42 |
43 | protected static SqlSessionFactory setUpDb(DataSource ds, String initSql) throws SQLException, IOException {
44 | try (final Connection cnx = ds.getConnection(); final Statement st = cnx.createStatement()) {
45 | st.execute(getResourceAsString(initSql));
46 | }
47 |
48 | // Init mybatis
49 | TransactionFactory transactionFactory = new JdbcTransactionFactory();
50 | Environment environment = new Environment("jneat", transactionFactory, ds);
51 | Configuration configuration = new Configuration(environment);
52 | configuration.getTypeHandlerRegistry().register("com.github.jneat.mybatis");
53 | configuration.addMapper(JsonMapper.class);
54 |
55 | return new SqlSessionFactoryBuilder().build(configuration);
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/test/java/com/github/jneat/mybatis/JsonMapper.java:
--------------------------------------------------------------------------------
1 | package com.github.jneat.mybatis;
2 |
3 | import com.fasterxml.jackson.databind.node.ArrayNode;
4 | import com.fasterxml.jackson.databind.node.ObjectNode;
5 | import org.apache.ibatis.annotations.Param;
6 |
7 | public interface JsonMapper {
8 |
9 | JsonEntity get(@Param("id") long id);
10 |
11 | int insert(JsonEntity entity);
12 |
13 | int insertValues(
14 | @Param("id") long id,
15 | @Param("jsonArray") ArrayNode jArray,
16 | @Param("jsonObject") ObjectNode jObj,
17 | @Param("nodeArray") JsonNodeValue nArr,
18 | @Param("nodeObject") JsonNodeValue nObj
19 | );
20 | }
21 |
--------------------------------------------------------------------------------
/src/test/java/com/github/jneat/mybatis/JsonNodeValueTest.java:
--------------------------------------------------------------------------------
1 | package com.github.jneat.mybatis;
2 |
3 | import static org.assertj.core.api.Assertions.*;
4 |
5 | import org.junit.Test;
6 | import org.junit.runner.RunWith;
7 | import org.junit.runners.Parameterized;
8 | import org.junit.runners.Parameterized.Parameters;
9 |
10 | import java.util.Arrays;
11 | import java.util.Collection;
12 |
13 | @RunWith(Parameterized.class)
14 | public class JsonNodeValueTest {
15 |
16 | @Parameters
17 | public static Collection