一、SqlMapper類
Ibatis中,加載、分析配置以及映射文件是在創建SqlMapper實例的時候進行的,另外對數據庫的操作,也是在SqlMapper實例上調用方法來完成。在IBatis外部的程序中,創建SqlMapper的實例的方式是:
ISqlMapper mapper = Mapper.Instance();
在第一次調用Mapper.Instance()的時候,由DomSqlMapBuilder對象解析SqlMap.config(默認路徑和命名)文件來創建SqlMapper實例,然后會緩存該mapper對象,如果程序運行過程中,修改了映射文件,那么再調用Mapper.Instance()創建SqlMapper實例時,SqlMapper會被重新加載創建。相當於一個文件緩存依賴,這個文件緩存依賴由DomSqlMapBuilder.ConfigureAndWatch方法來實現。
如果你不希望修改了配置文件就重新加載,可以通過
ISqlMapper mapper = builder.Configure();
來創建實例。具體的位置寫法,可以第二篇。
IBatis.net的這個東西有個地方不好,默認是使用HttpContext作為xxx容器的。
當非Web請求線程調用時,如Timer調用時會報如下錯誤:
ibatis.net:WebSessionStore: Could not obtain reference to HttpContext
這個問題可以在創建SQLMapper的時候指定
SqlMapper.SessionStore = new HybridWebThreadSessionStore(sqlMapper.Id); /// <summary> /// 初始化 /// </summary> /// <param name="sqlMapperPath"></param> public void InitMapper(string sqlMapperPath) { ConfigureHandler handler = new ConfigureHandler(Configure); DomSqlMapBuilder builder = new DomSqlMapBuilder(); _mapper = builder.ConfigureAndWatch(sqlMapperPath, handler); _mapper.SessionStore = new IBatisNet.DataMapper.SessionStore.HybridWebThreadSessionStore(_mapper.Id); }
就可以解決問題。
詳細解釋見:http://www.iloveher.cn/ibatis/hybridwebthreadsessionstore.html
