当前位置: 首页 > news >正文

MyBatis的运行原理

目录

1、目的:梳理一下MyBatis运行时的几个对象,我们需要搞清楚他们的作用,进而需要理解mybatis的整个工作流程和执行原理。

2、简要概括各个类

2.1 Resources

作用:编写资源加载类,使用类加载器加载 配置文件(mybatis-config.xml ),即:读取mybatis-config.xml

2.2 SqlSessionFactoryBuilder

作用:将配置资源封装成Configuration对象,并且将该资源对象传到工厂对象中

2.2.1 Configuration

作用:配置类 --> 把所有的配置信息 都装进去

2.2.2 MappedStatement

作用:用来封装 sql语句 和 查询结果集(即:返回值类型)

2.3 SqlSessionFactory

2.4 SqlSession

2.5 ProxyFactory

3、测试


1、目的:梳理一下MyBatis运行时的几个对象,我们需要搞清楚他们的作用,进而需要理解mybatis的整个工作流程和执行原理。

mybatis-config.xml

mapper.xml mapper.xml mapper.xml...

|

|

Resources

|

|---->InputStream

|

SqlSessionFactoryBuilder

|

|---->Configuration(conn, Map<namespace+"."+id, MappedStatement(sql, resultType)>)

|

SqlSessionFactory

|

|---->Configuration(conn, Map<namespace+"."+id, MappedStatement(sql, resultType)>)

|

SqlSession

|

|---->conn, Map<namespace+"."+id, MappedStatement(sql, resultType)>

|

ProxyFactory

|

|---->MappedStatement(sql, resultType), conn

|

Executor

| |

| |

| |

输入出映射

2、简要概括各个类

2.1 Resources

作用:编写资源加载类,使用类加载器加载 配置文件(mybatis-config.xml ),即:读取mybatis-config.xml

package com.by.io;import java.io.InputStream;public class Resources {//根据文件名称,加载类路径下面的配置文件public static InputStream getResourceAsStream(String filePath){return Resources.class.getClassLoader().getResourceAsStream(filePath);}
}

2.2 SqlSessionFactoryBuilder

作用:将配置资源封装成Configuration对象,并且将该资源对象传到工厂对象中

package com.by.builder;import com.by.cfg.Configuration;
import com.by.factory.DefaultSqlSessionFactory;
import com.by.factory.SqlSessionFactory;
import com.by.utils.XMLConfigBuilder;import java.io.InputStream;public class SqlSessionFactoryBuilder {/*** 构建SqlSessionFactory对象* @param in* @return*/public SqlSessionFactory build(InputStream in){Configuration configuration = XMLConfigBuilder.loadConfiguration(in);return  new DefaultSqlSessionFactory(configuration);}
}

2.2.1 Configuration

作用:配置类 --> 把所有的配置信息 都装进去

