0%

MyBatis使用和配置详解

2018年2月25日 下午6:57

项目结构

MyBatis的重要依赖包

  1. myBatis-3.2.3.jar
  2. Mysql-connector-java-5.1.34.jar
    配置文件(xml)
  3. TeacherMapper.xml
  4. StudentMapper.xml
  5. Reource.xml

配置文件详解:

TeacherMapper.xml


解析:

  1. 本质:将sql语句与代码进行分离,方便管理
  2. 绿色:是命名空间,是找到TeacherMapper.xml的关键标识,对应于测试代码TeacherEntity teacherEntity=sqlSession.selectOne("teacher.selectTeacherOne", "804”);中的teacher
  3. 红色:是一个对应于*Entity.java实体的xml表示
  4. 红色

source.xml


解析:

  1. 红色:是连接数据库的各种配置信息,用于登录数据库
  2. 绿色:是用于声明项目中实体类对应的xml配置文件有哪些

实体类

本质就是对应于数据库表字段的get,set方法

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
package com.pan.teacher;

import java.io.Serializable;
import java.util.List;

import com.pan.test.StudentEntity;

public class TeacherEntity implements Serializable{

/**
*
*/
private static final long serialVersionUID = 1L;
private String t_id;
private String t_name;
private String t_sex;
private String t_birthday;
private String prof;
private String depart;
private List<StudentEntity> studentList;
public String getT_id() {
return t_id;
}
public void setT_id(String t_id) {
this.t_id = t_id;
}
public String getT_name() {
return t_name;
}
public void setT_name(String t_name) {
this.t_name = t_name;
}
public String getT_sex() {
return t_sex;
}
public void setT_sex(String t_sex) {
this.t_sex = t_sex;
}
public String getT_birthday() {
return t_birthday;
}
public void setT_birthday(String t_birthday) {
this.t_birthday = t_birthday;
}
public String getProf() {
return prof;
}
public void setProf(String prof) {
this.prof = prof;
}
public String getDepart() {
return depart;
}
public void setDepart(String depart) {
this.depart = depart;
}
public List<StudentEntity> getStudentList() {
return studentList;
}
public void setStudentList(List<StudentEntity> studentList) {
this.studentList = studentList;
}

}

测试

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
package com.pan.teacher;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.pan.test.StudentEntity;

public class TeacherTest {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub

//读取myBatis的核心配置文件
Reader reader=Resources.getResourceAsReader("reource.xml");
//创建二级缓存
SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader);
//创建一级缓存
SqlSession sqlSession=sessionFactory.openSession();

//-------------------级联查询----------------
//注意:在myBatis中只有级联查询,没有级联增删改
TeacherEntity teacherEntity=sqlSession.selectOne("teacher.selectTeacherOne", "804");
System.out.println(teacherEntity.getT_name());
List<StudentEntity> list=teacherEntity.getStudentList();
System.out.println(list.size());
}

}