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", "[email protected]", "甘肃", "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 protected], 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 = 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
大前提使用 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 protected], address=甘肃, phone=18888888888]]
}
知识点四:级联一对多的查询(N+1方式)
mapper.xml
测试
@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
知识点五:使用resultMap实现加载集合数据(联合查询方式)
测试
@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