2 Commits 7dbc4d08ff ... df53feaf9e

Auteur SHA1 Message Date
  zxy df53feaf9e Merge branch 'master' of http://git.semsx.com/zhangxiaoyu/spring_demo il y a 2 ans
  zxy 023edb59f4 事务的传播行为 测试、 il y a 2 ans

+ 9 - 0
mybatis_train/src/main/java/com/pojo/test_entity/Person.java

@@ -0,0 +1,9 @@
1
+package com.pojo.test_entity;
2
+
3
+import lombok.Data;
4
+
5
+@Data
6
+public class Person {
7
+    private String name;
8
+    private Integer age;
9
+}

+ 14 - 0
mybatis_train/src/main/java/com/pojo/test_entity/Student.java

@@ -0,0 +1,14 @@
1
+package com.pojo.test_entity;
2
+
3
+import lombok.Data;
4
+import lombok.EqualsAndHashCode;
5
+import lombok.ToString;
6
+
7
+@Data
8
+@EqualsAndHashCode(callSuper = true)
9
+@ToString(callSuper = true)
10
+//@EqualsAndHashCode
11
+public class Student extends Person{
12
+    // 班级
13
+    private String classes;
14
+}

+ 19 - 0
mybatis_train/src/test/java/TestLombok.java

@@ -0,0 +1,19 @@
1
+import com.pojo.test_entity.Student;
2
+import org.junit.Test;
3
+
4
+public class TestLombok {
5
+
6
+    @Test
7
+    public void testEqualAndHashCode() {
8
+        Student student1 = new Student();
9
+        student1.setClasses("7年级");
10
+        student1.setName("zhang");
11
+        student1.setAge(11);
12
+        Student student2 = new Student();
13
+        student2.setClasses("7年级");
14
+        student2.setName("zhang");
15
+        student2.setAge(111);
16
+        System.out.println(student1);
17
+        boolean equals = student1.equals(student2);
18
+    }
19
+}

+ 2 - 0
springMvc/src/main/java/com/controller/ExceptionTestController.java

@@ -21,6 +21,8 @@ public class ExceptionTestController {
21 21
     public Map exception1() {
22 22
         HashMap stringStringHashMap = new HashMap<String, String>();
23 23
         stringStringHashMap.put("1","2");
24
+        
25
+
24 26
         throw new MyException();
25 27
 //        return stringStringHashMap;
26 28
     }

+ 12 - 8
spring_annotation/src/main/java/AnnotationMain.java

@@ -1,7 +1,9 @@
1 1
 import com.Config.JdbcUtil;
2 2
 import com.Config.SpringConfiguration;
3
+import com.Config.TestConfiguration;
3 4
 import com.alibaba.druid.pool.DruidDataSource;
4 5
 import com.alibaba.druid.pool.DruidPooledConnection;
6
+import com.entity.User;
5 7
 import com.service.JwtService;
6 8
 import com.service.StudentService;
7 9
 import com.service.UserService;
@@ -14,14 +16,16 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
14 16
 public class AnnotationMain {
15 17
     public static void main(String[] args) throws Exception{
16 18
         ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfiguration.class);
17
-        Object jdbcUrl = applicationContext.getBean("getJdbcUrl");
18
-        StudentService bean = applicationContext.getBean(StudentService.class);
19
-        JwtService bean1 = applicationContext.getBean(JwtService.class);
20
-        String s = bean1.generateToken("123");
21
-        bean1.parseToken(s);
22
-        UserService userService = applicationContext.getBean(UserService.class);
23
-        userService.addUserBatch();
24
-        System.out.println(JdbcUtil.URL);
19
+//        Object jdbcUrl = applicationContext.getBean("getJdbcUrl");
20
+//        StudentService bean = applicationContext.getBean(StudentService.class);
21
+//        JwtService bean1 = applicationContext.getBean(JwtService.class);
22
+//        String s = bean1.generateToken("123");
23
+//        bean1.parseToken(s);
24
+//        UserService userService = applicationContext.getBean(UserService.class);
25
+//        userService.addUserBatch();
26
+        TestConfiguration bean2 = applicationContext.getBean(TestConfiguration.class);
27
+        User user1 = applicationContext.getBean("user1", User.class);
28
+        User user11 = bean2.getUser1();
25 29
 //        JdbcUtil bean2 = applicationContext.getBean(JdbcUtil.class);
26 30
     }
27 31
 }

