多对一:

 <!--    方法一: -->
<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>

mybatis关联映射的更多相关文章

  1. MyBatis学习(七)MyBatis关联映射之多对多映射

    对于数据库中的多对多关系建议使用一个中间表来维护关系. 1.创建四张表,分别为用户表,商品表,订单表,中间表. DROP TABLE IF EXISTS `t_user`; CREATE TABLE ...

  2. spring boot(9)-mybatis关联映射

    一对多 查询type表的某一条数据,并且要同时查出所有typeid与之配置的user,最终要得到一个以下类型的Type对象 public class Type { String id; String ...

  3. 0049 MyBatis关联映射--一对一关系

    世上的事务总不是孤立存在的,表现在Java类里面,则是类与类之间的关系,比如继承is-a.依赖use-a.关联has-a,反映在数据库中,则是表与表之间的关系,比如外键 关联关系存在着以下几种类型:一 ...

  4. Spring Boot (11) mybatis 关联映射

    一对多 查询category中的某一条数据,同时查询该分类下的所有Product. Category.java public class Category { private Integer id; ...

  5. mybatis关联映射一对一

    在项目开发中,会存在一对一的关系,比如一个人只有一个身份证,一个身份证只能给一个人使用,这就是一对一关系.一对一关系使用主外键关联. table.sql,在数据库中创建如下两个表并插入数据 CREAT ...

  6. mybatis关联映射一对多

    实际项目中也存在很多的一对多的情况,下面看看这个简单的例子 table.sql CREATE TABLE tb_clazz( id INT PRIMARY KEY AUTO_INCREMENT, CO ...

  7. mybatis关联映射多对多

    项目开发中,多对多关系也是非常常见的关系 在数据库中创建表的脚本 table.sql CREATE TABLE tb_user( id INT PRIMARY KEY AUTO_INCREMENT, ...

  8. MyBatis学习(六)MyBatis关联映射之一对多映射

    数据库中一对多通常使用主外键关联,外键应该在多方,即多方维护关系. 下面举一个简单实例来看看MyBatis怎么处理一对多的关系. 1.创建一个项目,导入所需jar包,导入db.properties配置 ...

  9. MyBatis(五):mybatis关联映射

    Mybatis中表与表之间的关系分为一下4类: 1)一对一 2)一对多 3)多对一 4)多对多 创建数据Demo表 数据库表: 用户表user:记录了购买商品的用户信息. 订单表orders:记录了用 ...

  10. 0050 MyBatis关联映射--一对多关系

    一对多关系更加常见,比如用户和订单,一个用户可以有多个订单 DROP TABLE IF EXISTS customer; /*用户表*/ CREATE TABLE customer( `pk` INT ...

随机推荐

  1. XML技术之SAX解析器

    1.解析XML文件有三种解析方法:DOM SAX DOM4J. 2.首先SAX解析技术只能读取XML文档中的数据信息,不能对其文档中的数据进行添加,删除,修改操作:这就是SAX解析技术的一个缺陷. 3 ...

  2. EF(Entity Framework)系统学习系列

    好久没写博客了,继续开启霸屏模式,好了,废话不多说,这次准备重新系统学一下EF,一个偶然的机会找到了一个学习EF的网站(http://www.entityframeworktutorial.net/) ...

  3. Redis简单案例(四) Session的管理

    负载均衡,这应该是一个永恒的话题,也是一个十分重要的话题.毕竟当网站成长到一定程度,访问量自然也是会跟着增长,这个时候, 一般都会对其进行负载均衡等相应的调整.现如今最常见的应该就是使用Nginx来进 ...

  4. C# 复制指定节点的所有子孙节点到新建的节点下

    XML结构: 新建一个mask_list节点,一个procedure节点,将上面的mask_list和procedure节点的所有子孙节点添加到新建的mask_list和procedure节点 Xml ...

  5. 【无私分享:ASP.NET CORE 项目实战(第十章)】发布项目到 Linux 上运行 Core 项目

    目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 ASP.Net Core 给我们带来的最大的亮点就是跨平台,我在我电脑(win7)上用虚拟机建了个 CentOS7 ,来演示下 ...

  6. 【.NET MF】.NET Micro Framework USB移植

    1.开发环境 windows 7  32位 MDK 4.54 .Net Micro Framework Porting Kit 4.2(RTM QFE2) .Net Micro Framework   ...

  7. Scala Macros - scalamela 1.x,inline-meta annotations

    在上期讨论中我们介绍了Scala Macros,它可以说是工具库编程人员不可或缺的编程手段,可以实现编译器在编译源代码时对源代码进行的修改.扩展和替换,如此可以对用户屏蔽工具库复杂的内部细节,使他们可 ...

  8. python之最强王者(7)——元组(tuple)

    1.序列(sequence): 说明:在前面的字符串列表中其实我们已经用到了序列,之所以放到这篇来讲主要是为了承上启下,方便理解和记忆. python的数据访问模型:直接存取 ,序列 ,映射 对非容器 ...

  9. [连载]《C#通讯(串口和网络)框架的设计与实现》- 10.宿主程序详细设计

    目       录 第十章           宿主程序详细设计... 2 10.1        配置文件设计... 3 10.2        加载设备驱动... 4 10.3        加载 ...

  10. Qt——组件位置随窗口变化

    当我们用Qt Designer设计界面时,有时会面临这样一个问题:需要在窗口指定位置放置组件,并且当窗口位置大小改变时,该组件相对其父对象的位置是不变的,如下面两幅图所示 ,首先看上面这幅图,注意bu ...