2017-05-21 7 views
0

Ich baue eine kleine Anwendung in Spring und Spring Data JPA. Ich brauche Servol Schicht. Ich weiß nicht, ob es mir gut geht. HierService Layer im Frühjahr Daten

ein Beispiel:
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 UMSETZUNG

@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); 

    } 

} 

Es ist eine gute? Es sei denn, wie die Kommunikation mit der Datenbank im Frühjahr erstellen?

+3

Ich wähle diese Frage als Wegthema zu schließen, weil es gehört auf Code Review, nicht auf Stackoverflow –

+0

Unabhängig von der Frage ist off Thema: Es ist nicht wirklich klar, was Ihre Frage ist. Was sind Ihre Ziele, die Sie erreichen möchten, indem Sie eine mehrschichtige Architektur erstellen? –

Antwort

1

Im Quellen-Ordner erstellen Sie eine Ressourcendatei src/main/resources/application.properties und erwähnen Sie die folgenden Eigenschaften

spring.jpa.hibernate.ddl-auto=create 
spring.datasource.url=jdbc:mysql://localhost:3306/db_example 
spring.datasource.username=springuser 
spring.datasource.password=ThePassword