+ 10 - 0
spring_annotation/src/main/java/com/Config/TestConfiguration.java

@@ -1,12 +1,22 @@
1 1
 package com.Config;
2 2
 
3
+import com.entity.User;
3 4
 import org.springframework.context.annotation.Bean;
4 5
 import org.springframework.context.annotation.Configuration;
5 6
 
6 7
 @Configuration
8
+//@Configuration(proxyBeanMethods = false)
7 9
 public class TestConfiguration {
8 10
     @Bean
9 11
     public String testStringBean() {
10 12
         return "String";
11 13
     }
14
+    @Bean("user1")
15
+    public User getUser1() {
16
+        return new User(1,"user1");
17
+    }
18
+    @Bean("user2")
19
+    public User getUser2() {
20
+        return new User(2,"user2");
21
+    }
12 22
 }

+ 14 - 0
spring_annotation/src/main/java/com/entity/User.java

@@ -0,0 +1,14 @@
1
+package com.entity;
2
+
3
+import lombok.Data;
4
+
5
+@Data
6
+public class User {
7
+    private Integer userId;
8
+    private String userName;
9
+
10
+    public User(Integer userId, String userName) {
11
+        this.userId = userId;
12
+        this.userName = userName;
13
+    }
14
+}

+ 0 - 23
spring_annotation/src/main/resources/context.xml

@@ -1,23 +0,0 @@
1
-<?xml version="1.0" encoding="UTF-8"?>
2
-
3
-<beans xmlns="http://www.springframework.org/schema/beans"
4
-       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5
-       xmlns:context="http://www.springframework.org/schema/context"
6
-       xsi:schemaLocation="http://www.springframework.org/schema/beans
7
-       http://www.springframework.org/schema/beans/spring-beans.xsd
8
-       http://www.springframework.org/schema/context
9
-       https://www.springframework.org/schema/context/spring-context.xsd">
10
-<!--    <bean id="userService1" class="com.service.impl.UserServiceImpl">-->
11
-<!--&lt;!&ndash;        <constructor-arg name="userDao" ref="UserDao"></constructor-arg>&ndash;&gt;-->
12
-<!--    </bean>-->
13
-<!--    加载外部 properties文件-->
14
-<!--    <context:property-placeholder location="jdbc.properties"></context:property-placeholder>-->
15
-<!--    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">-->
16
-<!--        <property name="driverClassName" value="${jdbc.driver}"></property>-->
17
-<!--        <property name="url" value="${jdbc.url}"></property>-->
18
-<!--        <property name="username" value="${jdbc.username}"></property>-->
19
-<!--        <property name="password" value="${jdbc.password}"></property>-->
20
-<!--    </bean>-->
21
-    <!--    组件扫描-->
22
-    <context:component-scan base-package="com"></context:component-scan>
23
-</beans>

+ 2 - 2
spring_aop/src/main/java/com/transaction_anno/service/impl/AccountServiceImpl.java

@@ -20,8 +20,8 @@ public class AccountServiceImpl implements AccountService {
20 20
         accountEntity.setBalance(300.0);
21 21
         int i = accountDao.saveAccount(accountEntity);
22 22
     }
23
-    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
24
-//    @Transactional
23
+//    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
24
+    @Transactional
25 25
     @Override
26 26
     public int reduceAccount(AccountEntity toEntity) {
27 27
         int in = accountDao.incomeAccount(toEntity, "to");

+ 2 - 2
spring_aop/src/main/java/com/transaction_anno/service/impl/AccountTxServiceImpl.java

@@ -16,8 +16,8 @@ public class AccountTxServiceImpl implements AccountTxService {
16 16
     @Autowired
17 17
     AccountTwoService accountTwoService;
18 18
 
19
-    @Transactional(propagation = Propagation.REQUIRES_NEW)
20
-//    @Transactional
19
+//    @Transactional(propagation = Propagation.REQUIRED)
20
+    @Transactional
21 21
     @Override
22 22
     public void transformTx() {
23 23
         // 转账操作