浠庢簮鐮佸垎鏋?MyBatis缂撳瓨 宸ヤ綔鍘熺悊

Posted 娣卞叆鍘熺悊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了浠庢簮鐮佸垎鏋?MyBatis缂撳瓨 宸ヤ綔鍘熺悊相关的知识,希望对你有一定的参考价值。

鐐瑰嚮馃憜钃濊壊鈥?nbsp;娣卞叆鍘熺悊鈥濓紝鍏虫敞骞垛€?/span>璁句负鏄熸爣鈥?/span>

鎶€鏈共璐э紝绗竴鏃堕棿鎺ㄩ€?/span>

鍓嶈█

鍦ㄨ绠楁満鐨勪笘鐣屼腑锛岀紦瀛樻棤澶勪笉鍦紝鎿嶄綔绯荤粺鏈夋搷浣滅郴缁熺殑缂撳瓨锛屾暟鎹簱涔熶細鏈夋暟鎹簱鐨勭紦瀛橈紝鍚勭涓棿浠跺Redis涔熸槸鐢ㄦ潵鍏呭綋缂撳瓨鐨勪綔鐢紝缂栫▼璇█涓張鍙互鍒╃敤鍐呭瓨鏉ヤ綔涓虹紦瀛樸€?/p>

鑷劧鐨勶紝浣滀负涓€娆句紭绉€鐨凮RM妗嗘灦锛孧yBatis涓張宀傝兘灏戝緱浜嗙紦瀛橈紝閭d箞鏈枃鐨勭洰鐨勫氨鏄甫棰嗗ぇ瀹朵竴璧锋帰绌朵竴涓婱yBatis鐨勭紦瀛樻槸濡備綍瀹炵幇鐨勩€傜粰鎴戜簲鍒嗛挓锛屽甫浣犲交搴曟帉鎻yBatis鐨勭紦瀛樺伐浣滃師鐞?/p>

涓轰粈涔堣缂撳瓨

鍦ㄨ绠楁満鐨勪笘鐣屼腑锛孋PU鐨勫鐞嗛€熷害鍙皳鏄竴椹綋鍏堬紝杩滆繙鐢╁紑浜嗗叾浠栨搷浣滐紝灏ゅ叾鏄疘/O鎿嶄綔锛岄櫎浜嗛偅绉岰PU瀵嗛泦鍨嬬殑绯荤粺锛屽叾浣欏ぇ閮ㄥ垎鐨勪笟鍔$郴缁熸€ц兘鐡堕鏈€鍚庢垨澶氭垨灏戦兘浼氬嚭鐜板湪I/O鎿嶄綔涓婏紝鎵€浠ヤ负浜嗗噺灏戠鐩樼殑I/O娆℃暟锛岄偅涔堢紦瀛樻槸蹇呬笉鍙皯鐨勶紝閫氳繃缂撳瓨鐨勪娇鐢ㄦ垜浠彲浠ュぇ澶у噺灏慖/O鎿嶄綔娆℃暟锛屼粠鑰屽湪涓€瀹氱▼搴︿笂寮ヨˉ浜咺/O鎿嶄綔鍜孋PU澶勭悊閫熷害涔嬮棿鐨勯缚娌熴€傝€屽湪鎴戜滑ORM妗嗘灦涓紩鍏ョ紦瀛樼殑鐩殑灏辨槸涓轰簡鍑忓皯璇诲彇鏁版嵁搴撶殑娆℃暟锛屼粠鑰屾彁鍗囨煡璇㈢殑鏁堢巼銆?/p>

MyBatis缂撳瓨

MyBatis涓殑缂撳瓨鐩稿叧绫婚兘鍦╟ache鍖呬笅闈紝鑰屼笖瀹氫箟浜嗕竴涓《绾ф帴鍙ache锛岄粯璁ゅ彧鏈変竴涓疄鐜扮被PerpetualCache锛孭erpetualCache涓槸鍐呴儴缁存姢浜嗕竴涓狧ashMap鏉ュ疄鐜扮紦瀛樸€?/p>

鍦ㄨ繖閲屾彃鍏ュ浘鐗囨弿杩?

涓嬪浘灏辨槸MyBatis涓紦瀛樼浉鍏崇被锛?/p>

浠庢簮鐮佸垎鏋?MyBatis缂撳瓨 宸ヤ綔鍘熺悊
鍦ㄨ繖閲屾彃鍏ュ浘鐗囨弿杩?

闇€瑕佹敞鎰忕殑鏄痙ecorators鍖呬笅闈㈢殑鎵€鏈夌被涔熷疄鐜颁簡Cache鎺ュ彛锛岄偅涔堜负浠€涔堟垜杩樻槸瑕佽Cache鍙湁涓€涓疄鐜扮被鍛紵鍏跺疄鐪嬪悕瀛楀氨鐭ラ亾浜嗭紝杩欎釜鍖呴噷闈㈠叏閮ㄦ槸瑁呴グ鍣紝涔熷氨鏄杩欏叾瀹炴槸瑁呴グ鍣ㄦā寮忕殑涓€绉嶅疄鐜般€?/p>

