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 ...
随机推荐
- J2EE 项目读写分离
先回答下 1.为啥要读写分离? 大家都知道最初开始,一个项目对应一个数据库,基本是一对一的,但是由于后来用户及数据还有访问的急剧增多, 系统在数据的读写上出现了瓶颈,为了让提高效率,想读和写不相互影响 ...
- 多线程同步工具——volatile变量
关于volatile,找了一堆资料看,看完后想找一个方法去做测试,测了很久,感觉跟没有一样. 这本书<深入理解Java内存模型>,对volatile描述中有这样一个比喻的说法,如下代码所示 ...
- 纯WebApi,不包含MVC Demo
1.创建项目 只是单纯的使用Web API的功能,而不需要使用的MVC,这个时候就该抛开MVC来新建项目了. 首先,新建一个Asp.Net空应用程序,在程序集中添加引用System.Web.Http和 ...
- SSRS ----环境配置,没有 ReportServer DB 怎么办?
今天项目进入报表开发阶段,按照习惯,打开报表管理器,发现提示下面的错误: 错误:报表服务器无法打开与报表服务器数据库的连接.所有请求和处理都要求与数据库建立连接. 这是怎么回事儿呢,经过排查,发现数据 ...
- 微信小程序的应用及信息整合,都放到这里了
微信小程序终于开始公测了,这篇文章也终于可以发布了. 这篇文章可以说是微信小程序系列三部曲最后一篇.8 月份,小程序推出前,我写了<别开发 app 了>详细阐述了为什么创业应该放弃原生 a ...
- 在DevExpress程序中使用SplashScreenManager控件实现启动闪屏和等待信息窗口
在我很早的WInform随笔<WinForm界面开发之"SplashScreen控件">有介绍如何使用闪屏的处理操作,不过那种是普通WInform和DevExpress ...
- SQL SERVER 竖表变成横表
现有数据如下: Sql: select a.MODELID, max( case a.PNAME when'计划开始' then a.PVALUE end) as RStart, max( case ...
- C#开发微信门户及应用(34)--微信裂变红包
在上篇随笔<C#开发微信门户及应用(33)--微信现金红包的封装及使用>介绍了普通现金红包的封装和使用,这种红包只能单独一次发给一个人,用户获取了红包就完成了,如果我们让用户收到红包后,可 ...
- 【Java每日一题】20161228
package Dec2016; import java.util.ArrayList; import java.util.List; public class Ques1228 { public s ...
- nginx添加 nginx_heath模块
原因?为什么会使用nginx_heath 这个模块,主要是如nginx+tomcat部署的时,tomcat挂了之后nginx->upstream 轮询是可以踢掉挂掉的tomcat服务的,如果部署 ...