Skip to content

Commit 5dfa6c8

Browse files
committed
使用事务控制业务逻辑的完整性
1 parent f0ff416 commit 5dfa6c8

File tree

7 files changed

+236
-0
lines changed

7 files changed

+236
-0
lines changed

Chapter3-3-1/pom.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.didispace</groupId>
7+
<artifactId>Chapter3-3-1</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>Chapter3-3-1</name>
12+
<description>Spring Boot project</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.3.2.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter</artifactId>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-test</artifactId>
35+
<scope>test</scope>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>mysql</groupId>
40+
<artifactId>mysql-connector-java</artifactId>
41+
<version>5.1.21</version>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-data-jpa</artifactId>
47+
</dependency>
48+
49+
</dependencies>
50+
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-maven-plugin</artifactId>
56+
</plugin>
57+
</plugins>
58+
</build>
59+
60+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.didispace;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
13+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.didispace.domain;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.Id;
7+
8+
/**
9+
* @author 程序猿DD
10+
* @version 1.0.0
11+
* @date 16/3/21 下午3:35.
12+
* @blog http://blog.didispace.com
13+
*/
14+
@Entity
15+
public class User {
16+
17+
@Id
18+
@GeneratedValue
19+
private Long id;
20+
21+
@Column(nullable = false, length = 5)
22+
private String name;
23+
24+
@Column(nullable = false)
25+
private Integer age;
26+
27+
public User(){}
28+
29+
public User(String name, Integer age) {
30+
this.name = name;
31+
this.age = age;
32+
}
33+
34+
public Long getId() {
35+
return id;
36+
}
37+
38+
public void setId(Long id) {
39+
this.id = id;
40+
}
41+
42+
public String getName() {
43+
return name;
44+
}
45+
46+
public void setName(String name) {
47+
this.name = name;
48+
}
49+
50+
public Integer getAge() {
51+
return age;
52+
}
53+
54+
public void setAge(Integer age) {
55+
this.age = age;
56+
}
57+
58+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.didispace.domain;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.data.jpa.repository.Query;
5+
import org.springframework.data.repository.query.Param;
6+
7+
8+
/**
9+
* @author 程序猿DD
10+
* @version 1.0.0
11+
* @date 16/3/23 下午2:34.
12+
* @blog http://blog.didispace.com
13+
*/
14+
public interface UserRepository extends JpaRepository<User, Long> {
15+
16+
User findByName(String name);
17+
18+
User findByNameAndAge(String name, Integer age);
19+
20+
@Query("from User u where u.name=:name")
21+
User findUser(@Param("name") String name);
22+
23+
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.didispace.service;
2+
3+
import com.didispace.domain.User;
4+
5+
import javax.transaction.Transactional;
6+
7+
/**
8+
* Created by Administrator on 2016/5/27.
9+
*/
10+
public interface UserService {
11+
12+
@Transactional
13+
User login(String name, String password);
14+
15+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
spring.datasource.url=jdbc:mysql://localhost:3306/test
2+
spring.datasource.username=root
3+
spring.datasource.password=123456
4+
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
5+
6+
spring.jpa.properties.hibernate.hbm2ddl.auto=create
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.didispace;
2+
3+
import com.didispace.domain.User;
4+
import com.didispace.domain.UserRepository;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.SpringApplicationConfiguration;
10+
import org.springframework.test.annotation.Rollback;
11+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
12+
13+
import javax.transaction.Transactional;
14+
15+
16+
@RunWith(SpringJUnit4ClassRunner.class)
17+
@SpringApplicationConfiguration(Application.class)
18+
public class ApplicationTests {
19+
20+
@Autowired
21+
private UserRepository userRepository;
22+
23+
@Test
24+
@Transactional
25+
public void test() throws Exception {
26+
27+
// 创建10条记录
28+
userRepository.save(new User("AAA", 10));
29+
userRepository.save(new User("BBB", 20));
30+
userRepository.save(new User("CCC", 30));
31+
userRepository.save(new User("DDD", 40));
32+
userRepository.save(new User("EEE", 50));
33+
userRepository.save(new User("FFF", 60));
34+
userRepository.save(new User("GGG", 70));
35+
userRepository.save(new User("HHHHHHHHH", 80));
36+
userRepository.save(new User("III", 90));
37+
userRepository.save(new User("JJJ", 100));
38+
39+
// 测试findAll, 查询所有记录
40+
Assert.assertEquals(10, userRepository.findAll().size());
41+
42+
// 测试findByName, 查询姓名为FFF的User
43+
Assert.assertEquals(60, userRepository.findByName("FFF").getAge().longValue());
44+
45+
// 测试findUser, 查询姓名为FFF的User
46+
Assert.assertEquals(60, userRepository.findUser("FFF").getAge().longValue());
47+
48+
// 测试findByNameAndAge, 查询姓名为FFF并且年龄为60的User
49+
Assert.assertEquals("FFF", userRepository.findByNameAndAge("FFF", 60).getName());
50+
51+
// 测试删除姓名为AAA的User
52+
userRepository.delete(userRepository.findByName("AAA"));
53+
54+
// 测试findAll, 查询所有记录, 验证上面的删除是否成功
55+
Assert.assertEquals(9, userRepository.findAll().size());
56+
57+
}
58+
59+
60+
}

0 commit comments

Comments
 (0)