日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MyBatis缓存分为一级缓存和二级缓存

發布時間:2024/1/23 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MyBatis缓存分为一级缓存和二级缓存 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一級緩存

MyBatis的一級緩存指的是在一個Session域內,session為關閉的時候執行的查詢會根據SQL為key被緩存(跟mysql緩存一樣,修改任何參數的值都會導致緩存失效)

1)單獨使用MyBatis而不繼承Spring,使用原生的MyBatis的SqlSessionFactory來構造sqlSession查詢,是可以使用以及緩存的,示例代碼如下

public class Test {public static void main(String[] args) throws IOException {String config = "mybatis-config.xml";InputStream is = Resources.getResourceAsStream(config);SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);SqlSession session = factory.openSession();System.out.println(session.selectOne("selectUserByID", 1));// 同一個session的相同sql查詢,將會使用一級緩存 System.out.println(session.selectOne("selectUserByID", 1));// 參數改變,需要重新查詢System.out.println(session.selectOne("selectUserByID", 2));// 清空緩存后需要重新查詢 session.clearCache();System.out.println(session.selectOne("selectUserByID", 1));// session close以后,仍然使用同一個db connection session.close();session = factory.openSession();System.out.println(session.selectOne("selectUserByID", 1));} }

輸出如下

DEBUG - Openning JDBC Connection
DEBUG - Created connection 10044878.
DEBUG - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@9945ce]
DEBUG - ==> Preparing: SELECT * FROM user WHERE id = ??
DEBUG - ==> Parameters: 1(Integer)
1|test1|19|beijing
1|test1|19|beijing
DEBUG - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@9945ce]
DEBUG - ==> Preparing: SELECT * FROM user WHERE id = ??
DEBUG - ==> Parameters: 2(Integer)
2|test2|18|guangzhou
DEBUG - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@9945ce]
DEBUG - ==> Preparing: SELECT * FROM user WHERE id = ??
DEBUG - ==> Parameters: 1(Integer)
1|test1|19|beijing
DEBUG - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@9945ce]
DEBUG - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@9945ce]
DEBUG - Returned connection 10044878 to pool.
DEBUG - Openning JDBC Connection
DEBUG - Checked out connection 10044878 from pool.
DEBUG - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@9945ce]
DEBUG - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@9945ce]
DEBUG - ==> Preparing: SELECT * FROM user WHERE id = ??
DEBUG - ==> Parameters: 1(Integer)
1|test1|19|beijing

看以看出來,當參數不變的時候只進行了一次查詢,參數變更以后,則需要重新進行查詢,而清空緩存以后,參數相同的查詢過的SQL也需要重新查詢,而且使用的數據庫連接是同一個數據庫連接,這里要得益于我們在mybatis-config.xml里面的datasource設置

<environments default="development"><environment id="development"><transactionManager type="JDBC"></transactionManager><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatistest?characterEncoding=utf8" /><property name="username" value="root" /><property name="password" value="root" /></dataSource></environment></environments>

注意datasource使用的是POOLED,也就是使用了連接池,所以數據庫連接可回收利用,當然這個environment屬性在集成spring的時候是不需要的,因為我們需要另外配置datasource的bean.

?

2) 跟Spring集成的時候(使用mybatis-spring)

直接在dao里查詢兩次同樣參數的sql