鎴戜滑闅忔剰鎵撳紑涓€涓楗板櫒锛?/p>

浠庢簮鐮佸垎鏋?MyBatis缂撳瓨 宸ヤ綔鍘熺悊
鍦ㄨ繖閲屾彃鍏ュ浘鐗囨弿杩?

鍙互鐪嬪埌锛屾渶缁堥兘鏄皟鐢ㄤ簡delegate鏉ュ疄鐜帮紝鍙槸灏嗛儴鍒嗗姛鑳藉仛浜嗗寮猴紝鍏舵湰韬兘闇€瑕佷緷璧朇ache鐨勫敮涓€瀹炵幇绫籔erpetualCache(鍥犱负瑁呴グ鍣ㄥ唴闇€瑕佷紶鍏ache瀵硅薄锛屾晠鑰屽彧鑳戒紶鍏erpetualCache瀵硅薄锛屽洜涓烘帴鍙f槸鏃犳硶鐩存帴new鍑烘潵浼犺繘鍘荤殑)銆?/p>

鍦∕yBatis涓瓨鍦ㄤ袱绉嶇紦瀛橈紝鍗充竴绾х紦瀛樺拰浜岀骇缂撳瓨銆?/p>

涓€绾х紦瀛?/span>

涓€绾х紦瀛樹篃鍙湰鍦扮紦瀛橈紝鍦∕yBatis涓紝涓€绾х紦瀛樻槸鍦ㄤ細璇?SqlSession)灞傞潰瀹炵幇鐨勶紝杩欏氨璇存槑涓€绾х紦瀛樹綔鐢ㄨ寖鍥村彧鑳藉湪鍚屼竴涓猄qlSession涓紝璺⊿qlSession鏄棤鏁堢殑銆?/p>

MyBatis涓竴绾х紦瀛樻槸榛樿寮€鍚殑锛屼笉闇€瑕佷换浣曢厤缃€?/p>

鎴戜滑鍏堟潵鐪嬩竴涓緥瀛愰獙璇佷竴涓嬩竴绾х紦瀛樻槸涓嶆槸鐪熺殑瀛樺湪锛屼綔鐢ㄨ寖鍥村張鏄笉鏄湡鐨勫彧鏄鍚屼竴涓猄qlSession鏈夋晥銆?/p>

涓€绾х紦瀛樼湡鐨勫瓨鍦ㄥ悧

package com.lonelyWolf.mybatis;

import com.lonelyWolf.mybatis.mapper.UserAddressMapper;
import com.lonelyWolf.mybatis.mapper.UserMapper;
import com.lonelyWolf.mybatis.model.LwUser;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class TestMyBatisCache {
    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        //璇诲彇mybatis-config閰嶇疆鏂囦欢
        InputStream inputStream = Resources.getResourceAsStream(resource);
        //鍒涘缓SqlSessionFactory瀵硅薄
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //鍒涘缓SqlSession瀵硅薄
        SqlSession session = sqlSessionFactory.openSession();

        UserMapper userMapper = session.getMapper(UserMapper.class);
        List<LwUser> userList =  userMapper.selectUserAndJob();
        List<LwUser> userList2 =  userMapper.selectUserAndJob();
    }
}

鎵ц鍚庯紝杈撳嚭缁撴灉濡備笅锛?/p>

浠庢簮鐮佸垎鏋?MyBatis缂撳瓨 宸ヤ綔鍘熺悊
鍦ㄨ繖閲屾彃鍏ュ浘鐗囨弿杩?

鎴戜滑鍙互鐪嬪埌锛宻ql璇彞鍙墦鍗颁簡涓€娆★紝杩欏氨璇存槑绗?娆$敤鍒颁簡缂撳瓨锛岃繖涔熻冻浠ヨ瘉鏄庝竴绾х紦瀛樼‘瀹炴槸瀛樺湪鐨勮€屼笖榛樿灏辨槸鏄紑鍚殑銆?/p>

涓€绾х紦瀛樹綔鐢ㄨ寖鍥?/h3>

鐜板湪鎴戜滑鍐嶆潵楠岃瘉涓€涓嬩竴绾х紦瀛樻槸鍚︾湡鐨勫彧瀵瑰悓涓€涓猄qlSession鏈夋晥锛屾垜浠涓婇潰鐨勭ず渚嬩唬鐮佽繘琛屽涓嬫敼鍙橈細

 SqlSession session1 = sqlSessionFactory.openSession();
 SqlSession session2 = sqlSessionFactory.openSession();

 UserMapper userMapper1 = session1.getMapper(UserMapper.class);
 UserMapper userMapper2 = session2.getMapper(UserMapper.class);
 List<LwUser> userList =  userMapper1.selectUserAndJob();
 List<LwUser> userList2 =  userMapper2.selectUserAndJob();

