终结篇 MyBatis 解析 (四)--

MappedStatement:
一个MappedStatement对象对应Mapper配置文件中的一个select/update/insert/delete节点,主要描述的是一条SQL语句。其属性有:

//节点中的id属性加要命名空间  
private String id;  
//直接从节点属性中取  
private Integer fetchSize;  
//直接从节点属性中取  
private Integer timeout;  
private StatementType statementType;  
private ResultSetType resultSetType;  
//对应一条SQL语句  
//SqlSource表示从mapper.xml或注解中读取的sql内容,该sql一般还不能都被直接执行
private SqlSource sqlSource;  

//每条语句都对就一个缓存,如果有的话。  
private Cache cache;  
//这个已经过时了  
private ParameterMap parameterMap;  
private List<ResultMap> resultMaps;  
private boolean flushCacheRequired;  
private boolean useCache;  
private boolean resultOrdered;  
//SQL的类型,select/update/insert/detete  
private SqlCommandType sqlCommandType;  
private KeyGenerator keyGenerator;  
private String[] keyProperties;  
private String[] keyColumns;  

//是否有内映射  
private boolean hasNestedResultMaps;  
private String databaseId;  
private Log statementLog;  
private LanguageDriver lang;  
private String[] resultSets;  

SQL Source对象:
SqlSource只有一个方法:getBoundSql(paramenterObject),其中paramenterObject为运行sql里的实际参数.

<select id="selectUserDetail" resultMap="detailUserResultMap">  
    <!--CDATA里内容会都解析成一个SqlSource对象-->  
                <![CDATA[ 
            select user_id,user_name,user_type,cust_id from tf_f_user a where a.user_id=#${userId} 
        ]]>/select> 

每个线程都应该有它自己的SqlSession实例。SqlSession的实例不能共享使用,它也是线程不安全的。因此最佳的范围是请求或方法范围。绝对不能将SqlSession实例的引用放在一个类的静态字段或实例字段中。
打开一个 SqlSession;使用完毕就要关闭它。通常把这个关闭操作放到 finally 块中以确保每次都能执行关闭。如下:

         SqlSession session = sqlSessionFactory.openSession();
         try {
                  // do work
         } finally {
                session.close();
        }
if (ExecutorType.BATCH == executorType) {
      executor = newBatchExecutor(this, transaction);
    } else if (ExecutorType.REUSE == executorType) {
      executor = new ReuseExecutor(this, transaction);
    } else {
      executor = new SimpleExecutor(this, transaction);
    }
if (cacheEnabled) {
      executor = new CachingExecutor(executor, autoCommit);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值