json formatting of springboot+mybatis

Suppose that there are five attribute values in the database, namely id,name,sex,age and hobbies. It is required that hobbies are presented in the front end in the form of array. At this time, it can be realized through JSON. At first, I thought of using JSON directly in impl, but there is a problem that there are not only hobbies, but also ID, name, sex and age in the database, If you convert hobbies into String and output it at the front end, there is return... Mapper in this method How to get all the members? In this way, how to output the converted String together with the four obtained values at the same time? (I'm really Xiaobai. I really can't think of any way to 5555). Later, I'll customize MySQL jsonhandler, and then define the corresponding hobbies in the entity class. At the same time... Mapper XML also needs to be adjusted. The code is as follows:
The entity class code is as follows:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private Integer id;
    private String name;
    private String sex;
    private Integer age;
//    private List<String> hobbies;
//      private String hobbies;
    private JSONArray hobbies;
    public JSONArray getHobbies(){
        return hobbies;
    }

    public void setHobbies(JSONArray hobbies){
        this.hobbies = hobbies == null ? null : hobbies;
    }

The custom MySQL jsonhandler is as follows:

@MappedTypes(JSONObject.class)
@MappedJdbcTypes(JdbcType.VARCHAR)
public class MySqlJsonHandler extends BaseTypeHandler<JSONObject>{
    /**
     * Set non empty parameters
     * @param ps
     * @param i
     * @param parameter
     * @param jdbcType
     * @throws SQLException
     */

    @Override
    public void setNonNullParameter(PreparedStatement ps,int i,JSONObject parameter,JdbcType jdbcType) throws SQLException{
        ps.setString(i,String.valueOf(parameter.toJSONString()));
    }

    /**
     * According to the list, get the results that can be empty
     * @param rs
     * @param columnName
     * @return
     * @throws SQLException
     */

    @Override
    public JSONObject getNullableResult(ResultSet rs,String columnName) throws  SQLException{
        String sqlJson = rs.getString(columnName);
        if(null!=sqlJson){
            return JSONObject.parseObject(sqlJson);
        }
        return null;
    }



    @Override
    public JSONObject getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String sqlJson = rs.getString(columnIndex);
        if(null!=sqlJson){
            return JSONObject.parseObject(sqlJson);
        }
        return null;
    }

    @Override
    public JSONObject getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String sqlJson = cs.getString(columnIndex);
        if(null!=sqlJson){
            return JSONObject.parseObject(sqlJson);
        }
        return null;
    }
}

Mapper. The XML code is as follows:

   <resultMap id="getStudent" type="com.tian.pojo.User">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="sex" property="sex" jdbcType="VARCHAR"/>
        <result column="age" property="age" jdbcType="INTEGER"/>
        <result column="hobbies" property="hobbies" typeHandler="com.tian.handler.MySqlJsonHandler"/>
<!--        <result column="hobbies" property="hobbies" javaType="list"/>-->
    </resultMap>


Take adding operation as an example:
<insert id="addUser" parameterType="com.tian.pojo.User">
   insert into mybatis.user (name,sex,age,hobbies) values (#{name,jdbcType=VARCHAR},#{sex,jdbcType=VARCHAR},#{age,jdbcType=INTEGER},#{hobbies,jdbcType=OTHER,typeHandler=com.tian.handler.MySqlJsonHandler})
</insert>

Take the lookup operation as an example:
<select id="queryAll" parameterType="com.tian.pojo.User" resultMap="getStudent" >
    select * from mybatis.user
</select>

I think the above code can solve the problem I want to solve, but in the end, I report the following error

com.alibaba.fastjson.JSONException: syntax error, pos 1, line 1, column 2

Baidu really hasn't found a solution for a long time.
In order to solve this problem, I spent a lot of time. Finally, I think we can start from the type of database hobbies and make it json in the database. The code in the entity class is as follows:

  private JSONArray hobbies;
      public JSONArray getHobbies(){
        return hobbies;
    }

    public void setHobbies(JSONArray hobbies){
        this.hobbies = hobbies == null ? null : hobbies;
    }

Customize an ArrayJsonHandler

@MappedTypes(JSONArray.class)
@MappedJdbcTypes(JdbcType.VARCHAR)
public class ArrayJsonHandler extends BaseTypeHandler<JSONArray> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, JSONArray parameter, JdbcType jdbcType) throws SQLException {
         ps.setString(i,String.valueOf(parameter.toJSONString()));
    }

    @Override
    public JSONArray getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String sqlJson = rs.getString(columnName);
        if(null != sqlJson){
            return JSONArray.parseArray(sqlJson);
        }
        return null;
    }

    @Override
    public JSONArray getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String sqlJson = rs.getString(columnIndex);
        if(null != sqlJson){
            return JSONArray.parseArray(sqlJson);
        }
        return null;
    }

    @Override
    public JSONArray getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String sqlJson = cs.getString(columnIndex);
        if(null != sqlJson){
            return JSONArray.parseArray(sqlJson);
        }
        return null;
    }
}

Define the following code in xml

 <resultMap id="getStudent" type="com.tian.pojo.User">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="sex" property="sex" jdbcType="VARCHAR"/>
        <result column="age" property="age" jdbcType="INTEGER"/>
        <result column="hobbies" property="hobbies" typeHandler="com.tian.handler.ArrayJsonHandler"/>
    </resultMap>

The rest is just business as usual. Finally, you can realize the array of hobbies
However, it seems that you can directly define the type of hobbies as varchar in the database, and then define the type of hobbies as List in the entity class, and directly convert it with json method in serviceimpl. However, after thinking for a long time, I really can't think of what to do, so I keep reporting errors and saying that the type is wrong. I'm really Xiaobai. I hope the boss who knows can teach me!

Keywords: Java JSON Mybatis Spring Boot

Added by uidzer0b on Fri, 25 Feb 2022 06:36:01 +0200