Mybatis核心源码分析

一.数据存贮对象

mybatis-config.xml --> Configuration

Configuration(核心对象):

1.封装了mybatis-config.xml

2.封装了mapper 文件 MapperStatement

对应的就是mapper.xml中 的一个标签

3.创建了Mybatis其他相关对象

xxxDaoMapper.xml --> MapperStatement

MapperStatement 封装sql语句 --> BoundSql

核心代码:

 InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
 SqlSession sqlSession = sqlSessionFactory.openSession();
 //1.代理实现
 UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
 List<User> users = userMapper.queryAllUsers();
 //2.调用底层sqlSession方法
 List<User> users = sqlSession.selectList("com.mybatis.mapper.UserMapper.queryAllUsers");

二.操作类型的对象

Excutor(接口)--处理功能的核心

作用:1.增删改update 查query 2.事务 3.缓存

实现类:

BatchExcutor:JDBC中的批处理操作

ReuseExcutor:复用 Statement

SimpleExcutor:常用的Excutor mybatis推荐 (默认)

protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE;

StatementHandler(接口)

Mybatis封装了JDBC Statement,StatementHandler是真正Mybatis进行数据库访问操作的核心

实现:SimpleStatementHandler,PreparedStatementHandler,CallableStatement

ParameterHandler

目的:将Mybatis参数转换为JDBC使用的参数

@Param --> #{} --> ?

ResultSetHandler

目的:对JDBC中查询结果集 ResultSet 进行封装

TypeHandler

Java类型 --> 数据库类型

例如:String --> varchar

MappedStatement

封装的是XXXMapper.xml文件中对数据库操作的标签<select/insert/update/delete>


三. Mapper接口创建实现类

interface UserDao{
    List<USer> queryAllUsers();
}

UserDao userDao = SqlSession.getMapper(UserMapper.class);

UserDao接口的实现类:
    动态字节码技术,在JVM运行时创建  JVM运行结束后消失
1.如何创建 UserDao接口的实现类 
        代理(动态代理)
2.实现类如何实现方法
UserDapImpl implements UserDao{
        queryAllUsers(){
            sqlSession.select("namespace.id",参数)
                |-Excutor
                    |-StatementHandler
                        |-ParameterHandler,ResultSetHandler
                            TypeHandler
        }
}

MapperRegistry-->MapperProxyFactory-->MapperProxy
MapperProxyFactory:
    Proxy.newProxyInstrace(ClassLoader,Class[]{xxxDaoMapper.class},mapperProxy)

四.解析xml文件

 InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

1.解析 mybatis-config.xml --> Configuration对象
       xPathParser XNode --> Configuration
                bulid() --> parseConfiguration(parser.evalNode("/configuration"))
        xPathParser XNode --> MapperStatement --> namespace.id
            并且把MapperStatement存放在了Configuration中
        parseConfiguration --> mapperElement(root.evalNode("mappers"));
            XMLMapperBuilder mapperParser = new XMLMapperBuilder(..);
            mapperParser.parse();
                configurationElement(parser.evalNode("/mapper"));
                    buildStatementFromContext(context.evalNodes("select|insert|update|delete"));
                        statementParser.parseStatementNode();
                            builderAssistant.addMappedStatement
                                  MappedStatement.Builder statementBuilder = new MappedStatement.Builder

SqlSession sqlSession = sqlSessionFactory.openSession();

     final Environment environment = configuration.getEnvironment();
     final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
     tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
     final Executor executor = configuration.newExecutor(tx, execType);
     return new DefaultSqlSession(configuration, executor, autoCommit);

  • 37
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值