瀏覽代碼

事务隔离级别以及传播行为

zxy 2 年之前
父節點
當前提交
a243eeab3c

+ 1 - 2
springMvc/src/main/java/com/controller/JdbcTemplateController.java

@@ -1,6 +1,6 @@
1 1
 package com.controller;
2 2
 
3
-import com.pojo.entity.AEntity;
3
+import com.transaction_.AEntity;
4 4
 import org.springframework.beans.factory.annotation.Autowired;
5 5
 import org.springframework.jdbc.core.BeanPropertyRowMapper;
6 6
 import org.springframework.jdbc.core.JdbcTemplate;
@@ -10,7 +10,6 @@ import org.springframework.web.bind.annotation.ResponseBody;
10 10
 
11 11
 import java.util.HashMap;
12 12
 import java.util.List;
13
-import java.util.Map;
14 13
 
15 14
 @Controller
16 15
 @RequestMapping("jdbc")

+ 1 - 1
springMvc/src/main/java/com/controller/ParamController.java

@@ -1,6 +1,6 @@
1 1
 package com.controller;
2 2
 
3
-import com.pojo.Student;
3
+import com.transaction_.pojo.entity.Student;
4 4
 import org.springframework.stereotype.Controller;
5 5
 import org.springframework.web.bind.annotation.RequestBody;
6 6
 import org.springframework.web.bind.annotation.RequestMapping;

+ 1 - 5
springMvc/src/main/java/com/controller/UserController.java

@@ -1,11 +1,7 @@
1 1
 package com.controller;
2 2
 
3
-import com.interceptor.MyInterceptor;
4
-import com.listener.WebApplicationContextUtils;
5
-import com.pojo.Student;
6
-import org.aopalliance.intercept.Interceptor;
3
+import com.transaction_.pojo.entity.Student;
7 4
 import org.springframework.beans.factory.annotation.Autowired;
8
-import org.springframework.context.ApplicationContext;
9 5
 import org.springframework.stereotype.Controller;
10 6
 import org.springframework.web.bind.annotation.RequestMapping;
11 7
 import org.springframework.web.bind.annotation.ResponseBody;

+ 1 - 1
springMvc/src/main/java/com/pojo/entity/AEntity.java

@@ -1,4 +1,4 @@
1
-package com.pojo.entity;
1
+package com.transaction_;
2 2
 
3 3
 import lombok.Data;
4 4
 

+ 1 - 1
springMvc/src/main/java/com/pojo/Student.java

@@ -1,4 +1,4 @@
1
-package com.pojo;
1
+package com.transaction_.pojo.entity;
2 2
 
3 3
 import lombok.Data;
4 4
 

+ 2 - 0
spring_annotation/src/main/java/com/dao/imp/StudentDaoImpl.java

@@ -1,11 +1,13 @@
1 1
 package com.dao.imp;
2 2
 
3 3
 import com.dao.StudentDao;
4
+import org.springframework.beans.factory.annotation.Autowired;
4 5
 import org.springframework.stereotype.Repository;
5 6
 
6 7
 //<bean id="studentDao" class="com.com.dao.imp.StudentDaoImpl"></bean>
7 8
 @Repository
