MyBatis嵌套查询Collection标签的column属性如何传入实体?

在使用MyBatis的过程中,想要嵌套查询一个List如下

  <resultMap id="MenuRoleResultMap" type="Role" extends="BaseResultMap">
    <collection property="menus" ofType="Menu" column="id"
                select="MenuMapper.listByRole"/>
  </resultMap>

对应的listByRole如下

<select id="listByRole" parameterType="Role" resultMap="BaseResultMap">
    SELECT *
    FROM sys_menu m
    LEFT JOIN sys_role_menu srm ON m.id = srm.menu_id
    WHERE m.del_flag = #{DEL_NOT}
      and srm.role_id = #{id}
  </select>

这个listByRole接收的参数是一个实体类Role,这个Role有一个静态常量DEL_NOT,这个常量是拿不到的,只能拿到id这个参数
报错如下

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='DEL_NOT', mode=IN, javaType=int, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

我估摸着应该是 resultMap里collection指定了column="id"才导致后面的id拿到了,我知道可以用
column="{rid = id,DEL_NOT = 0}"
然后
paramterType="HashMap"
来解决传多个参的问题,但还是想知道能不能传实体呢?

阅读 16.3k
1 个回答

惊了居然这样就行了。。。

  <resultMap id="MenuRoleResultMap" type="Role" extends="BaseResultMap">
    <collection property="menus" ofType="pers.octave.server.sys.entity.Menu"
                javaType="java.util.ArrayList" column="{id = id}"
                select="pers.octave.server.sys.dao.MenuMapper.listByRole"/>
  </resultMap>

为自己的鱼唇感到悲哀

推荐问题