diff --git a/.gitignore b/.gitignore
index a285bb3..3ef0a49 100644
--- a/.gitignore
+++ b/.gitignore
@@ -20,7 +20,7 @@
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
.idea/sonarlint-state.xml
-.idea/sonarlint
+/.idea/sonarlint/
# Gradle
.idea/**/gradle.xml
@@ -44,3 +44,5 @@ target/
build/
logs/
data/
+
+.idea/dataSources.xml
diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml
index fdc392f..b3e9cbd 100644
--- a/.idea/jarRepositories.xml
+++ b/.idea/jarRepositories.xml
@@ -16,5 +16,10 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
index 62269df..5d98256 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -1,6 +1,9 @@
+
+
+
diff --git a/build.gradle b/build.gradle
index fa0ea28..fe3e228 100644
--- a/build.gradle
+++ b/build.gradle
@@ -1,7 +1,6 @@
-
-
plugins {
- id 'java'
+ id 'war'
+ id "org.gretty" version "3.0.3"
}
group 'ru.dimon.spring'
@@ -9,10 +8,34 @@ version '1.0-SNAPSHOT'
repositories {
mavenCentral()
+ jcenter()
+
}
+def springVersion = '5.2.10.RELEASE'
+def jettyVersion = '9.2.26.v20180806'
+
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
+
+ compile group: 'org.springframework', name: 'spring-webmvc', version: springVersion
+ compile group: 'org.eclipse.jetty', name: 'jetty-server', version: jettyVersion
+ compile group: 'org.eclipse.jetty', name: 'jetty-webapp', version: jettyVersion
+ compile group: 'org.thymeleaf', name: 'thymeleaf-spring5', version: '3.0.10.RELEASE'
+ compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.6'
+ compile group: 'javax.annotation', name: 'javax.annotation-api', version: '1.3.2'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.13.3'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.13.3'
+
+ compile group: 'org.hibernate', name: 'hibernate-core', version: '5.4.4.Final'
+ compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.22'
+ // https://mvnrepository.com/artifact/com.h2database/h2
+ compile group: 'com.h2database', name: 'h2', version: '1.4.200'
+
}
+
+gretty {
+ httpPort = 8080
+ contextPath = '/'
+}
+
diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar
index 62d4c05..490fda8 100644
Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ
diff --git a/gradlew b/gradlew
index fbd7c51..2fe81a7 100644
--- a/gradlew
+++ b/gradlew
@@ -82,7 +82,6 @@ esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
-
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
@@ -130,7 +129,6 @@ fi
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
-
JAVACMD=`cygpath --unix "$JAVACMD"`
# We build the pattern for arguments to be converted via cygpath
diff --git a/gradlew.bat b/gradlew.bat
index 5093609..62bd9b9 100644
--- a/gradlew.bat
+++ b/gradlew.bat
@@ -84,7 +84,6 @@ set CMD_LINE_ARGS=%*
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
-
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
diff --git a/src/main/java/lessonthree/hibernate/AppConfig.java b/src/main/java/lessonthree/hibernate/AppConfig.java
new file mode 100644
index 0000000..4a5d322
--- /dev/null
+++ b/src/main/java/lessonthree/hibernate/AppConfig.java
@@ -0,0 +1,46 @@
+package lessonthree.hibernate;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.EnableWebMvc;
+import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+import org.thymeleaf.spring5.SpringTemplateEngine;
+import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
+import org.thymeleaf.spring5.view.ThymeleafViewResolver;
+
+@EnableWebMvc
+@Configuration
+@ComponentScan("lessonthree.hibernate")
+public class AppConfig implements WebMvcConfigurer {
+ @Override
+ public void addResourceHandlers(ResourceHandlerRegistry registry) {
+ registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
+ }
+
+ @Bean
+ public SpringResourceTemplateResolver templateResolver() {
+ SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
+ templateResolver.setPrefix("/WEB-INF/templates/");
+ templateResolver.setSuffix(".html");
+ return templateResolver;
+ }
+
+ @Bean
+ public SpringTemplateEngine templateEngine() {
+ SpringTemplateEngine templateEngine = new SpringTemplateEngine();
+ templateEngine.setTemplateResolver(templateResolver());
+ return templateEngine;
+ }
+
+ @Bean
+ public ThymeleafViewResolver thymeleafViewResolver() {
+ ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
+ thymeleafViewResolver.setTemplateEngine(templateEngine());
+ thymeleafViewResolver.setCharacterEncoding("UTF-8");
+ return thymeleafViewResolver;
+ }
+
+
+}
diff --git a/src/main/java/lessonthree/hibernate/Launcher.java b/src/main/java/lessonthree/hibernate/Launcher.java
new file mode 100644
index 0000000..e48ac04
--- /dev/null
+++ b/src/main/java/lessonthree/hibernate/Launcher.java
@@ -0,0 +1,24 @@
+package lessonthree.hibernate;
+
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.webapp.WebAppContext;
+
+import java.net.URL;
+import java.security.ProtectionDomain;
+
+public class Launcher {
+ public static void main(String[] args) throws Exception {
+ Server server = new Server(8189);
+
+ ProtectionDomain domain = Launcher.class.getProtectionDomain();
+ URL location = domain.getCodeSource().getLocation();
+
+ WebAppContext webAppContext = new WebAppContext();
+ webAppContext.setContextPath("/");
+ webAppContext.setWar(location.toExternalForm());
+
+ server.setHandler(webAppContext);
+ server.start();
+ server.join();
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/lessonthree/hibernate/WebInitializer.java b/src/main/java/lessonthree/hibernate/WebInitializer.java
new file mode 100644
index 0000000..de46647
--- /dev/null
+++ b/src/main/java/lessonthree/hibernate/WebInitializer.java
@@ -0,0 +1,36 @@
+package lessonthree.hibernate;
+
+import javax.servlet.Filter;
+import org.springframework.web.filter.CharacterEncodingFilter;
+import org.springframework.web.filter.HiddenHttpMethodFilter;
+import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
+
+public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
+ @Override
+ protected Class>[] getRootConfigClasses() {
+ return null;
+ }
+
+ @Override
+ protected Class>[] getServletConfigClasses() {
+ return new Class[]{AppConfig.class};
+ }
+
+ protected String[] getServletMappings() {
+ return new String[]{"/"};
+ }
+
+ @Override
+ protected Filter[] getServletFilters() {
+ // Создание фильтра кодировки, который позволит работать с русскими
+ // символами
+ CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
+ characterEncodingFilter.setEncoding("UTF-8");
+ characterEncodingFilter.setForceEncoding(true);
+ // Создание фильтра, который добавляет поддержку HTTP-методов (например
+ // таких, как PUT), необходимых для REST API
+ HiddenHttpMethodFilter httpMethodFilter = new HiddenHttpMethodFilter();
+ return new Filter[]{characterEncodingFilter, httpMethodFilter};
+ }
+
+}
diff --git a/src/main/java/lessonthree/hibernate/controllers/CustomersController.java b/src/main/java/lessonthree/hibernate/controllers/CustomersController.java
new file mode 100644
index 0000000..7c50c2e
--- /dev/null
+++ b/src/main/java/lessonthree/hibernate/controllers/CustomersController.java
@@ -0,0 +1,57 @@
+package lessonthree.hibernate.controllers;
+
+import lessonthree.hibernate.model.Customer;
+import lessonthree.hibernate.services.CustomerService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.ModelAttribute;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+
+import java.util.List;
+
+@Controller
+@RequestMapping("customers")
+public class CustomersController {
+ @Autowired
+ CustomerService customerService;
+
+ @RequestMapping(value = "", method = RequestMethod.GET)
+ public String showAllCustomers(Model model) {
+ List customerList = customerService.getAllCustomers();
+ model.addAttribute("customers", customerList);
+ return "customers/all_customers";
+ }
+
+ @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
+ public String showInfoCustomerForm(@PathVariable long id, Model model) {
+ model.addAttribute("customer", customerService.get(id));
+ return "customers/info_customer";
+ }
+
+ @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
+ public String showEditCustomerForm(@PathVariable long id, Model model) {
+ model.addAttribute("customer", customerService.get(id));
+ return "customers/edit_customer_form";
+ }
+
+ @RequestMapping(value = "/edit", method = RequestMethod.POST)
+ public String editCustomer(@ModelAttribute Customer customer) {
+ customerService.update(customer);
+ return "redirect:/customers/";
+ }
+
+ @RequestMapping(value = "/add", method = RequestMethod.GET)
+ public String showAddCustomerForm(Model model) {
+ model.addAttribute("customer", new Customer());
+ return "customers/add_customer_form";
+ }
+
+ @RequestMapping(value = "/add", method = RequestMethod.POST)
+ public String addCustomer(@ModelAttribute Customer customer) {
+ customerService.add(customer);
+ return "redirect:/customers/";
+ }
+}
diff --git a/src/main/java/lessonthree/hibernate/controllers/MainController.java b/src/main/java/lessonthree/hibernate/controllers/MainController.java
new file mode 100644
index 0000000..a3a40c9
--- /dev/null
+++ b/src/main/java/lessonthree/hibernate/controllers/MainController.java
@@ -0,0 +1,14 @@
+package lessonthree.hibernate.controllers;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.*;
+
+@Controller
+public class MainController {
+
+ @RequestMapping(value = "/", method = RequestMethod.GET)
+ public String showHomePage() {
+ return "index";
+ }
+
+}
diff --git a/src/main/java/lessonthree/hibernate/controllers/OrdersController.java b/src/main/java/lessonthree/hibernate/controllers/OrdersController.java
new file mode 100644
index 0000000..00f64da
--- /dev/null
+++ b/src/main/java/lessonthree/hibernate/controllers/OrdersController.java
@@ -0,0 +1,32 @@
+package lessonthree.hibernate.controllers;
+
+import lessonthree.hibernate.model.Order;
+import lessonthree.hibernate.services.OrdersService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+
+import java.util.List;
+
+@Controller
+@RequestMapping("orders")
+public class OrdersController {
+ @Autowired
+ OrdersService ordersService;
+
+ @RequestMapping(value = "", method = RequestMethod.GET)
+ public String showAllOrder(Model model) {
+ List orderList = ordersService.getAllOrders();
+ model.addAttribute("orders", orderList);
+ return "orders/all_orders";
+ }
+
+ @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
+ public String showInfoOrderForm(@PathVariable long id, Model model) {
+ model.addAttribute("order", ordersService.get(id));
+ return "orders/info_order";
+ }
+}
diff --git a/src/main/java/lessonthree/hibernate/controllers/ProductsController.java b/src/main/java/lessonthree/hibernate/controllers/ProductsController.java
new file mode 100644
index 0000000..df34ad6
--- /dev/null
+++ b/src/main/java/lessonthree/hibernate/controllers/ProductsController.java
@@ -0,0 +1,62 @@
+package lessonthree.hibernate.controllers;
+
+import lessonthree.hibernate.model.Product;
+import lessonthree.hibernate.services.ProductsService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.ModelAttribute;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+
+import java.util.List;
+
+@Controller
+@RequestMapping("products")
+public class ProductsController {
+
+ @Autowired
+ ProductsService productsService;
+
+ @RequestMapping(value = "", method = RequestMethod.GET)
+ public String showAllProducts(Model model) {
+ List productList = productsService.getAllProducts();
+ model.addAttribute("products", productList);
+ return "products/all_products";
+ }
+
+ @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
+ public String showEditProductForm(@PathVariable long id, Model model) {
+ model.addAttribute("product", productsService.get(id));
+ return "products/edit_product_form";
+ }
+
+ @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
+ public String showInfoProductForm(@PathVariable long id, Model model) {
+ model.addAttribute("product", productsService.get(id));
+ return "products/info_product";
+ }
+
+ @RequestMapping(value = "/edit", method = RequestMethod.POST)
+ public String editProduct(@ModelAttribute Product product) {
+ productsService.update(product);
+ return "redirect:/products/";
+ }
+
+ @RequestMapping(value = "/add", method = RequestMethod.GET)
+ public String showAddProductForm(Model model) {
+ model.addAttribute("product", new Product());
+ return "products/add_product_form";
+ }
+
+ @RequestMapping(value = "/add", method = RequestMethod.POST)
+ public String addProduct(@ModelAttribute Product product) {
+ productsService.add(product);
+ return "redirect:/products/";
+ }
+
+
+
+
+}
diff --git a/src/main/java/lessonthree/hibernate/model/Customer.java b/src/main/java/lessonthree/hibernate/model/Customer.java
new file mode 100644
index 0000000..66e90c1
--- /dev/null
+++ b/src/main/java/lessonthree/hibernate/model/Customer.java
@@ -0,0 +1,43 @@
+package lessonthree.hibernate.model;
+
+import javax.persistence.*;
+import java.util.ArrayList;
+import java.util.List;
+
+@Entity
+@Table(name = "customers")
+public class Customer {
+ @Id
+ @Column(name = "id")
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
+ private Long id;
+
+ @Column(name = "first_name")
+ private String first_name;
+
+ @OneToMany(mappedBy = "customer", cascade = {CascadeType.REMOVE})
+ List orders = new ArrayList<>();
+
+ public Long getId() {
+ return id;
+ }
+
+ public String getFirst_name() {
+ return first_name;
+ }
+
+ public void setFirst_name(String first_name) {
+ this.first_name = first_name;
+ }
+
+ public List getOrders() {
+ return orders;
+ }
+
+ public void setOrders(List orders) {
+ this.orders = orders;
+ }
+
+
+
+}
diff --git a/src/main/java/lessonthree/hibernate/model/Order.java b/src/main/java/lessonthree/hibernate/model/Order.java
new file mode 100644
index 0000000..e875751
--- /dev/null
+++ b/src/main/java/lessonthree/hibernate/model/Order.java
@@ -0,0 +1,53 @@
+package lessonthree.hibernate.model;
+
+import javax.persistence.*;
+import java.util.Date;
+import java.util.List;
+
+
+@Entity
+@Table(name = "orders")
+public class Order {
+ @Id
+ @Column(name = "id")
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
+ private Long id;
+
+ @Column(name = "date", nullable = false)
+ private Date date;
+
+ @ManyToOne
+ @JoinColumn(name = "customer_id", nullable = false)
+ private Customer customer;
+
+ @OneToMany(mappedBy="order", fetch = FetchType.LAZY)
+ private List orderProducts;
+
+ public Long getId() {
+ return id;
+ }
+
+ public Date getDate() {
+ return date;
+ }
+
+ public void setDate(Date date) {
+ this.date = date;
+ }
+
+ public Customer getCustomer() {
+ return customer;
+ }
+
+ public void setCustomer(Customer customer) {
+ this.customer = customer;
+ }
+
+ public List getOrderProducts() {
+ return orderProducts;
+ }
+
+ public void setOrderProducts(List orderProducts) {
+ this.orderProducts = orderProducts;
+ }
+}
diff --git a/src/main/java/lessonthree/hibernate/model/OrderProduct.java b/src/main/java/lessonthree/hibernate/model/OrderProduct.java
new file mode 100644
index 0000000..0babf4f
--- /dev/null
+++ b/src/main/java/lessonthree/hibernate/model/OrderProduct.java
@@ -0,0 +1,67 @@
+package lessonthree.hibernate.model;
+
+import javax.persistence.*;
+
+@Entity
+@Table(name = "order_products")
+public class OrderProduct {
+
+ @Id
+ @Column(name = "id")
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
+ private Long id;
+
+ @ManyToOne
+ @JoinColumn(name = "order_id", nullable = false)
+ private Order order;
+
+ @OneToOne
+ @JoinColumn(name = "product_id")
+ private Product product;
+
+ @Column(name = "count")
+ private int count;
+
+ @Column(name = "cost")
+ private int cost;
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public Order getOrder() {
+ return order;
+ }
+
+ public void setOrder(Order order) {
+ this.order = order;
+ }
+
+ public Product getProduct() {
+ return product;
+ }
+
+ public void setProduct(Product product) {
+ this.product = product;
+ }
+
+ public int getCount() {
+ return count;
+ }
+
+ public void setCount(int count) {
+ this.count = count;
+ }
+
+ public int getCost() {
+ return cost;
+ }
+
+ public void setCost(int cost) {
+ this.cost = cost;
+ }
+}
diff --git a/src/main/java/lessonthree/hibernate/model/Product.java b/src/main/java/lessonthree/hibernate/model/Product.java
new file mode 100644
index 0000000..b74fa85
--- /dev/null
+++ b/src/main/java/lessonthree/hibernate/model/Product.java
@@ -0,0 +1,76 @@
+package lessonthree.hibernate.model;
+
+import javax.persistence.*;
+import java.util.List;
+import java.util.Objects;
+
+@Entity
+@Table(name = "products")
+public class Product {
+ @Id
+ @Column(name = "id")
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
+ private Long id;
+
+ @Column(name = "title")
+ private String title;
+
+ @Column(name = "cost")
+ private float cost;
+
+ @ManyToMany
+ @JoinTable(
+ name = "order_products",
+ joinColumns = @JoinColumn(name = "product_id"),
+ inverseJoinColumns = @JoinColumn(name = "order_id")
+ )
+ private List orders;
+
+ public Product() {
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ public float getCost() {
+ return cost;
+ }
+
+ public void setCost(float cost) {
+ this.cost = cost;
+ }
+
+ public Product(Long id, String title, float cost) {
+ this.id = id;
+ this.title = title;
+ this.cost = cost;
+ }
+
+ public List getOrders() {
+ return orders;
+ }
+ @Override
+ public boolean equals(Object o) { if ( this == o ) return true;
+ if ( o == null || getClass() != o.getClass() ) return false;
+ Product product = (Product) o;
+ return Objects.equals(id, product.id);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(id);
+ }
+}
diff --git a/src/main/java/lessonthree/hibernate/repositories/CustomerRepository.java b/src/main/java/lessonthree/hibernate/repositories/CustomerRepository.java
new file mode 100644
index 0000000..1122b48
--- /dev/null
+++ b/src/main/java/lessonthree/hibernate/repositories/CustomerRepository.java
@@ -0,0 +1,32 @@
+package lessonthree.hibernate.repositories;
+
+import lessonthree.hibernate.model.Customer;
+import lessonthree.hibernate.services.DBService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Repository;
+
+import java.util.Collections;
+import java.util.List;
+
+@Repository
+public class CustomerRepository {
+ @Autowired
+ private DBService dbService;
+
+ public List getAllCustomers() {
+ List customerList = dbService.executeQuery("SELECT c FROM Customer c", Customer.class);
+ return Collections.unmodifiableList(customerList);
+ }
+
+ public Customer get(Long id) {
+ return dbService.get(Customer.class, id);
+ }
+
+ public boolean add(Customer customer) {
+ return dbService.saveOrUpdate(customer);
+ }
+
+ public boolean update(Customer customer) {
+ return dbService.saveOrUpdate(customer);
+ }
+}
diff --git a/src/main/java/lessonthree/hibernate/repositories/OrderRepository.java b/src/main/java/lessonthree/hibernate/repositories/OrderRepository.java
new file mode 100644
index 0000000..60aac16
--- /dev/null
+++ b/src/main/java/lessonthree/hibernate/repositories/OrderRepository.java
@@ -0,0 +1,24 @@
+package lessonthree.hibernate.repositories;
+
+import lessonthree.hibernate.model.Order;
+import lessonthree.hibernate.services.DBService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Repository;
+
+import java.util.Collections;
+import java.util.List;
+
+@Repository
+public class OrderRepository {
+ @Autowired
+ private DBService dbService;
+
+ public Order get(Long id) {
+ return dbService.get(Order.class, id);
+ }
+
+ public List getAllOrders() {
+ List orderList = dbService.executeQuery("SELECT o FROM Order o ORDER BY o.date", Order.class);
+ return Collections.unmodifiableList(orderList);
+ }
+}
diff --git a/src/main/java/lessonthree/hibernate/repositories/ProductRepository.java b/src/main/java/lessonthree/hibernate/repositories/ProductRepository.java
new file mode 100644
index 0000000..33ef17a
--- /dev/null
+++ b/src/main/java/lessonthree/hibernate/repositories/ProductRepository.java
@@ -0,0 +1,31 @@
+package lessonthree.hibernate.repositories;
+
+import lessonthree.hibernate.model.Product;
+import lessonthree.hibernate.services.DBService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Repository;
+import java.util.Collections;
+import java.util.List;
+
+@Repository
+public class ProductRepository {
+ @Autowired
+ private DBService dbService;
+
+ public List getAllProducts() {
+ List productList = dbService.executeQuery("SELECT p FROM Product p", Product.class);
+ return Collections.unmodifiableList(productList);
+ }
+
+ public Product get(Long id) {
+ return dbService.get(Product.class, id);
+ }
+
+ public boolean add(Product product) {
+ return dbService.saveOrUpdate(product);
+ }
+
+ public boolean update(Product product) {
+ return dbService.saveOrUpdate(product);
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/lessonthree/hibernate/services/CustomerService.java b/src/main/java/lessonthree/hibernate/services/CustomerService.java
new file mode 100644
index 0000000..b47c5e9
--- /dev/null
+++ b/src/main/java/lessonthree/hibernate/services/CustomerService.java
@@ -0,0 +1,32 @@
+package lessonthree.hibernate.services;
+
+import lessonthree.hibernate.model.Customer;
+import lessonthree.hibernate.repositories.CustomerRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import javax.transaction.Transactional;
+import java.util.List;
+
+@Service
+public class CustomerService {
+
+ @Autowired
+ private CustomerRepository customerRepository;
+
+ public List getAllCustomers() {
+ return customerRepository.getAllCustomers();
+ }
+
+ public Customer get(long id) {
+ return customerRepository.get(id);
+ }
+
+ public boolean add(Customer customer) {
+ return customerRepository.add(customer);
+ }
+
+ public boolean update(Customer customer) {
+ return customerRepository.update(customer);
+ }
+}
diff --git a/src/main/java/lessonthree/hibernate/services/DBService.java b/src/main/java/lessonthree/hibernate/services/DBService.java
new file mode 100644
index 0000000..c3fb511
--- /dev/null
+++ b/src/main/java/lessonthree/hibernate/services/DBService.java
@@ -0,0 +1,66 @@
+package lessonthree.hibernate.services;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.hibernate.HibernateException;
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.query.Query;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.PostConstruct;
+import java.util.List;
+
+@Service
+public class DBService {
+ private static final Logger logger = LogManager.getLogger(DBService.class);
+
+ private SessionFactory sessionFactory;
+ private Session session;
+
+ @PostConstruct
+ public void init() {
+ sessionFactory = new Configuration()
+ .configure("hibernate.h2.cfg.xml")
+ .buildSessionFactory();
+ session = sessionFactory.openSession();
+ }
+
+ public SessionFactory getSessionFactory() {
+ return sessionFactory;
+ }
+
+ public boolean saveOrUpdate(Object o) {
+ try {
+
+// Session session = sessionFactory.openSession();
+ session.beginTransaction();
+ session.saveOrUpdate(o);
+ session.getTransaction().commit();
+// session.close();
+ return true;
+ }catch (HibernateException e) {
+ logger.error(e.getMessage(), e);
+ return false;
+ }
+ }
+
+ public T get(Class tClass, Long id) {
+// Session session = sessionFactory.getCurrentSession();
+ session.beginTransaction();
+ T result = session.get(tClass, id);
+ session.getTransaction().commit();
+ return result;
+ }
+
+ public List executeQuery(String query, Class tClass) {
+// Session session = sessionFactory.getCurrentSession();
+ session.beginTransaction();
+ Query executeQuery = session.createQuery(query, tClass);
+ List result = executeQuery.getResultList();
+ session.getTransaction().commit();
+ return result;
+ }
+
+}
diff --git a/src/main/java/lessonthree/hibernate/services/OrdersService.java b/src/main/java/lessonthree/hibernate/services/OrdersService.java
new file mode 100644
index 0000000..412fa50
--- /dev/null
+++ b/src/main/java/lessonthree/hibernate/services/OrdersService.java
@@ -0,0 +1,21 @@
+package lessonthree.hibernate.services;
+
+import lessonthree.hibernate.model.Order;
+import lessonthree.hibernate.repositories.OrderRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import java.util.List;
+
+@Service
+public class OrdersService {
+ @Autowired
+ private OrderRepository orderRepository;
+
+ public Order get(long id) {
+ return orderRepository.get(id);
+ }
+
+ public List getAllOrders() {
+ return orderRepository.getAllOrders();
+ }
+}
diff --git a/src/main/java/lessonthree/hibernate/services/ProductsService.java b/src/main/java/lessonthree/hibernate/services/ProductsService.java
new file mode 100644
index 0000000..6e10e16
--- /dev/null
+++ b/src/main/java/lessonthree/hibernate/services/ProductsService.java
@@ -0,0 +1,31 @@
+package lessonthree.hibernate.services;
+
+import lessonthree.hibernate.model.Product;
+import lessonthree.hibernate.repositories.ProductRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import java.util.List;
+
+@Service
+public class ProductsService {
+
+ @Autowired
+ private ProductRepository productRepository;
+
+ public List getAllProducts() {
+ return productRepository.getAllProducts();
+ }
+
+ public Product get(long id) {
+ return productRepository.get(id);
+ }
+
+ public boolean add(Product product) {
+ return productRepository.add(product);
+ }
+
+ public boolean update(Product product) {
+ return productRepository.update(product);
+ }
+
+}
diff --git a/src/main/resources/hibernate.h2.cfg.xml b/src/main/resources/hibernate.h2.cfg.xml
new file mode 100644
index 0000000..c86b71b
--- /dev/null
+++ b/src/main/resources/hibernate.h2.cfg.xml
@@ -0,0 +1,23 @@
+
+
+
+
+ org.h2.Driver
+ jdbc:h2:./data/h2db
+ root
+ 12345
+ 8
+ org.hibernate.dialect.H2Dialect
+ true
+ thread
+ update
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/main/resources/hibernate.mysql.cfg.xml b/src/main/resources/hibernate.mysql.cfg.xml
new file mode 100644
index 0000000..aae5ab1
--- /dev/null
+++ b/src/main/resources/hibernate.mysql.cfg.xml
@@ -0,0 +1,23 @@
+
+
+
+
+ com.mysql.cj.jdbc.Driver
+ jdbc:mysql://localserver:3306/shop?serverTimezone=UTC
+ root
+ 12345
+ 8
+ org.hibernate.dialect.MySQL8Dialect
+ true
+ thread
+ update
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml
new file mode 100644
index 0000000..78aa60c
--- /dev/null
+++ b/src/main/resources/log4j2.xml
@@ -0,0 +1,25 @@
+
+
+
+ logs
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/main/webapp/WEB-INF/templates/customers/add_customer_form.html b/src/main/webapp/WEB-INF/templates/customers/add_customer_form.html
new file mode 100644
index 0000000..40c54ed
--- /dev/null
+++ b/src/main/webapp/WEB-INF/templates/customers/add_customer_form.html
@@ -0,0 +1,23 @@
+
+
+
+
+
+
+ Spring MVC GeekApp
+
+
+
+ Add new customer
+
+
+
+
+ to main page
+
+
\ No newline at end of file
diff --git a/src/main/webapp/WEB-INF/templates/customers/all_customers.html b/src/main/webapp/WEB-INF/templates/customers/all_customers.html
new file mode 100644
index 0000000..7e0c71e
--- /dev/null
+++ b/src/main/webapp/WEB-INF/templates/customers/all_customers.html
@@ -0,0 +1,35 @@
+
+
+
+
+
+
+ Spring MVC GeekApp
+
+
+
+ customers list:
+
+
+
+ Id
+ Name
+ ...
+ ...
+
+
+
+
+
+
+ edit
+ info
+
+
+
+
+ add new customer
+
+ to main page
+
+
\ No newline at end of file
diff --git a/src/main/webapp/WEB-INF/templates/customers/edit_customer_form.html b/src/main/webapp/WEB-INF/templates/customers/edit_customer_form.html
new file mode 100644
index 0000000..a6a5ae8
--- /dev/null
+++ b/src/main/webapp/WEB-INF/templates/customers/edit_customer_form.html
@@ -0,0 +1,22 @@
+
+
+
+
+
+
+ Spring MVC GeekApp
+
+
+
+ Edit customer
+
+
+ to main page
+
+
\ No newline at end of file
diff --git a/src/main/webapp/WEB-INF/templates/customers/info_customer.html b/src/main/webapp/WEB-INF/templates/customers/info_customer.html
new file mode 100644
index 0000000..0662699
--- /dev/null
+++ b/src/main/webapp/WEB-INF/templates/customers/info_customer.html
@@ -0,0 +1,37 @@
+
+
+
+
+
+
+ Spring MVC GeekApp
+
+
+
+Info customer
+
+
Id:
+
Name:
+
+
Orders:
+
+
+
+ Id
+ date
+ ...
+
+
+
+
+
+
+ info
+
+
+
+
+
+to main page
+
+
\ No newline at end of file
diff --git a/src/main/webapp/WEB-INF/templates/index.html b/src/main/webapp/WEB-INF/templates/index.html
new file mode 100644
index 0000000..fd0bb3c
--- /dev/null
+++ b/src/main/webapp/WEB-INF/templates/index.html
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+ Spring MVC GeekApp
+
+
+
+ WEB: Spring MVC GeekApp
+ Site map
+ Products list
+
+ Customers list
+
+ Orders list
+
+
+
\ No newline at end of file
diff --git a/src/main/webapp/WEB-INF/templates/orders/all_orders.html b/src/main/webapp/WEB-INF/templates/orders/all_orders.html
new file mode 100644
index 0000000..203b264
--- /dev/null
+++ b/src/main/webapp/WEB-INF/templates/orders/all_orders.html
@@ -0,0 +1,33 @@
+
+
+
+
+
+
+ Spring MVC GeekApp
+
+
+
+ Orders list:
+
+
+
+ Id
+ Customer
+ Date
+ ...
+
+
+
+
+
+
+
+ info
+
+
+
+
+ to main page
+
+
\ No newline at end of file
diff --git a/src/main/webapp/WEB-INF/templates/orders/info_order.html b/src/main/webapp/WEB-INF/templates/orders/info_order.html
new file mode 100644
index 0000000..0b6d535
--- /dev/null
+++ b/src/main/webapp/WEB-INF/templates/orders/info_order.html
@@ -0,0 +1,38 @@
+
+
+
+
+
+
+ Spring MVC GeekApp
+
+
+
+Info order
+
+
Id:
+
Date:
+
Customer:
+
+
Products:
+
+
+
+ product
+ count
+ cost
+
+
+
+
+
+
+
+
+
+
+
+
+to main page
+
+
\ No newline at end of file
diff --git a/src/main/webapp/WEB-INF/templates/products/add_product_form.html b/src/main/webapp/WEB-INF/templates/products/add_product_form.html
new file mode 100644
index 0000000..1e23789
--- /dev/null
+++ b/src/main/webapp/WEB-INF/templates/products/add_product_form.html
@@ -0,0 +1,27 @@
+
+
+
+
+
+
+ Spring MVC GeekApp
+
+
+
+ Add new product
+
+
+
+
+ to main page
+
+
\ No newline at end of file
diff --git a/src/main/webapp/WEB-INF/templates/products/all_products.html b/src/main/webapp/WEB-INF/templates/products/all_products.html
new file mode 100644
index 0000000..a4abee1
--- /dev/null
+++ b/src/main/webapp/WEB-INF/templates/products/all_products.html
@@ -0,0 +1,36 @@
+
+
+
+
+
+
+ Spring MVC GeekApp
+
+
+
+ Products list:
+
+
+
+ Id
+ Title
+ Cost
+ ...
+ ...
+
+
+
+
+
+
+
+ edit
+ info
+
+
+
+ add new product
+
+ to main page
+
+
\ No newline at end of file
diff --git a/src/main/webapp/WEB-INF/templates/products/edit_product_form.html b/src/main/webapp/WEB-INF/templates/products/edit_product_form.html
new file mode 100644
index 0000000..18d2913
--- /dev/null
+++ b/src/main/webapp/WEB-INF/templates/products/edit_product_form.html
@@ -0,0 +1,24 @@
+
+
+
+
+
+
+ Spring MVC GeekApp
+
+
+
+ Edit product
+
+
+ to main page
+
+
\ No newline at end of file
diff --git a/src/main/webapp/WEB-INF/templates/products/info_product.html b/src/main/webapp/WEB-INF/templates/products/info_product.html
new file mode 100644
index 0000000..2c8ac0d
--- /dev/null
+++ b/src/main/webapp/WEB-INF/templates/products/info_product.html
@@ -0,0 +1,40 @@
+
+
+
+
+
+
+ Spring MVC GeekApp
+
+
+
+Info product
+
+
Id:
+
Title:
+
Cost:
+
+
+
Orders:
+
+
+
+ Id
+ date
+ ...
+
+
+
+
+
+
+ info
+
+
+
+
+
+
+to main page
+
+
\ No newline at end of file