杩欐椂鍊欏啀娆¤繍琛岋紝杈撳嚭缁撴灉濡備笅锛?/p>

浠庢簮鐮佸垎鏋?MyBatis缂撳瓨 宸ヤ綔鍘熺悊
鍦ㄨ繖閲屾彃鍏ュ浘鐗囨弿杩?

鍙互鐪嬪埌锛屾墦鍗颁簡2娆★紝娌℃湁鐢ㄥ埌缂撳瓨锛屼篃灏辨槸涓嶅悓SqlSession涓笉鑳藉叡浜竴绾х紦瀛樸€?/p>

涓€绾х紦瀛樺師鐞嗗垎鏋?/h3>

棣栧厛璁╂垜浠潵鎯充竴鎯筹紝鏃㈢劧涓€绾х紦瀛樼殑浣滅敤鍩熷彧瀵瑰悓涓€涓猄qlSession鏈夋晥锛岄偅涔堜竴绾х紦瀛樺簲璇ュ瓨鍌ㄥ湪鍝噷姣旇緝鍚堥€傛槸鍛紵

鏄殑锛岃嚜鐒舵槸瀛樺偍鍦⊿qlSession鍐呮槸鏈€鍚堥€傜殑锛岄偅鎴戜滑鏉ョ湅鐪婼qlSession鐨勫敮涓€瀹炵幇绫籇efaultSqlSession锛?/p>

浠庢簮鐮佸垎鏋?MyBatis缂撳瓨 宸ヤ綔鍘熺悊
鍦ㄨ繖閲屾彃鍏ュ浘鐗囨弿杩?

鏃㈢劧杩欐牱锛岄偅鎴戜滑灏辫繘鍘荤湅鐪婨xecutor鐨勫疄鐜扮被BaseExecutor锛?/p>

浠庢簮鐮佸垎鏋?MyBatis缂撳瓨 宸ヤ綔鍘熺悊
鍦ㄨ繖閲屾彃鍏ュ浘鐗囨弿杩?

鐪嬪埌鏋滅劧鏈変竴涓猯ocalCache銆傝€屼笂闈㈡垜浠湁鎻愬埌PerpetualCache鍐呯紦瀛樻槸鐢ㄤ竴涓狧ashMap鏉ュ瓨鍌ㄧ紦瀛樼殑锛岄偅涔堟帴涓嬫潵澶у鑲畾灏辨湁浠ヤ笅闂锛?/p>

  • 缂撳瓨鏄粈涔堟椂鍊欏垱寤虹殑锛?/p>

  • 缂撳瓨鐨刱ey鏄€庝箞瀹氫箟鐨勶紵

  • 缂撳瓨鍦ㄤ綍鏃朵娇鐢?/p>

  • 缂撳瓨鍦ㄤ粈涔堟椂鍊欎細澶辨晥?

鎺ヤ笅鏉ュ氨璁╂垜浠€愪竴鍒嗘瀽

涓€绾х紦瀛楥acheKey鐨勬瀯鎴?/h4>

鏃㈢劧缂撳瓨閭d箞鑲畾鏄拡瀵圭殑鏌ヨ璇彞锛屼竴绾х紦瀛樼殑鍒涘缓灏辨槸鍦˙aseExecutor涓殑query鏂规硶鍐呭垱寤虹殑锛?/p>

浠庢簮鐮佸垎鏋?MyBatis缂撳瓨 宸ヤ綔鍘熺悊
鍦ㄨ繖閲屾彃鍏ュ浘鐗囨弿杩?

createCacheKey杩欎釜鏂规硶鐨勪唬鐮佸氨涓嶈创浜嗭紝鍦ㄨ繖閲屾垜鎬荤粨浜嗕竴涓婥acheKey鐨勭粍鎴愶紝CacheKey涓昏鏄敱浠ヤ笅6閮ㄥ垎缁勬垚

  • 1銆佸皢Statement涓殑id娣诲姞鍒癈acheKey瀵硅薄涓殑updateList灞炴€?/p>

  • 2銆佸皢offset(鍒嗛〉鍋忕Щ閲?娣诲姞鍒癈acheKey瀵硅薄涓殑updateList灞炴€?濡傛灉娌℃湁鍒嗛〉鍒欓粯璁?)

  • 3銆佸皢limit(姣忛〉鏄剧ず鐨勬潯鏁?娣诲姞鍒癈acheKey瀵硅薄涓殑updateList灞炴€?濡傛灉娌℃湁鍒嗛〉鍒欓粯璁nteger.MAX_VALUE)

  • 4銆佸皢sql璇彞(鍖呮嫭鍗犱綅绗?)娣诲姞鍒癈acheKey瀵硅薄涓殑updateList灞炴€?/p>

  • 5銆佸惊鐜敤鎴蜂紶鍏ョ殑鍙傛暟锛屽苟灏嗘瘡涓弬鏁版坊鍔犲埌CacheKey瀵硅薄涓殑updateList灞炴€?/p>

  • 6銆佸鏋滄湁閰嶇疆Environment锛屽垯灏咵nvironment涓殑id娣诲姞鍒癈acheKey瀵硅薄涓殑updateList灞炴€?/p>

