mybatis一对多关联查询注意事项

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();
}


转载于:https://my.oschina.net/tangcoffee/blog/538775

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值