sqlsqssion简单的理解

 

Sqlsqssion理解 

MyBatis的持久化解决方案是将用户从原始的JDBC访问中解放出来,用户只需要定义需要操作的SQL语句,无须关注底层的JDBC操作,就可以以面向对象的方式来进行持久化层操作.底层数据库连接的获取,数据访问的实现,事务控制等都无须用户关心,从而将应用层从底层的JDBC/JTA API抽取出来.通过配置文件管理JDBC连接,让MyBatis解决持久化的实现.在MyBatis中的常见对象有SqlSessionFactory和SqlSession.本文这种介绍一下两者的概念和使用.

一、 SqlSessionFactory

SqlSessionFactory是MyBatis的关键对象,它是个单个数据库映射关系经过编译后的内存镜像.SqlSessionFactory对象的实例可以通过SqlSessionFactoryBuilder对象类获得,而SqlSessionFactoryBuilder则可以从XML配置文件或一个预先定制的Configuration的实例构建出SqlSessionFactory的实例.每一个MyBatis的应用程序都以一个SqlSessionFactory对象的实例为核心.同时SqlSessionFactory也是线程安全的,SqlSessionFactory一旦被创建,应该在应用执行期间都存在.在应用运行期间不要重复创建多次,建议使用单例模式.SqlSessionFactory是创建SqlSession的工厂.

 

复制代码
package org.apache.ibatis.session;

import java.sql.Connection;

public interface SqlSessionFactory {

  SqlSession openSession();//这个方法最经常用,用来创建SqlSession对象.

  SqlSession openSession(boolean autoCommit);

  SqlSession openSession(Connection connection);

  SqlSession openSession(TransactionIsolationLevel level);

  SqlSession openSession(ExecutorType execType);

  SqlSession openSession(ExecutorType execType, boolean autoCommit);

  SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level);

  SqlSession openSession(ExecutorType execType, Connection connection);

  Configuration getConfiguration();

}
复制代码

 

二、SqlSession

SqlSession是MyBatis的关键对象,是执行持久化操作的独享,类似于JDBC中的Connection.它是应用程序与持久层之间执行交互操作的一个单线程对象,也是MyBatis执行持久化操作的关键对象.SqlSession对象完全包含以数据库为背景的所有执行SQL操作的方法,它的底层封装了JDBC连接,可以用SqlSession实例来直接执行被映射的SQL语句.每个线程都应该有它自己的SqlSession实例.SqlSession的实例不能被共享,同时SqlSession也是线程不安全的,绝对不能讲SqlSeesion实例的引用放在一个类的静态字段甚至是实例字段中.也绝不能将SqlSession实例的引用放在任何类型的管理范围中,比如Servlet当中的HttpSession对象中.使用完SqlSeesion之后关闭Session很重要,应该确保使用finally块来关闭它.

复制代码
 1 //SqlSession接口源码如下所示:
 2 
 3 package org.apache.ibatis.session;
 4 
 5 import java.io.Closeable;
 6 import java.sql.Connection;
 7 import java.util.List;
 8 import java.util.Map;
 9 
10 import org.apache.ibatis.executor.BatchResult;
11 
12 public interface SqlSession extends Closeable {
13 
14   <T> T selectOne(String statement);
15 
16   <T> T selectOne(String statement, Object parameter);
17 
18   <E> List<E> selectList(String statement);
19 
20   <E> List<E> selectList(String statement, Object parameter);
21 
22   <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds);
23 
24   <K, V> Map<K, V> selectMap(String statement, String mapKey);
25 
26   <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey);
27 
28   <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds);
29 
30   void select(String statement, Object parameter, ResultHandler handler);
31 
32   void select(String statement, ResultHandler handler);
33 
34   void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler);
35 
36   int insert(String statement);
37 
38   int insert(String statement, Object parameter);
39 
40   int update(String statement);
41 
42   int update(String statement, Object parameter);
43 
44   int delete(String statement);
45 
46   int delete(String statement, Object parameter);
47 
48   void commit();
49 
50   void commit(boolean force);
51 
52   void rollback();
53 
54   void rollback(boolean force);
55 
56   List<BatchResult> flushStatements();
57 
58   void close();
59 
60   void clearCache();
61 
62   Configuration getConfiguration();
63 
64   <T> T getMapper(Class<T> type);
65 
66   Connection getConnection();
67 }
复制代码

转载于:https://www.cnblogs.com/rzbwyj/p/11604031.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值