mybatis关联映射

时间:2023-11-10 17:48:38

多对一:

 <!--    方法一: -->
<select id="getNewsListByPage" parameterType="com.zqc.share.model.news.NewsPage" resultMap="newsPageMap">
select * from news limit #{newsPage.page},#{newsPage.size}
</select> <resultMap id="newsPageMap" type="com.zqc.share.model.news.NewsPage">
<id column="id" property="id"/>
<result column="description" property="description"/>
<result column="title" property="title"/>
<result column="head_image" property="head_image"/>
<result column="publishtime" property="publishtime"/>
<result column="readtimes" property="readtimes"/>
<result column="goodcount" property="goodcount"/>
<result column="badcount" property="badcount"/>
<association property="user" column="user_id" javaType="com.zqc.share.model.user.User" select="com.zqc.share.dao.user.UserDao.getUserById" />
<association property="topic" column="topic_id" javaType="com.zqc.share.model.topic.Topic" select="com.zqc.share.dao.topic.TopicDao.getTopicById" />
</resultMap> <select id="getTopicById" parameterType="int" resultType="com.zqc.share.model.topic.Topic">
select * from topic where id=#{topic_id}
</select> <select id="getUserById" parameterType="int" resultType="com.zqc.share.model.user.User">
select * from user where id=#{user_id}
</select>
<!-- 方法2 -->

<select id="getNewsListByPage" parameterType="com.zqc.share.model.news.NewsPage" resultMap="newsPageMap">
select * from user u,news n,topic t where n.user_id=u.id and n.topic_id=t.id limit #{newsPage.page},#{newsPage.size}
</select> <resultMap type="com.zqc.share.model.news.NewsPage" id="newsPageMap">
<id column="id" property="id"/>
<result column="description" property="description"/>
<result column="title" property="title"/>
<result column="topic_id" property="topic_id"/>
<result column="user_id" property="user_id"/>
<result column="head_image" property="head_image"/>
<result column="publishtime" property="publishtime"/>
<result column="readtimes" property="readtimes"/>
<result column="goodcount" property="goodcount"/>
<result column="badcount" property="badcount"/>
<association property="user" javaType="com.zqc.share.model.user.User" resultMap="userMap" />
<association property="topic" javaType="com.zqc.share.model.topic.Topic" resultMap="topicMap" /> </resultMap> <resultMap type="com.zqc.share.model.user.User" id="userMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="head_image" property="head_image"/>
<result column="mobile" property="mobile"/>
<result column="email" property="email"/>
<result column="address" property="address"/>
<result column="age" property="age"/>
<result column="user_no" property="user_no"/>
<result column="password" property="password"/>
</resultMap> <resultMap type="com.zqc.share.model.topic.Topic" id="topicMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="description" property="description"/>
</resultMap>

一对多+多对一:

 <select id="getNewsById" parameterType="com.zqc.share.model.news.News" resultMap="newsMap">
select n.id,n.description,n.title,n.head_image,n.publishtime,n.readtimes,n.goodcount,n.badcount,n.topic_id,n.user_id,
i.image,i.context,i.news_id,i.id
from news_item i,news n,topic t,user u
where i.news_id=n.id and n.id = #{news.id}
</select> <resultMap id="newsMap" type="com.zqc.share.model.news.News">
<id column="id" property="id"/>
<result column="description" property="description"/>
<result column="title" property="title"/>
<result column="head_image" property="head_image"/>
<result column="publishtime" property="publishtime"/>
<result column="readtimes" property="readtimes"/>
<result column="goodcount" property="goodcount"/>
<result column="badcount" property="badcount"/>
<association property="user" column="user_id" javaType="com.zqc.share.model.user.User" select="com.zqc.share.dao.user.UserDao.getUserById" />
<association property="topic" column="topic_id" javaType="com.zqc.share.model.topic.Topic" select="com.zqc.share.dao.topic.TopicDao.getTopicById" />
<collection property="newsItems" ofType="com.zqc.share.model.news.NewsItem" resultMap="newsItemsMap"/>
</resultMap> <resultMap type="com.zqc.share.model.news.NewsItem" id="newsItemsMap">
<result column="id" property="id"/>
<result column="news_id" property="news_id"/>
<result column="image" property="image"/>
<result column="context" property="context"/>
</resultMap> <select id="getTopicById" parameterType="int" resultType="com.zqc.share.model.topic.Topic">
select * from topic where id=#{topic_id}
</select> <select id="getUserById" parameterType="int" resultType="com.zqc.share.model.user.User">
select * from user where id=#{user_id}
</select>