package com.by.cfg;import com.by.mapping.MappedStatement;import java.util.HashMap;
import java.util.Map;public class Configuration {private String driver;private String url;private String username;private String password;/**Map<"com.by.dao.UserDao.findAll", MappedStatement>     */private Map<String, MappedStatement> mappers = new HashMap<String,MappedStatement>();public Map<String, MappedStatement> getMappers() {return mappers;}public void setMappers(Map<String, MappedStatement> mappers) {this.mappers.putAll(mappers);//此处需要使用追加的方式}public String getDriver() {return driver;}public void setDriver(String driver) {this.driver = driver;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}
}

2.2.2 MappedStatement

作用:用来封装 sql语句 和 查询结果集(即:返回值类型)

package com.by.mapping;public class MappedStatement {private String queryString;//SQLprivate String resultType;//实体类的全限定类名public String getQueryString() {return queryString;}public void setQueryString(String queryString) {this.queryString = queryString;}public String getResultType() {return resultType;}public void setResultType(String resultType) {this.resultType = resultType;}
}

2.3 SqlSessionFactory

package com.by.factory;
public interface SqlSessionFactory {//获取SQLSession对象public SqlSession openSession();
}
package com.by.factory;import com.by.cfg.Configuration;
import com.by.session.DefaultSqlSession;
import com.by.session.SqlSession;public class DefaultSqlSessionFactory implements SqlSessionFactory {private Configuration cfg;public DefaultSqlSessionFactory(Configuration cfg) {this.cfg = cfg;}/*** 获取一个SqlSession对象* @return*/@Overridepublic SqlSession openSession() {return new DefaultSqlSession(cfg);}
}

2.4 SqlSession

public interface SqlSession {//获取代理对象public <T> T getMapper(Class<T> tClass);//释放资源void close();
}
package com.by.session;import com.by.utils.DataSourceUtil;
import com.by.cfg.Configuration;import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.SQLException;public class DefaultSqlSession implements SqlSession {private Configuration cfg;private Connection conn;public DefaultSqlSession(Configuration cgf){this.cfg = cgf;this.conn = DataSourceUtil.getConnection(cfg);}/** 创建代理对象*/@Overridepublic <T> T getMapper(Class<T> tClass) {/*** tClass.getClassLoader():类加载器* new Class[]{tClass}:Class数组,让代理对象和被代理对象有相同的行为*  new ProxyFactory:调用真实角色,附加自己的操作*/return (T) Proxy.newProxyInstance(tClass.getClassLoader(),new Class[]{tClass},new ProxyFactory(cfg.getMappers(),conn));}@Overridepublic void close() {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}
}

2.5 ProxyFactory

package com.by.session;import com.by.mapping.MappedStatement;
import com.by.utils.Executor;import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.util.Map;public class ProxyFactory implements InvocationHandler {private Map<String, MappedStatement> mappers;private Connection conn;public ProxyFactory(Map<String, MappedStatement> mappers, Connection conn){this.mappers = mappers;this.conn = conn;}//调用代理对象的任何方法,都会在这执行public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {//1.获取方法名String methodName = method.getName();//2.获取方法所在类的名称String className = method.getDeclaringClass().getName();//3.组合keyString key = className+"."+methodName;//4.获取mappers中的Mapper对象MappedStatement mappedStatement = mappers.get(key);//5.判断是否有mapperif(mappedStatement == null){throw new IllegalArgumentException("传入的参数有误");}//6.调用工具类执行查询所有return new Executor().selectList(mappedStatement,conn);}
}

3、测试

//1.读取配置文件InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");//2.创建SqlSessionFactory工厂SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();SqlSessionFactory factory = builder.build(in);System.out.println("-----" + factory);//3.使用工厂生产SqlSession对象SqlSession session = factory.openSession();//4.使用SqlSession创建Dao接口的代理对象UserDao userDao = session.getMapper(UserDao.class);//5.使用代理对象执行方法List<User> users = userDao.findAll();for(User user : users){System.out.println(user);}//6.释放资源session.close();in.close();
http://www.lryc.cn/news/375157.html

相关文章:

  • 算法题解记录29+++全排列(百日筑基)
  • 苹果AI功能,AI训练数据缺乏,SD3推出,MJ6推出新特性
  • 超越中心化:Web3如何塑造未来数字生态
  • 【ic-tool】timegen使用
  • 1:25万基础电子地图(云南版)
  • springboot宠物领养系统-计算机毕业设计源码07863
  • 牛客热题:最长回文子串
  • 如何访问寄存器
  • 苍穹外卖笔记-18-修改密码、bug记录
  • java如何截取字符串
  • 虚拟淘宝-Virtual-Taobao论文解读(AAAI2019)
  • 低代码组件扩展方案在复杂业务场景下的设计与实践
  • 震撼科技界的GPT-4o发布首日即遭“越狱破防”
  • 保护密码安全,探讨密码加盐及其在Go语言中的实现
  • Sqoop学习详细介绍!!
  • 【2024最新华为OD-C/D卷试题汇总】[支持在线评测] 生成哈夫曼树(100分) - 三语言AC题解(Python/Java/Cpp)
  • ctfshow web 单身杯
  • 天锐绿盾加密软件,它的适用范围是什么?
  • mysql面试题 Day2
  • Excel加密怎么设置?这5个方法不容错过!(2024总结)
  • 2024年下一个风口是什么?萤领优选 轻资产创业项目全国诚招合伙人
  • Redis 网络模型
  • 【设计模式之组合模式 -- C++】
  • C# 通过Win32API设置客户端系统时间
  • VirtualHere 允许通过网络远程使用 USB 设备,就像本地连接一样!
  • 【Kubernetes】k8s 自动伸缩机制—— HPA 部署
  • MT1415 大小相同
  • 使用python库moviepy完成视频剪辑
  • Java高手的30k之路|面试宝典|精通泛型
  • 清理Linux操作系统buff/cache缓存