feat: added Lombok library and implemented for entities.

This commit is contained in:
miller 2020-09-04 15:35:33 +04:30
parent 579b62a81c
commit 93954e1eca
14 changed files with 69 additions and 337 deletions

View file

@ -46,6 +46,12 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>

View file

@ -1,15 +0,0 @@
package org.springframework.samples.petclinic.configuration;
import org.modelmapper.ModelMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppBean {
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
}

View file

@ -1,77 +0,0 @@
package org.springframework.samples.petclinic.dto;
public class OwnerDto {
private Long id;
private String firstName;
private String lastName;
private String address;
private String city;
private String telephone;
public OwnerDto() {
}
public OwnerDto(Long id, String firstName, String lastName, String address, String city, String telephone) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.city = city;
this.telephone = telephone;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
}

View file

@ -1,60 +0,0 @@
package org.springframework.samples.petclinic.dto;
import org.springframework.samples.petclinic.owner.Owner;
import org.springframework.samples.petclinic.owner.PetType;
import java.time.LocalDate;
public class PetDto {
private Long id;
private LocalDate birthDate;
private PetType type;
private Owner owner;
public PetDto() {
}
public PetDto(Long id, LocalDate birthDate, PetType type, Owner owner) {
this.id = id;
this.birthDate = birthDate;
this.type = type;
this.owner = owner;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public LocalDate getBirthDate() {
return birthDate;
}
public void setBirthDate(LocalDate birthDate) {
this.birthDate = birthDate;
}
public PetType getType() {
return type;
}
public void setType(PetType type) {
this.type = type;
}
public Owner getOwner() {
return owner;
}
public void setOwner(Owner owner) {
this.owner = owner;
}
}

View file

@ -1,57 +0,0 @@
package org.springframework.samples.petclinic.dto;
import java.time.LocalDate;
public class VisitDto {
private Long id;
private LocalDate date;
private String description;
private Integer petId;
public VisitDto() {
}
public VisitDto(Long id, LocalDate date, String description, Integer petId) {
this.id = id;
this.date = date;
this.description = description;
this.petId = petId;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public LocalDate getDate() {
return date;
}
public void setDate(LocalDate date) {
this.date = date;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getPetId() {
return petId;
}
public void setPetId(Integer petId) {
this.petId = petId;
}
}

View file

@ -15,12 +15,15 @@
*/
package org.springframework.samples.petclinic.model;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import java.io.Serializable;
/**
* Simple JavaBean domain object with an id property. Used as a base class for objects
@ -30,20 +33,15 @@ import javax.persistence.MappedSuperclass;
* @author Juergen Hoeller
*/
@MappedSuperclass
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BaseEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public boolean isNew() {
return this.id == null;
}

View file

@ -15,6 +15,11 @@
*/
package org.springframework.samples.petclinic.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
@ -26,6 +31,10 @@ import javax.persistence.MappedSuperclass;
* @author Juergen Hoeller
*/
@MappedSuperclass
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class NamedEntity extends BaseEntity {
@Column(name = "name")

View file

@ -15,6 +15,11 @@
*/
package org.springframework.samples.petclinic.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.validation.constraints.NotEmpty;
@ -25,6 +30,10 @@ import javax.validation.constraints.NotEmpty;
* @author Ken Krebs
*/
@MappedSuperclass
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class Person extends BaseEntity {
@Column(name = "first_name")
@ -35,20 +44,4 @@ public class Person extends BaseEntity {
@NotEmpty
private String lastName;
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}

View file

@ -15,9 +15,6 @@
*/
package org.springframework.samples.petclinic.owner;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.dto.OwnerDto;
import org.springframework.samples.petclinic.visit.VisitRepository;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
@ -48,9 +45,6 @@ class OwnerController {
private VisitRepository visits;
@Autowired
private ModelMapper modelMapper;
public OwnerController(OwnerRepository clinicService, VisitRepository visits) {
this.owners = clinicService;
this.visits = visits;
@ -69,8 +63,7 @@ class OwnerController {
}
@PostMapping("/owners/new")
public String processCreationForm(@Valid OwnerDto ownerDto, BindingResult result) {
Owner owner = modelMapper.map(ownerDto, Owner.class);
public String processCreationForm(@Valid Owner owner, BindingResult result) {
if (result.hasErrors()) {
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
}
@ -87,8 +80,7 @@ class OwnerController {
}
@GetMapping("/owners")
public String processFindForm(OwnerDto ownerDto, BindingResult result, Map<String, Object> model) {
Owner owner = modelMapper.map(ownerDto, Owner.class);
public String processFindForm(Owner owner, BindingResult result, Map<String, Object> model) {
// allow parameterless GET request for /owners to return all records
if (owner.getLastName() == null) {
@ -122,9 +114,8 @@ class OwnerController {
}
@PostMapping("/owners/{ownerId}/edit")
public String processUpdateOwnerForm(@Valid OwnerDto ownerDto, BindingResult result,
public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result,
@PathVariable("ownerId") int ownerId) {
Owner owner = modelMapper.map(ownerDto, Owner.class);
if (result.hasErrors()) {
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;

View file

@ -31,6 +31,10 @@ import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator;
import org.springframework.format.annotation.DateTimeFormat;
@ -46,6 +50,10 @@ import org.springframework.samples.petclinic.visit.Visit;
*/
@Entity
@Table(name = "pets")
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class Pet extends NamedEntity {
@Column(name = "birth_date")
@ -63,30 +71,6 @@ public class Pet extends NamedEntity {
@Transient
private Set<Visit> visits = new LinkedHashSet<>();
public void setBirthDate(LocalDate birthDate) {
this.birthDate = birthDate;
}
public LocalDate getBirthDate() {
return this.birthDate;
}
public PetType getType() {
return this.type;
}
public void setType(PetType type) {
this.type = type;
}
public Owner getOwner() {
return this.owner;
}
protected void setOwner(Owner owner) {
this.owner = owner;
}
protected Set<Visit> getVisitsInternal() {
if (this.visits == null) {
this.visits = new HashSet<>();

View file

@ -15,10 +15,7 @@
*/
package org.springframework.samples.petclinic.owner;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.dto.OwnerDto;
import org.springframework.samples.petclinic.dto.PetDto;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
@ -45,8 +42,6 @@ class PetController {
private final OwnerRepository owners;
@Autowired
private ModelMapper modelMapper;
public PetController(PetRepository pets, OwnerRepository owners) {
this.pets = pets;
this.owners = owners;
@ -73,8 +68,7 @@ class PetController {
}
@GetMapping("/pets/new")
public String initCreationForm(OwnerDto ownerDto, ModelMap model) {
Owner owner = modelMapper.map(ownerDto, Owner.class);
public String initCreationForm(Owner owner, ModelMap model) {
Pet pet = new Pet();
owner.addPet(pet);
model.put("pet", pet);
@ -82,9 +76,7 @@ class PetController {
}
@PostMapping("/pets/new")
public String processCreationForm(OwnerDto ownerDto, @Valid PetDto petDto, BindingResult result, ModelMap model) {
Owner owner = modelMapper.map(ownerDto, Owner.class);
Pet pet = modelMapper.map(petDto, Pet.class);
public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult result, ModelMap model) {
if (StringUtils.hasLength(pet.getName()) && pet.isNew() && owner.getPet(pet.getName(), true) != null) {
result.rejectValue("name", "duplicate", "already exists");
@ -108,9 +100,7 @@ class PetController {
}
@PostMapping("/pets/{petId}/edit")
public String processUpdateForm(@Valid PetDto petDto, BindingResult result, OwnerDto ownerDto, ModelMap model) {
Pet pet = modelMapper.map(petDto, Pet.class);
Owner owner = modelMapper.map(ownerDto, Owner.class);
public String processUpdateForm(@Valid Pet pet, BindingResult result, Owner owner, ModelMap model) {
if (result.hasErrors()) {
pet.setOwner(owner);
model.put("pet", pet);

View file

@ -15,23 +15,16 @@
*/
package org.springframework.samples.petclinic.owner;
import java.util.Map;
import javax.validation.Valid;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.dto.VisitDto;
import org.springframework.samples.petclinic.visit.Visit;
import org.springframework.samples.petclinic.visit.VisitRepository;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.Map;
/**
* @author Juergen Hoeller
@ -48,8 +41,6 @@ class VisitController {
private final PetRepository pets;
@Autowired
private ModelMapper modelMapper;
public VisitController(VisitRepository visits, PetRepository pets) {
this.visits = visits;
this.pets = pets;
@ -85,9 +76,8 @@ class VisitController {
// Spring MVC calls method loadPetWithVisit(...) before processNewVisitForm is called
@PostMapping("/owners/{ownerId}/pets/{petId}/visits/new")
public String processNewVisitForm(@Valid VisitDto visitDto, BindingResult result) {
public String processNewVisitForm(@Valid Visit visit, BindingResult result) {
Visit visit = modelMapper.map(visitDto, Visit.class);
if (result.hasErrors()) {
return "pets/createOrUpdateVisitForm";
}

View file

@ -29,6 +29,10 @@ import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlElement;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator;
import org.springframework.samples.petclinic.model.Person;
@ -43,6 +47,10 @@ import org.springframework.samples.petclinic.model.Person;
*/
@Entity
@Table(name = "vets")
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class Vet extends Person {
@ManyToMany(fetch = FetchType.EAGER)
@ -57,10 +65,6 @@ public class Vet extends Person {
return this.specialties;
}
protected void setSpecialtiesInternal(Set<Specialty> specialties) {
this.specialties = specialties;
}
@XmlElement
public List<Specialty> getSpecialties() {
List<Specialty> sortedSpecs = new ArrayList<>(getSpecialtiesInternal());

View file

@ -15,15 +15,18 @@
*/
package org.springframework.samples.petclinic.visit;
import java.time.LocalDate;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.samples.petclinic.model.BaseEntity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.validation.constraints.NotEmpty;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.samples.petclinic.model.BaseEntity;
import java.time.LocalDate;
/**
* Simple JavaBean domain object representing a visit.
@ -33,6 +36,10 @@ import org.springframework.samples.petclinic.model.BaseEntity;
*/
@Entity
@Table(name = "visits")
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class Visit extends BaseEntity {
@Column(name = "visit_date")
@ -46,35 +53,4 @@ public class Visit extends BaseEntity {
@Column(name = "pet_id")
private Integer petId;
/**
* Creates a new instance of Visit for the current date
*/
public Visit() {
this.date = LocalDate.now();
}
public LocalDate getDate() {
return this.date;
}
public void setDate(LocalDate date) {
this.date = date;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getPetId() {
return this.petId;
}
public void setPetId(Integer petId) {
this.petId = petId;
}
}