mybatis关联映射
多对一:
<!-- 方法一: -->
<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关联映射的更多相关文章
- MyBatis学习(七)MyBatis关联映射之多对多映射
对于数据库中的多对多关系建议使用一个中间表来维护关系. 1.创建四张表,分别为用户表,商品表,订单表,中间表. DROP TABLE IF EXISTS `t_user`; CREATE TABLE ...
- spring boot(9)-mybatis关联映射
一对多 查询type表的某一条数据,并且要同时查出所有typeid与之配置的user,最终要得到一个以下类型的Type对象 public class Type { String id; String ...
- 0049 MyBatis关联映射--一对一关系
世上的事务总不是孤立存在的,表现在Java类里面,则是类与类之间的关系,比如继承is-a.依赖use-a.关联has-a,反映在数据库中,则是表与表之间的关系,比如外键 关联关系存在着以下几种类型:一 ...
- Spring Boot (11) mybatis 关联映射
一对多 查询category中的某一条数据,同时查询该分类下的所有Product. Category.java public class Category { private Integer id; ...
- mybatis关联映射一对一
在项目开发中,会存在一对一的关系,比如一个人只有一个身份证,一个身份证只能给一个人使用,这就是一对一关系.一对一关系使用主外键关联. table.sql,在数据库中创建如下两个表并插入数据 CREAT ...
- mybatis关联映射一对多
实际项目中也存在很多的一对多的情况,下面看看这个简单的例子 table.sql CREATE TABLE tb_clazz( id INT PRIMARY KEY AUTO_INCREMENT, CO ...
- mybatis关联映射多对多
项目开发中,多对多关系也是非常常见的关系 在数据库中创建表的脚本 table.sql CREATE TABLE tb_user( id INT PRIMARY KEY AUTO_INCREMENT, ...
- MyBatis学习(六)MyBatis关联映射之一对多映射
数据库中一对多通常使用主外键关联,外键应该在多方,即多方维护关系. 下面举一个简单实例来看看MyBatis怎么处理一对多的关系. 1.创建一个项目,导入所需jar包,导入db.properties配置 ...
- MyBatis(五):mybatis关联映射
Mybatis中表与表之间的关系分为一下4类: 1)一对一 2)一对多 3)多对一 4)多对多 创建数据Demo表 数据库表: 用户表user:记录了购买商品的用户信息. 订单表orders:记录了用 ...
- 0050 MyBatis关联映射--一对多关系
一对多关系更加常见,比如用户和订单,一个用户可以有多个订单 DROP TABLE IF EXISTS customer; /*用户表*/ CREATE TABLE customer( `pk` INT ...
随机推荐
- 数据库日常维护-CheckList_03有关数据库数据文件大小检查
日常数据维护中容量规划是每个DBA的基础工作之一,也是非常重要的工作.在生产环境中一些比较重要的业务数据库会用前期容量规划不足,或出现意外的爆发式的数据增长,直至盛满整个磁盘空间,系统会无法使用,最终 ...
- 读书笔记--SQL必知必会13--创建高级联结
13.1 使用表别名 SQL可以对列名.计算字段和表名起别名. 缩短SQL语句 允许在一条SELECT语句中多次使用相同的表. 注意:表别名只在查询执行中使用,不返回到客户端. MariaDB [sq ...
- 网站实现微信登录之回调函数中登录逻辑的处理--基于yii2开发的描述
上一篇文章网站实现微信登录之嵌入二维码中描述了如何在自己的登录页面内嵌入登录二维码,今天的这篇文章主要是描述下在扫码成功之后微信重定向回网站后登录逻辑的处理,其实也就是验证身份信息,授权用户登录的逻辑 ...
- LinqToDB 源码分析——生成表达式树
当我们知道了Linq查询要用到的数据库信息之后.接下就是生成对应的表达式树.在前面的章节里面笔者就已经介绍过.生成表达式树是事实离不开IQueryable<T>接口.而处理表达式树离不开I ...
- Cesium原理篇:5最长的一帧之影像
如果把地球比做一个人,地形就相当于这个人的骨骼,而影像就相当于这个人的外表了.之前的几个系列,我们全面的介绍了Cesium的地形内容,详见: Cesium原理篇:1最长的一帧之渲染调度 Cesium原 ...
- 使用C#,轻松发邮件之QQ邮箱
参考来源 http://www.cnblogs.com/youring2/archive/2008/11/29/1343911.html
- 学习EF之贪懒加载和延迟加载(2)
通过昨天对EF贪婪加载和延迟加载的学习,不难发现,延迟加载还是很好用的,但是问题也就来了,有的时候我们只需要加载一个实体,不需要和他相关的外部实体,这时候我们来看看EF延迟加载时怎么作用的吧 打开pr ...
- js第三方
1.0 https://github.com/aui/artDialog 2.0 前端开发仓库 http://code.ciaoca.com/ 3.0 打赏 https://github.com/gr ...
- 配置IIS的通配符应用程序映射
使用IIS 6架设网站,如果要使用伪静态的功能,可能需要设置“通配符应用程序映射(执行顺序)”. 在Windows Server 2012 r2 的IIS 8中,对应的是添加设置“通配符脚本映射”,参 ...
- JavaWeb_day03_员工信息添加修改删除
day03员工的添加,修改,删除 修改功能 思路 : 点击修改员工数据之后,跳转到单行文本,查询要修改的员工id的全部信息,主键id设置为readonly,其余的都可以修改, 修改之后,提交按钮,提交 ...