Created
September 28, 2023 08:04
-
-
Save JoopAue/7035cfa80653ff20023d903a8580e043 to your computer and use it in GitHub Desktop.
GsonTypeHandler in Kotlin for MyBatis and PostgreSQL JSONB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.joopaue | |
import com.google.gson.JsonElement | |
import java.time.Instant | |
data class Data( | |
val time: Instant, | |
val data: JsonElement, | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.joopaue.repositories.mapper | |
import com.joopaue.Data | |
import org.springframework.stereotype.Repository | |
@Repository | |
interface DataMapper { | |
fun insertData(dataPoint: Data) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" ?> | |
<!DOCTYPE mapper | |
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | |
"https://mybatis.org/dtd/mybatis-3-mapper.dtd"> | |
<mapper namespace="com.joopaue.repositories.mapper.DataMapper"> | |
<insert id="insertData" parameterType="com.joopaue.Data"> | |
insert into data (time, data) | |
values ( | |
#{time}, | |
#{data, jdbcType=OTHER, typeHandler=com.joopaue.repositories.mapper.GsonTypeHandler} | |
) | |
; | |
</insert> | |
</mapper> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.joopaue.repositories.mapper | |
import com.google.gson.JsonElement | |
import com.google.gson.JsonParser | |
import org.apache.ibatis.type.BaseTypeHandler | |
import org.apache.ibatis.type.JdbcType | |
import org.postgresql.util.PGobject | |
import java.sql.CallableStatement | |
import java.sql.PreparedStatement | |
import java.sql.ResultSet | |
import java.sql.SQLException | |
class GsonTypeHandler : BaseTypeHandler<JsonElement>() { | |
@Throws(SQLException::class) | |
override fun setNonNullParameter(ps: PreparedStatement, i: Int, parameter: JsonElement?, jdbcType: JdbcType) { | |
try { | |
val pgObject = PGobject() | |
pgObject.type = "jsonb" | |
pgObject.value = parameter?.toString() | |
ps.setObject(i, pgObject) | |
} catch (e: Exception) { | |
throw SQLException("Error converting JSON to String: " + e.message, e) | |
} | |
} | |
@Throws(SQLException::class) | |
override fun getNullableResult(rs: ResultSet, columnName: String): JsonElement? { | |
try { | |
val jsonString = rs.getString(columnName) | |
return parseJson(jsonString) | |
} catch (e: Exception) { | |
throw SQLException("Error converting String to JSON: " + e.message, e) | |
} | |
} | |
@Throws(SQLException::class) | |
override fun getNullableResult(rs: ResultSet, columnIndex: Int): JsonElement? { | |
try { | |
val jsonString = rs.getString(columnIndex) | |
return parseJson(jsonString) | |
} catch (e: Exception) { | |
throw SQLException("Error converting String to JSON: " + e.message, e) | |
} | |
} | |
@Throws(SQLException::class) | |
override fun getNullableResult(cs: CallableStatement, columnIndex: Int): JsonElement? { | |
try { | |
val jsonString = cs.getString(columnIndex) | |
return parseJson(jsonString) | |
} catch (e: Exception) { | |
throw SQLException("Error converting String to JSON: " + e.message, e) | |
} | |
} | |
private fun parseJson(jsonString: String?): JsonElement? { | |
if (jsonString != null) { | |
return JsonParser.parseString(jsonString) | |
} | |
return null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment