MyBatis 高级
一. MyBatis 多表操作
- 创建之后用到的表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58create database db4;
use db4;
create table card(
id int primary key auto_increment,
number varchar(20),
pid int,
constraint cp_fk foreign key (pid) references person(id)
);
insert into card values (null,'12345',1);
insert into card values (null,'23456',2);
insert into card values (null,'34567',3);
==============================================================
create table person(
id int primary key auto_increment,
name varchar(20),
age int
);
insert into person values (null,'张三',23);
insert into person values (null,'李四',24);
insert into person values (null,'王五',25);
==============================================================
create table classes(
id int primary key auto_increment,
name varchar(20)
);
insert into classes values (null,'黑马一班');
insert into classes values (null,'黑马二班');
==============================================================
create table student(
id int primary key auto_increment,
name varchar(30),
age int,
cid int,
constraint cs_fk foreign key (cid) references classes(id)
);
insert into student values (null,'张三',23,1);
insert into student values (null,'李四',24,1);
insert into student values (null,'王五',25,2);
insert into student values (null,'马六',26,2);
==============================================================
create table course(
id int primary key auto_increment,
name varchar(30)
);
insert into course values (null,'语文');
insert into course values (null,'数学');
==============================================================
create table stu_cr(
id int primary key auto_increment,
sid int,
cid int,
constraint sc_fk1 foreign key (sid) references student(id),
constraint sc_fk2 foreign key (cid) references course(id)
);
insert into stu_cr values (null,1,1),(null,1,2),(null,2,1),(null,2,2);
获取sqlsession会话对象工具类
- SqlsessionUtils
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15public classSessionUtils {
public staticSqlSession getSqlsession(){
SqlSession sqlSession =null;
try{
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory factory =newSqlSessionFactoryBuilder().build(is);
is.close();
sqlSession = factory.openSession(true);
}catch(IOException e) {
e.printStackTrace();
}finally{
returnsqlSession;
}
}
}
1.一对一
表:
1 | create table card( |
pojo类
- Person
1
2
3privateInteger id;
privateString name;
privateInteger age; - Card
1
2
3
4privateInteger id;
privateString number;
privatePerson p;
interface接口:
1 | public interface OtoMapper { |
mapper映射:
1 | <mapper namespace="com.itheima.mapper.OtoMapper"> |
Test类:
1 | //一对一 |
查询结果:
Card{id=1, number=’123456’, p=Person{id=1, name=’张三’, age=23}}
Card{id=2, number=’234567’, p=Person{id=2, name=’李四’, age=24}}
Card{id=3, number=’345678 ‘, p=Person{id=3, name=’王五 ‘, age=25}}
2.一对多
表:
1 | create table classes( |
pojo类:
- Classes
1
2
3
4privateInteger id;
privateString name;
privateList<Student> students; - Student
1
2
3
4
5privateInteger id;
privateString name;
privateInteger age;
privateList<Course> courses;//多对多时增加一个属性
interface接口:
1 | public interfaceOtmMapper { |
mapper映射:
1 | <mapper namespace="com.itheima.mapper.OtmMapper"> |
Test类:
1 | //一对多 |
执行结果:
1–黑马一班
Student{id=1, name=’张三’, age=23}
Student{id=2, name=’李四’, age=24}
================
2–黑马二班
Student{id=3, name=’王五’, age=25}
Student{id=4, name=’马六’, age=26}
================
3.多对多
表:
1 | create table student( |
pojo类:
- Student
1
2
3
4
5privateInteger id;
privateString name;
privateInteger age;
privateList<Course> courses;//多对多时增加一个属性 - Course
1
2privateInteger id;
privateString name;
interface接口:
1 | public interfaceMtmMapper { |
mapper映射:
1 | <mapper namespace="com.itheima.mapper.MtmMapper"> |
Test类:
1 | //多对多 |
运行结果:
1–张三–23
Course{id=1, name=’语文’}
Course{id=2, name=’数学’}
================
2–李四–24
Course{id=1, name=’语文’}
Course{id=2, name=’数学’}
4.小结
注意:本多表操作以查询作为演示,表中的外键字段并没有在实体类中体现。
二. MyBatis 注解实现多表操作
1.一对一
- pojo类 Card Person
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49public classCard {
privateInteger id;
privateString number;
privatePerson p;
publicCard() {
}
publicCard(Integer id, String number, Person p) {
this.id = id;
this.number = number;
this.p = p;
}
publicInteger getId() {
returnid;
}
public voidsetId(Integer id) {
this.id = id;
}
publicString getNumber() {
returnnumber;
}
public voidsetNumber(String number) {
this.number = number;
}
publicPerson getP() {
returnp;
}
public voidsetP(Person p) {
this.p = p;
}
publicString toString() {
return"Card{" +
"id=" + id +
", number='" + number + '\'' +
", p=" + p +
'}';
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48public classPerson {
privateInteger id;
privateString name;
privateInteger age;
publicPerson() {
}
publicPerson(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
publicInteger getId() {
returnid;
}
public voidsetId(Integer id) {
this.id = id;
}
publicString getName() {
returnname;
}
public voidsetName(String name) {
this.name = name;
}
publicInteger getAge() {
returnage;
}
public voidsetAge(Integer age) {
this.age = age;
}
publicString toString() {
return"Person{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
interface接口:
1 | public interface CardMapper { |
mapper映射文件(省略)
Test类:
1 | //注解一对一查询所有 |
查询结果:
Card{id=1, number=’12345’, p=Person{id=1, name=’张三’, age=23}}
Card{id=2, number=’23456’, p=Person{id=2, name=’李四’, age=24}}
Card{id=3, number=’34567’, p=Person{id=3, name=’王五’, age=25}}
2.一对多
- pojo类 Classes Student
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48public classClasses {
privateInteger id;
privateString name;
privateList<Student> students;
publicClasses() {
}
publicClasses(Integer id, String name, List<Student> students) {
this.id = id;
this.name = name;
this.students = students;
}
publicInteger getId() {
returnid;
}
public voidsetId(Integer id) {
this.id = id;
}
publicString getName() {
returnname;
}
public voidsetName(String name) {
this.name = name;
}
publicList<Student> getStudents() {
returnstudents;
}
public voidsetStudents(List<Student> students) {
this.students = students;
}
publicString toString() {
return"Classes{" +
"id=" + id +
", name='" + name + '\'' +
", students=" + students +
'}';
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65public classStudent {
privateInteger id;
privateString name;
privateInteger age;
//private List<Course> courses; //多对多时增加一个属性
publicStudent() {
}
publicStudent(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
/*public Student(Integer id, String name, Integer age, List<Course> courses) {
this.id = id;
this.name = name;
this.age = age;
this.courses = courses;
}*/
/*public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
}*/
publicInteger getId() {
returnid;
}
public voidsetId(Integer id) {
this.id = id;
}
publicString getName() {
returnname;
}
public voidsetName(String name) {
this.name = name;
}
publicInteger getAge() {
returnage;
}
public voidsetAge(Integer age) {
this.age = age;
}
publicString toString() {
return"Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
interface接口:
1 | public interfaceClassesMapper { |
mapper映射文件(省略)
Test类:
1 | //注解一对多查询所有 |
查询结果:
1–黑马一班
Student{id=1, name=’张三’, age=23}
Student{id=2, name=’李四’, age=24}
2–黑马二班
Student{id=3, name=’王五’, age=25}
Student{id=4, name=’马六’, age=26}
3.多对多
- pojo类 Course Student
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37public classCourse {
privateInteger id;
privateString name;
publicCourse() {
}
publicCourse(Integer id, String name) {
this.id = id;
this.name = name;
}
publicInteger getId() {
returnid;
}
public voidsetId(Integer id) {
this.id = id;
}
publicString getName() {
returnname;
}
public voidsetName(String name) {
this.name = name;
}
publicString toString() {
return"Course{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65public classStudent {
privateInteger id;
privateString name;
privateInteger age;
privateList<Course> courses;//多对多时增加一个属性
publicStudent() {
}
publicStudent(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
/*public Student(Integer id, String name, Integer age, List<Course> courses) {
this.id = id;
this.name = name;
this.age = age;
this.courses = courses;
}*/
/*public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
}*/
publicInteger getId() {
returnid;
}
public voidsetId(Integer id) {
this.id = id;
}
publicString getName() {
returnname;
}
public voidsetName(String name) {
this.name = name;
}
publicInteger getAge() {
returnage;
}
public voidsetAge(Integer age) {
this.age = age;
}
publicString toString() {
return"Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
interface接口:
1 | public interface MtmStudentMapper { |
mapper映射文件(省略)
Test类:
1 | //注解多对多查询所有 |
查询结果:
1--张三--2
Course{id=1, name='语文'}
Course{id=2, name='数学'}
2--李四--24
Course{id=1, name='语文'}
Course{id=2, name='数学'}
4.小结
注意:本多表操作以查询作为演示,表中的外键字段并没有在实体类中体现。
三. 构建SQL
1.基本介绍
2.代码示例:
- pojo类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48public classStudent {
privateInteger id;
privateString name;
privateInteger age;
publicStudent() {
}
publicStudent(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
publicInteger getId() {
returnid;
}
public voidsetId(Integer id) {
this.id = id;
}
publicString getName() {
returnname;
}
public voidsetName(String name) {
this.name = name;
}
publicInteger getAge() {
returnage;
}
public voidsetAge(Integer age) {
this.age = age;
}
publicString toString() {
return"Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
SqlReturn类:
1 | public classSqlReturn { |
接口StudentMapper类:
1 | public interfaceStudentMapper { |