举例:

一个文章有一个用户,当查询文章时,显示文章信息和文章作者信息,这里不考虑一个文章多个作者的问题。

表结构:

文章表:

DROP TABLE IF EXISTS `article`;

CREATE TABLE `article` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`article_name` varchar(50) NOT NULL COMMENT '文章名',

`article_content` varchar(1000) NOT NULL COMMENT '文章内容',

`author_id` int(11) NOT NULL COMMENT '作者id',

`create_time` datetime NOT NULL COMMENT '创建时间',

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

作者表:

DROP TABLE IF EXISTS `author`;

CREATE TABLE `author` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`author_name` varchar(50) NOT NULL COMMENT '作者名',

`author_age` int(11) NOT NULL COMMENT '作者年龄',

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

文章实体:

package com.heli.core.user.model;

import java.io.Serializable;

import java.util.Date;

public class Article implements Serializable {

private Integer id;

private String articleName;

private String articleContent;

private Integer authorId;

private Date createTime;

private Author author;

public Author getAuthor() {

return author;

}

public void setAuthor(Author author) {

this.author = author;

}

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getArticleName() {

return articleName;

}

public void setArticleName(String articleName) {

this.articleName = articleName;

}

public String getArticleContent() {

return articleContent;

}

public void setArticleContent(String articleContent) {

this.articleContent = articleContent;

}

public Integer getAuthorId() {

return authorId;

}

public void setAuthorId(Integer authorId) {

this.authorId = authorId;

}

public Date getCreateTime() {

return createTime;

}

public void setCreateTime(Date createTime) {

this.createTime = createTime;

}

}

作者实体:

package com.heli.core.user.model;

import java.io.Serializable;

public class Author implements Serializable {

private Integer id;

private String authorName;

private Integer authorAge;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getAuthorName() {

return authorName;

}

public void setAuthorName(String authorName) {

this.authorName = authorName;

}

public Integer getAuthorAge() {

return authorAge;

}

public void setAuthorAge(Integer authorAge) {

this.authorAge = authorAge;

}

}

文章的映射文件ArticleMapper.xml:

mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >

id, article_name, article_content, author_id, create_time

select

from author

where id = #{authorId,jdbcType=INTEGER}

select

from article

where id = #{id,jdbcType=INTEGER}

delete from article

where id = #{id,jdbcType=INTEGER}

insert into article (article_name, article_content, author_id,

create_time)

