跳至主要內容
一、MyBatis入门
MyBatis结构

1.1 MyBatis 的下载

​ MyBatis 可以在 Github 官网下载:[ https://github.com/mybatis/mybatis-3 ]

1.2 MyBatis 概述

1.2.1 MyBatis 简介

​ MyBatis是一个优秀的基于Java的持久层框架,它内部封装了JDBC,使开发者 只需关注SQL语句本身 ,而不用再花费精力去处理诸如注册驱动、创建 Connection 、配置 Statement 等繁杂过程。


hahg大约 13 分钟SSM框架学习MyBatis
二、单表的 CURD 操作

​ CURD 操作,即指对数据库中实体对象的 增 Create改 Update查 Read删 Delete 操作。

2.1 自定义 Dao 接口实现类

2.1.1 修改 Dao 接口

IStudentDao.java:增加 增 、改 、查 、删 这四个接口方法

public interface IStudentDao {
	// 插入
	void insertStudent(Student student);
	void insertStudentCatchId(Student student);

	// 删改
	void deleteStudentById(int id);
	void updateStudent(Student student);

	// 查询所有
	List<Student> selectAllStudents();
	Map<String, Student> selectStudentMap();

	// 查询指定学生
	Student selectStudentById(int id);
	
	// 根据姓名查询
	List<Student> selectStudentsByName(String name);

}

hahg大约 19 分钟SSM框架学习MyBatis
四、查询缓存

​ 查询缓存的使用,主要是为了 提高查询访问速度。将用户对同一数据的重复查询过程简化,不再每次均从数据库查询获取结果数据,从而提高访问速度。

​ MyBatis的查询缓存机制,根据缓存区的作用域(生命周期)可划分为两种:

  • 一级查询缓存
  • 二级查询缓存

4.1 一级查询缓存

​ MyBatis 一级查询缓存是基于 org.apache.ibatis.cache.impl.PerpetualCache 类的 HashMap 本地缓存,其作用域是 SqlSession。


hahg大约 12 分钟SSM框架学习MyBatis