以下是项目中使用过的频道(channel)与新闻(news)的对应关系,一个channel下可以有多个news,
一个news必须属于某个channel,在增加一个news的时候必须同时增加一个channel_id在news的数据库表字段上,
public class Channel implements Serializable {

/**
*
*/
private static final long serialVersionUID = -5876679362585875098L;
private int id;
private Date create_time; //
private String create_man; //
private String title; //
private String description; //

private List<News> news; //一对执有多端的一个集合的引用
getter();
setter();
}
*/
public class News implements Serializable {
/**
*
*/
private static final long serialVersionUID = -6179014966049424520L;
private int news_id;
private String title; // 新闻标题
private String author; // 作者
private String summary; //
private String content; //
private Date create_time;//
private String source; //
private int visit_num; //
private int reply_num;
private int status;
private int order_pos;
private String keywords;
private Channel channel; //多端执有一端的一个实体引用

public void setChannelId(int channelId){
this.channel.setId(channelId);
}
public int getChannelId(){
return this.channel.getId();
}
// 以上两个方法可以维护它们的映射关系,


getter();
setter();

}
ibatis中的channel.xml
<sqlMap namespace="channel">
<typeAlias alias="channel" type="com.tongdainfo.model.channel.Channel" />
<typeAlias alias="news" type="com.tongdainfo.model.news.News"/>


<resultMap id="channelResult" class="channel">
<result property="id" column="id"/>
<result property="create_time" column="create_time"/>
<result property="create_man" column="create_man"/>
<result property="title" column="title"/>
<result property="description" column="description"/>
<result property="news" column="id" select="getNewsByChannelId" />
</resultMap>




<!-- 添加频道 -->
<insert id="saveChannel" parameterClass="channel">
<!--
<selectKey keyProperty="id" resultClass="int">
<!--[CDATA[
SELECT LAST_INSERT_ID() AS VALUE
]]>

</selectKey>
-->
<![CDATA[
INSERT INTO channel(title,description,create_man,create_time)
VALUES (#title#,#description#,#create_man#,#create_time#)
]]>
</insert>
</sqlMap>
iBatis 中的news.xml
<sqlMap namespace="news">
<typeAlias alias="news" type="com.tongdainfo.model.news.News" />
<typeAlias alias="channel" type="com.tongdainfo.model.channel.Channel" />
<resultMap id="newsResult" class="news">
<result property="news_id" column="news_id" />
<result property="title" column="title" />
<result property="author" column="author" />
<result property="summary" column="summary" />
<result property="create_time" column="create_time" />
<result property="source" column="source" />
<result property="visit_num" column="visit_num" />
<result property="reply_num" column="reply_num" />
<result property="status" column="status" />
<result property="order_pos" column="order_pos" />
<result property="keywords" column="keywords" />
<result property="channel.id" column="cid" />
<result property="channel.title" column="ctitle" />
<result property="channel.description" column="cdescription" />
<result property="channel.create_man" column="ccreate_man" />
<result property="channel.create_time" column="ccreate_time" />
</resultMap>
<select id="getNewsByChannelId" parameterClass="int" resultMap="newsResult">
select n.news_id as news_id,n.title as title,n.author as
author,n.summary as summary,
n.create_time as create_time,n.source as
source,n.visit_num as
visit_num,n.reply_num as reply_num,
n.keywords as
keywords,c.id as cid,c.title as ctitle,c.description as
cdescription,c.creat_man as ccreate_man,
c.create_time as ccreate_time
from news AS n
left join channel as c
on n.channel_id =c.id
where
n.channel_id=#id#
</select>
<!-- 添加新闻 -->
<insert id="saveNews" parameterClass="news">
<!--
<selectKey keyProperty="id" resultClass="int"> <![CDATA[ SELECT
LAST_INSERT_ID() AS VALUE ]]> </selectKey>
-->
<![CDATA[
INSERT INTO News(title,author,summary,content,create_time,source,visit_num,reply_num,status,order_pos,keywords,channel_id)
VALUES(#title#,#author#,#summary#,#content#,#create_time#,#source#,#visit_num#,#reply_num#,#status#,#order_pos#,#keywords#,#channelId#)

]]-->
</insert>
<!-- 查询channel下所有新闻列表 -->
<select id="findNewsByChannelId" parameterClass="int" resultMap="newsResult">
select n.news_id as news_id,n.title as title,n.author as
author,n.summary as summary,
n.create_time as create_time,n.source as
source,n.visit_num as
visit_num,n.reply_num as reply_num,
n.status as
status,
n.order_pos as order_pos,
n.keywords as keywords,
c.id as
cid,c.title as ctitle,c.description as
cdescription,c.create_man as
ccreate_man,
c.create_time as ccreate_time
from news AS n
left join
channel as c
on n.channel_id =c.id
where n.channel_id=#id#
</select>

</sqlMap>