{"id":5791,"date":"2024-04-15T23:48:16","date_gmt":"2024-04-15T15:48:16","guid":{"rendered":"http:\/\/zyg.luchers.cn\/?p=5791"},"modified":"2024-04-15T23:48:16","modified_gmt":"2024-04-15T15:48:16","slug":"%e7%ac%ac%e5%85%ad%e7%ab%a0%e3%80%81junit%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95","status":"publish","type":"post","link":"http:\/\/zyg.luchers.cn\/index.php\/2024\/04\/15\/%e7%ac%ac%e5%85%ad%e7%ab%a0%e3%80%81junit%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95\/","title":{"rendered":"\u7b2c\u516d\u7ae0\u3001JUnit\u5355\u5143\u6d4b\u8bd5"},"content":{"rendered":"<h1>\u7b2c\u516d\u7ae0\u3001JUnit\u5355\u5143\u6d4b\u8bd5<\/h1>\n<h2>\u9700\u6c42\u5206\u6790<\/h2>\n<p>1\u3001\u5728\u524d\u9762\u6211\u4eec\u90fd\u4f7f\u7528\u4e00\u4e0b\u4e24\u884c\u4ee3\u7801<\/p>\n<pre><code>ApplicationContext context = new ClassPathXmlApplicationContext(&quot;xxx.xml&quot;);\n        Calculator calculator = context.getBean(yyy.class);\n        \/\/\u5176\u4e2dxxx\u662f\u914d\u7f6e\u6587\u4ef6\n        \/\/yyy\u662f\u7c7b<\/code><\/pre>\n<p>2\u3001\u8fd9\u4e24\u884c\u4ee3\u7801\u662f\u5bb9\u5668\u7684\u521b\u5efa\u4ee3\u7801\uff0c\u6700\u7ec8\u83b7\u53d6\u5bf9\u8c61\u3002<\/p>\n<p>3\u3001\u9488\u5bf9\u4e0a\u9762\u9700\u8981\u624b\u52a8\u521b\u5efa\u7684\u95ee\u9898\u6765\u8bf4\uff0c\u6211\u4eec\u60f3\u8981\u7684\u662f\u81ea\u52a8\u521b\u5efa\uff01<\/p>\n<p>4\u3001\u6211\u4eec\u5c31\u60f3\u5230\u4e86JUnit\u5355\u5143\u6d4b\u8bd5\uff0c\u4f46\u662f\u5355\u5143\u6d4b\u8bd5\u4e0d\u77e5\u9053\u662f\u4e0d\u662f\u7528\u4e86spring\u6846\u67b6<\/p>\n<p>5\u3001\u4f46\u662fspring\u63d0\u4f9b\u4e86\u4e2a\u8fd0\u884c\u5668\uff0c\u53ef\u4ee5\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\uff08\u6216\u6ce8\u89e3\uff09\u6765\u521b\u5efa\u5bb9\u5668\u3002\u53ea\u8981\u544a\u8bc9\u914d\u7f6e\u6587\u4ef6\u4f4d\u7f6e\u5c31\u884c\u3002<\/p>\n<p>6\u3001\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u8ba9\u5355\u5143\u6d4b\u8bd5\u77e5\u9053\u662f\u4e0d\u662f\u7528\u4e86spring\u6846\u67b6\uff0c\u4ece\u800c\u8ba9\u5176\u81ea\u52a8\u521b\u5efaspring\u5bb9\u5668\uff01<\/p>\n<h2>1\u3001\u6574\u5408JUnit5<\/h2>\n<h3>1\u3001\u642d\u5efa\u5b50\u6a21\u5757<\/h3>\n<p>1\u3001\u521b\u5efaspring-junit5<\/p>\n<h4>1\u3001\u5f15\u5165\u4f9d\u8d56<\/h4>\n<p>1\u3001\u5728\u7236\u5de5\u7a0b\u4e2d\u7684pom.xml\u4e2d\u5f15\u5165<\/p>\n<pre><code>    &lt;dependency&gt;\n        &lt;groupId&gt;org.springframework&lt;\/groupId&gt;\n        &lt;artifactId&gt;spring-test&lt;\/artifactId&gt;\n        &lt;version&gt;6.0.2&lt;\/version&gt;\n    &lt;\/dependency&gt;<\/code><\/pre>\n<p>2\u3001\u8fd9\u4e2a\u4f9d\u8d56\u7684\u4f5c\u7528\u662f\uff1aspring\u6846\u67b6\u6574\u5408JUnit\u76f8\u5173\u4f9d\u8d56<\/p>\n<h4>2\u3001\u914d\u7f6e\u6587\u4ef6<\/h4>\n<h5>1\u3001\u914d\u7f6exml\u6587\u4ef6<\/h5>\n<p>1\u3001bean.xml<\/p>\n<pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n&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:aop=&quot;http:\/\/www.springframework.org\/schema\/aop&quot;\n       xsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans\n       http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd\n       http:\/\/www.springframework.org\/schema\/context\n       http:\/\/www.springframework.org\/schema\/context\/spring-context.xsd\n       http:\/\/www.springframework.org\/schema\/aop\n       http:\/\/www.springframework.org\/schema\/aop\/spring-aop.xsd&quot;&gt;\n&lt;!--    \u914d\u7f6e\u626b\u63cf--&gt;\n    &lt;context:component-scan base-package=&quot;com.atqt.spring6.junit5&quot;&gt;&lt;\/context:component-scan&gt;\n&lt;\/beans&gt;<\/code><\/pre>\n<h5>2\u3001\u7f16\u5199User\u7c7b<\/h5>\n<p>1\u3001User.java(\u8fdb\u884c\u6d4b\u8bd5)<\/p>\n<pre><code>package com.atqt.spring6.junit.junit5;\nimport org.springframework.stereotype.Component;\n\/\/\u521b\u5efa\u5bf9\u8c61\n@Component\npublic class User {\n    public void add(){\n        System.out.println(&quot;run.....&quot;);\n    }\n}<\/code><\/pre>\n<h5>3\u3001\u7f16\u5199\u6d4b\u8bd5\u7c7bUserTestJunit5.java<\/h5>\n<p>1\u3001UserTestJunit5.java<\/p>\n<pre><code>package com.atqt.spring6.junit.junit5;\n\nimport org.junit.jupiter.api.Test;\nimport org.junit.jupiter.api.extension.ExtendWith;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.test.context.ContextConfiguration;\nimport org.springframework.test.context.junit.jupiter.SpringExtension;\nimport org.springframework.test.context.junit.jupiter.SpringJUnitConfig;\n\/\/\u65b9\u6cd5\u4e00\n\/\/@SpringJUnitConfig(locations = &quot;classpath:bean.xml&quot;)\n\/\/\u65b9\u6cd5\u4e8c\n@ExtendWith(SpringExtension.class)\n@ContextConfiguration(&quot;classpath:bean.xml&quot;)\npublic class UserTestJunit5 {\n    @Autowired\n    private User user;\n    @Test\n    public void test1(){\n        System.out.println(user);\n        user.add();\n    }\n}<\/code><\/pre>\n<h5>4\u3001\u4e24\u79cd\u914d\u7f6e\u65b9\u6cd5<\/h5>\n<p>1\u3001\u65b9\u6cd5\u4e00<\/p>\n<pre><code>@ExtendWith(SpringExtension.class)\n@ContextConfiguration(&quot;classpath:xxx.xml&quot;)\/\/\u914d\u7f6e\u6587\u4ef6<\/code><\/pre>\n<p>2\u3001\u65b9\u6cd5\u4e8c<\/p>\n<pre><code>@SpringJUnitConfig(locations = &quot;classpath:xxx.xml&quot;)\/\/\u914d\u7f6e\u6587\u4ef6<\/code><\/pre>\n<h2>2\u3001\u6574\u5408JUnit4\u6709\u95ee\u9898\uff01\uff1f<\/h2>\n<p>1\u3001\u56e0\u4e3aJUnit4\u516c\u53f8\u4f1a\u7528\u5230<\/p>\n<h3>1\u3001\u6dfb\u52a0\u4f9d\u8d56<\/h3>\n<p>1\u3001\u4f9d\u8d56<\/p>\n<pre><code>&lt;dependency&gt;\n    &lt;groupId&gt;junit&lt;\/groupId&gt;\n    &lt;artifactId&gt;junit&lt;\/artifactId&gt;\n    &lt;version&gt;4.12&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>2\u3001\u53ea\u9700\u8981\u628axml\u6587\u4ef6\u4e2d\u7684\u5305\u9009\u5bf9\u5c31\u884c\u4e86<\/p>\n<h1>\u7b2c\u4e03\u7ae0\u3001\u4e8b\u52a1<\/h1>\n<h2>1\u3001JdbcTemplate<\/h2>\n<h3>1\u3001\u4ecb\u7ecd<\/h3>\n<p>1\u3001spring\u6846\u67b6\u5bf9JDBC\u8fdb\u884c\u4e86\u5c01\u88c5\uff0c\u6240\u4ee5\u4f7f\u7528JdbcTemplate<\/p>\n<h3>2\u3001\u51c6\u5907\u5de5\u4f5c\uff08\u51c6\u5907\u4f9d\u8d56\uff09<\/h3>\n<h4>1\u3001\u51c6\u5907\u6709\u5173\u6570\u636e\u5e93\u7684\u76f8\u5173\u4f9d\u8d56<\/h4>\n<pre><code>&lt;dependencies&gt;\n    &lt;!--spring jdbc  Spring \u6301\u4e45\u5316\u5c42\u652f\u6301jar\u5305--&gt;\n    &lt;dependency&gt;\n        &lt;groupId&gt;org.springframework&lt;\/groupId&gt;\n        &lt;artifactId&gt;spring-jdbc&lt;\/artifactId&gt;\n        &lt;version&gt;6.0.2&lt;\/version&gt;\n    &lt;\/dependency&gt;\n    &lt;!-- MySQL\u9a71\u52a8 --&gt;\n    &lt;dependency&gt;\n        &lt;groupId&gt;mysql&lt;\/groupId&gt;\n        &lt;artifactId&gt;mysql-connector-java&lt;\/artifactId&gt;\n        &lt;version&gt;8.0.30&lt;\/version&gt;\n    &lt;\/dependency&gt;\n    &lt;!-- \u6570\u636e\u6e90 --&gt;\n    &lt;dependency&gt;\n        &lt;groupId&gt;com.alibaba&lt;\/groupId&gt;\n        &lt;artifactId&gt;druid&lt;\/artifactId&gt;\n        &lt;version&gt;1.2.15&lt;\/version&gt;\n    &lt;\/dependency&gt;\n&lt;\/dependencies&gt;<\/code><\/pre>\n<h4>2\u3001\u521b\u5efajdbc.properties<\/h4>\n<pre><code>jdbc.user=root\njdbc.password=123456\njdbc.url=jdbc:mysql:\/\/localhost:3306\/spring?characterEncoding=utf8&amp;useSSL=false\njdbc.driver=com.mysql.cj.jdbc.Driver<\/code><\/pre>\n<h4>3\u3001\u914d\u7f6espring\u7684\u914d\u7f6e\u6587\u4ef6<\/h4>\n<p>1\u3001\u521b\u5efa\u5916\u90e8\u5c5e\u6027\u6587\u4ef6\uff0c\u521b\u5efa\u6570\u636e\u5e93\u5bf9\u8c61<\/p>\n<p>2\u3001\u521b\u5efajdbcTemplate\u5bf9\u8c61\uff0c\u6ce8\u5165\u6570\u636e\u5e93<\/p>\n<pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n&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       xsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans\n       http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd\n       http:\/\/www.springframework.org\/schema\/context\n       http:\/\/www.springframework.org\/schema\/context\/spring-context.xsd&quot;&gt;\n\n&lt;!--    \u521b\u5efa\u5916\u52a1\u90e8\u5c5e\u6027\u6587\u4ef6\uff0c\u521b\u5efa\u6570\u636e\u5e93\u5bf9\u8c61--&gt;\n    &lt;context:property-placeholder location=&quot;classpath:jdbc.properties&quot;&gt;&lt;\/context:property-placeholder&gt;\n    &lt;bean id=&quot;druidDataSource&quot; class=&quot;com.alibaba.druid.pool.DruidDataSource&quot;&gt;\n        &lt;property name=&quot;url&quot; value=&quot;${jdbc.url}&quot;&gt;&lt;\/property&gt;\n        &lt;property name=&quot;driverClassName&quot; value=&quot;${jdbc.driver}&quot;&gt;&lt;\/property&gt;\n        &lt;property name=&quot;username&quot; value=&quot;${jdbc.user}&quot;&gt;&lt;\/property&gt;\n        &lt;property name=&quot;password&quot; value=&quot;${jdbc.password}&quot;&gt;&lt;\/property&gt;\n    &lt;\/bean&gt;\n&lt;!--    \u521b\u5efajdbcTemplate\u5bf9\u8c61\uff0c\u6ce8\u5165\u6570\u636e\u5e93--&gt;\n    &lt;bean id=&quot;jdbcTemplate&quot; class=&quot;org.springframework.jdbc.core.JdbcTemplate&quot;&gt;\n        &lt;property name=&quot;dataSource&quot; ref=&quot;druidDataSource&quot;&gt;&lt;\/property&gt;\n    &lt;\/bean&gt;\n\n&lt;\/beans&gt;<\/code><\/pre>\n<h4>4\u3001\u521b\u5efa\u6570\u636e\u5e93\u4ee3\u7801<\/h4>\n<p>1\u3001\u6570\u636e\u5e93sql\u6587\u4ef6<\/p>\n<pre><code>CREATE DATABASE `spring`\n\nUSE `spring`\n\nCREATE TABLE `t_emp` (\n  `id` INT(11) NOT NULL AUTO_INCREMENT,\n  `name` VARCHAR(20) DEFAULT NULL COMMENT &#039;\u59d3\u540d&#039;,\n  `age` INT(11) DEFAULT NULL COMMENT &#039;\u5e74\u9f84&#039;,\n  `sex` VARCHAR(2) DEFAULT NULL COMMENT &#039;\u6027\u522b&#039;,\n  PRIMARY KEY (`id`)\n) ENGINE=INNODB DEFAULT CHARSET=utf8mb4;\n\nINSERT INTO t_emp VALUES(NULL,&#039;\u597d\u597d\u597d&#039;,11,&#039;\u7537&#039;)\n\nUPDATE t_emp SET NAME=&quot;\u55e8\u55e8\u55e8&quot; WHERE id=1;\n\nSELECT * FROM t_emp WHERE id=2;<\/code><\/pre>\n<h4>5\u3001\u521b\u5efa\u5b9e\u4f53\u7c7bEmp<\/h4>\n<p>1\u3001Emp.java<\/p>\n<pre><code>package com.atqt.spring6.jdbc;\n\npublic class Emp {\n    private int id;\n    private String name;\n    private int age;\n    private String sex;\n\n    public int getId() {\n        return id;\n    }\n\n    public void setId(int id) {\n        this.id = id;\n    }\n\n    public String getName() {\n        return name;\n    }\n\n    public void setName(String name) {\n        this.name = name;\n    }\n\n    public int getAge() {\n        return age;\n    }\n\n    public void setAge(int age) {\n        this.age = age;\n    }\n\n    public String getSex() {\n        return sex;\n    }\n\n    public void setSex(String sex) {\n        this.sex = sex;\n    }\n\n    @Override\n    public String toString() {\n        return &quot;Emp{&quot; +\n                &quot;id=&quot; + id +\n                &quot;, name=&#039;&quot; + name + &#039;\\&#039;&#039; +\n                &quot;, age=&quot; + age +\n                &quot;, sex=&#039;&quot; + sex + &#039;\\&#039;&#039; +\n                &#039;}&#039;;\n    }\n}<\/code><\/pre>\n<h3>3\u3001\u5b9e\u4f8b\u6f14\u793a<\/h3>\n<h4>1\u3001\u6dfb\u52a0<\/h4>\n<pre><code>    @Test\n    public void testAdd(){\n        String sql = &quot;INSERT INTO t_emp VALUES(NULL,?,?,?)&quot;;\n        int update = jdbcTemplate.update(sql, &quot;\u6f58\u91d1\u83b2&quot;, 18, &quot;\u7537&quot;);\n        System.out.println(update);\n    }<\/code><\/pre>\n<h4>2\u3001\u4fee\u6539<\/h4>\n<pre><code>   @Test\n    public void testUpdate(){\n        String sql = &quot;update t_emp set name=? where id=?&quot;;\n        int update = jdbcTemplate.update(sql,&quot;\u5927\u50bb\u74dc&quot;,1);\n        System.out.println(update);\n    }<\/code><\/pre>\n<h4>3\u3001\u5220\u9664<\/h4>\n<pre><code>    @Test\n    public void testDelete(){\n        String sql = &quot;delete from t_emp where id=?&quot;;\n        int update = jdbcTemplate.update(sql,1);\n        System.out.println(update);\n    }<\/code><\/pre>\n<h4>4\u3001\u67e5\u8be2<\/h4>\n<p>1\u3001\u4f7f\u7528<\/p>\n<pre><code>1\u3001\u65b9\u5f0f\u4e00\nnew BeanPropertyRowMapper&lt;&gt;(Emp.class)\n\u65b9\u5f0f\u4e8c<\/code><\/pre>\n<h5>1\u3001\u67e5\u8be2\u8fd4\u56de\u96c6\u5408<\/h5>\n<pre><code>    @Test\n    public void testSelectList(){\n        String sql = &quot;select * from t_emp &quot;;\n\/\/        \u8fd4\u56de\u96c6\u5408\u5bf9\u8c61\n        List&lt;Emp&gt; emp = jdbcTemplate.query(sql,new BeanPropertyRowMapper&lt;&gt;(Emp.class) );\n\n        System.out.println(emp);\n    }<\/code><\/pre>\n<h5>2\u3001\u67e5\u8be2\u8fd4\u56de\u5355\u4e2a\u503c<\/h5>\n<pre><code class=\"language-java\">    @Test\n    public void testSelectVaulue(){\n        String sql = &quot;select count(*) from t_emp&quot;;\n        Integer count = jdbcTemplate.queryForObject(sql,Integer.class);\n        System.out.println(count);\n    }<\/code><\/pre>\n<h5>3\u3001\u4ee3\u7801\u5b9e\u73b0<\/h5>\n<pre><code class=\"language-java\">package com.atqt.spring6.jdbc;\n\nimport org.junit.jupiter.api.Test;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.jdbc.core.BeanPropertyRowMapper;\nimport org.springframework.jdbc.core.JdbcTemplate;\nimport org.springframework.test.context.junit.jupiter.SpringJUnitConfig;\n\nimport java.util.List;\n\n@SpringJUnitConfig(locations=&quot;classpath:beans.xml&quot;)\npublic class JdbcTemplateTest {\n    @Autowired\n    private JdbcTemplate jdbcTemplate;\n\n    @Test\n    public void testAdd(){\n        String sql = &quot;INSERT INTO t_emp VALUES(NULL,?,?,?)&quot;;\n        int update = jdbcTemplate.update(sql, &quot;\u6f58\u91d1\u83b2&quot;, 18, &quot;\u7537&quot;);\n        System.out.println(update);\n    }\n\n    @Test\n    public void testUpdate(){\n        String sql = &quot;update t_emp set name=? where id=?&quot;;\n        int update = jdbcTemplate.update(sql,&quot;\u5927\u50bb\u74dc&quot;,1);\n        System.out.println(update);\n    }\n\n    @Test\n    public void testDelete(){\n        String sql = &quot;delete from t_emp where id=?&quot;;\n        int update = jdbcTemplate.update(sql,1);\n        System.out.println(update);\n    }\n\n\/\/    \u67e5\u8be2\u8fd4\u56de\u7684\u96c6\u5408\n    @Test\n    public void testGetObject(){\n        String sql = &quot;select * from t_emp where id=?&quot;;\n\/\/        \u8fd4\u56de\u96c6\u5408\u5bf9\u8c61\n        Emp emp = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper&lt;&gt;(Emp.class),2);\n\n        System.out.println(emp);\n    }\n\n    \/\/    \u67e5\u8be2\u8fd4\u56de\u7684\u96c6\u5408\n    @Test\n    public void testSelectList(){\n        String sql = &quot;select * from t_emp &quot;;\n\/\/        \u8fd4\u56de\u96c6\u5408\u5bf9\u8c61\n        List&lt;Emp&gt; emp = jdbcTemplate.query(sql,new BeanPropertyRowMapper&lt;&gt;(Emp.class) );\n\n        System.out.println(emp);\n    }\n\n\/\/    \u67e5\u8be2\u8fd4\u56de\u5355\u4e2a\u503c\n    @Test\n    public void testSelectVaulue(){\n        String sql = &quot;select count(*) from t_emp&quot;;\n        Integer count = jdbcTemplate.queryForObject(sql,Integer.class);\n        System.out.println(count);\n    }\n\n}<\/code><\/pre>\n<h2>2\u3001\u58f0\u660e\u4e8b\u52a1\u7684\u6982\u5ff5<\/h2>\n<h3>1\u3001\u4e8b\u52a1\u7684\u57fa\u672c\u6982\u5ff5<\/h3>\n<h4>1\u3001\u4ec0\u4e48\u662f\u4e8b\u52a1<\/h4>\n<p>1\u3001\u662f\u6570\u636e\u5e93\u4e8b\u52a1\u662f\u8bbf\u95ee\u5e76\u53ef\u80fd\u64cd\u4f5c\u5404\u79cd\u6570\u636e\u9879\u7684\u4e00\u4e2a\u6570\u636e\u5e93\u64cd\u4f5c\u5e8f\u5217\uff0c\u8fd9\u4e9b\u64cd\u4f5c\uff0c\u8981\u4e48\u5168\u90e8\u6267\u884c\uff0c\u8981\u4e48\u5168\u90e8\u4e0d\u6267\u884c\uff0c\u662f\u4e0d\u53ef\u5206\u5272\u7684\u4e00\u90e8\u5206\u3002\u4e8b\u52a1\u7684\u5f00\u59cb\u548c\u4e8b\u52a1\u7684\u7ed3\u675f\u4e4b\u95f4\u6267\u884c\u5168\u90e8\u6570\u636e\u5e93\u64cd\u4f5c\u7ec4\u6210<\/p>\n<h4>2\u3001\u4e8b\u52a1\u7684\u7279\u6027<\/h4>\n<h5>1\u3001\u539f\u5b50\u6027\uff08Atomicity\uff09<\/h5>\n<p>1\u3001\u4e00\u4e2a\u4e8b\u52a1\u7684\u7d20\u6709\u64cd\u4f5c\uff0c\u8981\u4e48\u5168\u90e8\u5b8c\u6210\uff0c\u8981\u4e48\u90fd\u4e0d\u6e05\u695a\uff0c\u4e0d\u4f1a\u7ed3\u675f\u5728\u67d0\u4e2a\u73af\u8282\uff0c\u4e8b\u52a1\u5728\u6267\u884c\u4e2d\u51fa\u73b0\u9519\u8bef\u5c31\u4f1a\u56de\u6eda\u5230\u4e8b\u52a1\u5f00\u59cb\u7684\u72b6\u6001\u3002<\/p>\n<h5>2\u3001\u4e00\u81f4\u6027\uff08Consistency\uff09<\/h5>\n<p>1\u3001\u4e8b\u52a1\u7684\u4e00\u81f4\u6027\u662f\u6307\u5728\u4e00\u4e2a\u4e8b\u52a1\u6267\u884c\u524d\u548c\u6267\u884c\u540e\u6570\u636e\u5e93\u90fd\u5fc5\u987b\u4fdd\u6301\u4e00\u4e2a\u72b6\u6001\u3002<\/p>\n<p>2\u3001\u5982\u679c\u4e8b\u52a1\u6210\u529f\u5b8c\u6210\uff0c\u6570\u636e\u5e93\u4fee\u6539\uff0c\u7cfb\u7edf\u627f\u8ba4\u6709\u6548<\/p>\n<p>3\u3001\u5982\u679c\u4e8b\u52a1\u5931\u8d25\uff0c\u6570\u636e\u5e93\u5185\u5bb9\u8fdb\u884c\u56de\u6eda\uff0c\u7cfb\u7edf\u8fd4\u56de\u6700\u521d\u503c<\/p>\n<h5>3\u3001\u9694\u79bb\u6027\uff08solation\uff09<\/h5>\n<p>1\u3001\u5e76\u53d1\u73af\u5883\u4e2d\uff0c\u6bcf\u4e2a\u4e8b\u52a1\u6267\u884c\u64cd\u4f5c\u540c\u6837\u7684\u6570\u636e\uff0c\u6bcf\u4e2a\u4e8b\u52a1\u90fd\u6709\u5404\u81ea\u5b8c\u6574\u7684\u6570\u636e\u7a7a\u95f4\u3002<\/p>\n<p>2\u3001\u7531\u4e8e\u5e76\u53d1\u7684\u5355\u4e2a\u4e8b\u52a1\u4fee\u6539\u9700\u8981\u548c\u5176\u4ed6\u4e8b\u52a1\u505a\u9694\u79bb\uff0c\u4e8b\u52a1\u67e5\u770b\u66f4\u65b0\u7684\u65f6\u5019\u8981\u4e48\u662f\u5176\u4ed6\u4e8b\u52a1\u64cd\u4f5c\u6570\u636e\u524d\u7684\uff0c\u6216\u5176\u4ed6\u4e8b\u52a1\u64cd\u4f5c\u6570\u636e\u66f4\u65b0\u540e\u7684\u72b6\u6001\u3002\u4e0d\u4f1a\u770b\u5230\u4e8b\u52a1\u4e2d\u95f4\u72b6\u6001\u7684\u6570\u636e\u3002<\/p>\n<h5>4\u3001\u6301\u4e45\u6027\uff08Durability\uff09<\/h5>\n<p>1\u3001\u4e8b\u52a1\u7ed3\u675f\u6210\u529f\uff0c\u6570\u636e\u5e93\u66f4\u65b0\u5fc5\u987b\u4fdd\u5b58<\/p>\n<p>2\u3001\u5373\u65f6\u7cfb\u7edf\u5361\u6b7b\u4e5f\u4f1a\u4fdd\u5b58\u4e0b\u6765<\/p>\n<h3>2\u3001\u7f16\u7a0b\u5f0f\u4e8b\u52a1<\/h3>\n<h4>1\u3001\u4ee3\u7801\u683c\u5f0f<\/h4>\n<p>1\u3001\u5c31\u662f\u81ea\u5df1\u7528\u4ee3\u7801\u624b\u52a8\u5b9e\u73b0<\/p>\n<h4>2\u3001\u7f3a\u70b9<\/h4>\n<p>1\u3001\u7ec6\u8282\u6ca1\u6709\u88ab\u5c4f\u853d\uff0c\u7a0b\u5e8f\u5458\u9700\u8981\u81ea\u5df1\u5b8c\u6210<\/p>\n<p>2\u3001\u4ee3\u7801\u590d\u7528\u6027\u4e0d\u9ad8\uff0c\u6ca1\u6709\u8fdb\u884c\u5c01\u88c5<\/p>\n<h3>3\u3001\u58f0\u660e\u5f0f\u4e8b\u52a1<\/h3>\n<h4>1\u3001\u5185\u5bb9\u4ecb\u7ecd<\/h4>\n<p>1\u3001\u5c31\u662f\u5c06\u7f16\u7a0b\u5f0f\u7684\u4ee3\u7801\u8fdb\u884c\u5c01\u88c5<\/p>\n<p>2\u3001\u5c01\u88c5\u540e\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5728\u914d\u7f6e\u6587\u4ef6\u8fdb\u884c\u7b80\u5355\u7684\u914d\u7f6e\u5373\u53ef\u5b8c\u6210\u64cd\u4f5c<\/p>\n<p>3\u3001\u901a\u8fc7\u914d\u7f6e\u8ba9\u6846\u67b6\u5b9e\u73b0\u529f\u80fd<\/p>\n<h4>2\u3001\u4f18\u70b9<\/h4>\n<p>1\u3001\u63d0\u9ad8\u5f00\u53d1\u6548\u7387<\/p>\n<p>2\u3001\u6d88\u9664\u5197\u4f59\u4ee3\u7801<\/p>\n<p>3\u3001\u6846\u67b6\u4f1a\u8003\u8651\u76f8\u5173\u9886\u57df\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u53ef\u80fd\u9047\u5230\u7684\u5404\u79cd\u95ee\u9898\uff0c\u8fdb\u884c\u4e86\u5065\u58ee\u6027\u3001\u6027\u80fd\u7b49\u5404\u65b9\u9762\u7684\u4f18\u5316<\/p>\n<h3>4\u3001\u57fa\u4e8e\u6ce8\u89e3\u7684\u58f0\u660e\u5f0f\u7f16\u7a0b<\/h3>\n<h4>1\u3001\u642d\u5efa\u73af\u5883<\/h4>\n<h5>1\u3001\u7f16\u5199BookService\u53ca\u63a5\u53e3<\/h5>\n<p>1\u3001BookService.java<\/p>\n<pre><code class=\"language-java\">package com.atqt.spring6.tx.service;\n\npublic interface BookService {\n    void buyBook(Integer bookId, Integer userId);\n}<\/code><\/pre>\n<p>2\u3001BookServiceImpl.java<\/p>\n<pre><code class=\"language-java\">package com.atqt.spring6.tx.service;\n\nimport com.atqt.spring6.tx.dao.BookDao;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Service;\n\n@Service\npublic class BookServiceImpl implements BookService{\n    @Autowired\n    private BookDao bookDao;\n    @Override\n    public void buyBook(Integer bookId, Integer userId) {\n\/\/        \u6839\u636e\u56fe\u4e66id\u67e5\u8be2\u56fe\u4e66\u4ef7\u683c\n        Integer price = bookDao.getBookPriceById(bookId);\n\/\/        \u66f4\u65b0\u56fe\u4e66\u8868\u5e93\u5b58\u91cf-1\n        bookDao.getBookStock(bookId);\n\/\/        \u66f4\u65b0\u7528\u6237\u8868\u91cc\u7684\u4f59\u989d-\u56fe\u4e66\u4ef7\u683c\n        bookDao.getBookBalancePrice(userId,price);\n\n    }\n}<\/code><\/pre>\n<h5>2\u3001BookController.java<\/h5>\n<pre><code class=\"language-java\">package com.atqt.spring6.tx.controller;\n\nimport com.atqt.spring6.tx.service.BookService;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Component;\n\n@Component\npublic class BookController {\n    @Autowired\n    private BookService bookService;\n\n    public void buyBook(Integer bookId, Integer userId) {\n\/\/        \u8c03\u7528service\u65b9\u6cd5\n        bookService.buyBook(bookId,userId);\n    }\n\n}<\/code><\/pre>\n<h5>3\u3001BookDao\u53ca\u63a5\u53e3<\/h5>\n<p>1\u3001BookDao.java<\/p>\n<pre><code class=\"language-java\">package com.atqt.spring6.tx.dao;\n\npublic interface BookDao {\n\n    Integer getBookPriceById(Integer bookId);\n\n    void getBookStock(Integer bookId);\n\n    void getBookBalancePrice(Integer userId, Integer price);\n}<\/code><\/pre>\n<p>2\u3001BookDaoImpl.java<\/p>\n<pre><code class=\"language-java\">package com.atqt.spring6.tx.dao;\n\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.jdbc.core.JdbcTemplate;\nimport org.springframework.stereotype.Component;\nimport org.springframework.stereotype.Repository;\n\n@Repository\npublic class BookDaoImpl implements BookDao{\n\n    @Autowired\n    private JdbcTemplate jdbcTemplate;\n\n    @Override\n    public Integer getBookPriceById(Integer bookId) {\n        String sql = &quot;select price from t_book where book_id=?&quot;;\n        Integer price = jdbcTemplate.queryForObject(sql,Integer.class,bookId);\n        return price;\n    }\n\n    @Override\n    public void getBookStock(Integer bookId) {\n        String sql = &quot;update t_book set stock=stock-1 where book_id= ?&quot;;\n        jdbcTemplate.update(sql,bookId);\n    }\n\n    @Override\n    public void getBookBalancePrice( Integer userId,Integer price) {\n        String sql = &quot;update t_user set balance=balance-? where user_id=?&quot;;\n        jdbcTemplate.update(sql,price,userId);\n    }\n}<\/code><\/pre>\n<h5>4\u3001\u6d4b\u8bd5<\/h5>\n<p>1\u3001TestUser.java<\/p>\n<pre><code class=\"language-java\">package com.atqt.spring6.tx;\n\nimport com.atqt.spring6.tx.controller.BookController;\nimport org.junit.jupiter.api.Test;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.test.context.junit.jupiter.SpringJUnitConfig;\n\n@SpringJUnitConfig(locations = &quot;classpath:beans.xml&quot;)\npublic class TestUser {\n    @Autowired\n    private BookController bookController ;\n    @Test\n    public void testBuyBook(){\n        bookController.buyBook(1,1);\n    }\n}<\/code><\/pre>\n<h4>2\u3001\u53d1\u73b0\u95ee\u9898<\/h4>\n<p>1\u3001\u94b1\u548c\u56fe\u4e66\u5e93\u5b58\u6ca1\u6709\u540c\u6b65\u66f4\u65b0<\/p>\n<p>2\u3001\u8981\u4e48\u90fd\u6210\u529f\uff0c\u8981\u4e48\u90fd\u5931\u8d25<\/p>\n<h4>3\u3001\u52a0\u5165\u4e8b\u52a1<\/h4>\n<h5>1\u3001\u914d\u7f6ebean.xml\u6587\u4ef6<\/h5>\n<p>1\u3001<\/p>\n<pre><code class=\"language-java\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n&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\n       http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd\n       http:\/\/www.springframework.org\/schema\/context\n       http:\/\/www.springframework.org\/schema\/context\/spring-context.xsd\n       http:\/\/www.springframework.org\/schema\/tx\n       http:\/\/www.springframework.org\/schema\/tx\/spring-tx.xsd&quot;&gt;\n\n&lt;!--    \u5f00\u542f\u7ec4\u4ef6\u626b\u63cf--&gt;\n    &lt;context:component-scan base-package=&quot;com.atqt.spring6.tx&quot;&gt;&lt;\/context:component-scan&gt;\n\n&lt;!--    \u521b\u5efa\u5916\u52a1\u90e8\u5c5e\u6027\u6587\u4ef6\uff0c\u521b\u5efa\u6570\u636e\u5e93\u5bf9\u8c61--&gt;\n    &lt;context:property-placeholder location=&quot;classpath:jdbc.properties&quot;&gt;&lt;\/context:property-placeholder&gt;\n    &lt;bean id=&quot;druidDataSource&quot; class=&quot;com.alibaba.druid.pool.DruidDataSource&quot;&gt;\n        &lt;property name=&quot;url&quot; value=&quot;${jdbc.url}&quot;&gt;&lt;\/property&gt;\n        &lt;property name=&quot;driverClassName&quot; value=&quot;${jdbc.driver}&quot;&gt;&lt;\/property&gt;\n        &lt;property name=&quot;username&quot; value=&quot;${jdbc.user}&quot;&gt;&lt;\/property&gt;\n        &lt;property name=&quot;password&quot; value=&quot;${jdbc.password}&quot;&gt;&lt;\/property&gt;\n    &lt;\/bean&gt;\n&lt;!--    \u521b\u5efajdbcTemplate\u5bf9\u8c61\uff0c\u6ce8\u5165\u6570\u636e\u5e93--&gt;\n    &lt;bean id=&quot;jdbcTemplate&quot; class=&quot;org.springframework.jdbc.core.JdbcTemplate&quot;&gt;\n        &lt;property name=&quot;dataSource&quot; ref=&quot;druidDataSource&quot;&gt;&lt;\/property&gt;\n    &lt;\/bean&gt;\n\n    &lt;bean id=&quot;transactionManager&quot; class=&quot;org.springframework.jdbc.datasource.DataSourceTransactionManager&quot;&gt;\n        &lt;property name=&quot;dataSource&quot; ref=&quot;druidDataSource&quot;&gt;&lt;\/property&gt;\n    &lt;\/bean&gt;\n\n&lt;!--    \u5f00\u542f\u4e8b\u52a1\u7684\u6ce8\u89e3\u9a71\u52a8--&gt;\n    &lt;tx:annotation-driven&gt;&lt;\/tx:annotation-driven&gt;\n\n&lt;\/beans&gt;<\/code><\/pre>\n<h5>2\u3001\u7ed9Service\u6dfb\u52a0\u4e8b\u52a1\u7684\u6ce8\u89e3<\/h5>\n<p>1\u3001\u4ee3\u7801<\/p>\n<pre><code class=\"language-java\">package com.atqt.spring6.tx.service;\n\nimport com.atqt.spring6.tx.dao.BookDao;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Service;\nimport org.springframework.transaction.annotation.Transactional;\n\n@Transactional\n@Service\npublic class BookServiceImpl implements BookService{\n    @Autowired\n    private BookDao bookDao;\n    @Override\n    public void buyBook(Integer bookId, Integer userId) {\n\/\/        \u6839\u636e\u56fe\u4e66id\u67e5\u8be2\u56fe\u4e66\u4ef7\u683c\n        Integer price = bookDao.getBookPriceById(bookId);\n\/\/        \u66f4\u65b0\u56fe\u4e66\u8868\u5e93\u5b58\u91cf-1\n        bookDao.getBookStock(bookId);\n\/\/        \u66f4\u65b0\u7528\u6237\u8868\u91cc\u7684\u4f59\u989d-\u56fe\u4e66\u4ef7\u683c\n        bookDao.getBookBalancePrice(userId,price);\n\n    }\n}<\/code><\/pre>\n<h5>3\u3001\u8fdb\u884c\u6d4b\u8bd5<\/h5>\n<p>1\u3001<\/p>\n<pre><code class=\"language-java\">package com.atqt.spring6.tx;\n\nimport com.atqt.spring6.tx.controller.BookController;\nimport org.junit.jupiter.api.Test;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.test.context.junit.jupiter.SpringJUnitConfig;\n\n@SpringJUnitConfig(locations = &quot;classpath:beans.xml&quot;)\npublic class TestUser {\n    @Autowired\n    private BookController bookController ;\n    @Test\n    public void testBuyBook(){\n        bookController.buyBook(1,1);\n    }\n\n}<\/code><\/pre>\n<p>1\u3001\u6267\u884c\u6210\u529f\uff0c\u6ca1\u6709\u95ee\u9898<\/p>\n<h4>4\u3001@Transactional\u6ce8\u89e3\u6807\u8bc6\u7684\u4f4d\u7f6e<\/h4>\n<p>1\u3001@Transactional\u6807\u8bc6\u5728\u65b9\u6cd5\u4e0a\uff0c\u53ea\u4f1a\u5f71\u54cd\u65b9\u6cd5<\/p>\n<p>2\u3001\u5982\u679c\u5728\u7c7b\u4e0a\uff0c\u4f1a\u5f71\u54cd\u7c7b\u4e2d\u6240\u6709\u65b9\u6cd5<\/p>\n<h4>5\u3001\u4e8b\u52a1\u5c5e\u6027\uff1a\u53ea\u8bfb<\/h4>\n<h5>1\u3001\u4ecb\u7ecd<\/h5>\n<p>1\u3001\u5bf9\u4e00\u4e2aService\u6765\u8bbe\u7f6e\u4e00\u4e2aTransactional\u6765\u8fdb\u884c\u6807\u6ce8<\/p>\n<p>2\u3001\u683c\u5f0f<\/p>\n<pre><code>@Transactional(readOnly=true)<\/code><\/pre>\n<h5>2\u3001\u6ce8\u610f<\/h5>\n<p>1\u3001\u5982\u679c\u8bbe\u7f6e\u4e86\u53ea\u8bfb\uff0c\u8fd8\u5f04\u589e\u5220\u6539\uff0c\u5c31\u4f1a\u62a5\u5f02\u5e38<\/p>\n<p>2\u3001\u5f02\u5e38\u4fe1\u606f\uff1aCaused by: java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed<\/p>\n<h4>6\u3001\u4e8b\u52a1\u5c5e\u6027\uff1a\u8d85\u65f6<\/h4>\n<h5>1\u3001\u4ecb\u7ecd<\/h5>\n<p>1\u3001\u4e8b\u52a1\u5728\u8fd0\u884c\u4e2d\uff0c\u5982\u679c\u9047\u5230\u95ee\u9898\u5bfc\u81f4\u6b7b\u9501\u5361\u4f4f\uff0c\u6b64\u65f6\u7a0b\u5e8f\u5e94\u8be5\u8fdb\u884c\u56de\u6eda\u3002\u64a4\u9500\u5df2\u7ecf\u505a\u4e86\u7684\u64cd\u4f5c\uff0c\u628a\u8d44\u6e90\u8ba9\u51fa\u6765<\/p>\n<p>2\u3001\u5c31\u662f\u8bf4\uff1a\u8d85\u65f6\u5c31\u56de\u6eda\uff0c\u522b\u62e6\u7740\u5176\u4ed6\u8d44\u6e90<\/p>\n<p>3\u3001\u683c\u5f0f<\/p>\n<pre><code>@Transactional(timeout = xxx) \/\/\u79d2<\/code><\/pre>\n<h5>2\u3001\u6ce8\u610f<\/h5>\n<p>1\u3001\u5f02\u5e38\u4fe1\u606f\uff1a<\/p>\n<p>org.springframework.transaction.<strong>TransactionTimedOutException<\/strong>: Transaction timed out: deadline was Fri Jun 04 16:25:39 CST 2022<\/p>\n<h4>7\u3001\u4e8b\u52a1\u5c5e\u6027\uff1a\u56de\u6eda\u7b56\u7565<\/h4>\n<h5>1\u3001\u4ecb\u7ecd<\/h5>\n<p>1\u3001\u8fd0\u884c\u65f6\u5f02\u5e38\u56de\u6eda\uff0c\u7f16\u8bd1\u4e0d\u56de\u6eda<\/p>\n<p>2\u3001\u683c\u5f0f<\/p>\n<pre><code>@Transactional(xxxxx\u5c5e\u6027 = xxxx\u5bf9\u8c61) <\/code><\/pre>\n<p>3\u3001\u76f8\u5173\u5c5e\u6027<\/p>\n<table>\n<thead>\n<tr>\n<th>\u5c5e\u6027<\/th>\n<th>\u8bbe\u7f6e\u5bf9\u8c61<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>rollbackFor<\/td>\n<td>\u8bbe\u7f6e\u4e00\u4e2aClass\u7c7b\u578b\u7684\u5bf9\u8c61<\/td>\n<\/tr>\n<tr>\n<td>rollbackForClassName<\/td>\n<td>\u9700\u8981\u8bbe\u7f6e\u4e00\u4e2a\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u5168\u7c7b\u540d<\/td>\n<\/tr>\n<tr>\n<td>noRollbackFor<\/td>\n<td>\u9700\u8981\u8bbe\u7f6e\u4e00\u4e2aClass\u7c7b\u578b\u7684\u5bf9\u8c61<\/td>\n<\/tr>\n<tr>\n<td>rollbackFor<\/td>\n<td>\u9700\u8981\u8bbe\u7f6e\u4e00\u4e2a\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u5168\u7c7b\u540d<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>4\u3001\u4f8b\u5b50<\/p>\n<pre><code class=\"language-java\">@Transactional(noRollbackFor = ArithmeticException.class)\n\/\/@Transactional(noRollbackForClassName = &quot;java.lang.ArithmeticException&quot;)\npublic void buyBook(Integer bookId, Integer userId) {\n    \/\/\u67e5\u8be2\u56fe\u4e66\u7684\u4ef7\u683c\n    Integer price = bookDao.getPriceByBookId(bookId);\n    \/\/\u66f4\u65b0\u56fe\u4e66\u7684\u5e93\u5b58\n    bookDao.updateStock(bookId);\n    \/\/\u66f4\u65b0\u7528\u6237\u7684\u4f59\u989d\n    bookDao.updateBalance(userId, price);\n    System.out.println(1\/0);\n}<\/code><\/pre>\n<p>5\u3001\u7ed3\u679c\uff1a\u8fd9\u4e2a\u65b9\u6cd5\u91cc\u6709\u5f02\u5e38\u4f46\u662f\u6ca1\u6709\u8fdb\u884c\u56de\u6eda\uff0c\u56e0\u4e3a\u8bbe\u7f6e\u4e86noRollbackFor<\/p>\n<h4>8\u3001\u4e8b\u52a1\u5c5e\u6027\uff1a\u9694\u79bb\u7ea7\u522b<\/h4>\n<h5>1\u3001\u4ecb\u7ecd<\/h5>\n<p>1\u3001\u5fc5\u987b\u8981\u6709\u4e8b\u52a1\u9694\u79bb\uff0c\u8fdb\u884c\u5e76\u53d1\u5404\u4e2a\u4e8b\u52a1\u7684\u80fd\u529b\uff0c\u8ba9\u5b83\u4eec\u4e0d\u4f1a\u4e92\u76f8\u6253\u6270<\/p>\n<p>2\u3001\u9694\u79bb\u7ea7\u522b\u8d8a\u9ad8\uff0c\u6570\u636e\u4e00\u81f4\u6027\u5c31\u8d8a\u597d\uff0c\u4f46\u5e76\u53d1\u6027\u8d8a\u5f31<\/p>\n<p>3\u3001\u56db\u79cd\u9694\u79bb\u7ea7\u522b<\/p>\n<table>\n<thead>\n<tr>\n<th>\u7ea7\u522b<\/th>\n<th>\u4f5c\u7528<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u8bfb\u672a\u63d0\u4ea4\uff1aREAD UNCOMMITTED<\/td>\n<td>\u5141\u8bb8Transaction01\u8bfb\u53d6Transaction02\u672a\u63d0\u4ea4\u7684\u4fee\u6539<\/td>\n<\/tr>\n<tr>\n<td>\u8bfb\u5df2\u63d0\u4ea4\uff1aREAD COMMITTED<\/td>\n<td>\u8981\u6c42Transaction01\u53ea\u80fd\u8bfb\u53d6Transaction02\u5df2\u63d0\u4ea4\u7684\u4fee\u6539<\/td>\n<\/tr>\n<tr>\n<td>\u53ef\u91cd\u590d\u8bfb\uff1aREPEATABLE READ<\/td>\n<td>\u5373Transaction01\u6267\u884c\u671f\u95f4\u7981\u6b62\u5176\u5b83\u4e8b\u52a1\u5bf9\u8fd9\u4e2a\u5b57\u6bb5\u8fdb\u884c\u66f4\u65b0\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u4e32\u884c\u5316\uff1aSERIALIZABLE<\/td>\n<td>\u5728Transaction01\u6267\u884c\u671f\u95f4\uff0c\u7981\u6b62\u5176\u5b83\u4e8b\u52a1\u5bf9\u8fd9\u4e2a\u8868\u8fdb\u884c\u6dfb\u52a0\u3001\u66f4\u65b0\u3001\u5220\u9664\u64cd\u4f5c\u3002\u53ef\u4ee5\u907f\u514d\u4efb\u4f55\u5e76\u53d1\u95ee\u9898\uff0c\u4f46\u6027\u80fd\u5341\u5206\u4f4e\u4e0b\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>4\u3001\u5404\u4e2a\u9694\u79bb\u7ea7\u522b\u89e3\u51b3\u5e76\u53d1\u95ee\u9898\u7684\u80fd\u529b<\/p>\n<table>\n<thead>\n<tr>\n<th>\u9694\u79bb\u7ea7\u522b<\/th>\n<th>\u810f\u8bfb<\/th>\n<th>\u4e0d\u53ef\u91cd\u590d\u8bfb<\/th>\n<th>\u5e7b\u8bfb<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>READ UNCOMMITTED<\/td>\n<td>\u6709<\/td>\n<td>\u6709<\/td>\n<td>\u6709<\/td>\n<\/tr>\n<tr>\n<td>READ COMMITTED<\/td>\n<td>\u65e0<\/td>\n<td>\u6709<\/td>\n<td>\u6709<\/td>\n<\/tr>\n<tr>\n<td>REPEATABLE READ<\/td>\n<td>\u65e0<\/td>\n<td>\u65e0<\/td>\n<td>\u6709<\/td>\n<\/tr>\n<tr>\n<td>SERIALIZABLE<\/td>\n<td>\u65e0<\/td>\n<td>\u65e0<\/td>\n<td>\u65e0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>5\u3001\u5404\u79cd\u6570\u636e\u5e93\u4ea7\u54c1\u5bf9\u4e8b\u52a1\u9694\u79bb\u7ea7\u522b\u7684\u652f\u6301\u7a0b\u5ea6<\/p>\n<table>\n<thead>\n<tr>\n<th>\u9694\u79bb\u7ea7\u522b<\/th>\n<th>Oracle<\/th>\n<th>MySQL<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>READ UNCOMMITTED<\/td>\n<td>\u00d7<\/td>\n<td>\u221a<\/td>\n<\/tr>\n<tr>\n<td>READ COMMITTED<\/td>\n<td>\u221a(\u9ed8\u8ba4)<\/td>\n<td>\u221a<\/td>\n<\/tr>\n<tr>\n<td>REPEATABLE READ<\/td>\n<td>\u00d7<\/td>\n<td>\u221a(\u9ed8\u8ba4)<\/td>\n<\/tr>\n<tr>\n<td>SERIALIZABLE<\/td>\n<td>\u221a<\/td>\n<td>\u221a<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>9\u3001\u4e8b\u52a1\u5c5e\u6027\uff1a\u4f20\u64ad\u884c\u4e3a<\/h4>\n<h5>1\u3001\u4ec0\u4e48\u662f\u4e8b\u52a1\u4f20\u64ad\u7684\u884c\u4e3a\uff1f<\/h5>\n<p>1\u3001\u65b9\u6cd5a\u6709\u4e8b\u52a1\uff0c\u65b9\u6cd5b\u4e5f\u6709\u4e8b\u52a1\uff0c\u5982\u679c\u65b9\u6cd5a\u8c03\u7528\u4e86\u65b9\u6cd5b\uff0c\u90a3\u4e8b\u52a1\u8be5\u600e\u4e48\u529e?<\/p>\n<p>2\u3001\u662f\u6574\u5408\u4e24\u4e2a\u4e8b\u52a1\u5417\uff1f\u8fd9\u5c31\u662f\u4e8b\u52a1\u4f20\u64ad\u7684\u884c\u4e3a<\/p>\n<h5>2\u3001\u4e00\u5171\u6709\u4e03\u4e2d\u4f20\u64ad\u7684\u884c\u4e3a\uff1a<\/h5>\n<p>1\u3001\u5982\u4e0b\u8868<\/p>\n<table>\n<thead>\n<tr>\n<th>\u4f20\u64ad\u884c\u4e3a<\/th>\n<th>\u4f5c\u7528<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>?REQUIRED<\/td>\n<td>\u652f\u6301\u5f53\u524d\u4e8b\u52a1\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u5c31\u65b0\u5efa\u4e00\u4e2a(\u9ed8\u8ba4)<strong>\u3010\u6ca1\u6709\u5c31\u65b0\u5efa\uff0c\u6709\u5c31\u52a0\u5165\u3011<\/strong><\/td>\n<\/tr>\n<tr>\n<td>SUPPORTS<\/td>\n<td>\u652f\u6301\u5f53\u524d\u4e8b\u52a1\uff0c\u5982\u679c\u5f53\u524d\u6ca1\u6709\u4e8b\u52a1\uff0c\u5c31\u4ee5\u975e\u4e8b\u52a1\u65b9\u5f0f\u6267\u884c<strong>\u3010\u6709\u5c31\u52a0\u5165\uff0c\u6ca1\u6709\u5c31\u4e0d\u7ba1\u4e86\u3011<\/strong><\/td>\n<\/tr>\n<tr>\n<td>MANDATORY<\/td>\n<td>\u5fc5\u987b\u8fd0\u884c\u5728\u4e00\u4e2a\u4e8b\u52a1\u4e2d\uff0c\u5982\u679c\u5f53\u524d\u6ca1\u6709\u4e8b\u52a1\u6b63\u5728\u53d1\u751f\uff0c\u5c06\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38<strong>\u3010\u6709\u5c31\u52a0\u5165\uff0c\u6ca1\u6709\u5c31\u629b\u5f02\u5e38\u3011<\/strong><\/td>\n<\/tr>\n<tr>\n<td>?REQUIRES_NEW<\/td>\n<td>\u5f00\u542f\u4e00\u4e2a\u65b0\u7684\u4e8b\u52a1\uff0c\u5982\u679c\u4e00\u4e2a\u4e8b\u52a1\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u5c06\u8fd9\u4e2a\u5b58\u5728\u7684\u4e8b\u52a1\u6302\u8d77<strong>\u3010\u4e0d\u7ba1\u6709\u6ca1\u6709\uff0c\u76f4\u63a5\u5f00\u542f\u4e00\u4e2a\u65b0\u4e8b\u52a1\uff0c\u5f00\u542f\u7684\u65b0\u4e8b\u52a1\u548c\u4e4b\u524d\u7684\u4e8b\u52a1\u4e0d\u5b58\u5728\u5d4c\u5957\u5173\u7cfb\uff0c\u4e4b\u524d\u4e8b\u52a1\u88ab\u6302\u8d77\u3011<\/strong><\/td>\n<\/tr>\n<tr>\n<td>NOT_SUPPORTED<\/td>\n<td>\u4ee5\u975e\u4e8b\u52a1\u65b9\u5f0f\u8fd0\u884c\uff0c\u5982\u679c\u6709\u4e8b\u52a1\u5b58\u5728\uff0c\u6302\u8d77\u5f53\u524d\u4e8b\u52a1<strong>\u3010\u4e0d\u652f\u6301\u4e8b\u52a1\uff0c\u5b58\u5728\u5c31\u6302\u8d77\u3011<\/strong><\/td>\n<\/tr>\n<tr>\n<td>NEVER<\/td>\n<td>\u4ee5\u975e\u4e8b\u52a1\u65b9\u5f0f\u8fd0\u884c\uff0c\u5982\u679c\u6709\u4e8b\u52a1\u5b58\u5728\uff0c\u629b\u51fa\u5f02\u5e38<strong>\u3010\u4e0d\u652f\u6301\u4e8b\u52a1\uff0c\u5b58\u5728\u5c31\u629b\u5f02\u5e38\u3011<\/strong><\/td>\n<\/tr>\n<tr>\n<td>NESTED<\/td>\n<td>\u5982\u679c\u5f53\u524d\u6b63\u6709\u4e00\u4e2a\u4e8b\u52a1\u5728\u8fdb\u884c\u4e2d\uff0c\u5219\u8be5\u65b9\u6cd5\u5e94\u5f53\u8fd0\u884c\u5728\u4e00\u4e2a\u5d4c\u5957\u5f0f\u4e8b\u52a1\u4e2d\u3002\u88ab\u5d4c\u5957\u7684\u4e8b\u52a1\u53ef\u4ee5\u72ec\u7acb\u4e8e\u5916\u5c42\u4e8b\u52a1\u8fdb\u884c\u63d0\u4ea4\u6216\u56de\u6eda\u3002\u5982\u679c\u5916\u5c42\u4e8b\u52a1\u4e0d\u5b58\u5728\uff0c\u884c\u4e3a\u5c31\u50cfREQUIRED\u4e00\u6837\u3002<strong>\u3010\u6709\u4e8b\u52a1\u7684\u8bdd\uff0c\u5c31\u5728\u8fd9\u4e2a\u4e8b\u52a1\u91cc\u518d\u5d4c\u5957\u4e00\u4e2a\u5b8c\u5168\u72ec\u7acb\u7684\u4e8b\u52a1\uff0c\u5d4c\u5957\u7684\u4e8b\u52a1\u53ef\u4ee5\u72ec\u7acb\u7684\u63d0\u4ea4\u548c\u56de\u6eda\u3002\u6ca1\u6709\u4e8b\u52a1\u5c31\u548cREQUIRED\u4e00\u6837\u3002\u3011<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>3\u3001\u4f20\u64ad\u4e8b\u52a1\u4f8b\u5b50<\/h5>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/04\/image-20231022120446864.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/04\/image-20231022120446864.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20231022120446864\" \/><\/div><\/p>\n<p>1\u3001checkoutBookService.java<\/p>\n<pre><code>package com.atqt.spring6.tx.service;\n\npublic interface CheckoutService {\n    void checkoutBugBook(Integer[] bookIds,Integer userId);\n}<\/code><\/pre>\n<p>2\u3001checkoutBookServiceImpl.java<\/p>\n<pre><code>package com.atqt.spring6.tx.service;\n\nimport com.atqt.spring6.tx.dao.BookDao;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Component;\nimport org.springframework.stereotype.Repository;\nimport org.springframework.transaction.annotation.Transactional;\n\n@Repository\npublic class CheckoutServiceImpl implements CheckoutService{\n\n    @Autowired\n    private BookService bookService;\n\n    @Transactional\n    @Override\n    public void checkoutBugBook(Integer[] bookIds,Integer userId) {\n        for(Integer bookId:bookIds){\n            bookService.buyBook(bookId,userId);\n        }\n    }\n}<\/code><\/pre>\n<p>3\u3001TestUser<\/p>\n<pre><code>package com.atqt.spring6.tx;\n\nimport com.atqt.spring6.tx.controller.BookController;\nimport org.junit.jupiter.api.Test;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.test.context.junit.jupiter.SpringJUnitConfig;\n\n@SpringJUnitConfig(locations = &quot;classpath:beans.xml&quot;)\npublic class TestUser {\n    @Autowired\n    private BookController bookController ;\n    @Test\n    public void testBuyBook(){\n        Integer[] books = new Integer[]{1,2};\n        bookController.chekcout(books,1);\n    }\n\n}<\/code><\/pre>\n<p>4\u3001BookController.java<\/p>\n<pre><code>package com.atqt.spring6.tx.controller;\n\nimport com.atqt.spring6.tx.service.BookService;\nimport com.atqt.spring6.tx.service.CheckoutService;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Component;\n\n@Component\npublic class BookController {\n\/\/    @Autowired\n\/\/    private BookService bookService;\n\/\/\n\/\/    public void buyBook(Integer bookId, Integer userId) {\n\/\/\/\/        \u8c03\u7528service\u65b9\u6cd5\n\/\/        bookService.buyBook(bookId,userId);\n\/\/    }\n\n    @Autowired\n    CheckoutService checkoutService;\n\n    public void chekcout(Integer[] bookId,Integer userId){\n        checkoutService.checkoutBugBook(bookId,userId);\n    }\n\n}<\/code><\/pre>\n<p>5\u3001Dao\u662f\u4e00\u6837\u7684<\/p>\n<h4>10\u3001\u5168\u6ce8\u89e3\u914d\u7f6e\u4e8b\u52a1<\/h4>\n<h5>1\u3001\u7f16\u5199SpringConfig\u914d\u7f6e\u7c7b\u6587\u4ef6<\/h5>\n<p>1\u3001\u4ee3\u7801\u5b9e\u73b0<\/p>\n<pre><code class=\"language-java\">package com.atqt.spring6.tx.config;\n\nimport com.alibaba.druid.pool.DruidDataSource;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.ComponentScan;\nimport org.springframework.context.annotation.Configuration;\nimport org.springframework.jdbc.core.JdbcTemplate;\nimport org.springframework.jdbc.datasource.DataSourceTransactionManager;\nimport org.springframework.transaction.annotation.EnableTransactionManagement;\n\nimport javax.sql.DataSource;\n\n@Configuration\/\/\u914d\u7f6e\u7c7b\n@ComponentScan(&quot;com.atqt.spring6.tx&quot;) \/\/\u5f00\u542f\u626b\u63cf\u7684\u6ce8\u89e3\n@EnableTransactionManagement    \/\/\u5f00\u542f\u4e8b\u52a1\u7684\u6ce8\u89e3\npublic class SpringConfig {\n\n\/\/    \u521b\u5efa\u6570\u636e\u5e93\u5bf9\u8c61\n    @Bean\n    public DataSource getDataSource(){\n        DruidDataSource dataSource = new DruidDataSource();\n        dataSource.setDriverClassName(&quot;com.mysql.cj.jdbc.Driver&quot;);\n        dataSource.setUsername(&quot;root&quot;);\n        dataSource.setPassword(&quot;123456&quot;);\n        dataSource.setUrl(&quot;jdbc:mysql:\/\/localhost:3306\/spring?characterEncoding=utf8&amp;useSSL=false&quot;);\n        return dataSource;\n    }\n\n\/\/&lt;!--&lt;!\u2013    \u521b\u5efajdbcTemplate\u5bf9\u8c61\uff0c\u6ce8\u5165\u6570\u636e\u5e93\u2013&gt;--&gt;\n    @Bean(name = &quot;jdbcTemplate&quot;)\n    public JdbcTemplate getJdbcTemplate(DataSource dataSource){\n        JdbcTemplate jdbcTemplate = new JdbcTemplate();\n        jdbcTemplate.setDataSource(dataSource);\n        return  jdbcTemplate;\n    }\n\/\/    \u5f00\u542f\u4e8b\u52a1\u7ba1\u7406\u5668\n    @Bean\n    public DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource){\n        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();\n        dataSourceTransactionManager.setDataSource(dataSource);\n        return dataSourceTransactionManager;\n    }\n\n}<\/code><\/pre>\n<h5>2\u3001\u7f16\u5199jdbc.properties\u914d\u7f6e\u6587\u4ef6<\/h5>\n<p>1\u3001\u4ee3\u7801\u5b9e\u73b0<\/p>\n<pre><code class=\"language-properties\">jdbc.user=root\njdbc.password=1234565\njdbc.url=jdbc:mysql:\/\/localhost:3306\/spring?characterEncoding=utf8&amp;useSSL=false\njdbc.driver=com.mysql.cj.jdbc.Driver<\/code><\/pre>\n<h5>3\u3001TestAnno\u6d4b\u8bd5<\/h5>\n<p>1\u3001\u4ee3\u7801\u5b9e\u73b0<\/p>\n<pre><code class=\"language-java\">package com.atqt.spring6.tx;\n\nimport com.atqt.spring6.tx.config.SpringConfig;\nimport com.atqt.spring6.tx.controller.BookController;\nimport org.junit.jupiter.api.Test;\nimport org.springframework.context.ApplicationContext;\nimport org.springframework.context.annotation.AnnotationConfigApplicationContext;\n\npublic class TestAnno {\n\n    @Test\n    public void testTxAllAnnotation(){\n        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);\n        BookController bookController = context.getBean(&quot;bookController&quot;, BookController.class);\n        Integer[] bookIds = {1,2};\n        bookController.checkOut(bookIds,1);\n\n    }\n}<\/code><\/pre>\n<h3>5\u3001\u57fa\u4e8eXML\u7684\u58f0\u660e\u5f0f\u4e8b\u52a1<\/h3>\n<h4>1\u3001\u573a\u666f\u6a21\u62df<\/h4>\n<p>1\u3001\u57fa\u4e8e\u6ce8\u89e3\u7684\u58f0\u660e\u4e8b\u52a1<\/p>\n<h4>2\u3001\u4fee\u6539spring\u7684\u914d\u7f6e\u6587\u4ef6<\/h4>\n","protected":false},"excerpt":{"rendered":"<p>\u7b2c\u516d\u7ae0\u3001JUnit\u5355\u5143\u6d4b\u8bd5 \u9700\u6c42\u5206\u6790 1\u3001\u5728\u524d\u9762\u6211\u4eec\u90fd\u4f7f\u7528\u4e00\u4e0b\u4e24\u884c\u4ee3\u7801 ApplicationContext  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-5791","post","type-post","status-publish","format-standard","hentry","category-spring-spring"],"_links":{"self":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/5791","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/comments?post=5791"}],"version-history":[{"count":1,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/5791\/revisions"}],"predecessor-version":[{"id":5792,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/5791\/revisions\/5792"}],"wp:attachment":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/media?parent=5791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/categories?post=5791"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/tags?post=5791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}