1. 程式人生 > >Mybatis懶載入——返回前端資料 json序列化錯誤

Mybatis懶載入——返回前端資料 json序列化錯誤

    先來個大家一大丟的錯誤

HTTP Status 500 - Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory$EnhancedResultObjectProxyImpl]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory$EnhancedResultObjectProxyImpl and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.ssm.model.domain.UserEntity_$$_jvstf7f_0["handler"])

背景:

mybatis級聯查詢,配置了懶載入模式,結果通過springMvc返回json時報的錯。

推測:

因為不懂mybatis的原始碼怎麼實現懶載入的,既然是通過用到懶載入部分,再去sql查詢,那麼肯定是json序列化的時候查詢了,由於可能會用到mybatis的一些類,估計就json序列化失敗了吧。

解決:

請忽略上面兩步,直接百度就能解決了。哈哈哈哈。

在所有相關的類前加上@JsonIgnoreProperties, 作用是json序列化時忽略bean中的一些屬性序列化和反序列化時丟擲的異常.

@JsonIgnoreProperties(value = {"handler"})
public abstract class BaseEntity implements Serializable

問題篇

配置檔案篇的cloumn指的是級聯時傳遞過來的引數。

對於關聯的collection的屬性,一定是集合,否則mybatis會報查一條,卻返回多條的錯誤。

看到這裡就可以結束了,下面都是程式碼,算是本人的一個程式碼庫。

程式碼篇

先說第一句話,好像mybatis在除錯的情況下懶載入是不起作用的。反正我除錯的時候不起作用。只有執行的時候起作用。

mapper配置篇

UserMapper配置

<resultMap type="com.ssm.model.domain.UserEntity" id="userMap"  >
        <id property="id" column="id"/>
        <result property="passWord" column="password"/>
        <result property="userName" column="username"/>
        <result property="email" column="email"/>
        <result property="created" column="created"/>
        <result property="updated" column="updated"/>
        <result property="phone" column="phone"/>
        <result property="sex" column="sex" typeHandler="com.ssm.mybatis.typehandler.SexEnumHandler"  />
        <association fetchType="lazy" property="bodyEntity" column="id"  select="com.ssm.dao.BodyMapper.findBodyByUserId">
        </association>
        <association fetchType="eager" property="cardEntity" column="id"  select="com.ssm.dao.CardMapper.findCardByUserId">
        </association>
        <collection property="scoreEntity" column="id"  select="com.ssm.dao.ScoreMapper.findScoreListByUserId">
        </collection>
    </resultMap>
    <select id="selectUserById" resultMap="userMap" >
        select * from tb_user where id = #{id}
    </select>

BodyMapper配置

<resultMap id="bodyMap" type="com.ssm.model.domain.BodyEntity">
        <id property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="height" column="height"/>
        <result property="pullUp" column="pull_up"/>
        <result property="pulmonary" column="pulmonary"/>
    </resultMap>

    <select id="findBodyByUserId" resultMap="bodyMap">
        SELECT  * from tb_body
        where user_id = #{id}
    </select>

ScoreMapper配置

    <resultMap id="scoreMap" type="com.ssm.model.domain.ScoreEntity">
        <id property="id" column="id"/>
        <result property="userName" column="user_name"/>
        <result property="userName" column="user_name"/>
        <result property="score" column="score"/>
        <result property="userId" column="user_id"/>
    </resultMap>

<select id="findScoreListByUserId" resultMap="scoreMap">
    SELECT  * from tb_score
    where user_id = #{userId}
</select>

CardMapper配置

 <resultMap id="cardMap" type="com.ssm.model.domain.CardEntity">
        <id property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="note" column="note"/>
        <result property="grade" column="grade"/>
    </resultMap>
    
    <select id="findCardByUserId" resultType="card">
        SELECT  * from tb_card
        where user_id = #{id}
    </select>

Service程式碼

 @Override
    public UserEntity selectUserById(Long userId) {
        String resource = "mybatis-config.xml";
        SqlSessionFactory sqlSessionFactory=null;
        SqlSession session = null;
        UserEntity userEntity = null;
        try{
            InputStream in = new ClassPathResource(resource).getInputStream();
            //建立單例工廠
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
            session = sqlSessionFactory.openSession();
            XmlMybatisTest xmlMybatisTest = new XmlMybatisTest();
            userEntity=xmlMybatisTest.testMapper(session);
            session.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(session!= null){
                session.close();
            }
        }
        return userEntity;
    }