Skip to content

Commit b09fd15

Browse files
alejandrogervasioecheyne
authored andcommitted
Initial Commit (eugenp#5581)
1 parent afc62ca commit b09fd15

11 files changed

Lines changed: 476 additions & 0 deletions

File tree

‎spring-boot-crud/pom.xml‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung.spring-boot-crud</groupId>
8+
<artifactId>spring-boot-crud</artifactId>
9+
<version>0.1.0</version>
10+
11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>2.0.6.RELEASE</version>
15+
</parent>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-web</artifactId>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-data-jpa</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-test</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.mockito</groupId>
36+
<artifactId>mockito-core</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>com.h2database</groupId>
41+
<artifactId>h2</artifactId>
42+
<scope>runtime</scope>
43+
</dependency>
44+
</dependencies>
45+
<properties>
46+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
47+
<java.version>1.8</java.version>
48+
</properties>
49+
50+
<build>
51+
<finalName>spring-boot-crud</finalName>
52+
<resources>
53+
<resource>
54+
<directory>src/main/resources</directory>
55+
<filtering>true</filtering>
56+
</resource>
57+
</resources>
58+
<plugins>
59+
<plugin>
60+
<groupId>org.springframework.boot</groupId>
61+
<artifactId>spring-boot-maven-plugin</artifactId>
62+
</plugin>
63+
</plugins>
64+
</build>
65+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.crud;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
6+
import org.springframework.boot.autoconfigure.domain.EntityScan;
7+
import org.springframework.context.annotation.ComponentScan;
8+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
9+
import org.springframework.transaction.annotation.EnableTransactionManagement;
10+
11+
@SpringBootApplication
12+
@EnableAutoConfiguration
13+
@ComponentScan(basePackages={"com.baeldung.crud"})
14+
@EnableJpaRepositories(basePackages="com.baeldung.crud.repositories")
15+
@EnableTransactionManagement
16+
@EntityScan(basePackages="com.baeldung.crud.entities")
17+
18+
@SpringBootApplication
19+
public class Application {
20+
21+
public static void main(String[] args) {
22+
SpringApplication.run(Application.class, args);
23+
}
24+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.baeldung.crud.controllers;
2+
3+
import com.baeldung.crud.UserRepository;
4+
import com.baeldung.crud.entities.User;
5+
import javax.validation.Valid;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.ui.Model;
9+
import org.springframework.validation.BindingResult;
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.PathVariable;
12+
import org.springframework.web.bind.annotation.PostMapping;
13+
14+
@Controller
15+
public class UserController {
16+
17+
private final UserRepository userRepository;
18+
19+
@Autowired
20+
public UserController(UserRepository userRepository) {
21+
this.userRepository = userRepository;
22+
}
23+
24+
@GetMapping("/signup")
25+
public String showSignUpForm(User user) {
26+
return "add-user";
27+
}
28+
29+
@PostMapping("/adduser")
30+
public String addUser(@Valid User user, BindingResult result, Model model) {
31+
if (result.hasErrors()) {
32+
return "add-user";
33+
}
34+
35+
userRepository.save(user);
36+
model.addAttribute("users", userRepository.findAll());
37+
return "index";
38+
}
39+
40+
@GetMapping("/edit/{id}")
41+
public String showUpdateForm(@PathVariable("id") long id, Model model) {
42+
User user = userRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id));
43+
model.addAttribute("user", user);
44+
return "update-user";
45+
}
46+
47+
@PostMapping("/update/{id}")
48+
public String updateUser(@PathVariable("id") long id, @Valid User user, BindingResult result, Model model) {
49+
if (result.hasErrors()) {
50+
user.setId(id);
51+
return "update-user";
52+
}
53+
54+
userRepository.save(user);
55+
model.addAttribute("users", userRepository.findAll());
56+
return "index";
57+
}
58+
59+
@GetMapping("/delete/{id}")
60+
public String deleteUser(@PathVariable("id") long id, Model model) {
61+
User user = userRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id));
62+
userRepository.delete(user);
63+
model.addAttribute("users", userRepository.findAll());
64+
return "index";
65+
}
66+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.crud.entities;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
import javax.validation.constraints.NotBlank;
8+
9+
@Entity
10+
public class User {
11+
12+
@Id
13+
@GeneratedValue(strategy = GenerationType.AUTO)
14+
private long id;
15+
@NotBlank(message = "Name is mandatory")
16+
private String name;
17+
18+
@NotBlank(message = "Email is mandatory")
19+
private String email;
20+
21+
public User() {}
22+
23+
public User(String name, String email) {
24+
this.name = name;
25+
this.email = email;
26+
}
27+
28+
public void setId(long id) {
29+
this.id = id;
30+
}
31+
32+
public long getId() {
33+
return id;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
40+
public void setEmail(String email) {
41+
this.email = email;
42+
}
43+
44+
public String getName() {
45+
return name;
46+
}
47+
48+
public String getEmail() {
49+
return email;
50+
}
51+
52+
@Override
53+
public String toString() {
54+
return "User{" + "id=" + id + ", name=" + name + ", email=" + email + '}';
55+
}
56+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.crud.repositories;
2+
3+
import com.baeldung.crud.entities.User;
4+
import java.util.List;
5+
import org.springframework.data.repository.CrudRepository;
6+
import org.springframework.stereotype.Repository;
7+
8+
@Repository
9+
public interface UserRepository extends CrudRepository<User, Long> {
10+
11+
List<User> findByName(String name);
12+
13+
}

‎spring-boot-crud/src/main/resources/css/shards.min.css‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="x-ua-compatible" content="ie=edge">
6+
<title>Add User</title>
7+
<meta name="viewport" content="width=device-width, initial-scale=1">
8+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
9+
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css" integrity="sha384-5sAR7xN1Nv6T6+dT2mhtzEpVJvfS3NScPQTrOxhwjIuvcA67KV2R5Jz6kr4abQsz" crossorigin="anonymous">
10+
<link rel="stylesheet" href="../css/shards.min.css">
11+
</head>
12+
<body>
13+
<div class="container my-5">
14+
<h2 class="mb-5">New User</h2>
15+
<div class="row">
16+
<div class="col-md-6">
17+
<form action="#" th:action="@{/adduser}" th:object="${user}" method="post">
18+
<div class="row">
19+
<div class="form-group col-md-6">
20+
<label for="name" class="col-form-label">Name</label>
21+
<input type="text" th:field="*{name}" class="form-control" id="name" placeholder="Name">
22+
<span th:if="${#fields.hasErrors('name')}" th:errors="*{name}" class="text-danger"></span>
23+
</div>
24+
<div class="form-group col-md-6">
25+
<label for="email" class="col-form-label">Email</label>
26+
<input type="text" th:field="*{email}" class="form-control" id="email" placeholder="Email">
27+
<span th:if="${#fields.hasErrors('email')}" th:errors="*{email}" class="text-danger"></span>
28+
</div>
29+
</div>
30+
<div class="row">
31+
<div class="col-md-6 mt-5">
32+
<input type="submit" class="btn btn-primary" value="Add User">
33+
</div>
34+
</div>
35+
</form>
36+
</div>
37+
</div>
38+
</div>
39+
</body>
40+
</html>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="x-ua-compatible" content="ie=edge">
6+
<title>Users</title>
7+
<meta name="viewport" content="width=device-width, initial-scale=1">
8+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
9+
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css" integrity="sha384-5sAR7xN1Nv6T6+dT2mhtzEpVJvfS3NScPQTrOxhwjIuvcA67KV2R5Jz6kr4abQsz" crossorigin="anonymous">
10+
<link rel="stylesheet" href="../css/shards.min.css">
11+
</head>
12+
<body>
13+
<div th:switch="${users}" class="container my-5">
14+
<div class="row">
15+
<div class="col-md-6">
16+
<h2 th:case="null">No users yet!</h2>
17+
<div th:case="*">
18+
<h2 class="my-5">Users</h2>
19+
<table class="table table-striped table-responsive-md">
20+
<thead>
21+
<tr>
22+
<th>Name</th>
23+
<th>Email</th>
24+
<th>Edit</th>
25+
<th>Delete</th>
26+
</tr>
27+
</thead>
28+
<tbody>
29+
<tr th:each="user : ${users}">
30+
<td th:text="${user.name}"></td>
31+
<td th:text="${user.email}"></td>
32+
<td><a th:href="@{/edit/{id}(id=${user.id})}" class="btn btn-primary"><i class="fas fa-user-edit ml-2"></i></a></td>
33+
<td><a th:href="@{/delete/{id}(id=${user.id})}" class="btn btn-primary"><i class="fas fa-user-times ml-2"></i></a></td>
34+
</tr>
35+
</tbody>
36+
</table>
37+
</div>
38+
<p class="my-5"><a href="/signup" class="btn btn-primary"><i class="fas fa-user-plus ml-2"></i></a></p>
39+
</div>
40+
</div>
41+
</div>
42+
</body>
43+
</html>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="x-ua-compatible" content="ie=edge">
6+
<title>Update User</title>
7+
<meta name="viewport" content="width=device-width, initial-scale=1">
8+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
9+
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css" integrity="sha384-5sAR7xN1Nv6T6+dT2mhtzEpVJvfS3NScPQTrOxhwjIuvcA67KV2R5Jz6kr4abQsz" crossorigin="anonymous">
10+
<link rel="stylesheet" href="../css/shards.min.css">
11+
</head>
12+
<body>
13+
<div class="container my-5">
14+
<h2 class="mb-5">Update User</h2>
15+
<div class="row">
16+
<div class="col-md-6">
17+
<form action="#" th:action="@{/update/{id}(id=${user.id})}" th:object="${user}" method="post">
18+
<div class="row">
19+
<div class="form-group col-md-6">
20+
<label for="name" class="col-form-label">Name</label>
21+
<input type="text" th:field="*{name}" class="form-control" id="name" placeholder="Name">
22+
<span th:if="${#fields.hasErrors('name')}" th:errors="*{name}" class="text-danger"></span>
23+
</div>
24+
<div class="form-group col-md-6">
25+
<label for="email" class="col-form-label">Email</label>
26+
<input type="text" th:field="*{email}" class="form-control" id="email" placeholder="Email">
27+
<span th:if="${#fields.hasErrors('email')}" th:errors="*{email}" class="text-danger"></span>
28+
</div>
29+
</div>
30+
<div class="row">
31+
<div class="col-md-6 mt-5">
32+
<input type="submit" class="btn btn-primary" value="Update User">
33+
</div>
34+
</div>
35+
</form>
36+
</div>
37+
</div>
38+
</div>
39+
</body>
40+
</html>

0 commit comments

Comments
 (0)