涓€绾х紦瀛樼殑浣跨敤

鍒涘缓瀹孋acheKey涔嬪悗锛屾垜浠户缁繘鍏uery鏂规硶锛?/p>

浠庢簮鐮佸垎鏋?MyBatis缂撳瓨 宸ヤ綔鍘熺悊
鍦ㄨ繖閲屾彃鍏ュ浘鐗囨弿杩?

鍙互鐪嬪埌锛屽湪鏌ヨ涔嬪墠灏变細鍘籰ocalCache涓牴鎹瓹acheKey瀵硅薄鏉ヨ幏鍙栫紦瀛橈紝鑾峰彇涓嶅埌鎵嶄細璋冪敤鍚庨潰鐨剄ueryFromDatabase鏂规硶

涓€绾х紦瀛樼殑鍒涘缓

queryFromDatabase鏂规硶涓細灏嗘煡璇㈠緱鍒扮殑缁撴灉瀛樺偍鍒發ocalCache涓?/p>

浠庢簮鐮佸垎鏋?MyBatis缂撳瓨 宸ヤ綔鍘熺悊
鍦ㄨ繖閲屾彃鍏ュ浘鐗囨弿杩?

涓€绾х紦瀛樹粈涔堟椂鍊欎細琚竻闄?/h4>

涓€绾х紦瀛樼殑娓呴櫎涓昏鏈変互涓嬩袱涓湴鏂癸細

  • 1銆佸氨鏄幏鍙栫紦瀛樹箣鍓嶄細鍏堣繘琛屽垽鏂敤鎴锋槸鍚﹂厤缃簡flushCache=true灞炴€э紙鍙傝€冧竴绾х紦瀛樼殑鍒涘缓浠g爜鎴浘锛夛紝濡傛灉閰嶇疆浜嗗垯浼氭竻闄や竴绾х紦瀛樸€?/p>

  • 2銆丮yBatis鍏ㄥ眬閰嶇疆灞炴€ocalCacheScope閰嶇疆涓篠tatement鏃讹紝閭d箞瀹屾垚涓€娆℃煡璇㈠氨浼氭竻闄ょ紦瀛樸€?/p>

  • 3銆佸湪鎵цcommit锛宺ollback锛寀pdate鏂规硶鏃朵細娓呯┖涓€绾х紦瀛樸€?/p>

PS锛氬埄鐢ㄦ彃浠舵垜浠篃鍙互鑷繁鍘诲皢缂撳瓨娓呴櫎锛屽悗闈㈡垜浠細浠嬬粛鎻掍欢鐩稿叧鐭ヨ瘑銆?/p>

浜岀骇缂撳瓨

涓€绾х紦瀛樺洜涓哄彧鑳藉湪鍚屼竴涓猄qlSession涓叡浜紝鎵€浠ヤ細瀛樺湪涓€涓棶棰橈紝鍦ㄥ垎甯冨紡鎴栬€呭绾跨▼鐨勭幆澧冧笅锛屼笉鍚屼細璇濅箣闂村浜庣浉鍚岀殑鏁版嵁鍙兘浼氫骇鐢熶笉鍚岀殑缁撴灉锛屽洜涓鸿法浼氳瘽淇敼浜嗘暟鎹槸涓嶈兘浜掔浉鎰熺煡鐨勶紝鎵€浠ュ氨鏈夊彲鑳藉瓨鍦ㄨ剰鏁版嵁鐨勯棶棰橈紝姝e洜涓轰竴绾х紦瀛樺瓨鍦ㄨ繖绉嶄笉瓒筹紝鎵€浠ユ垜浠渶瑕佷竴绉嶄綔鐢ㄥ煙鏇村ぇ鐨勭紦瀛橈紝杩欏氨鏄簩绾х紦瀛樸€?/p>

浜岀骇缂撳瓨鐨勪綔鐢ㄨ寖鍥?/h3>

涓€绾х紦瀛樹綔鐢ㄥ煙鏄疭qlSession绾у埆锛屾墍浠ュ畠瀛樺偍鐨凷qlSession涓殑BaseExecutor涔嬩腑锛屼絾鏄簩绾х紦瀛樼洰鐨勫氨鏄瀹炵幇浣滅敤鑼冨洿鏇村箍锛岄偅鑲畾鏄瀹炵幇璺ㄤ細璇濆叡浜殑锛屽湪MyBatis涓簩绾х紦瀛樼殑浣滅敤鍩熸槸namespace锛屼篃灏辨槸浣滅敤鑼冨洿鏄悓涓€涓懡鍚嶇┖闂达紝鎵€浠ュ緢鏄剧劧浜岀骇缂撳瓨鏄渶瑕佸瓨鍌ㄥ湪SqlSession涔嬪鐨勶紝閭d箞浜岀骇缂撳瓨搴旇瀛樺偍鍦ㄥ摢閲屽悎閫傚憿锛?/p>

