Mybatis第二章——多表同时插入和级联查询

知识点一:多表同时插入,其中要插入的Blog的数据中的author_id依赖于另一个要插入的author对象的id

此时需要在mapper.xml文件中配置 useGeneratedKeys="true" keyProperty="id",只需要在被依赖的对象中配置。

insert into tb_author(username,password,email,address,phone) values(#{username},#{password},#{email},#{address},#{phone});

insert into tb_blog(title,content,type,author_id) values(#{title},#{content},#{type},#{author.id});

注意:如果不写useGeneratedKeys=”true” keyProperty=”id”,那么返回给author对象的id将为默认值’0’,

而数据库的author转换成的该条记录可以自增;

useGeneratedKeys的主键自动增长,不是数据库中的数据primary key自动增长,而是让数据库主键匹配到的对象的主键自动增长。

@org.junit.Test

public void test1(){

SqlSessionFactory sqlSessionFactory = MyBatisUtils.getSqlSessionFactory();

SqlSession sqlSession = sqlSessionFactory.openSession();

TestMapper testMapper = sqlSession.getMapper(TestMapper.class);

Author author = new Author("吴健红", "123456", "wjh@163.com", "甘肃", "18888888888");

Blog blog = new Blog("旅游", "去上海旅游", "游玩", author);

testMapper.addAuthor(author);//需要主键生成s

//testMapper.addBlog(blog); 在不使用主键自动增长的情况下,只插入author对象,这样不会报错,从而来测试author对象的id

sqlSession.commit();

System.out.println(author);

sqlSession.close();

//运行结果:Author [id=0, username=吴健红, password=123456, email=wjh@163.com, address=甘肃, phone=18888888888]

知识点二:Mybatis 实现多表查询方式

1.1 业务装配.对两个表编写单表查询语句,在业务(Service)把查询

的两个结果进行关联.

1.2 使用 Auto Mapping 特性,在实现两表联合查询时通过别名完成

映射.

1.3 使用 MyBatis 的标签进行实现.

知识点三:级联一对一的查询(N+1)

N+1 查询方式,先查询出某个表的全部信息,根据这个表的信息

查询另一个表的信息.需要使用到标签

pojo类——Author

package com.xtkj.pojo;

public class Author {

private int id;

private String username;

private String password;

private String email;

private String address;

private String phone;

public Author() {

super();

// TODO Auto-generated constructor stub

}

public Author(String username, String password, String email,

String address, String phone) {

super();

this.username = username;

this.password = password;

this.email - Domain Name For Sale | DAN.COM = email;

this.address = address;

this.phone = phone;

}

@Override

public String toString() {

return "Author [id=" + id + ", username=" + username + ", password="

+ password + ", email=" + email + ", address=" + address

+ ", phone=" + phone + "]";

}

get...

set...

}

pojo类——blog

package com.xtkj.pojo;

import java.util.ArrayList;

import java.util.List;

public class Blog {

private int id;

private String title;

private String content;

private String type;

private Author author;//author_id select * from tb_author where id = author_id;-->author

private List comments = new ArrayList();//id select * from tb_comment where blog_id=id;-->N comment

public Blog() {

super();

// TODO Auto-generated constructor stub

}

public Blog(String title, String content, String type, Author author) {

super();

this.title = title;

this.content = content;

this.type = type;

this.author = author;

}

@Override

public String toString() {

return "Blog [id=" + id + ", title=" + title + ", content=" + content

+ ", type=" + type + ", author=" + author + "]";

}

get...

set...

}

mapper.xml

select * from tb_blog where id=#{id}

select * from tb_author where id=#{id}

大前提使用 N+1 方式.时如果列名和属性名相同可

以不配置,使用 Auto mapping 特性.但是 mybatis 默认只会给列

专配一次

测试

@org.junit.Test

public void test2(){

SqlSessionFactory sqlSessionFactory = MyBatisUtils.getSqlSessionFactory();

SqlSession sqlSession = sqlSessionFactory.openSession();

TestMapper testMapper = sqlSession.getMapper(TestMapper.class);

Blog blog = testMapper.findBlogById(15);

System.out.println(blog);

//sqlSession.commit();

sqlSession.close();

//结果:Blog [id=15, title=旅游, content=去上海旅游, type=游玩, author=Author [id=17, username=吴健红, password=123456, email=wjh@163.com, address=甘肃, phone=18888888888]]

}

知识点四:级联一对多的查询(N+1方式)

mapper.xml

select * from tb_blog where id=#{id}

select * from tb_comment where blog_id=#{blog_id}

测试

@org.junit.Test

public void test3(){

SqlSessionFactory sqlSessionFactory = MyBatisUtils.getSqlSessionFactory();

SqlSession sqlSession = sqlSessionFactory.openSession();

TestMapper testMapper = sqlSession.getMapper(TestMapper.class);

Blog blog = testMapper.findBlogById(15);

System.out.println(blog);

List comments = blog.getComments();

for(int i=0;i

System.out.println("content:"+comments.get(i).getContent());

}

//sqlSession.commit();

sqlSession.close();

}

/*

结果:

Blog [id=15, title=旅游, content=去上海旅游, type=游玩, author=Author [id=17, username=吴健红, password=123456, email=wjh@163.com, address=甘肃, phone=18888888888]]

content:niho

content:lala

*/

知识点五:使用resultMap实现加载集合数据(联合查询方式)

select b.id bid,b.title btitle,b.content bcontent,b.type btype,b.author_id aid,

c.id cid,c.content ccontent from tb_blog b left join tb_comment c on b.id = c.blog_id

where b.id=#{0}

测试

@org.junit.Test

public void test4(){

SqlSessionFactory sqlSessionFactory = MyBatisUtils.getSqlSessionFactory();

SqlSession sqlSession = sqlSessionFactory.openSession();

TestMapper testMapper = sqlSession.getMapper(TestMapper.class);

Blog blog = testMapper.findBlogAndComments(15);

System.out.println(blog);

List comments = blog.getComments();

for(int i=0;i

System.out.println("content:"+comments.get(i).getContent());

}

//sqlSession.commit();

sqlSession.close();

}

/*

结果:

Blog [id=15, title=旅游, content=去上海旅游, type=游玩, author=Author [id=17, username=吴健红, password=123456, email=wjh@163.com, address=甘肃, phone=18888888888]]

content:niho

content:lala

*/

mapper同时添加数据只能添加一条_Mybatis第二章——多表同时插入和级联查询相关推荐

  1. mapper同时添加数据只能添加一条_springcloud项目搭建第二节:eureka+数据库

    在上一节搭建的项目基础上,在父项目spring-cloud的pom文件中添加mapper启动器和mysql驱动的配置,如果项目中使用lombok也可以引用,这里需要注意的是lombok引用的配置不在d ...

  2. 《数据密集型计算和模型》第二章大数据时代的计算机体系结构复习

    <数据密集型计算和模型>第二章的有关内容.主要复习内容为:计算部件.存储部件.网络部件.软件定义部件.虚拟资源管理系统等. 文章目录 大数据时代的计算机体系结构 一.计算部件 1. 多核和 ...

  3. mysql删除新添加数据,MySQL添加、更新与删除数据

    添加.更新与删除数据 添加数据 为表中所有字段添加数据 INSERT INTO 表名(字段名1,字段名2,--) VALUES(值1,值2,--); insert into 表名 values(值1, ...

  4. thinkphp中mysql添加数据_thinkphp添加数据 add()方法

    thinkphpz内置的add()方法用于向数据库表添加数据,相当于SQL中的INSERT INTO 行为 添加数据 add 方法是 CURD(Create,Update,Read,Delete / ...

  5. 工程数据计算机处理的方法有,第二章 CADCAM技术基础-工程数据的计算机处理2011.ppt...

    文档介绍: CAD/CAM技术基础 CAD/CAM Technology Base 主讲人:XXX E-mail:XXX 第二章 工程数据的计算机处理 Computer Processing of E ...

  6. python加数据库_python向数据库添加数据(添加一条数据)

    原博文 2020-03-18 22:44 − 前置准备条件 1.cmd命令 下载第三方模块 2.连接数据库 3.创建数据库和表 在做一下操作 源码: #引入模块import pymysql# 链接数据 ...

  7. excel数据技巧:6条最实用的透视表偏方

    函数学得少,所以就把劲往数据透视表上使.数据透视表也没辜负人,总有一些小东西可以解决统计上的大问题.这里的6条偏方就是这样的.所谓偏方,就是指平时少见,但是对于特定情况有特效的方子.我们今天跟大家分享 ...

  8. 大数据技术原理与应用(第二章 大数据处理架构Hadoop)

    目录 2.1Hadoop简介 HDFS(分布式文件系统) MapReduce(分布式并行编程框架) Hadoop的特点 Hadoop的应用 ​编辑Hadoop版本的变化 2.2Hadoop项目结构 T ...

  9. python向mysql中添加数据标签_用python在MySQL中写入数据和添加数据

    在笔者之前的博文中,已介绍了用python连接与mysql数据库的知识.包括如何安装python连接mysql的pymysql包,如何通过cusor语句将python与mysql连接起来,以及如何用p ...

最新文章

  1. 资源 | Hinton、LeCun、吴恩达......不容错过的15大机器学习课程都在这儿了
  2. 都在喂大规模互联网文本,有人把著名的 C4 语料库“读”透了
  3. 全国信息化计算机应用技术水平教育考试,全国信息化计算机应用技术水平教育考试试卷.doc...
  4. 信管师培训之第十二节课作业(外包管理+需求管理+组织级与大项目管理)
  5. php 获取 uri,获取URI地址
  6. 2017前端资源汇总
  7. frame或者iframe的contentwindow属性
  8. faster rcnn resnet_Faster-rcnn 代码详解
  9. A. Gamer Hemose
  10. react 组件怎么公用_用 react 做一个跟随组件的 tooltip
  11. 一起学《Troubleshooting Oracle Performance》吧
  12. c语言 除法小数点怎么写,高精度除法小数点位数
  13. 微信SDK配置wx.config报invalid signature签名错误
  14. python算法题_Python算法练习题:硬币数量
  15. android 工程师级别划分及学习路线
  16. Practical_RichFaces要点Chapter11
  17. K_均值聚类算法(算法设计与C代码实现)
  18. Thinkpad 笔记本散热风扇声音大解决办法
  19. 华为云教程(云容器引擎CCE)
  20. 记前谷歌台湾区总经理– 张成秀

热门文章

  1. C语言的除法与取余运算
  2. 在Xcode中使用C++与Objective-C混编
  3. for 循环的流程图等价形式
  4. 下拉刷新、上拉获取更多、左右滑动的ListView
  5. 相亲与恋爱套路不一样:相亲有哪些需要注意的?
  6. 什么是CV?CV和resume有什么区别?
  7. 广州Java培训,疯狂Java如何?谈谈疯狂Java,谈谈李刚
  8. JavaScript 获取一元素的所有子元素
  9. 线性代数基本定理(核空间与行空间)——The Fundamental Theorem of Linear Algebra
  10. c语言小程序作业,c语言小程序(c语言简单小程序代码)