@Repository public class UserDao extends SqlSessionDaoSupport {public User selectUserById(int id) {SqlSession session = getSqlSession();session.selectOne("dao.userdao.selectUserByID", id);// 由于session的實現是SqlSessionTemplate的動態代理實現// 它已經在代理類內執行了session.close(),所以無需手動關閉sessionreturn session.selectOne("dao.userdao.selectUserByID", id);} }

觀察日志

DEBUG - Creating a new SqlSession
DEBUG - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1e389b8] was not registered for synchronization because synchronization is not active
DEBUG - Fetching JDBC Connection from DataSource
DEBUG - JDBC Connection [jdbc:mysql://127.0.0.1:3306/mybatistest?characterEncoding=utf8, UserName=root@localhost, MySQL-AB JDBC Driver] will not be managed by Spring
DEBUG - ooo Using Connection [jdbc:mysql://127.0.0.1:3306/mybatistest?characterEncoding=utf8, UserName=root@localhost, MySQL-AB JDBC Driver]
DEBUG - ==> Preparing: SELECT * FROM user WHERE id = ?
DEBUG - ==> Parameters: 1(Integer)
DEBUG - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1e389b8]
DEBUG - Returning JDBC Connection to DataSource
DEBUG - Creating a new SqlSession
DEBUG - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@169da74] was not registered for synchronization because synchronization is not active
DEBUG - Fetching JDBC Connection from DataSource
DEBUG - JDBC Connection [jdbc:mysql://127.0.0.1:3306/mybatistest?characterEncoding=utf8, UserName=root@localhost, MySQL-AB JDBC Driver] will not be managed by Spring
DEBUG - ooo Using Connection [jdbc:mysql://127.0.0.1:3306/mybatistest?characterEncoding=utf8, UserName=root@localhost, MySQL-AB JDBC Driver]
DEBUG - ==> Preparing: SELECT * FROM user WHERE id = ?
DEBUG - ==> Parameters: 1(Integer)
DEBUG - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@169da74]
DEBUG - Returning JDBC Connection to DataSource

?

這里執行了2次sql查詢,看似我們使用了同一個sqlSession,但是實際上因為我們的dao繼承了SqlSessionDaoSupport,而SqlSessionDaoSupport內部sqlSession的實現是使用用動態代理實現的,這個動態代理sqlSessionProxy使用一個模板方法封裝了select()等操作,每一次select()查詢都會自動先執行openSession(),執行完close()以后調用close()方法,相當于生成了一個新的session實例,所以我們無需手動的去關閉這個session()(關于這一點見下面mybatis的官方文檔),當然也無法使用mybatis的一級緩存,也就是說mybatis的一級緩存在spring中是沒有作用的.

官方文檔摘要

MyBatis?SqlSession?provides you with specific methods to handle transactions programmatically. But when using MyBatis-Spring your beans will be injected with a Spring managed?SqlSession?or a Spring managed mapper. That means that Spring will?always?handle your transactions.

You cannot call?SqlSession.commit(),?SqlSession.rollback()?or?SqlSession.close()?over a Spring managed?SqlSession. If you try to do so, a?UnsupportedOperationException?exception will be thrown. Note these methods are not exposed in injected mapper classes.

?

二級緩存

二級緩存就是global caching,它超出session范圍之外,可以被所有sqlSession共享,它的實現機制和mysql的緩存一樣,開啟它只需要在mybatis的配置文件開啟settings里的

<setting name="cacheEnabled" value="true"/>

以及在相應的Mapper文件(例如userMapper.xml)里開啟

<mapper namespace="dao.userdao">... select statement ...<!-- Cache 配置 --><cacheeviction="FIFO"flushInterval="60000"size="512"readOnly="true" /> </mapper>

需要注意的是global caching的作用域是針對Mapper的Namespace而言的,也就是說只在有在這個Namespace內的查詢才能共享這個cache.例如上面的 dao.userdao namespace, 下面是官方文檔的介紹

It's important to remember that a cache configuration and the cache instance are bound to the namespace of the SQL Map file. Thus, all statements in the same namespace as the cache are bound by it.

例如下面的示例,我們執行兩次對同一個sql語句的查詢,觀察輸出日志

@RequestMapping("/getUser")public String getUser(Model model) {User user = userDao.selectUserById(1);model.addAttribute(user);return "index";}

當我們訪問兩次 /getUser 這個url,查看日志輸出

DEBUG - Creating a new SqlSession
DEBUG - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@659812] was not registered for synchronization because synchronization is not active
DEBUG - Cache Hit Ratio [dao.userdao]: 0.0
DEBUG - Fetching JDBC Connection from DataSource
DEBUG - JDBC Connection [jdbc:mysql://127.0.0.1:3306/mybatistest?characterEncoding=utf8, UserName=root@localhost, MySQL-AB JDBC Driver] will not be managed by Spring
DEBUG - ooo Using Connection [jdbc:mysql://127.0.0.1:3306/mybatistest?characterEncoding=utf8, UserName=root@localhost, MySQL-AB JDBC Driver]
DEBUG - ==> Preparing: SELECT * FROM user WHERE id = ??
DEBUG - ==> Parameters: 1(Integer)
DEBUG - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@659812]
DEBUG - Returning JDBC Connection to DataSource
DEBUG - Invoking afterPropertiesSet() on bean with name 'index'
DEBUG - Rendering view [org.springframework.web.servlet.view.JstlView: name 'index'; URL [/index.jsp]] in DispatcherServlet with name 'dispatcher'
DEBUG - Added model object 'org.springframework.validation.BindingResult.user' of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name 'index'
DEBUG - Added model object 'user' of type [bean.User] to request in view with name 'index'
DEBUG - Forwarding to resource [/index.jsp] in InternalResourceView 'index'
DEBUG - Successfully completed request
DEBUG - Returning cached instance of singleton bean 'sqlSessionFactory'
DEBUG - DispatcherServlet with name 'dispatcher' processing GET request for [/user/getUser]
DEBUG - Looking up handler method for path /user/getUser
DEBUG - Returning handler method [public java.lang.String controller.UserController.getUser(org.springframework.ui.Model)]
DEBUG - Returning cached instance of singleton bean 'userController'
DEBUG - Last-Modified value for [/user/getUser] is: -1
DEBUG - Creating a new SqlSession
DEBUG - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@539a92] was not registered for synchronization because synchronization is not active
DEBUG - Cache Hit Ratio [dao.userdao]: 0.5
DEBUG - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@539a92]
DEBUG - Rendering view [org.springframework.web.servlet.view.JstlView: name 'index'; URL [/index.jsp]] in DispatcherServlet with name 'dispatcher'
DEBUG - Added model object 'org.springframework.validation.BindingResult.user' of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name 'index'
DEBUG - Added model object 'user' of type [bean.User] to request in view with name 'index'
DEBUG - Forwarding to resource [/index.jsp] in InternalResourceView 'index'
DEBUG - Successfully completed request