鍦∕yBatis涓负浜嗗疄鐜颁簩绾х紦瀛橈紝涓撻棬鐢ㄤ簡涓€涓楗板櫒鏉ョ淮鎶わ細CachingExecutor銆?/p>

濡備綍寮€鍚簩绾х紦瀛?/h3>

浜岀骇缂撳瓨鐩稿叧鐨勯厤缃湁涓変釜鍦版柟锛?/p>

1銆乵ybatis-config涓湁涓€涓叏灞€閰嶇疆灞炴€э紝杩欎釜涓嶉厤缃篃琛岋紝鍥犱负榛樿灏辨槸true銆?/p>

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

鎯宠缁嗕簡瑙ybatis-config鐨勫彲浠ョ偣鍑昏繖閲岋細https://blog.csdn.net/zwx900102/article/details/108492263

2銆佸湪Mapper鏄犲皠鏂囦欢鍐呴渶瑕侀厤缃紦瀛樻爣绛撅細

<cache/>
鎴?br class="mq-234"><cache-ref namespace="com.lonelyWolf.mybatis.mapper.UserAddressMapper"/>

鎯宠缁嗕簡瑙apper鏄犲皠鐨勬墍鏈夋爣绛惧睘鎬ч厤缃彲浠ョ偣鍑昏繖閲岋細

https://blog.csdn.net/zwx900102/article/details/108534736

3銆佸湪select鏌ヨ璇彞鏍囩涓婇厤缃畊seCache灞炴€э紝濡備笅锛?/p>

<select id="selectUserAndJob" resultMap="JobResultMap2" useCache="true">
        select * from lw_user
    </select>

涓嶈繃寮€鍚簩绾х紦瀛樼殑鏃跺€欐湁涓ょ偣闇€瑕佹敞鎰忥細

  • 闇€瑕乧ommit浜嬪姟涔嬪悗鎵嶄細鐢熸晥
  • 濡傛灉浣跨敤鐨勬槸榛樿缂撳瓨锛岄偅涔堢粨鏋滈泦瀵硅薄闇€瑕佸疄鐜板簭鍒楀寲鎺ュ彛(Serializable)

濡傛灉涓嶅疄鐜板簭鍒楀寲鎺ュ彛鍒欎細鎶ュ涓嬮敊璇細

浠庢簮鐮佸垎鏋?MyBatis缂撳瓨 宸ヤ綔鍘熺悊
鍦ㄨ繖閲屾彃鍏ュ浘鐗囨弿杩?

鎺ヤ笅鏉ユ垜浠€氳繃涓€涓緥瀛愭潵楠岃瘉涓€涓嬩簩绾х紦瀛樼殑瀛樺湪锛岃繕鏄敤涓婇潰涓€绾х紦瀛樼殑渚嬪瓙杩涜濡備笅鏀归€狅細

 SqlSession session1 = sqlSessionFactory.openSession();
        UserMapper userMapper1 = session1.getMapper(UserMapper.class);
        List<LwUser> userList =  userMapper1.selectUserAndJob();
        session1.commit();//娉ㄦ剰杩欓噷闇€瑕乧ommit,鍚﹀垯缂撳瓨涓嶄細鐢熸晥

        SqlSession session2 = sqlSessionFactory.openSession();
        UserMapper userMapper2 = session2.getMapper(UserMapper.class);
        List<LwUser> userList2 =  userMapper2.selectUserAndJob();

鐒跺悗UserMapper.xml鏄犲皠鏂囦欢涓紝鏂板濡備笅閰嶇疆锛?/p>

<cache/>

杩愯浠g爜锛岃緭鍑哄涓嬬粨鏋?

浠庢簮鐮佸垎鏋?MyBatis缂撳瓨 宸ヤ綔鍘熺悊
鍦ㄨ繖閲屾彃鍏ュ浘鐗囨弿杩?

涓婇潰杈撳嚭缁撴灉涓彧杈撳嚭浜嗕竴娆ql锛岃鏄庣敤鍒颁簡缂撳瓨锛岃€屽洜涓烘垜浠槸璺ㄤ細璇濈殑锛屾墍浠ヨ偗瀹氬氨鏄簩绾х紦瀛樼敓鏁堜簡銆?/p>

浜岀骇缂撳瓨鍘熺悊鍒嗘瀽

涓婇潰鎴戜滑鎻愬埌浜岀骇缂撳瓨鏄€氳繃CachingExecutor瀵硅薄鏉ュ疄鐜扮殑锛岄偅涔堝氨璁╂垜浠厛鏉ョ湅鐪嬭繖涓璞★細

浠庢簮鐮佸垎鏋?MyBatis缂撳瓨 宸ヤ綔鍘熺悊
鍦ㄨ繖閲屾彃鍏ュ浘鐗囨弿杩?

