mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-14 06:03:52 +08:00
完善章节4单元测试
This commit is contained in:
@@ -1,12 +1,40 @@
|
||||
package spring.boot.core;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import spring.boot.core.domain.User;
|
||||
import spring.boot.core.domain.UserRepository;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ValidatingFormInputApplication {
|
||||
public class ValidatingFormInputApplication implements CommandLineRunner {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ValidatingFormInputApplication.class, args);
|
||||
}
|
||||
|
||||
private Logger LOG = LoggerFactory.getLogger(ValidatingFormInputApplication.class);
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ValidatingFormInputApplication.class, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
User user1 = new User("Sergey", 24, "1994-01-01");
|
||||
User user2 = new User("Ivan", 26, "1994-01-01");
|
||||
User user3 = new User("Adam", 31, "1994-01-01");
|
||||
LOG.info("Inserting data in DB.");
|
||||
userRepository.save(user1);
|
||||
userRepository.save(user2);
|
||||
userRepository.save(user3);
|
||||
LOG.info("User count in DB: {}", userRepository.count());
|
||||
LOG.info("User with ID 1: {}", userRepository.findById(1L));
|
||||
LOG.info("Deleting user with ID 2L form DB.");
|
||||
userRepository.deleteById(2L);
|
||||
LOG.info("User count in DB: {}", userRepository.count());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,6 +79,15 @@ public class User implements Serializable {
|
||||
this.birthday = birthday;
|
||||
}
|
||||
|
||||
|
||||
public User(String name, Integer age, String birthday) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.birthday = birthday;
|
||||
}
|
||||
|
||||
public User() {}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" +
|
||||
|
||||
@@ -103,7 +103,7 @@ public class UserController {
|
||||
/**
|
||||
* 处理 "/users/{id}" 的 GET 请求,用来删除 User 信息
|
||||
*/
|
||||
@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
|
||||
@RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
|
||||
public String deleteUser(@PathVariable Long id) {
|
||||
|
||||
userService.delete(id);
|
||||
|
||||
@@ -1,2 +1,15 @@
|
||||
## 开启 H2 数据库
|
||||
spring.h2.console.enabled=true
|
||||
|
||||
## 配置 H2 数据库连接信息
|
||||
spring.datasource.url=jdbc:h2:mem:testdb
|
||||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
|
||||
|
||||
|
||||
## 是否显示 SQL 语句
|
||||
spring.jpa.show-sql=true
|
||||
spring.jpa.show-sql=true
|
||||
hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||
hibernate.hbm2ddl.auto=create
|
||||
|
||||
Reference in New Issue
Block a user