在mybatis的xml配置文件中添加了一对多查询的场景,调用接口后控制台报错:No serializer found for class org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory$EnhancedResultObjectProxyImpl and no properties discovered to create BeanSerializer 但是接口可以正常返回数据

mybatis一对多配置

实体类

public class YourEntity {
    private Long id;
    private String name;
    private List<String> stringList;
}

xml配置

<resultMap id="yourEntityMap" type="com.example.YourEntity">
    <id column="id" property="id"/>
    <result column="name" property="name"/>
    <collection property="stringList" column="id" 
                select="selectStringListByEntityId" fetchType="lazy"/>
</resultMap>

<select id="selectPageWithList" resultMap="yourEntityMap">
    SELECT id, name FROM your_entity
    WHERE [你的查询条件]
</select>

<select id="selectStringListByEntityId" resultType="java.lang.String">
    SELECT value FROM list_table WHERE entity_id = #{id}
</select>

解决方案

mybatis懒加载会在实体类代理过程中添加一个handler的属性,故导致解析失败.

取消懒加载

LAZY:懒加载,加载一个实体时,定义懒加载的属性不会马上从数据库中加载。
EAGER:急加载,加载一个实体时,定义急加载的属性会立即从数据库中加载。

<resultMap id="yourEntityMap" type="com.example.YourEntity">
    <id column="id" property="id"/>
    <result column="name" property="name"/>
    <collection property="stringList" column="id" 
                select="selectStringListByEntityId" fetchType="eager"/>
</resultMap>

<select id="selectPageWithList" resultMap="yourEntityMap">
    SELECT id, name FROM your_entity
    WHERE [你的查询条件]
</select>

<select id="selectStringListByEntityId" resultType="java.lang.String">
    SELECT value FROM list_table WHERE entity_id = #{id}
</select>

使用JsonIgnoreProperties注解

@JsonIgnoreProperties(value = "handler")
public class YourEntity {
    private Long id;
    private String name;
    private List<String> stringList;
}