首页 > 代码库 > mybatis关联映射
mybatis关联映射
多对一:
1 <!-- 方法一: --> 2 <select id="getNewsListByPage" parameterType="com.zqc.share.model.news.NewsPage" resultMap="newsPageMap"> 3 select * from news limit #{newsPage.page},#{newsPage.size} 4 </select> 5 6 <resultMap id="newsPageMap" type="com.zqc.share.model.news.NewsPage"> 7 <id column="id" property="id"/> 8 <result column="description" property="description"/> 9 <result column="title" property="title"/>10 <result column="head_image" property="head_image"/>11 <result column="publishtime" property="publishtime"/>12 <result column="readtimes" property="readtimes"/>13 <result column="goodcount" property="goodcount"/>14 <result column="badcount" property="badcount"/> 15 <association property="user" column="user_id" javaType="com.zqc.share.model.user.User" select="com.zqc.share.dao.user.UserDao.getUserById" />16 <association property="topic" column="topic_id" javaType="com.zqc.share.model.topic.Topic" select="com.zqc.share.dao.topic.TopicDao.getTopicById" />17 </resultMap>18 19 <select id="getTopicById" parameterType="int" resultType="com.zqc.share.model.topic.Topic">20 select * from topic where id=#{topic_id}21 </select>22 23 <select id="getUserById" parameterType="int" resultType="com.zqc.share.model.user.User">24 select * from user where id=#{user_id}25 </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>
一对多+多对一:
1 <select id="getNewsById" parameterType="com.zqc.share.model.news.News" resultMap="newsMap"> 2 select n.id,n.description,n.title,n.head_image,n.publishtime,n.readtimes,n.goodcount,n.badcount,n.topic_id,n.user_id, 3 i.image,i.context,i.news_id,i.id 4 from news_item i,news n,topic t,user u 5 where i.news_id=n.id and n.id = #{news.id} 6 </select> 7 8 <resultMap id="newsMap" type="com.zqc.share.model.news.News"> 9 <id column="id" property="id"/> 10 <result column="description" property="description"/>11 <result column="title" property="title"/>12 <result column="head_image" property="head_image"/>13 <result column="publishtime" property="publishtime"/>14 <result column="readtimes" property="readtimes"/>15 <result column="goodcount" property="goodcount"/>16 <result column="badcount" property="badcount"/> 17 <association property="user" column="user_id" javaType="com.zqc.share.model.user.User" select="com.zqc.share.dao.user.UserDao.getUserById" />18 <association property="topic" column="topic_id" javaType="com.zqc.share.model.topic.Topic" select="com.zqc.share.dao.topic.TopicDao.getTopicById" />19 <collection property="newsItems" ofType="com.zqc.share.model.news.NewsItem" resultMap="newsItemsMap"/>20 </resultMap>21 22 <resultMap type="com.zqc.share.model.news.NewsItem" id="newsItemsMap">23 <result column="id" property="id"/>24 <result column="news_id" property="news_id"/>25 <result column="image" property="image"/>26 <result column="context" property="context"/>27 </resultMap>28 29 <select id="getTopicById" parameterType="int" resultType="com.zqc.share.model.topic.Topic">30 select * from topic where id=#{topic_id}31 </select>32 33 <select id="getUserById" parameterType="int" resultType="com.zqc.share.model.user.User">34 select * from user where id=#{user_id}35 </select>
mybatis关联映射
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。