crud implementation

This commit is contained in:
Anton Romanov 2021-10-28 17:07:25 +04:00
parent 261fab0bfa
commit b126afac5e
8 changed files with 245 additions and 1 deletions

View File

@ -29,6 +29,8 @@ dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-devtools")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("com.h2database:h2")
testCompile("junit:junit")
}

View File

@ -1,6 +1,8 @@
package email;
import email.model.Email;
import email.model.EmailForm;
import email.service.EmailService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@ -9,6 +11,11 @@ import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class EmailController {
private final EmailService emailService;
public EmailController(EmailService emailService) {
this.emailService = emailService;
}
@GetMapping("/")
public String indexForm(Model model) {
@ -22,7 +29,14 @@ public class EmailController {
model.addAttribute("error", "'Кому' не должно быть пустым");
return "index";
}
return "result";
emailService.save(new Email(emailForm.getTo(), emailForm.getSubject(), emailForm.getMessage()));
model.addAttribute("emails", emailService.getAllEmails());
return "list";
}
@GetMapping("/list")
public String list(Model model) {
model.addAttribute("emails", emailService.getAllEmails());
return "list";
}
}

View File

@ -0,0 +1,83 @@
package email.core;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
@MappedSuperclass
public abstract class BaseEntity implements Serializable, Comparable<BaseEntity> {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Integer id;
@Version
private Integer version;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!getClass().isAssignableFrom(obj.getClass())) {
return false;
}
BaseEntity other = (BaseEntity) obj;
if (id == null) {
if (other.id != null) {
return false;
}
} else if (!id.equals(other.id)) {
return false;
}
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (id == null ? 0 : id.hashCode());
return result;
}
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
"id=" + id +
", version=" + version +
'}';
}
@Override
public int compareTo(@NotNull BaseEntity o) {
return id != null ? id.compareTo(o.getId()) : -1;
}
public void reset() {
this.id = null;
this.version = null;
}
}

View File

@ -0,0 +1,46 @@
package email.model;
import email.core.BaseEntity;
import javax.persistence.Entity;
@Entity
public class Email extends BaseEntity {
private String to;
private String subject;
private String message;
public Email() {
}
public Email(String to, String subject, String message) {
this.to = to;
this.subject = subject;
this.message = message;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}

View File

@ -0,0 +1,10 @@
package email.repository;
import email.model.Email;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface EmailRepository extends JpaRepository<Email, Integer> {
}

View File

@ -0,0 +1,33 @@
package email.service;
import email.model.Email;
import email.repository.EmailRepository;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class EmailService {
private final EmailRepository emailRepository;
public EmailService(EmailRepository emailRepository) {
this.emailRepository = emailRepository;
}
public List<Email> getAllEmails() {
return emailRepository.findAll();
}
public Email save(Email email) {
return emailRepository.save(email);
}
public Email getEmailById(Integer id) {
return emailRepository.getOne(id);
}
public void delete(Integer setId) {
emailRepository.deleteById(setId);
}
}

View File

@ -0,0 +1,14 @@
# Server Settings
spring.main.banner-mode=off
server.port=8080
# Log settings (TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF)
logging.level.ru.ulstu=DEBUG
#JPA
# go to http://localhost:8080/h2-console
spring.datasource.url=jdbc:h2:file:./data/emails
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.jpa.hibernate.ddl-auto=update

View File

@ -0,0 +1,42 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Список записей в БД</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap-theme.min.css"
integrity="sha384-6pzBo3FDv/PJ8r2KRkGHifhEocL+1X2rVCTTkUfGk7/0pbek5mMa1upzvWbrUbOZ" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"
integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd"
crossorigin="anonymous"></script>
</head>
<body>
<h1>Список записей в БД:</h1>
<table class="table table-striped">
<thead class="thead-dark">
<tr>
<th scope="col">Кому</th>
<th scope="col">Тема</th>
<th scope="col">Сообщение</th>
</tr>
</thead>
<tbody>
<tr th:each="email: ${emails}">
<td th:text="${email.to}">
</td>
<td th:text="${email.subject}">
</td>
<td th:text="${email.message}">
</td>
</tr>
</tbody>
</table>
<a href="/">Отправить другое сообщение</a>
</body>
</html>