{"id":305,"date":"2022-04-08T19:39:07","date_gmt":"2022-04-08T11:39:07","guid":{"rendered":"https:\/\/blog.lijiajia.top\/?p=305"},"modified":"2026-06-19T21:38:30","modified_gmt":"2026-06-19T13:38:30","slug":"spring%e6%a1%86%e6%9e%b6%e5%ad%a6%e4%b9%a0%ef%bc%9a%e4%ba%8b%e5%8a%a1%e6%93%8d%e4%bd%9c%e5%8d%81%e4%b9%9d","status":"publish","type":"post","link":"https:\/\/blog.ttwow.top\/?p=305","title":{"rendered":"Spring5\u6846\u67b6\u5b66\u4e60\uff1a\u58f0\u660e\u5f0f\u4e8b\u52a1\u914d\u7f6e-\u6ce8\u89e3\u65b9\u5f0f(\u4e8c\u5341)"},"content":{"rendered":"<h2>\u5f15\u51fa\u4e8b\u52a1<\/h2>\n<pre><code class=\"language-java\">@Repository\npublic class TransferDaoImpl implements TransferDao{\n\n    @Autowired\n    private JdbcTemplate jdbcTemplate;\n\n    @Override\n    public void reduceMoney() {\n        String sql = &quot;update account set money = money - ? where name = ?&quot;;\n        jdbcTemplate.update(sql,100,&quot;Jack&quot;);\n    }\n\n    @Override\n    public void addMoney() {\n        String sql = &quot;update account set money = money + ? where name = ?&quot;;\n        jdbcTemplate.update(sql,100,&quot;Tom&quot;);\n    }\n\n}<\/code><\/pre>\n<pre><code class=\"language-java\">@Service\npublic class TransferService {\n\n    @Autowired\n    private TransferDao transferDao;\n\n    \/\/\u8f6c\u5e10\n    public void transfer() {\n\n        \/\/ \u51cf\u5c11\u94b1\n        transferDao.reduceMoney();\n\n        \/\/\u6a21\u62df\u5f02\u5e38\n        int i = 1\/0;\n        \/\/\u589e\u52a0\u94b1\n        transferDao.addMoney();\n    }\n}<\/code><\/pre>\n<p>\u5f02\u5e38\u65f6\uff0c\u53ea\u51cf\u5c11\u4e86\u94b1\uff0c\u6ca1\u6709\u589e\u52a0\u94b1\u3002<\/p>\n<h2>\u4e8b\u52a1\u64cd\u4f5c\u8fc7\u7a0b\uff08\u7f16\u7a0b\u5f0f\u4e8b\u52a1\u7ba1\u7406\uff09<\/h2>\n<pre><code class=\"language-java\">    \/\/\u8f6c\u5e10\n    public void transfer() {\n        try{\n            \/\/\u7b2c\u4e00\u6b65 \u5f00\u542f\u4e8b\u52a1\n            \/\/\u7b2c\u4e8c\u6b65 \u8fdb\u884c\u4e1a\u52a1\u64cd\u4f5c\n            \/\/ \u51cf\u5c11\u94b1\n            transferDao.reduceMoney();\n            \/\/\u6a21\u62df\u5f02\u5e38\n            int i = 1\/0;\n            \/\/\u589e\u52a0\u94b1\n            transferDao.addMoney();\n            \/\/\u7b2c\u4e09\u6b65 \u6ca1\u6709\u53d1\u751f\u5f02\u5e38\uff0c\u63d0\u4ea4\u4e8b\u52a1\n        }catch (Exception e){\n            \/\/\u7b2c\u56db\u6b65 \u51fa\u73b0\u5f02\u5e38\uff0c\u4e8b\u52a1\u56de\u6eda\n        }\n    }<\/code><\/pre>\n<h2>Spring\u4e8b\u52a1\u7ba1\u7406\u4ecb\u7ecd<\/h2>\n<p>1\u3001\u4e8b\u52a1\u6dfb\u52a0\u5230JavaEE\u4e09\u5c42\u7ed3\u6784\u91cc\u9762Service\u5c42\uff08\u4e1a\u52a1\u903b\u8f91\u5c42\uff09<br \/>\n2\u3001\u5728Spring\u8fdb\u884c\u4e8b\u52a1\u7ba1\u7406\u64cd\u4f5c<br \/>\n(1)\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a\u7f16\u7a0b\u5f0f\u4e8b\u52a1\u7ba1\u7406\u548c\u58f0\u660e\u5f0f\u4e8b\u52a1\u7ba1\u7406\uff08\u4f7f\u7528\uff09<br \/>\n\u7f16\u7a0b\u5f0f\u5c31\u662f\u5199\u4ee3\u7801\uff0c\u4e0a\u9762\u90a3\u79cd<br \/>\n3\u3001\u58f0\u660e\u5f0f\u4e8b\u52a1\u7ba1\u7406<br \/>\n(1)\u57fa\u4e8e\u6ce8\u89e3\u65b9\u5f0f\uff08\u4f7f\u7528\uff09<br \/>\n(2)\u57fa\u4e8exml\u914d\u7f6e\u6587\u4ef6\u65b9\u5f0f<br \/>\n4\u3001\u5728Spring\u8fdb\u884c\u58f0\u660e\u5f0f\u4e8b\u52a1\u7ba1\u7406\uff0c\u5e95\u5c42\u4f7f\u7528AOP\u539f\u7406<br \/>\n5\u3001Spring\u4e8b\u52a1\u7ba1\u7406API<br \/>\n(1)\u63d0\u4f9b\u4e00\u4e2a\u63a5\u53e3\uff0c\u4ee3\u8868\u4e8b\u52a1\u7ba1\u7406\u5668\uff0c\u8fd9\u4e2a\u63a5\u53e3\u9488\u5bf9\u4e0d\u540c\u7684\u6846\u67b6\u63d0\u4f9b\u4e0d\u540c\u7684\u5b9e\u73b0\u7c7b<br \/>\n\u4e8b\u52a1\u7ba1\u7406\u5668\uff1a<br \/>\n<div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/bucket-blog-img.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2022\/04\/image-1649423974104.png'><img class=\"lazyload lazyload-style-11\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/bucket-blog-img.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2022\/04\/image-1649423974104.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"file\" \/><\/div><\/p>\n<h2>\u58f0\u660e\u4e66\u4e8b\u52a1-\u6ce8\u89e3\u65b9\u5f0f<\/h2>\n<p>1.\u5728spring\u914d\u7f6e\u6587\u4ef6\u4e2d\u914d\u7f6e\u4e8b\u52a1\u7ba1\u7406\u5668<br \/>\n\u5f53\u524djdbcTemplate\u6240\u4ee5\u4f7f\u7528DataSourceTransactionManager<\/p>\n<pre><code class=\"language-xml\">    &lt;!--\u521b\u5efa\u4e8b\u52a1\u7ba1\u7406\u5668--&gt;\n    &lt;bean id=&quot;dataSourceTransactionManager&quot; class=&quot;org.springframework.jdbc.datasource.DataSourceTransactionManager&quot;&gt;\n        &lt;!--\u6ce8\u5165\u6570\u636e\u6e90--&gt;\n        &lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot;&gt;&lt;\/property&gt;\n    &lt;\/bean&gt;<\/code><\/pre>\n<p>2.\u5728spring\u914d\u7f6e\u6587\u4ef6\uff0c\u5f00\u542f\u4e8b\u52a1\u6ce8\u89e3<br \/>\n\uff081\uff09\u5f15\u5165tx\u540d\u79f0\u7a7a\u95f4<\/p>\n<pre><code class=\"language-xml\">&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\n       xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\n       xmlns:context=&quot;http:\/\/www.springframework.org\/schema\/context&quot;\n       xmlns:tx=&quot;http:\/\/www.springframework.org\/schema\/tx&quot;\n       xsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd\n                            http:\/\/www.springframework.org\/schema\/context http:\/\/www.springframework.org\/schema\/context\/spring-context.xsd\n                            http:\/\/www.springframework.org\/schema\/tx http:\/\/www.springframework.org\/schema\/tx\/spring-tx.xsd&quot;&gt;\n<\/code><\/pre>\n<p>\uff082\uff09\u5f00\u542f\u4e8b\u52a1\u6ce8\u89e3<\/p>\n<pre><code class=\"language-xml\">&lt;!--\u5f00\u542f\u4e8b\u52a1\u6ce8\u89e3--&gt;\n    &lt;tx:annotation-driven transaction-manager=&quot;dataSourceTransactionManager&quot;&gt;&lt;\/tx:annotation-driven&gt;<\/code><\/pre>\n<p>3\u3001\u5728service\u7c7b\u4e0a\u9762\uff08\u6216\u8005service\u7c7b\u91cc\u9762\u65b9\u6cd5\u4e0a\u9762\uff09\u6dfb\u52a0\u4e8b\u52a1\u6ce8\u89e3<br \/>\n(1)@Transactional,\u8fd9\u4e2a\u6ce8\u89e3\u6dfb\u52a0\u5230\u7c7b\u4e0a\u9762\uff0c\u4e5f\u53ef\u4ee5\u6dfb\u52a0\u65b9\u6cd5\u4e0a\u9762<br \/>\n(2)\u5982\u679c\u628a\u8fd9\u4e2a\u6ce8\u89e3\u6dfb\u52a0\u7c7b\u4e0a\u9762\uff0c\u8fd9\u4e2a\u7c7b\u91cc\u9762\u6240\u6709\u7684\u65b9\u6cd5\u90fd\u6dfb\u52a0\u4e8b\u52a1<br \/>\n(3)\u5982\u679c\u628a\u8fd9\u4e2a\u6ce8\u89e3\u6dfb\u52a0\u65b9\u6cd5\u4e0a\u9762\uff0c\u53ea\u4e3a\u8fd9\u4e2a\u65b9\u6cd5\u6dfb\u52a0\u4e8b\u52a1<\/p>\n<pre><code class=\"language-java\">@Service\n@Transactional\npublic class TransferService {\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u51fa\u4e8b\u52a1 @Repository public class TransferDaoImpl implement [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[125],"tags":[68,83],"class_list":["post-305","post","type-post","status-publish","format-standard","hentry","category-java-backend","tag-spring","tag-83"],"_links":{"self":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=305"}],"version-history":[{"count":1,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/305\/revisions"}],"predecessor-version":[{"id":2143,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/305\/revisions\/2143"}],"wp:attachment":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}