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 ...
随机推荐
- jQuery -原生 如何互转
今天研究源码的时候发现,不需要用get() 也能进行原生转换,使用原生方法. 原生- jQuery对象 var obj=document.xxx $(obj).css(); 也可以直接 $(doc ...
- Node.js简单操作
在node中是不支持BOM和DOM操作的,所以像alert().document.write...都是不支持的,可以是console.log() 首先我们来输出"hello world&qu ...
- GO语言下载、安装、配置
一.Go语言下载 go语言官方下载地址:https://golang.org/dl/ 找到适合你系统的版本下载,本人下载的是windows版本.也可以下载Source自己更深层次研究go语言. 二.G ...
- LinqToDB 源码分析——DataContext类
LinqToDB框架是一个轻量级的ORM框架.当然,功能上来讲一定比不上Entity Framework的强大.但是在使用上总让笔者感觉有一点Entity Framework的影子.笔者想过可能的原因 ...
- .NET跨平台之旅:将QPS 100左右的ASP.NET Core站点部署到Linux服务器上
今天下午我们将生产环境中一个单台服务器 QPS(每秒请求数)在100左右的 ASP.NET Core 站点部署到了 Linux 服务器上,这是我们解决了在 .NET Core 上使用 EnyimMem ...
- ASP.NET MVC View 和 Web API 的基本权限验证
ASP.NET MVC 5.0已经发布一段时间了,适应了一段时间,准备把原来的MVC项目重构了一遍,先把基本权限验证这块记录一下. 环境:Windows 7 Professional SP1 + Mi ...
- A chatroom for all! Part 1 - Introduction to Node.js(转发)
项目组用到了 Node.js,发现下面这篇文章不错.转发一下.原文地址:<原文>. ------------------------------------------- A chatro ...
- Navisworks 提供了.NET, COM和NwCreate 三种API
Navisworks 提供了.NET, COM和NwCreate 三种API.而通常我们说Navisworks API其实指的只是COM或.NET,因为NwCreate的功能比较特殊.待我一一道来: ...
- 如何在一个页面添加多个不同的kindeditor编辑器
kindeditor官方下载地址:http://kindeditor.net/down.php (入门必看)kindeditor官方文档:http://kindeditor.net/doc.ph ...
- 数据上下文【 DnContext】【EF基础系列7】
DBContext: As you have seen in the previous Create Entity Data Model section, EDM generates the Scho ...