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
*/