values (#{articleName,jdbcType=VARCHAR}, #{articleContent,jdbcType=VARCHAR}, #{authorId,jdbcType=INTEGER},

#{createTime,jdbcType=TIMESTAMP})

insert into article

article_name,

article_content,

author_id,

create_time,

#{articleName,jdbcType=VARCHAR},

#{articleContent,jdbcType=VARCHAR},

#{authorId,jdbcType=INTEGER},

#{createTime,jdbcType=TIMESTAMP},

update article

article_name = #{articleName,jdbcType=VARCHAR},

article_content = #{articleContent,jdbcType=VARCHAR},

author_id = #{authorId,jdbcType=INTEGER},

create_time = #{createTime,jdbcType=TIMESTAMP},

where id = #{id,jdbcType=INTEGER}

update article

set article_name = #{articleName,jdbcType=VARCHAR},

article_content = #{articleContent,jdbcType=VARCHAR},

author_id = #{authorId,jdbcType=INTEGER},

create_time = #{createTime,jdbcType=TIMESTAMP}

where id = #{id,jdbcType=INTEGER}

这样查询文章时,就可以获取文章的作者信息了。

mysql association_mybatis association 一对一相关推荐

  1. mysql association_MyBatis association的两种形式——MyBatis学习笔记之四

    一.嵌套的resultMap 这 种方法本质上就是上篇博文介绍的方法,只是把教师实体映射从association元素中提取出来,用一个resultMap元素表示.然后 association元素再引用 ...

  2. Mybatis | Mybatis标签association一对一的使用

    Mybatis标签association一对一的使用 一.association 二.使用方法 1. 方法一: 嵌套结果映射 2. 方法二: 嵌套select 查询 三.colleciton 一对多 ...

  3. mysql association_mybatis 一对一与一对多collection和association的使用

    在mybatis如何进行一对一.一对多的多表查询呢?这里用一个简单的例子说明. 一.一对一 1.association association通常用来映射一对一的关系,例如,有个类user,对应的实体 ...

  4. mysql association_Mybatis的一对多(collection)和一对一(association)查询

    1.mybatis支持映射复杂的查询结果集 2.表之间的关系 3.实体及其对应关系: @Data @EqualsAndHashCode(callSuper= false)public class Te ...

  5. mysql association_MyBatis的association示例——MyBatis学习笔记之三

    前两篇博文介绍的都是单表映射,而实际上很多时候我们需要用到较复杂的映射.今天学会的association的用法,就是一例,现写出来和大家分享(为简洁起见,ant工程中各文件.目录的布局,以及其它与前面 ...

  6. ef mysql 外键 一对一_MySQL 外键 一对一 一对多 多对多 复制

    #外键 ## 1,学习外键首先要明白表与表之间的关系 ​ 首先要换位思考 在考虑了这边 还要考虑另一边 ​ 然后在下定论### 判断表关系的语法 #### 图书与出版社 ​ 一本书可不可以有多个出版社 ...

  7. ef mysql 外键 一对一_EFCore-一对一配置外键小记2

    前后两次遇到这样的错误: The property 'xx' on entity type 'xxxx' has a temporary value. Either set a permanent v ...

  8. mysql简单介绍一对一,一对多,多对多关系处理办法

    一对一关系示例:一个学生对应一个学生档案材料,或者每个人都有唯一的身份证编号. 一对多关系示例:一个学生只属于一个班,但是一个学院有多名学生. 多对多关系示例:一个学生可以选择多门课,一门课也有多名学 ...

  9. mysql一对一级联_MySQL 表的一对一、一对多、多对多问题

    将实体与实体的关系,反应到最终数据库表的设计上,将关系分为三种:一对一,一对多(多对一)和多对多,所有的关系都是表与表之间的关系; 一对一 一对一:一张表的一条记录只能与另外一条记录进行对应,反之亦然 ...

最新文章

  1. 推荐8个实用性十足,但是很小众的手机APP
  2. 273 Integer to English Words 整数转换英文表示
  3. Netty4服务端和客户端实现
  4. java redis 商品秒杀_redis编写lua脚本实现商品秒杀
  5. 实习一年算工作一年吗?_经过一年的努力,我如何找到软件工程工作
  6. JBPM工作流入门总结
  7. Bootstrap3 滚动监听插件的方法
  8. 《自动化技术中的进给电气传动》1.1节和1.2节读书笔记
  9. 堆排序python代码实现_八大排序算法的python实现(五)堆排序
  10. Win10 PSCAD4.5安装心路历程Mark
  11. originpro2021 下载安装(含有安装包)
  12. PayPal WebHook事件异步通知
  13. mysql数据库增左右连接_Nodejs连接MySQL数据库 增、删、改、查等操作
  14. 训练和推理阶段的BN和Dropout
  15. ##超市会员卡管理,利用我们之前的知识进行温习
  16. CF140C.New Year Snowmen(优先队列+map)
  17. 错误:类 SubClass 是公共的, 应在名为 SubClass.java 的文件中声明
  18. AWS云、阿里云、腾讯云、华为云--云服务器全球区域对比图
  19. 关于电脑磁盘满了爆红解决方法之一
  20. kettle java交互_java程序中给Kettle传参数,其实很简单

热门文章

  1. 新冠最凶变种出现!突变量德尔塔两倍,专家称感染率超原毒株500%,引发全球股市震荡...
  2. 关于KN95口罩:可以使用多久?要不要呼吸阀?怎么佩戴?
  3. 李航「机器学习」最全综述
  4. 18个让程序员流泪的瞬间,已笑喷,哈哈哈哈哈哈!
  5. 集成学习之Bagging
  6. 收藏 | 多目标跟踪(MOT)入门
  7. 自动驾驶高清地图问题的研究
  8. Task02——零基础入门NLP - 新闻文本分类之数据读取与分析
  9. Eclipse Jetty 9.4.15 发布,建议使用 JDK 12
  10. MySQL解压版配置