鎴戜滑鐪嬪埌CachingExecutor涓彧鏈?涓睘鎬э紝绗?涓睘鎬т笉鐢ㄨ浜嗭紝鍥犱负CachingExecutor鏈韩灏辨槸Executor鐨勫寘瑁呭櫒锛屾墍浠ュ睘鎬ransactionalCacheManager鑲畾灏辨槸鐢ㄦ潵绠$悊浜岀骇缂撳瓨鐨勶紝鎴戜滑鍐嶈繘鍘荤湅鐪婽ransactionalCacheManager瀵硅薄鏄浣曠鐞嗙紦瀛樼殑锛?/p>

浠庢簮鐮佸垎鏋?MyBatis缂撳瓨 宸ヤ綔鍘熺悊
鍦ㄨ繖閲屾彃鍏ュ浘鐗囨弿杩?

TransactionalCacheManager鍐呴儴闈炲父绠€鍗曪紝涔熸槸缁存姢浜嗕竴涓狧ashMap鏉ュ瓨鍌ㄧ紦瀛樸€?/p>

HashMap涓殑value鏄竴涓猅ransactionalCache瀵硅薄锛岀户鎵夸簡Cache銆?/p>

浠庢簮鐮佸垎鏋?MyBatis缂撳瓨 宸ヤ綔鍘熺悊
鍦ㄨ繖閲屾彃鍏ュ浘鐗囨弿杩?

娉ㄦ剰涓婇潰鏈変竴涓睘鎬ф槸涓存椂瀛樺偍浜岀骇缂撳瓨鐨勶紝涓轰粈涔堣鏈夎繖涓睘鎬э紝鎴戜滑涓嬮潰浼氳В閲娿€?/p>

浜岀骇缂撳瓨鐨勫垱寤哄拰浣跨敤

鎴戜滑鍦ㄨ鍙杕ybatis-config鍏ㄥ眬閰嶇疆鏂囦欢鐨勬椂鍊欎細鏍规嵁鎴戜滑閰嶇疆鐨凟xecutor绫诲瀷鏉ュ垱寤哄搴旂殑涓夌Executor涓殑涓€绉嶏紝鐒跺悗濡傛灉鎴戜滑寮€鍚簡浜岀骇缂撳瓨涔嬪悗锛屽彧瑕佸紑鍚?鍏ㄥ眬閰嶇疆鏂囦欢涓厤缃负true)灏变細浣跨敤CachingExecutor鏉ュ鎴戜滑鐨勪笁绉嶅熀鏈珽xecutor杩涜鍖呰锛屽嵆浣縈apper.xml鏄犲皠鏂囦欢娌℃湁寮€鍚篃浼氳繘琛屽寘瑁呫€?/p>

鎺ヤ笅鏉ユ垜浠湅鐪婥achingExecutor涓殑query鏂规硶锛?/p>

浠庢簮鐮佸垎鏋?MyBatis缂撳瓨 宸ヤ綔鍘熺悊
鍦ㄨ繖閲屾彃鍏ュ浘鐗囨弿杩?

涓婇潰鏂规硶澶ц嚧缁忚繃濡備笅娴佺▼锛?/p>

  • 1銆佸垱寤轰竴绾х紦瀛樼殑CacheKey

  • 2銆佽幏鍙栦簩绾х紦瀛?/p>

  • 3銆佸鏋滄病鏈夎幏鍙栧埌浜岀骇缂撳瓨鍒欐墽琛岃鍖呰鐨凟xecutor瀵硅薄涓殑query鏂规硶锛屾鏃朵細璧颁竴绾х紦瀛樹腑鐨勬祦绋嬨€?/p>

  • 4銆佹煡璇㈠埌缁撴灉涔嬪悗灏嗙粨鏋滆繘琛岀紦瀛樸€?/p>

闇€瑕佹敞鎰忕殑鏄湪浜嬪姟鎻愪氦涔嬪墠锛屽苟涓嶄細鐪熸瀛樺偍鍒颁簩绾х紦瀛橈紝鑰屾槸鍏堝瓨鍌ㄥ埌涓€涓复鏃跺睘鎬э紝绛変簨鍔℃彁浜や箣鍚庢墠浼氱湡姝e瓨鍌ㄥ埌浜岀骇缂撳瓨銆傝繖涔堝仛鐨勭洰鐨勫氨鏄槻姝㈣剰璇汇€傚洜涓哄亣濡備綘鍦ㄤ竴涓簨鍔′腑淇敼浜嗘暟鎹紝鐒跺悗鍘绘煡璇紝杩欐椂鍊欑洿鎺ョ紦瀛樹簡锛岄偅涔堝亣濡備簨鍔″洖婊氫簡鍛紵鎵€浠ヨ繖閲屼細鍏堜复鏃跺瓨鍌ㄤ竴涓嬨€?/p>

鎵€浠ユ垜浠湅涓€涓媍ommit鏂规硶锛?/p>

浠庢簮鐮佸垎鏋?MyBatis缂撳瓨 宸ヤ綔鍘熺悊
鍦ㄨ繖閲屾彃鍏ュ浘鐗囨弿杩?

