\$\begingroup\$
\$\endgroup\$
2
I'm building a small application in Spring and Spring Data JPA. I need serval layer. I don't know if I'm doing well.
Here is an example:
POJO
@Entity
public class Product {
@Id
@GeneratedValue
private long Id;
@NotBlank
private String name;
@NotNull
private int price;
public long getId() {
return Id;
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
public void setId(long id) {
Id = id;
}
public void setName(String name) {
this.name = name;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "Product{" +
"Id=" + Id +
", name='" + name + '\'' +
", price=" + price +
'}';
}
}
REPOSITORY
public interface ProductRepository extends CrudRepository<Product, Long> {
Product findByName(String name);
List<Product> getAllProducts();
Product getProductById(String productId);
void addProduct(Product product);
}
SERVICE
public interface ProductService {
List<Product> getAllProducts();
Product getProductById(String productId);
void addProduct(Product product);
}
REPOSITORY IMPLEMENTATION
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductRepository productRepository;
@Override
public List<Product> getAllProducts() {
return productRepository.getAllProducts();
}
@Override
public Product getProductById(String productId) {
return productRepository.getProductById(productId);
}
@Override
public void addProduct(Product product) {
productRepository.addProduct(product);
}
}
It is a good? Unless How create communication with database in Spring?
asked May 21, 2017 at 10:21
-
\$\begingroup\$ What is the benefit of having Service Interface here? \$\endgroup\$Pawan– Pawan2019年02月25日 07:06:41 +00:00Commented Feb 25, 2019 at 7:06
-
\$\begingroup\$ @Pawan The use of service interface here is that if someone has to deliver ProductServices in a different way i.e by using a different database or using different business logic then by implementing the interface gives a reason to return the same output without affecting the presentation layer. \$\endgroup\$New one– New one2019年08月30日 05:11:23 +00:00Commented Aug 30, 2019 at 5:11
1 Answer 1
\$\begingroup\$
\$\endgroup\$
1
Well if I'd review this I'd add the following:
- The Id field in Product could be renamed id to follow java convention
- Generally it's a good practice to have equals & hashcode methods overridden.
As for the database connection it depends on which Spring & database you are using, does it happen to be Spring Boot?
answered May 21, 2017 at 11:08
-
\$\begingroup\$ @MaciejDymarczyk No problem Then it's very simple, assuming you use a mysql database you need: 1. Add dependency
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
` </dependency>` 2. Add connection properties in application.yml filespring.datasource.url= jdbc:mysql://localhost:3306/YOUR_DB_NAME
spring.datasource.username=YOUR_USERNAME
spring.datasource.password=YOUR_PASSWORD
For more check this \$\endgroup\$Mazen Melouk– Mazen Melouk2017年05月21日 12:24:43 +00:00Commented May 21, 2017 at 12:24
lang-java