有没有办法通过mybatis mapper 将json 转换为java bean?
Is there anyway to convert json to java bean through mybatis mapper?
如果我有一个 mysql 连接查询结果集,它是 json 格式,如下所示:
[
{
"user_id": 5294147,
"user_name": "rtm",
"user_created": "2020-11-11 12:27:41",
"user_updated": "2020-11-11 12:27:41",
"tag_id": 10002,
"tag_user_id": 5294147,
"tag": "lazy",
"tag_created": "2020-11-11 12:27:41",
"tag_updated": "2020-11-11 12:27:41"
},
{
"user_id": 5294147,
"user_name": "rtm",
"user_created": "2020-11-11 12:27:41",
"user_updated": "2020-11-11 12:27:41",
"tag_id": 10003,
"tag_user_id": 5294147,
"tag": "weak",
"tag_created": "2020-11-11 12:27:41",
"tag_updated": "2020-11-11 12:27:41"
}
]
和 mybatis 映射器
<?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="org.example.mybatis.mapper.UserMapper">
<resultMap id="UserTag" type="org.example.mybatis.dto.UserTag">
<id column="tag_id" jdbcType="INTEGER" property="id"/>
<result column="tag_user_id" jdbcType="INTEGER" property="userId"/>
<result column="tag" jdbcType="VARCHAR" property="tag"/>
<result column="tag_created" jdbcType="TIMESTAMP" property="created"/>
<result column="tag_updated" jdbcType="TIMESTAMP" property="updated"/>
</resultMap>
<resultMap id="User" type="org.example.mybatis.dto.User">
<id column="user_id" jdbcType="INTEGER" property="id"/>
<result column="user_name" jdbcType="VARCHAR" property="name"/>
<result column="user_created" jdbcType="TIMESTAMP" property="created"/>
<result column="user_updated" jdbcType="TIMESTAMP" property="updated"/>
<collection property="tagList" column="user_id"
notNullColumn="tag_user_id"
javaType="java.util.ArrayList"
ofType="org.example.mybatis.dto.UserTag"
resultMap="UserTag"/>
</resultMap>
<select id="getUserById" parameterType="hashmap" resultMap="User">
select
u.id as user_id,
u.name as user_name,
u.created as user_created,
u.updated as user_updated,
t.id as tag_id,
t.user_id as tag_user_id,
t.tag,
t.created as tag_created,
t.updated as tag_updated
from user u
left join
user_tag t
on
u.id = t.user_id
where u.id = #{userId,jdbcType=INTEGER}
</select>
</mapper>
我不能直接访问这个mysql实例,我只能通过一些“代理平台”以json格式导出查询结果,有没有把json转换成org.example.mybatis.dto.User
轻松通过现有的mybatis mapper?
PS:我试过用mybatisInterceptor
拦截ResultSetHandler.handleResultSets
,但是好像必须mockStatement
和ResultSet
JOOQ可以将txt/json/csv转换为ResultSet
String json = ... // json;
DSLContext dsl = DSL.using(SQLDialect.MYSQL);
Result<?> result= dsl.fetchFromJSON(json);
ResultSet rs = result.intoResultSet();
// mybaits Configuration
Configuration dummyConfiguration = new Configuration();
dummyConfiguration .addMappers("org.example.mybatis.mapper");
MappedStatement ms = dummyConfiguration.getMappedStatement("getUserById");
// just copy mybaits DefaultResultSetHandler and remove some code
FakeResultSetHandler resultHandler = new FakeResultSetHandler(ms, null, RowBounds.DEFAULT);
List<Object> list = resultHandler.handleResultSets(rs);
System.out.println(list);
// output
// [User(id=5294147, name=rtm, created=Wed Nov 11 12:27:41 CST 2020, updated=Wed Nov 11 12:27:41 CST 2020, tagList=[UserTag(id=10002, userId=5294147, tag=lazy, created=Wed Nov 11 12:27:41 CST 2020, updated=Wed Nov 11 12:27:41 CST 2020), UserTag(id=10003, userId=5294147, tag=weak, created=Wed Nov 11 12:27:41 CST 2020, updated=Wed Nov 11 12:27:41 CST 2020)])]
如果我有一个 mysql 连接查询结果集,它是 json 格式,如下所示:
[
{
"user_id": 5294147,
"user_name": "rtm",
"user_created": "2020-11-11 12:27:41",
"user_updated": "2020-11-11 12:27:41",
"tag_id": 10002,
"tag_user_id": 5294147,
"tag": "lazy",
"tag_created": "2020-11-11 12:27:41",
"tag_updated": "2020-11-11 12:27:41"
},
{
"user_id": 5294147,
"user_name": "rtm",
"user_created": "2020-11-11 12:27:41",
"user_updated": "2020-11-11 12:27:41",
"tag_id": 10003,
"tag_user_id": 5294147,
"tag": "weak",
"tag_created": "2020-11-11 12:27:41",
"tag_updated": "2020-11-11 12:27:41"
}
]
和 mybatis 映射器
<?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="org.example.mybatis.mapper.UserMapper">
<resultMap id="UserTag" type="org.example.mybatis.dto.UserTag">
<id column="tag_id" jdbcType="INTEGER" property="id"/>
<result column="tag_user_id" jdbcType="INTEGER" property="userId"/>
<result column="tag" jdbcType="VARCHAR" property="tag"/>
<result column="tag_created" jdbcType="TIMESTAMP" property="created"/>
<result column="tag_updated" jdbcType="TIMESTAMP" property="updated"/>
</resultMap>
<resultMap id="User" type="org.example.mybatis.dto.User">
<id column="user_id" jdbcType="INTEGER" property="id"/>
<result column="user_name" jdbcType="VARCHAR" property="name"/>
<result column="user_created" jdbcType="TIMESTAMP" property="created"/>
<result column="user_updated" jdbcType="TIMESTAMP" property="updated"/>
<collection property="tagList" column="user_id"
notNullColumn="tag_user_id"
javaType="java.util.ArrayList"
ofType="org.example.mybatis.dto.UserTag"
resultMap="UserTag"/>
</resultMap>
<select id="getUserById" parameterType="hashmap" resultMap="User">
select
u.id as user_id,
u.name as user_name,
u.created as user_created,
u.updated as user_updated,
t.id as tag_id,
t.user_id as tag_user_id,
t.tag,
t.created as tag_created,
t.updated as tag_updated
from user u
left join
user_tag t
on
u.id = t.user_id
where u.id = #{userId,jdbcType=INTEGER}
</select>
</mapper>
我不能直接访问这个mysql实例,我只能通过一些“代理平台”以json格式导出查询结果,有没有把json转换成org.example.mybatis.dto.User
轻松通过现有的mybatis mapper?
PS:我试过用mybatisInterceptor
拦截ResultSetHandler.handleResultSets
,但是好像必须mockStatement
和ResultSet
JOOQ可以将txt/json/csv转换为ResultSet
String json = ... // json;
DSLContext dsl = DSL.using(SQLDialect.MYSQL);
Result<?> result= dsl.fetchFromJSON(json);
ResultSet rs = result.intoResultSet();
// mybaits Configuration
Configuration dummyConfiguration = new Configuration();
dummyConfiguration .addMappers("org.example.mybatis.mapper");
MappedStatement ms = dummyConfiguration.getMappedStatement("getUserById");
// just copy mybaits DefaultResultSetHandler and remove some code
FakeResultSetHandler resultHandler = new FakeResultSetHandler(ms, null, RowBounds.DEFAULT);
List<Object> list = resultHandler.handleResultSets(rs);
System.out.println(list);
// output
// [User(id=5294147, name=rtm, created=Wed Nov 11 12:27:41 CST 2020, updated=Wed Nov 11 12:27:41 CST 2020, tagList=[UserTag(id=10002, userId=5294147, tag=lazy, created=Wed Nov 11 12:27:41 CST 2020, updated=Wed Nov 11 12:27:41 CST 2020), UserTag(id=10003, userId=5294147, tag=weak, created=Wed Nov 11 12:27:41 CST 2020, updated=Wed Nov 11 12:27:41 CST 2020)])]