浜岀骇缂撳瓨濡備綍杩涜鍖呰

鏈€寮€濮嬫垜浠彁鍒颁簡涓€浜涚紦瀛樼殑鍖呰绫伙紝杩欎簺閮藉埌搴曟湁浠€涔堢敤鍛紵

鍦ㄥ洖绛旇繖涓棶棰樹箣鍓嶏紝鎴戜滑鍏堟柇鐐逛竴涓嬬湅鐪嬭幏鍙栧埌鐨勪簩绾х紦瀛橀暱鍟ユ牱锛?/p>

浠庢簮鐮佸垎鏋?MyBatis缂撳瓨 宸ヤ綔鍘熺悊
鍦ㄨ繖閲屾彃鍏ュ浘鐗囨弿杩?

浠庝笂闈㈠彲浠ョ湅鍒帮紝缁忚繃浜嗗眰灞傚寘瑁咃紝浠庡唴鍒板涓€娆$粡杩囧涓嬪寘瑁咃細

  • 1銆丳erpetualCache锛氱涓€灞傜紦瀛橈紝杩欎釜鏄紦瀛樼殑鍞竴瀹炵幇绫伙紝鑲畾闇€瑕併€?/p>

  • 2銆丩ruCache锛氫簩绾х紦瀛樻窐姹版満鍒朵箣涓€銆傚洜涓烘垜浠厤缃殑榛樿鏈哄埗锛岃€岄粯璁ゅ氨鏄疞RU绠楁硶娣樻卑鏈哄埗銆傛窐姹版満鍒舵€诲叡鏈?涓紝鎴戜滑鍙互鑷繁杩涜鎵嬪姩閰嶇疆銆?/p>

  • 3銆丼erializedCache锛氬簭鍒楀寲缂撳瓨銆傝繖灏辨槸涓轰粈涔堝紑鍚簡榛樿浜岀骇缂撳瓨鎴戜滑鐨勭粨鏋滈泦瀵硅薄闇€瑕佸疄鐜板簭鍒楀寲鎺ュ彛銆?/p>

  • 4銆丩oggingCache锛氭棩蹇楃紦瀛樸€?/p>

  • 5銆丼ynchronizedCache锛氬悓姝ョ紦瀛樻満鍒躲€傝繖涓槸涓轰簡淇濊瘉澶氱嚎绋嬫満鍒朵笅鐨勭嚎绋嬪畨鍏ㄦ€с€?/p>

涓嬮潰灏辨槸MyBatis涓墍鏈夌紦瀛樼殑鍖呰姹囨€伙細

浠庢簮鐮佸垎鏋?MyBatis缂撳瓨 宸ヤ綔鍘熺悊

浜岀骇缂撳瓨搴旇寮€鍚悧

鏃㈢劧涓€绾х紦瀛橀粯璁ゆ槸寮€鍚殑锛岃€屼簩绾х紦瀛樻槸闇€瑕佹垜浠墜鍔ㄥ紑鍚殑锛岄偅涔堟垜浠粈涔堟椂鍊欏簲璇ュ紑鍚簩绾х紦瀛樺憿锛?/p>

1銆佸洜涓烘墍鏈夌殑update鎿嶄綔(insert,delete,uptede)閮戒細瑙﹀彂缂撳瓨鐨勫埛鏂帮紝浠庤€屽鑷翠簩绾х紦瀛樺け鏁堬紝鎵€浠ヤ簩绾х紦瀛橀€傚悎鍦ㄨ澶氬啓灏戠殑鍦烘櫙涓紑鍚€?/p>

2銆佸洜涓轰簩绾х紦瀛橀拡瀵圭殑鏄悓涓€涓猲amespace锛屾墍浠ュ缓璁槸鍦ㄥ崟琛ㄦ搷浣滅殑Mapper涓娇鐢紝鎴栬€呮槸鍦ㄧ浉鍏宠〃鐨凪apper鏂囦欢涓叡浜悓涓€涓紦瀛樸€?/p>

鑷畾涔夌紦瀛?/span>

涓€绾х紦瀛樺彲鑳藉瓨鍦ㄨ剰璇绘儏鍐碉紝閭d箞浜岀骇缂撳瓨鏄惁涔熷彲鑳藉瓨鍦ㄥ憿锛?/p>

鏄殑锛岄粯璁ょ殑浜岀骇缂撳瓨姣曠珶涔熸槸瀛樺偍鍦ㄦ湰鍦扮紦瀛橈紝鎵€浠ュ浜庡井鏈嶅姟涓嬫槸鍙兘鍑虹幇鑴忚鐨勬儏鍐电殑锛屾墍浠ヨ繖鏃跺€欐垜浠彲鑳戒細闇€瑕佽嚜瀹氫箟缂撳瓨锛屾瘮濡傚埄鐢╮edis鏉ュ瓨鍌ㄧ紦瀛橈紝鑰屼笉鏄瓨鍌ㄥ湪鏈湴鍐呭瓨褰撲腑銆?/p>

