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;
    }




阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wgp15732622312/article/details/79951977
个人分类: mybatis
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