Dubbo调用Service后返回null

问题

  • 使用Dubbo服务调用Service后,返回值是null,在Dao层可以确认结果已经查询出来了。
  • 其它的Service服务都没有问题。
  • 这个Service服务有这样一个特征:是一个树形结构的类,包含上级节点和所有子节点。

猜想

  • 可能是这个上级节点和所有子节点导致返回为null,经测,去除这个两个属性后,返回的是有值的。
  • 进一步猜测,可能是因为使用mybatis懒加载导致的
    ,经测,在该Mapper中的关联查询禁用懒加载后,没问题了!。

解决思路

查看MyBatis是否开启懒加载

先看下Mybatis的配置文件:

<configuration>
    <settings>
        <setting name="cacheEnabled" value="true" />
        <setting name="lazyLoadingEnabled" value="true" /><!-- 启用了懒加载 -->
        <setting name="multipleResultSetsEnabled" value="true" />
        <setting name="useColumnLabel" value="true" />
        <setting name="defaultExecutorType" value="REUSE" />
        <setting name="defaultStatementTimeout" value="25000" />
        <setting name="logImpl" value="SLF4J" />    
    </settings>
</configuration>

可以看出,MyBatis已经启用了懒加载。

调用的Service及POJO

// 调用的Service
public Area getAreaById(String id) {
    return areaMapper.selectAreaById(id);
}
// 返回的Area类
public class Area implements Serializable{
    // 序列化id
    private static final long serialVersionUID = 6385830546250681339L;
    // 该节点的上级节点
    private Area parent;
    // 该节点的下级节点
    private List<Area> children;
    // 其它属性及setter/getter方法略
}

调用的Mapper

<resultMap id="areaResultMap" type="com.woyi.mhub.area.domain.Area">
  <id column="id" jdbcType="CHAR" property="id" />
  <result column="createdate" jdbcType="TIMESTAMP" property="createdate" />
  <result column="updatedate" jdbcType="TIMESTAMP" property="updatedate" />
  <result column="orders" jdbcType="INTEGER" property="orders" />
  <result column="name" jdbcType="VARCHAR" property="name" />
  <result column="fullname" jdbcType="VARCHAR" property="fullname" />
  <result column="treepath" jdbcType="VARCHAR" property="treepath" />
  <result column="parentid" jdbcType="CHAR" property="parentid" />
  <result column="level" jdbcType="CHAR" property="level" />
  <!-- 关联查出上级区域 -->
  <association property="parent" column="parentid" select="selectAreaById"></association>
  <!-- 查询出下级区域 -->
  <collection property="children" column="id" ofType="com.woyi.mhub.area.domain.Area"
    javaType="java.util.ArrayList" select="selectChildArea"></collection>
</resultMap>
<!-- 根据id查询,同时查询出该区域的上级区域和子区域 -->
<select id="selectAreaById" resultMap="areaResultMap" parameterType="java.lang.String">
    select <include refid="Base_Column_List" />
    from sys_area 
    where id = #{id,jdbcType=CHAR}
</select>

使用上述代码,则调用Dubbo服务后返回null。

解决

禁用关联查询的懒加载,添加fetchType=”eager”属性即可。
<!-- 关联查出上级区域 -->
<association property="parent" column="parentid" fetchType="eager" select="selectAreaById"></association>
<!-- 查询出下级区域 -->
<collection property="children" column="id" fetchType="eager" ofType="com.woyi.mhub.area.domain.Area"
    javaType="java.util.ArrayList" select="selectChildArea"></collection>

参考

使用mybatis延迟加载时,因为接口返回的是代理对象,导致dubbo序列化后属性值全部为null
mybatis延迟加载导致dubbo值为null解决办法

猜你喜欢

转载自blog.csdn.net/u012383839/article/details/72875175
今日推荐