Mybatis的学习

一、Mybatis的介绍(入门_MyBatis中文网MyBatis中文网入门_MyBatis中文网

1.MyBatis 是一款优秀的持久层框架

2.支持定制化 SQL、存储过程以及高级映射。

3.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。

4.MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Ordinary Java Object,普通的 Java对象)映射成数据库中的记录。

注:持久层解释:

一、持久化:将程序的数据在瞬时状态转化为持久状态的过程

        瞬时状态:数据存放在内存中时。

        持久状态:数据存放在数据库(硬盘、IO文件)中时。

二、持久层:完成数据持久化的代码块

二、Mybatis的工作原理

1.读取Mybatis的配置文件:

        mybatis会加载映射mapper.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <!--事务管理-->
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <!--useSSL=true:安全连接 useUnicode=true characterEncoding=UTF-8设置编码-->
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="com/dao/UserMapper.xml"/>
    </mappers>
</configuration>

2.构造会话工厂(SqlSessionFactory):

        通常会创建一个配置类,配置类通过读取配置文件,使用SqlSessionFactoryBuild获取SqlSessionFactory

//SqlSessionFactor:sql会话工厂    工具类
public class MybatisUtils {
    private static SqlSessionFactory sqlSessionFactory;
    static {
        try {
            //读取配置类信息
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            //使用SqlSessionFactoryBuilder获取SqlSessionFactory
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //获取SqlSession
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}

4.构造会话对象

    public void Test01(){
        //通过工具类获取SqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //执行sql:参数为Mapper接口
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        List<User> user = userDao.getUser();
        for (User use: user) {
            System.out.println(use);
        }
        //关闭sqlSession
        sqlSession.close();
    }

5.Exectuor执行器创建MappedStatement对象,MappedStatement对象通过底层封装的JDBC去操作数据库。

        Exectuor:sql执行器,其对应的类全路径:org.apache.ibatis.executor.Executor。

  • Executor 执行器根据接口,定义update(更新或插入)、query(查询)、commit(提交事务)、rollback(回滚事务)。接下来简单介绍几个重要方法:
  1. int update(MappedStatement ms, Object parameter) throws SQLException 更新或插入方法,其参数含义如下:、 1)MappedStatement ms:SQL映射语句(Mapper.xml文件每一个方法对应一个MappedStatement对象) 2)Object parameter:参数,通常是List集合。
  2. < E> List< E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) 查询方法,其参数含义如下: 1)RowBounds:行边界,主要值分页参数limit、offset。 2)ResultHandler resultHandler:结果处理器。
  3. CacheKey createCacheKey(MappedStatement ms, Object parameterObj, RowBounds bounds, BoundSql bSql) 创建缓存Key,Mybatis一二级缓存的缓存Key,可以看出Key由上述4个参数来决定。 1)BoundSql boundSql:可以通过该对象获取SQL语句。
  • CachingExecutor 支持结果缓存的SQL执行器,注意其设计模式的应用,该类中,会持有Executor的一个委托对象,CachingExecutor关注与缓存特定的逻辑,其最终的SQL执行由其委托对象来实现,即其内部的委托对象为BaseExecutor的实现类。

  • BaseExecutor Executor的基础实现类,该类为抽象类,关于查询、更新具体的实现由其子类来实现,下面4个都是其子类。

  • SimpleExecutor 简单的Executor执行器。

  • BatchExecutor 支持批量执行的Executor执行器。

  • ClosedExecutor 表示一个已关闭的Executor。

  • ReuseExecutor 支持重复使用Statement,以SQL为键,缓存Statement对象。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值