- 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接続プールとトランザクション]
- MyBatis-スタディノート08 [08。動的SQL]
- MyBatis-スタディノート09 [09.Mybatisマルチテーブルオペレーション]
- MyBatis-スタディノート10 [10.JNDI拡張知識]
- MyBatis-スタディノート11 [11.Mybatisキャッシュ]
- MyBatis-スタディノート12 [12.Mybatisアノテーションの開発]
目次
1Mybatisの分析をカスタマイズします-クエリのすべての分析を実行します
2Mybatis分析のカスタマイズ-プロキシオブジェクト分析の作成
3mybatisのコーディングをカスタマイズします-不足しているテストクラスに基づいてインターフェイスとクラスを作成します
4カスタムmybatisエンコーディング-XMLを解析するためのツールの紹介
5 Mybatisコーディングのカスタマイズ-2つのデフォルトの実装クラスを作成し、クラス間の関係を分析します
6Mybatisコーディングのカスタマイズ-XMLクエリに基づいてすべての操作を実現します
7Mybatisコーディングのカスタマイズ-アノテーション構成に基づいてすべてのクエリを実現します
1Mybatisの分析をカスタマイズします-クエリのすべての分析を実行します
6.カスタムMybatisの分析:
プロキシdaoを使用して追加、削除、変更、およびチェックする場合、 Mybatisは何をしますか? 1
つ
はプロキシオブジェクトを作成すること、
もう1つはプロキシオブジェクトでselectListを呼び出すことです。
実行に使用されるステートメントとカプセル化する場所は何ですか?
ストリームを読み取り、構成ファイルを解析します。
(E)強制変換タイプ
2Mybatis分析のカスタマイズ-プロキシオブジェクト分析の作成
3mybatisのコーディングをカスタマイズします-不足しているテストクラスに基づいてインターフェイスとクラスを作成します
6.カスタムMybatisの分析:
カスタムMybatisは、エントリケース
クラスを通じてクラスを確認できます。リソース
クラスSqlSessionFactoryBuilder
インターフェイスSqlSessionFactory
インターフェイスSqlSession
カスタム実装クラスの利点:柔軟な制御を実現する(メソッドが増えるごとに、関数を増やすことができます)。
クラスリソース
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);
}
}
クラス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;
}
}
インターフェイスSqlSessionFactory
package com.itheima.mybatis.sqlsession;
public interface SqlSessionFactory {
/**
* 用于打开一个新的SqlSession对象
*
* @return
*/
SqlSession openSession();
}
インターフェイス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
マッパー
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;
}
}