renr
renr
采纳率0%
2017-02-04 06:05 阅读 2.0k

mybatis3.3 配置嵌套查询时,无法调用关联的select

<resultMap type="com.rr.one2many.Department" id="deptMap">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <!-- <collection property="emps" ofType="com.rr.one2many.Employee" javaType="java.util.ArrayList" column="id" select="empNamespace.findEmpById"></collection>
     -->
     <collection property="emps" column="id" select="findEmpById" ></collection>
</resultMap>

<select id="findAll" resultType="com.rr.one2many.Department">
    select * from department
</select>

<select id="findById" parameterType="int" resultType="com.rr.one2many.Department">
    select id,name from department where id=#{id}
</select>

<select id="findEmpById" parameterType="int" resultType="com.rr.one2many.Employee">
    select * from employee where depId=#{id}
</select>

<select id="findEmpById" parameterType="int" resultType="com.rr.one2many.Employee">
    select * from employee where depId=#{id}
</select>

<select id="findEmpByEmpId" parameterType="int" resultType="com.rr.one2many.Employee">
    select id,name,depId from employee where id=#{id}
</select>

<resultMap type="com.rr.one2many.Employee" id="empMap">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="age" column="age"/>
    <association property="dept" javaType="com.rr.one2many.Department" column="depId" select="deptNamespace.findById"></association>
</resultMap>

员工表和部门表,查询部门时,获取关联的员工,或者查询员工时,获取关联的部门。但是执行某个select时,不会执行关联select。
比如,执行findEmpByEmpId,但是assocation中select对应的deptNamespace.findById"不执行
没有使用懒加载

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • yf275908654 S1ow 2017-02-04 07:48

    select中resultType改成对应的resultMap
    你这样写当然不会执行了,返回均为实体,根本没有走自定义的resultMap,又怎么能执行assocation呢?

    点赞 评论 复制链接分享
  • zxc284049007 huaYan丶 2017-02-04 09:55


    select * from department

    改为

    select * from department

    点赞 评论 复制链接分享
  • zxc284049007 huaYan丶 2017-02-04 10:01
    
    <select id="findAll" resultType="com.rr.one2many.Department">
        select * from department
    </select>
    

    改为

    
    <select id="findAll" resultType="deptMap">
        select * from department
    </select>
    
    点赞 评论 复制链接分享

相关推荐