若是兩表聯查,主表和明細表的主鍵都是id的話,明細表的多條只能查詢出來第一條。mysql
級聯查詢的時候,主表和從表有同樣的字段名的時候,在mysql上命令查詢是沒問題的。但在mybatis中主從表須要爲相同字段名設置別名。設置了別名就OK了。sql
例子:mybatis
主表Standard, 從表StandEntity,均有名爲id的字段spa
<resultMap id="StandardAndEntityResultMap" type="whu.edu.irlab.model.Standard" extends="BaseResultMap"> <collection property="standEntities" ofType="whu.edu.irlab.model.StandEntity"> (依據下面的select中改名的字段id別名se_id,在此將相同的字段名改成別名) <id column="se_id" property="id" jdbcType="INTEGER" /> <result column="stand_id" property="standId" jdbcType="INTEGER" /> <result column="stand_name" property="standName" jdbcType="VARCHAR" /> <result column="entity_name" property="entityName" jdbcType="VARCHAR" /> </collection> </resultMap> <select id="findAllStandardAndEntity" resultMap="StandardAndEntityResultMap"> select standard.*, standard_entity.id se_id,(在此將兩表中相同的字段名id改成別名se_id,對應的上面collection部分也須要更改) standard_entity.stand_id, standard_entity.stand_name, standard_entity.entity_name from standard INNER JOIN standard_entity on standard.id = standard_entity.stand_id </select>