MyBatis瀹樻柟鎻愪緵鐨勭涓夋柟缂撳瓨

MyBatis瀹樻柟涔熸彁渚涗簡涓€浜涚涓夋柟缂撳瓨鐨勬敮鎸侊紝濡傦細encache鍜宺edis銆備笅闈㈡垜浠互redis涓轰緥鏉ユ紨绀轰竴涓嬶細

寮曞叆pom鏂囦欢锛?/p>

<dependency>
    <groupId>org.mybatis.caches</groupId>
    <artifactId>mybatis-redis</artifactId>
    <version>1.0.0-beta2</version>
</dependency>

鐒跺悗缂撳瓨閰嶇疆濡備笅锛?/p>

<cache type="org.mybatis.caches.redis.RedisCache"></cache>

鐒跺悗鍦ㄩ粯璁ょ殑resource璺緞涓嬫柊寤轰竴涓猺edis.properties鏂囦欢锛?/p>

host=localhost
port=6379

鐒跺悗鎵ц涓婇潰鐨勭ず渚嬶紝鏌ョ湅Cache锛屽凡缁忚Redis鍖呰锛?/p>

浠庢簮鐮佸垎鏋?MyBatis缂撳瓨 宸ヤ綔鍘熺悊
鍦ㄨ繖閲屾彃鍏ュ浘鐗囨弿杩?

鑷繁瀹炵幇浜岀骇缂撳瓨

濡傛灉瑕佸疄鐜颁竴涓嚜宸辩殑缂撳瓨鐨勮瘽锛岄偅涔堟垜浠彧闇€瑕佹柊寤轰竴涓被瀹炵幇Cache鎺ュ彛灏卞ソ浜嗭紝鐒跺悗閲嶅啓鍏朵腑鐨勬柟娉?濡備笅锛?/p>

package com.lonelyWolf.mybatis.cache;

import org.apache.ibatis.cache.Cache;

public class MyCache implements Cache {
    @Override
    public String getId() {
        return null;
    }
    @Override
    public void putObject(Object o, Object o1) {
        
    }
    @Override
    public Object getObject(Object o) {
        return null;
    }

    @Override
    public Object removeObject(Object o) {
        return null;
    }

    @Override
    public void clear() {
    }

    @Override
    public int getSize() {
        return 0;
    }
}

涓婇潰鑷畾涔夌殑缂撳瓨涓?鎴戜滑鍙渶瑕佸湪瀵瑰簲鏂规硶锛屽putObject鏂规硶锛屾垜浠妸缂撳瓨瀛樺埌鎴戜滑鎯冲瓨鐨勫湴鏂瑰氨琛屼簡锛屾柟娉曞叏閮ㄩ噸鍐欎箣鍚庯紝鐒跺悗閰嶇疆鐨勬椂鍊檛ype閰嶄笂鎴戜滑鑷繁鐨勭被灏卞彲浠ュ疄鐜颁簡锛屽湪杩欓噷鎴戜滑灏变笉鍋氭紨绀轰簡

鎬荤粨

鏈枃涓昏鍒嗘瀽浜哅yBatis鐨勭紦瀛樻槸濡備綍瀹炵幇鐨勶紝骞朵笖鍒嗗埆婕旂ず浜嗕竴绾х紦瀛樺拰浜岀骇缂撳瓨锛屽苟鍒嗘瀽浜嗕竴绾х紦瀛樺拰浜岀骇缂撳瓨鎵€瀛樺湪鐨勯棶棰橈紝鏈€鍚庝篃浠嬬粛浜嗗浣曚娇鐢ㄧ涓夋柟缂撳瓨鍜屽浣曡嚜瀹氫箟鎴戜滑鑷繁鐨勭紦瀛橈紝閫氳繃鏈枃锛屾垜鎯冲ぇ瀹跺簲璇ュ彲浠ュ交搴曟帉鎻yBatis鐨勭紦瀛樺伐浣滃師鐞嗕簡銆?/p>

浣滆€咃細鍙屽瓙瀛ょ嫾blog.csdn.net/zwx900102/article/details/108696005



  

-娣卞叆鍘熺悊-  

   鐭ュ叾鐒跺苟鐭ュ叾鎵€浠ョ劧    

浠庢簮鐮佸垎鏋?MyBatis缂撳瓨 宸ヤ綔鍘熺悊



以上是关于浠庢簮鐮佸垎鏋?MyBatis缂撳瓨 宸ヤ綔鍘熺悊的主要内容,如果未能解决你的问题,请参考以下文章

鍒嗗竷寮忕紦瀛樺涔犳€荤粨

鐮佸啘鎵嬭 | Spring WebFlux瀹炴垬浠ュ強鍘熺悊娴呮瀽

17.4-uC/OS-III娑堟伅绠$悊锛堜换鍔℃秷鎭槦鍒楋級

MyBatis鎺㈢┒-----缂撳瓨鏈哄埗璇﹁В