{"id":3764,"date":"2024-03-02T09:07:51","date_gmt":"2024-03-02T01:07:51","guid":{"rendered":"https:\/\/xinchewhd.com.cn\/?p=3764"},"modified":"2024-03-02T09:07:53","modified_gmt":"2024-03-02T01:07:53","slug":"mybaits-mybatisplus%e5%81%9a%e8%a1%8c%e9%94%81","status":"publish","type":"post","link":"https:\/\/xinchewhd.com.cn\/index.php\/mybatis\/mybaits-mybatisplus%e5%81%9a%e8%a1%8c%e9%94%81\/","title":{"rendered":"Mybaits\/MybatisPlus\u505a\u884c\u9501"},"content":{"rendered":"\n<p>\u65b9\u6cd5\u4e00<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Autowired\nprivate SqlSessionTemplate sqlSessionTemplate;\n\nWrapper&lt;TableDo > wrapper = Wrappers.&lt;TableDo >query().lambda()\n                .eq(!CommUtil.isEmpty(id), TableDo ::getId, id)\n                .last(\"FOR UPDATE\");\n        SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession();\n\/\/ \u4e1a\u52a1\u64cd\u4f5c\nTableDo tableDo = tableMapper.selectOne(wrapper);\n        try {\n            tableMapper.updateById(tableDo)\n            sqlSession.commit(); \/\/ \u91ca\u653e\u6240\u6709\u9501\n        } catch (Exception e) {\n            sqlSession.rollback(); \/\/ \u56de\u6eda\u4e8b\u52a1\uff0c\u91ca\u653e\u6240\u6709\u9501\n        }<\/code><\/pre>\n\n\n\n<p>\u65b9\u6cd5\u4e8c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>QueryWrapper&lt;User> queryWrapper = new QueryWrapper&lt;>();\nqueryWrapper.eq(\"id\", 1).last(\"FOR UPDATE\");\nUser user = userMapper.selectOne(queryWrapper);\nSystem.out.println(user);\n\ntry {\n    \/\/ \u4e1a\u52a1\u64cd\u4f5c\n    userMapper.updateById(user);\n    userMapper.update(user, queryWrapper);\n    sqlSession.commit(); \/\/ \u91ca\u653e\u6240\u6709\u9501\n} catch (Exception e) {\n    sqlSession.rollback(); \/\/ \u56de\u6eda\u4e8b\u52a1\uff0c\u91ca\u653e\u6240\u6709\u9501\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u4f7f\u7528\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/so.csdn.net\/so\/search?q=MyBatis-Plus&amp;spm=1001.2101.3001.7020\" target=\"_blank\" rel=\"nofollow\" >MyBatis-Plus<\/a>\u00a0\u67e5\u8be2\u65f6\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5728 SQL \u8bed\u53e5\u4e2d\u6dfb\u52a0 FOR UPDATE \u5b50\u53e5\u6765\u5b9e\u73b0\u52a0\u4e0a\u6570\u636e\u884c\u7684\u6392\u5b83\u9501\u3002\u540c\u6837\u5730\uff0c\u8981\u91ca\u653e\u9501\u9700\u8981\u6267\u884c commit \u6216 rollback \u64cd\u4f5c\uff0c\u8fd9\u6837\u4f1a\u81ea\u52a8\u91ca\u653e\u6240\u6709\u7684\u9501\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u8c03\u7528 updateById() \u6216 update() \u65b9\u6cd5\u6267\u884c\u4e1a\u52a1\u64cd\u4f5c\uff0c\u7136\u540e\u8c03\u7528 commit() \u65b9\u6cd5\u63d0\u4ea4\u4e8b\u52a1\uff0c\u91ca\u653e\u6240\u6709\u9501\u3002\u5982\u679c\u64cd\u4f5c\u51fa\u73b0\u5f02\u5e38\uff0c\u5219\u8c03\u7528 rollback() \u65b9\u6cd5\u56de\u6eda\u4e8b\u52a1\uff0c\u540c\u65f6\u4e5f\u4f1a\u91ca\u653e\u6240\u6709\u9501\u3002<\/p>\n\n\n\n<p>\u5173\u95ed SqlSession<br>\u6700\u540e\u9700\u8981\u8bb0\u5f97\u5173\u95ed SqlSession \u5bf9\u8c61\uff0c\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n\n\n\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u4f7f\u7528 MyBatis-Plus \u67e5\u8be2\u65f6\u52a0\u4e0a\u6570\u636e\u884c\u7684\u6392\u5b83\u9501\uff0c\u5e76\u5728\u9700\u8981\u7684\u65f6\u5019\u91ca\u653e\u9501\u3002<\/p>\n","raw":"","protected":false},"excerpt":{"rendered":"<p>\u65b9\u6cd5\u4e00 \u65b9\u6cd5\u4e8c \u5728\u4f7f\u7528\u00a0MyBatis-Plus\u00a0\u67e5\u8be2\u65f6\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5728 SQL \u8bed\u53e5\u4e2d\u6dfb\u52a0 FOR UPDATE \u5b50\u53e5\u6765\u5b9e\u73b0\u52a0\u4e0a\u6570 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[33],"tags":[],"class_list":["post-3764","post","type-post","status-publish","format-standard","hentry","category-mybatis"],"post_thumbnail_image":"","content_first_image":null,"post_medium_image_300":"","post_thumbnail_image_624":"","post_frist_image":null,"post_medium_image":"","post_large_image":"","post_full_image":"","post_all_images":[],"videoAdId":"","listAd":"0","listAdId":"","listAdEvery":6,"total_comments":0,"category_name":"Mybatis","post_date":"2024-03-02","like_count":"0","praiseWord":"\u9f13\u52b1","copyright_state":"","excitationAd":"0","rewardedVideoAdId":"","detailAdId":"","detailAd":"0","enterpriseMinapp":"0","audios":[],"postImageUrl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2023\/05\/20230519082947553.jpg","avatarurls":[],"related_posts":null,"pageviews":1587,"next_post_id":4330,"next_post_title":"Spring Boot\u6574\u5408Mybatis\u914d\u7f6e\u591a\u6570\u636e\u6e90","previous_post_id":4345,"previous_post_title":"Spring Boot\u6574\u5408MyBatis Plus\u8fde\u63a5\u6570\u636e\u5e93","_links":{"self":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/3764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/comments?post=3764"}],"version-history":[{"count":1,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/3764\/revisions"}],"predecessor-version":[{"id":3765,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/3764\/revisions\/3765"}],"wp:attachment":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=3764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=3764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=3764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}