mybatis一对多表查询时,注意表与表的主键字段名不要相同,主表的主键要排序。
(代码和xml只截取了一部分)
mybatis newsMapper.xml
<resultMap id="newsDetail" type="bean.NewsDetail">
<id column="NEWS_DETAIL_ID" jdbcType="VARCHAR" property="newsDetailId" />
<result column="AUTHOR" jdbcType="VARCHAR" property="author" />
</resultMap>
<resultMap id="news" type="bean.News">
<id column="NEWS_ID" jdbcType="VARCHAR" property="newsId" />
<result column="CREATE_TIME" jdbcType="DATE" property="createTime" />
<collection property="details" resultMap="newsDetail" />
</resultMap>
<!-- 测试查询方法,oracle -->
<select id="testGet" resultMap="news">
<![CDATA[
select c.NEWS_ID ,
d.NEWS_DETAIL_ID
from (select b.*,rownum r from
(select * from NP_NEWS a ORDER BY a.CREATE_TIME desc,a."NEWS_ID" )
b where ROWNUM <100 ) c
LEFT JOIN NP_NEWS_DETAIL d on c.NEWS_ID=d.NEWS_ID
where c.r>=0
]]>
</select>
java代码:
public class News implements java.io.Serializable{
private String newsId;
private Date createTime;
private List<NewsDetail> details = null;
。。。。
}
public class NewsDetail {
private String newsDetailId;
private String author;
。。。。。。。。。。。
}
java dao:
public interface NewsDao {
/**
* 测试用
* @return
*/
List<News> testGet();
}