关闭

【MyBatis】mybatis执行流程与缓存机制分析

331人阅读 评论(0) 收藏 举报
分类:

1. 编程式使用mybatis


首先,我们直观地看一下怎样编程式(非配置式)使用mybatis作为ORM框架实现数据库的基本操作。

    private static void testId() throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = factory.openSession();

        // 获取代理 -> MapperProxy
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);

        Student student = new Student();
        student.setStudentName("AutoIncrementId");
        student.setStudentNumber("2014216003");
        // sqlSession.insert("insertEntry", student);
        studentDao.insertEntry(student);
        System.out.println(student.getId());// 3

        // commit & close
        sqlSession.commit();
        sqlSession.close();
    }

可以清晰地看到一共分为三个步骤:第一步,使用mybatis配置文件创建SqlSessionFactory,并打开一个sqlSession;第二步,获取代理MapperProxy类;第三步,执行数据库基本操作。

2. 第一步,获取sqlSession




3. 第二步,获取代理MapperProxy


3.0 
sqlSession.getMapper(StudentDao.class);
3.1  DefaultSqlSession
	public <T> T getMapper(Class<T> type)
		configuration.<T>getMapper(type, this);
3.2 Configuration
	public <T> T getMapper(Class<T> type, SqlSession sqlSession)
		mapperRegistry.getMapper(type, sqlSession);
3.3 MapperRegistry
	public <T> T getMapper(Class<T> type, SqlSession sqlSession)
		mapperProxyFactory.newInstance(sqlSession);
3.4 MapperProxyFactory
	public T newInstance(SqlSession sqlSession)
		(T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
3.5 Proxy
	public static Object newProxyInstance(ClassLoader loader,
                                      Class<?>[] interfaces,
                                      InvocationHandler h)



4. 第三步,执行数据库操作


4.1 MapperProxy
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
		mapperMethod.execute(sqlSession, args);
4.2 MapperMethod
	public Object execute(SqlSession sqlSession, Object[] args)
		sqlSession.insert(command.getName(), param)
4.3 DefaultSqlSession
	public int insert(String statement, Object parameter)
		update(statement, parameter);
public intupdate(String statement, Object parameter)
executor.update(ms, wrapCollection(parameter));
4.4 CachingExcutor
public intupdate(MappedStatement ms, Object parameterObject)throwsSQLException
delegate.update(ms, parameterObject);
4.5 BaseExcutor
public intupdate(MappedStatement ms, Object parameter)throwsSQLException
doUpdate(ms, parameter);
4.6 SimpleExcutor
public intdoUpdate(MappedStatement ms, Object parameter)
handler.update(stmt);
4.7 PreparedStatementHandler
  public int update(Statement statement) throws SQLException {
    PreparedStatement ps = (PreparedStatement) statement;
    ps.execute();
    int rows = ps.getUpdateCount();
    Object parameterObject = boundSql.getParameterObject();
    KeyGenerator keyGenerator = mappedStatement.getKeyGenerator();
    keyGenerator.processAfter(executor, mappedStatement, ps, parameterObject);
    return rows;
  }





5. MyBatis缓存机制分析


    private static void testCache() throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis-config2.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = factory.openSession();

        long id = 1L;
        Student student = new Student();
        student.setStudentName("Ethan");
        student.setStudentNumber("2014216001");
        sqlSession.insert("insertEntry", student);
        System.out.println(student.getId());// null

        // query from db
        // 加入一级缓存和二级transactional缓存,由于sqlsession未commit,所以二级transactional缓存的代理未真正缓存
        student = sqlSession.selectOne("selectByKey", id);
        System.out.println(student.getId());
        System.out.println(student.getStudentName());

        // local cache hit
        // 由于二级缓存未真正缓存,只能从一级缓存那里命中
        student = sqlSession.selectOne("selectByKey", id);
        System.out.println(student.getId());
        System.out.println(student.getStudentName());

        // commit
        sqlSession.commit();

        // sqlsession在commit之后,transactional缓存的代理才真正缓存起来,二级缓存命中
        student = sqlSession.selectOne("selectByKey", id);
        System.out.println(student.getId());
        System.out.println(student.getStudentName());

        // update 会clear未commit的transactional缓存中的内容,同时设置标志位clearOnCommit为true,会clear掉local cache
        student.setStudentName("Hunt");
        int update = sqlSession.update("updateByKey", student);
        System.out.println("update: " + update);

        // select again
        // 在二级缓存中找到了缓存,但是由于clearOnCommit标志位为true,所以返回null
        student = sqlSession.selectOne("selectByKey", id);
        System.out.println(student.getId());
        System.out.println(student.getStudentName());

        // commit & close
        sqlSession.commit();
        sqlSession.close();
    }

其中,二级缓存默认为:



6. References




0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:14975次
    • 积分:510
    • 等级:
    • 排名:千里之外
    • 原创:37篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条