当前位置:编程学习 > JAVA >>

mybatis一对多中collection配置无效,求解释

在配置中我配置了一个subject 对应 多个student 配置如下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper   
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"   
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">   
<mapper namespace="Subject.mapper"> 
<select id="selectSubject" parameterType="int" resultType="com.demo.bean.Subject" resultMap="subjectMap">
select * from _subject where _id=#{id};
</select>
<resultMap type="com.demo.bean.Subject" id="subjectMap">
<id property="id" column="_id"/>
<result property="subName"  column="sub_name"/>
<collection property="students" column="_subject" javaType="java.util.ArrayList" ofType="com.demo.bean.Student" select="selectStudents"></collection>
</resultMap>

<select id="selectStudents" resultType="com.demo.bean.Student" parameterType="int">
select _id as id ,_name as name ,_sex as sex
</select>
</mapper>


但是在查询的时候 没有查询出students(数据库里面的数据是正确的) 好像是 <collection property="students" column="_subject" javaType="java.util.ArrayList" ofType="com.demo.bean.Student" select="selectStudents"></collection>这条配置无效一样,就算是我把配置中的select="selectStudents" 胡乱改成select="123" 也不会报错,下面我的sql只写了一半 也不会报错,为什么呢 ? --------------------编程问答-------------------- 可能是别名一致造成的,id改为别的试下。 --------------------编程问答-------------------- 貌似不是这个原因,奇怪了 弄了2天还是没弄好, --------------------编程问答-------------------- 是不是少了resultMap --------------------编程问答-------------------- 也遇到这个问题了,折腾了大半天,最后发现,是由于即使用了别名,又做了映射造成的。要么使用别名后,映射的时候要使用column要用别名才行,要么就只用映射或者别名
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,