- MyBatis-学习笔记01【01.Mybatis课程介绍及环境搭建】【day01】
- MyBatis-学习笔记02【02.Mybatis入门案例】
- MyBatis-学习笔记03【03.自定义Mybatis框架】
- MyBatis-学习笔记04【04.自定义Mybatis框架基于注解开发】【day02】
- MyBatis-学习笔记05【05.使用Mybatis完成CRUD】
- MyBatis-学习笔记06【06.使用Mybatis完成DAO层的开发】
- MyBatis-学习笔记07【07.Mybatis的连接池及事务】【day03】
- MyBatis-学习笔记08【08.动态SQL】
- MyBatis-学习笔记09【09.Mybatis的多表操作】
- MyBatis-学习笔记10【10.JNDI扩展知识】
- MyBatis-学习笔记11【11.Mybatis的缓存】【day04】
- MyBatis-学习笔记12【12.Mybatis注解开发】
目录
3 自定义mybatis的编码-根据测试类中缺少的创建接口和类
class SqlSessionFactoryBuilder
5 自定义Mybatis的编码-创建两个默认实现类并分析类之间的关系
6 自定义Mybatis的编码-实现基于XML的查询所有操作
mybatis框架 共四天
第一天:mybatis入门
mybatis的概述
mybatis的环境搭建
mybatis入门案例
自定义mybatis框架(主要的目的是为了让大家了解mybatis中执行细节)
第二天:mybatis基本使用
mybatis的单表crud操作
mybatis的参数和返回值
mybatis的dao编写
mybatis配置的细节
几个标签的使用
第三天:mybatis的深入和多表
mybatis的连接池
mybatis的事务控制及设计的方法
mybatis的多表查询
一对多(多对一)
多对多
第四天:mybatis的缓存和注解开发
mybatis中的加载时机(查询的时机)
mybatis中的一级缓存和二级缓存
mybatis的注解开发
单表CRUD
多表查询
-----------------------------------------------------------
1、什么是框架?
它是我们软件开发中的一套解决方案,不同的框架解决的是不同的问题。
使用框架的好处:框架封装了很多的细节,使开发者可以使用极简的方式实现功能。大大提高开发效率。
2、三层架构
表现层:是用于展示数据的
业务层:是处理业务需求
持久层:是和数据库交互的
3、持久层技术解决方案
JDBC技术:
Connection
PreparedStatement
ResultSet
Spring的JdbcTemplate:
Spring中对jdbc的简单封装
Apache的DBUtils:
它和Spring的JdbcTemplate很像,也是对Jdbc的简单封装
以上这些都不是框架
JDBC是规范
Spring的JdbcTemplate和Apache的DBUtils都只是工具类
4、mybatis的概述
mybatis是一个持久层框架,用java编写的。
它封装了jdbc操作的很多细节,使开发者只需要关注sql语句本身,而无需关注注册驱动,创建连接等繁杂过程
它使用了ORM思想实现了结果集的封装。
ORM:
Object Relational Mappging 对象关系映射
简单的说:
就是把数据库表和实体类及实体类的属性对应起来
让我们可以操作实体类就实现操作数据库表。
user User
id userId
user_name userName
今天我们需要做到:实体类中的属性和数据库表的字段名称保持一致。
user数据表 User实体类
id id
user_name user_name
5、mybatis的入门
mybatis的环境搭建
第一步:创建maven工程并导入坐标
第二步:创建实体类和dao的接口
第三步:创建Mybatis的主配置文件 SqlMapConifg.xml
第四步:创建映射配置文件 IUserDao.xml
环境搭建的注意事项:
第一个:创建IUserDao.xml 和 IUserDao.java时,名称是为了和我们之前的知识保持一致。
在Mybatis中它把持久层的操作接口名称和映射文件也叫做:Mapper
所以:IUserDao 和 IUserMapper是一样的。(Mapper==Dao)
第二个:在idea中创建目录的时候,它和包是不一样的
包在创建时:com.itheima.dao是三级结构
目录在创建时:com.itheima.dao是一级目录
第三个:mybatis的映射配置文件位置必须和dao接口的包结构相同
第四个:映射配置文件的mapper标签namespace属性的取值必须是dao接口的全限定类名
第五个:映射配置文件的操作配置(select),id属性的取值必须是dao接口的方法名
好处:当我们遵从了第三、四、五点之后,我们在开发中就无须再写dao的实现类。
mybatis的入门案例
第一步:读取配置文件
第二步:创建SqlSessionFactory工厂
第三步:创建SqlSession
第四步:创建Dao接口的代理对象
第五步:执行dao中的方法
第六步:释放资源
注意事项:
不要忘记在映射配置中告知mybatis要封装到哪个实体类中
配置的方式:指定实体类的全限定类名 resultType="com.itheima.domain.User"
mybatis基于注解的入门案例:
把IUserDao.xml移除,在dao接口的方法上使用@Select注解,并且指定SQL语句
同时需要在SqlMapConfig.xml中的mapper配置时,使用class属性指定dao接口的全限定类名。
明确(说明)-mybatis可写dao实现类:
我们在实际开发中,都是越简便越好,所以都是采用不写dao实现类的方式,不管使用XML配置还是使用注解配置。
但是Mybatis它是支持写dao实现类的。
6、自定义Mybatis的分析:
mybatis在使用代理dao的方式实现增删改查时做什么事呢?
只有两件事:
第一:创建代理对象;
第二:在代理对象中调用selectList。
自定义mybatis能通过入门案例看到类
class Resources
class SqlSessionFactoryBuilder
interface SqlSessionFactory
interface SqlSession
1 自定义Mybatis的分析-执行查询所有分析
6、自定义Mybatis的分析:
mybatis在使用代理dao的方式实现增删改查时做什么事呢?
只有两件事:
第一:创建代理对象
第二:在代理对象中调用selectList
用什么语句执行,封装到哪里去?
读流,解析配置文件。
(E) 强转类型
![]()
查询所有的分析
2 自定义Mybatis的分析-创建代理对象的分析
![]()
自定义mybatis分析
3 自定义mybatis的编码-根据测试类中缺少的创建接口和类
6、自定义Mybatis的分析:
自定义mybatis能通过入门案例看到类
class Resources
class SqlSessionFactoryBuilder
interface SqlSessionFactory
interface SqlSession
![]()
自定义实现类的好处:实现灵活控制(每多一个方法,就可以多一些功能)!
class Resources
package com.itheima.mybatis.io;
import java.io.InputStream;
/**
* 使用类加载器读取配置文件的类
*/
public class Resources {
/**
* 根据传入的参数,获取一个字节输入流
*
* @param filePath
* @return
*/
public static InputStream getResourceAsStream(String filePath) {
//Resources.class得到当前类的字节码;getClassLoader()获取字节码的类加载器;
//getResourceAsStream根据类加载器读取配置
return Resources.class.getClassLoader().getResourceAsStream(filePath);
}
}
class SqlSessionFactoryBuilder
package com.itheima.mybatis.sqlsession;
import com.itheima.mybatis.cfg.Configuration;
import com.itheima.mybatis.sqlsession.defaults.DefaultSqlSessionFactory;
import com.itheima.mybatis.utils.XMLConfigBuilder;
import java.io.InputStream;
/**
* 用于创建一个SqlSessionFactory对象
*/
public class SqlSessionFactoryBuilder {
/**
* 根据参数的字节输入流来构建一个SqlSessionFactory工厂
*
* @param config
* @return
*/
public SqlSessionFactory build(InputStream config) {
return null;
}
}
interface SqlSessionFactory
package com.itheima.mybatis.sqlsession;
public interface SqlSessionFactory {
/**
* 用于打开一个新的SqlSession对象
*
* @return
*/
SqlSession openSession();
}
interface SqlSession
package com.itheima.mybatis.sqlsession;
/**
* 自定义Mybatis中和数据库交互的核心类
* 它里面可以创建dao接口的代理对象
*/
public interface SqlSession {
/**
* 根据参数创建一个代理对象
*
* @param daoInterfaceClass dao的接口字节码
* @param <T>
* @return
*/
<T> T getMapper(Class<T> daoInterfaceClass);
/**
* 释放资源
*/
void close();
}
4 自定义mybatis的编码-解析XML的工具类介绍
导入dom4j的坐标
Configuration.java
Mapper
XMLConfigBuilder.java
package com.itheima.mybatis.utils;
//import com.itheima.mybatis.annotations.Select;
import com.itheima.mybatis.annotations.Select;
import com.itheima.mybatis.cfg.Configuration;
import com.itheima.mybatis.cfg.Mapper;
import com.itheima.mybatis.io.Resources;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 用于解析配置文件
*/
public class XMLConfigBuilder {
/**
* 解析主配置文件,把里面的内容填充到DefaultSqlSession所需要的地方
* 使用的技术:
* dom4j+xpath
*/
public static Configuration loadConfiguration(InputStream config) {
try {
//定义封装连接信息的配置对象(mybatis的配置对象)
Configuration cfg = new Configuration();
//1.获取SAXReader对象
SAXReader reader = new SAXReader();
//2.根据字节输入流获取Document对象
Document document = reader.read(config);
//3.获取根节点
Element root = document.getRootElement();
//4.使用xpath中选择指定节点的方式,获取所有property节点
List<Element> propertyElements = root.selectNodes("//property");
//5.遍历节点
for (Element propertyElement : propertyElements) {
//判断节点是连接数据库的哪部分信息
//取出name属性的值
String name = propertyElement.attributeValue("name");
if ("driver".equals(name)) {
//表示驱动
//获取property标签value属性的值
String driver = propertyElement.attributeValue("value");
cfg.setDriver(driver);
}
if ("url".equals(name)) {
//表示连接字符串
//获取property标签value属性的值
String url = propertyElement.attributeValue("value");
cfg.setUrl(url);
}
if ("username".equals(name)) {
//表示用户名
//获取property标签value属性的值
String username = propertyElement.attributeValue("value");
cfg.setUsername(username);
}
if ("password".equals(name)) {
//表示密码
//获取property标签value属性的值
String password = propertyElement.attributeValue("value");
cfg.setPassword(password);
}
}
//取出mappers中的所有mapper标签,判断他们使用了resource还是class属性
List<Element> mapperElements = root.selectNodes("//mappers/mapper");
//遍历集合
for (Element mapperElement : mapperElements) {
//判断mapperElement使用的是哪个属性
Attribute attribute = mapperElement.attribute("resource");
if (attribute != null) {
System.out.println("使用的是XML");
//表示有resource属性,用的是XML
//取出属性的值
String mapperPath = attribute.getValue();//获取属性的值"com/itheima/dao/IUserDao.xml"
//把映射配置文件的内容获取出来,封装成一个map
Map<String, Mapper> mappers = loadMapperConfiguration(mapperPath);
//给configuration中的mappers赋值
cfg.setMappers(mappers);
} else {
System.out.println("使用的是注解");
//表示没有resource属性,用的是注解
//获取class属性的值
String daoClassPath = mapperElement.attributeValue("class");
//根据daoClassPath获取封装的必要信息
Map<String, Mapper> mappers = loadMapperAnnotation(daoClassPath);
//给configuration中的mappers赋值
cfg.setMappers(mappers);
}
}
//返回Configuration
return cfg;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
config.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 根据传入的参数,解析XML,并且封装到Map中
*
* @param mapperPath 映射配置文件的位置
* @return map中包含了获取的唯一标识(key是由dao的全限定类名和方法名组成)
* 以及执行所需的必要信息(value是一个Mapper对象,里面存放的是执行的SQL语句和要封装的实体类全限定类名)
*/
private static Map<String, Mapper> loadMapperConfiguration(String mapperPath) throws IOException {
InputStream in = null;
try {
//定义返回值对象
Map<String, Mapper> mappers = new HashMap<String, Mapper>();
//1.根据路径获取字节输入流
in = Resources.getResourceAsStream(mapperPath);
//2.根据字节输入流获取Document对象
SAXReader reader = new SAXReader();
Document document = reader.read(in);
//3.获取根节点
Element root = document.getRootElement();
//4.获取根节点的namespace属性取值
String namespace = root.attributeValue("namespace");//是组成map中key的部分
//5.获取所有的select节点
List<Element> selectElements = root.selectNodes("//select");
//6.遍历select节点集合
for (Element selectElement : selectElements) {
//取出id属性的值 组成map中key的部分
String id = selectElement.attributeValue("id");
//取出resultType属性的值 组成map中value的部分
String resultType = selectElement.attributeValue("resultType");
//取出文本内容 组成map中value的部分
String queryString = selectElement.getText();
//创建Key
String key = namespace + "." + id;
//创建Value
Mapper mapper = new Mapper();
mapper.setQueryString(queryString);
mapper.setResultType(resultType);
//把key和value存入mappers中
mappers.put(key, mapper);
}
return mappers;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
in.close();
}
}
/**
* 根据传入的参数,得到dao中所有被select注解标注的方法。
* 根据方法名称和类名,以及方法上注解value属性的值,组成Mapper的必要信息
*
* @param daoClassPath
* @return
*/
private static Map<String, Mapper> loadMapperAnnotation(String daoClassPath) throws Exception {
//定义返回值对象
Map<String, Mapper> mappers = new HashMap<String, Mapper>();
//1.得到dao接口的字节码对象
Class daoClass = Class.forName(daoClassPath);
//2.得到dao接口中的方法数组
Method[] methods = daoClass.getMethods();
//3.遍历Method数组
for (Method method : methods) {
//取出每一个方法,判断是否有select注解
boolean isAnnotated = method.isAnnotationPresent(Select.class);
if (isAnnotated) {
//创建Mapper对象
Mapper mapper = new Mapper();
//取出注解的value属性值
Select selectAnno = method.getAnnotation(Select.class);
String queryString = selectAnno.value();
mapper.setQueryString(queryString);
//获取当前方法的返回值,还要求必须带有泛型信息
Type type = method.getGenericReturnType();//List<User>
//判断type是不是参数化的类型
if (type instanceof ParameterizedType) {
//强转
ParameterizedType ptype = (ParameterizedType) type;
//得到参数化类型中的实际类型参数
Type[] types = ptype.getActualTypeArguments();
//取出第一个
Class domainClass = (Class) types[0];
//获取domainClass的类名
String resultType = domainClass.getName();
//给Mapper赋值
mapper.setResultType(resultType);
}
//组装key的信息
//获取方法的名称
String methodName = method.getName();
String className = method.getDeclaringClass().getName();
String key = className + "." + methodName;
//给map赋值
mappers.put(key, mapper);
}
}
return mappers;
}
}
5 自定义Mybatis的编码-创建两个默认实现类并分析类之间的关系
DefaultSqlSession.java
6 自定义Mybatis的编码-实现基于XML的查询所有操作
7 自定义Mybatis的编码-实现基于注解配置的查询所有
day01附录
泛型术语
以ArrayList<E>为例:<>念作typeof
ArrayList<E>中的E称为类型参数变量,其实就是类型的占位符(传入什么类型就是什么类型)。
ArrayList<Integer>中的Integer称为实际类型参数
整个称为ArrayList<E>泛型类型
整个ArrayList<Integer>称为参数化的类型ParameterizedTypeSet<String>、Enumeration<String>、Map<String,Object>、Map<K,V>
截图
![]()
01三层架构 ![]()
02持久层总图 ![]()
03mapper配置文件的创建要求 ![]()
04mybatis的分析