8 9
 public class StudentDaoImpl implements StudentDao {
10
+
9 11
     @Override
10 12
     public void showStudent() {
11 13
         System.out.println("show...");

+ 25 - 0
spring_aop/pom.xml

@@ -38,6 +38,31 @@
38 38
             <version>4.13.1</version>
39 39
             <scope>compile</scope>
40 40
         </dependency>
41
+        <dependency>
42
+            <groupId>org.springframework</groupId>
43
+            <artifactId>spring-jdbc</artifactId>
44
+            <version>5.2.3.RELEASE</version>
45
+        </dependency>
46
+        <dependency>
47
+            <groupId>org.springframework</groupId>
48
+            <artifactId>spring-tx</artifactId>
49
+            <version>5.2.3.RELEASE</version>
50
+        </dependency>
51
+        <dependency>
52
+            <groupId>c3p0</groupId>
53
+            <artifactId>c3p0</artifactId>
54
+            <version>0.9.1.2</version>
55
+        </dependency>
56
+        <dependency>
57
+            <groupId>com.alibaba</groupId>
58
+            <artifactId>druid</artifactId>
59
+            <version>1.1.13</version>
60
+        </dependency>
61
+        <dependency>
62
+            <groupId>mysql</groupId>
63
+            <artifactId>mysql-connector-java</artifactId>
64
+            <version>8.0.17</version>
65
+        </dependency>
41 66
     </dependencies>
42 67
 
43 68
 </project>

+ 8 - 0
spring_aop/src/main/java/com/AopTest.java

@@ -2,6 +2,7 @@ package com;
2 2
 
3 3
 
4 4
 import com.aop.TargetInterface;
5
+import com.transaction_.service.AccountService;
5 6
 import org.junit.Test;
6 7
 import org.junit.runner.RunWith;
7 8
 import org.springframework.beans.factory.annotation.Autowired;
@@ -16,8 +17,15 @@ public class AopTest {
16 17
     private TargetInterface targetInterface;
17 18
     @Autowired
18 19
     private com.anno.TargetInterface targetInterface1;
20
+    @Autowired
21
+    private AccountService accountService;
19 22
     @Test
20 23
     public void test() {
21 24
         targetInterface1.save2();
22 25
     }
26
+    @Test
27
+    public void testInitAccount() {
28
+        accountService.initAccount();
29
+    }
30
+
23 31
 }

+ 39 - 0
spring_aop/src/main/java/com/config/JdbcConfig.java

@@ -0,0 +1,39 @@
1
+package com.config;
2
+
3
+import com.alibaba.druid.pool.DruidDataSource;
4
+import org.springframework.beans.factory.annotation.Value;
5
+import org.springframework.context.annotation.Bean;
6
+import org.springframework.context.annotation.Configuration;
7
+import org.springframework.context.annotation.PropertySource;
8
+import org.springframework.jdbc.core.JdbcTemplate;
9
+
10
+import javax.sql.DataSource;
11
+
12
+@Configuration
13
+@PropertySource("classpath:jdbc.properties")
14
+public class JdbcConfig {
15
+    // 写一个dataSource
16
+    @Value("${jdbc.url}")
17
+    String jdbcUrl;
18
+    @Value("${jdbc.driver}")
19
+    String driver;
20
+    @Value("${jdbc.username}")
21
+    String userName;
22
+    @Value("${jdbc.password}")
23
+    String password;
24
+    @Bean("getJdbcUrl")
25
+    public String getJdbcUrl() {
26
+        return jdbcUrl;
27
+    }
28
+    @Bean
29
+    public JdbcTemplate backJdbc() {
30
+        DruidDataSource druidDataSource = new DruidDataSource();
31
+        druidDataSource.setDriverClassName(driver);
32
+        druidDataSource.setUrl(jdbcUrl);
33
+        druidDataSource.setUsername(userName);
34
+        druidDataSource.setPassword(password);
35
+        JdbcTemplate jdbcTemplate = new JdbcTemplate();
36
+        jdbcTemplate.setDataSource(druidDataSource);
37
+        return jdbcTemplate;
38
+    }
39
+}

+ 9 - 0
spring_aop/src/main/java/com/transaction_/dao/AccountDao.java

@@ -0,0 +1,9 @@
1
+package com.transaction_.dao;
2
+
3
+
4
+import com.transaction_.entity.AccountEntity;
5
+
6
+public interface AccountDao {
7
+    public int saveAccount(AccountEntity accountEntity);
8
+
9
+}

+ 20 - 0
spring_aop/src/main/java/com/transaction_/dao/impl/AccountDaoImpl.java

@@ -0,0 +1,20 @@
1
+package com.transaction_.dao.impl;
2
+
3
+import com.transaction_.dao.AccountDao;
4
+import com.transaction_.entity.AccountEntity;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.jdbc.core.JdbcTemplate;
7
+import org.springframework.stereotype.Repository;
8
+
9
+@Repository
10
+public class AccountDaoImpl implements AccountDao {
11
+    @Autowired
12
+    private JdbcTemplate jdbcTemplate;
13
+    @Override
14
+    public int saveAccount(AccountEntity accountEntity) {
15
+        String addsql = "insert into account(user_name,balance) values(?,?)";
16
+        Object[] args = {accountEntity.getUserName(),accountEntity.getBalance()};
17
+        return jdbcTemplate.update(addsql,args);
18
+        //返回值update代表添加了几行
19
+    }
20
+}

+ 14 - 0
spring_aop/src/main/java/com/transaction_/entity/AccountEntity.java

@@ -0,0 +1,14 @@
1
+package com.transaction_.entity;
2
+
3
+import lombok.Data;
4
+
5
+import java.math.BigDecimal;
6
+import java.util.Date;
7
+
8
+@Data
9
+public class AccountEntity {
10
+    private Integer id;
11
+    private String userName;
12
+    private BigDecimal balance;
13
+    private Date ctime;
14
+}

+ 5 - 0
spring_aop/src/main/java/com/transaction_/service/AccountService.java

@@ -0,0 +1,5 @@
1
+package com.transaction_.service;
2
+
3
+public interface AccountService {
4
+     void initAccount();
5
+}

+ 22 - 0
spring_aop/src/main/java/com/transaction_/service/impl/AccountServiceImpl.java

@@ -0,0 +1,22 @@
1
+package com.transaction_.service.impl;
2
+
3
+import com.transaction_.dao.AccountDao;
4
+import com.transaction_.entity.AccountEntity;
5
+import com.transaction_.service.AccountService;
6
+import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.stereotype.Service;
8
+
9
+import java.math.BigDecimal;
10
+
11
+@Service
12
+public class AccountServiceImpl implements AccountService {
13
+    @Autowired
14
+    AccountDao accountDao;
15
+    @Override
16
+    public void initAccount() {
17
+        AccountEntity accountEntity = new AccountEntity();
18
+        accountEntity.setUserName("mr.zhang");
19
+        accountEntity.setBalance(new BigDecimal("500.5"));
20
+        int i = accountDao.saveAccount(accountEntity);
21
+    }
22
+}

+ 2 - 0
spring_aop/src/main/resources/context.xml

@@ -33,6 +33,8 @@
33 33
         </aop:aspect>
34 34
     </aop:config>
35 35
     <context:component-scan base-package="com.anno"></context:component-scan>
36
+    <context:component-scan base-package="com.config"></context:component-scan>
37
+    <context:component-scan base-package="com.transaction_"></context:component-scan>
36 38
 <!--    aop自动代理-->
37 39
     <aop:aspectj-autoproxy />
38 40
 </beans>

+ 4 - 0
spring_aop/src/main/resources/jdbc.properties

@@ -0,0 +1,4 @@
1
+jdbc.driver=com.mysql.cj.jdbc.Driver
2
+jdbc.url=jdbc:mysql://47.110.156.18:3306/sczn_eshop_test1?zeroDateTimeBehavior=convertToNull&useUnicode=true&autoReconnect=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
3
+jdbc.username=sczn
4
+jdbc.password=sczn159357

+ 1 - 1
spring_test/src/main/java/com/controller/UserController.java

@@ -1,6 +1,6 @@
1 1
 package com.controller;
2 2
 
3
-import com.pojo.entity.AEntity;
3
+import com.transaction_.AEntity;
4 4
 import org.springframework.beans.factory.annotation.Autowired;
5 5
 import org.springframework.dao.EmptyResultDataAccessException;
6 6
 import org.springframework.jdbc.core.BeanPropertyRowMapper;

+ 1 - 1
spring_test/src/main/java/com/pojo/entity/AEntity.java

@@ -1,4 +1,4 @@
1
-package com.pojo.entity;
1
+package com.transaction_;
2 2
 
3 3
 import lombok.Data;
4 4
 

+ 4 - 4
spring_test/src/main/resources/jdbc.properties

@@ -1,4 +1,4 @@
1
-jdbc.driver=com.mysql.jdbc.Driver
2
-jdbc.url=jdbc:mysql://47.110.156.18:3306/sczn_eshop_test?zeroDateTimeBehavior=convertToNull
3
-jdbc.username=sczn
4
-jdbc.password=sczn159357
1
+jdbc.driver=com.mysql.cj.jdbc.Driver
2
+jdbc.url=jdbc:mysql://192.168.50.132:3306/sczn_eshop_test?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
3
+jdbc.username=root
4
+jdbc.password=123456