可以看出第二次訪問同一個url的時候相同的查詢 hit cache了,這就是global cache的作用

總結

以上是生活随笔為你收集整理的MyBatis缓存分为一级缓存和二级缓存的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 久久狠 | 99国产在线观看 | 91成人一区二区三区 | 日本午夜在线 | 国内少妇精品 | 日韩精品片 | 国产做爰xxxⅹ久久久精华液 | 综合激情久久 | 日韩视频a | 国产人妖视频 | 大伊人久久 | 天天草夜夜操 | 粉嫩av四季av绯色av | 亚洲精品视频国产 | 男女午夜视频在线观看 | www.超碰在线观看 | 91精品婷婷国产综合久久竹菊 | 国产精品乱 | 一区二区三区偷拍 | 在线观看一区二区三区四区 | 免费高清毛片 | 亚洲理论在线 | 久久99国产精品成人 | 精品久久一区二区三区 | 欧美另类自拍 | 日韩欧美一区二区三区四区五区 | 亚洲精品在线视频 | 少妇乱淫36部 | 亚洲视频福利 | 无套内谢老熟女 | 狠狠操综合网 | 双女主黄文 | 大肉大捧一进一出好爽mba | 夜色视频在线观看 | 制服.丝袜.亚洲.中文.综合 | 久久精品99久久久久久久久 | 超清纯大学生白嫩啪啪 | 老妇free性videosxx | 亚洲va视频 | 毛片网站在线播放 | 中文字字幕一区二区三区四区五区 | 欧美黑人狂野猛交老妇 | 狠狠干狠狠干狠狠干 | 拍真实国产伦偷精品 | 美丽的姑娘在线观看免费 | 丁香社区五月天 | 日韩少妇裸体做爰视频 | 8050午夜一级毛片久久亚洲欧 | wwww欧美 | 欧美日韩成人一区 | 人妻一区二区三区在线 | 日本不卡一区二区在线观看 | 玖玖视频在线 | 色噜噜成人 | 日韩亚洲在线观看 | 国产精品麻豆成人av电影艾秋 | 国产日韩网站 | 成人在线免费观看网址 | 一本一道久久综合 | 少妇三级 | 在线观看成人网 | 欧美大尺度床戏做爰 | 极品人妻一区二区 | 韩国伦理大片 | 伦理欧美 | 强伦轩人妻一区二区电影 | 亚洲精品在线免费 | 黄色激情视频在线观看 | 日本女优在线看 | 99精品视频99 | 久久九九99 | 成人做受视频试看60秒 | av电影在线观看 | 欧洲天堂网 | 婷婷免费视频 | 日本特黄一级 | 亚洲影库 | 国产永久免费观看 | 桃色一区二区 | 在线观看av日韩 | 国产精品日本一区二区在线播放 | 久久精品区 | 国产精品视频1区 | 日本一区二区三区视频免费看 | 亚洲天堂avav | 色多多网站 | 日韩夜夜高潮夜夜爽无码 | 亚洲激情图片区 | 久久亚洲一区二区三区四区五区 | 国产成人99久久亚洲综合精品 | 久久精品视 | 深夜福利1000 | 久久精品天堂 | 一区二区三区四区国产精品 | 一曲二曲三曲在线观看中文字幕动漫 | 男生插女生视频在线观看 | 国产成人精品一二三区 | 国产精品无码在线 | 国产图区 |