forked from SpringForAll/SpringBoot-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.didispace</groupId> | ||
<artifactId>Chapter3-3-1</artifactId> | ||
<version>1.0.0</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>Chapter3-3-1</name> | ||
<description>Spring Boot project</description> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>1.3.2.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>5.1.21</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-jpa</artifactId> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.didispace; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class, args); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.didispace.domain; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
|
||
/** | ||
* @author 程序猿DD | ||
* @version 1.0.0 | ||
* @date 16/3/21 下午3:35. | ||
* @blog http://blog.didispace.com | ||
*/ | ||
@Entity | ||
public class User { | ||
|
||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
@Column(nullable = false, length = 5) | ||
private String name; | ||
|
||
@Column(nullable = false) | ||
private Integer age; | ||
|
||
public User(){} | ||
|
||
public User(String name, Integer age) { | ||
this.name = name; | ||
this.age = age; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Integer getAge() { | ||
return age; | ||
} | ||
|
||
public void setAge(Integer age) { | ||
this.age = age; | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
Chapter3-3-1/src/main/java/com/didispace/domain/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.didispace.domain; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
|
||
/** | ||
* @author 程序猿DD | ||
* @version 1.0.0 | ||
* @date 16/3/23 下午2:34. | ||
* @blog http://blog.didispace.com | ||
*/ | ||
public interface UserRepository extends JpaRepository<User, Long> { | ||
|
||
User findByName(String name); | ||
|
||
User findByNameAndAge(String name, Integer age); | ||
|
||
@Query("from User u where u.name=:name") | ||
User findUser(@Param("name") String name); | ||
|
||
|
||
} |
15 changes: 15 additions & 0 deletions
15
Chapter3-3-1/src/main/java/com/didispace/service/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.didispace.service; | ||
|
||
import com.didispace.domain.User; | ||
|
||
import javax.transaction.Transactional; | ||
|
||
/** | ||
* Created by Administrator on 2016/5/27. | ||
*/ | ||
public interface UserService { | ||
|
||
@Transactional | ||
User login(String name, String password); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
spring.datasource.url=jdbc:mysql://localhost:3306/test | ||
spring.datasource.username=root | ||
spring.datasource.password=123456 | ||
spring.datasource.driver-class-name=com.mysql.jdbc.Driver | ||
|
||
spring.jpa.properties.hibernate.hbm2ddl.auto=create |
60 changes: 60 additions & 0 deletions
60
Chapter3-3-1/src/test/java/com/didispace/ApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.didispace; | ||
|
||
import com.didispace.domain.User; | ||
import com.didispace.domain.UserRepository; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.SpringApplicationConfiguration; | ||
import org.springframework.test.annotation.Rollback; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
import javax.transaction.Transactional; | ||
|
||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@SpringApplicationConfiguration(Application.class) | ||
public class ApplicationTests { | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@Test | ||
@Transactional | ||
public void test() throws Exception { | ||
|
||
// 创建10条记录 | ||
userRepository.save(new User("AAA", 10)); | ||
userRepository.save(new User("BBB", 20)); | ||
userRepository.save(new User("CCC", 30)); | ||
userRepository.save(new User("DDD", 40)); | ||
userRepository.save(new User("EEE", 50)); | ||
userRepository.save(new User("FFF", 60)); | ||
userRepository.save(new User("GGG", 70)); | ||
userRepository.save(new User("HHHHHHHHH", 80)); | ||
userRepository.save(new User("III", 90)); | ||
userRepository.save(new User("JJJ", 100)); | ||
|
||
// 测试findAll, 查询所有记录 | ||
Assert.assertEquals(10, userRepository.findAll().size()); | ||
|
||
// 测试findByName, 查询姓名为FFF的User | ||
Assert.assertEquals(60, userRepository.findByName("FFF").getAge().longValue()); | ||
|
||
// 测试findUser, 查询姓名为FFF的User | ||
Assert.assertEquals(60, userRepository.findUser("FFF").getAge().longValue()); | ||
|
||
// 测试findByNameAndAge, 查询姓名为FFF并且年龄为60的User | ||
Assert.assertEquals("FFF", userRepository.findByNameAndAge("FFF", 60).getName()); | ||
|
||
// 测试删除姓名为AAA的User | ||
userRepository.delete(userRepository.findByName("AAA")); | ||
|
||
// 测试findAll, 查询所有记录, 验证上面的删除是否成功 | ||
Assert.assertEquals(9, userRepository.findAll().size()); | ||
|
||
} | ||
|
||
|
||
} |