Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Product adding jpa version #15

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Thumbs.db
.project
.classpath
.idea
.mvn
*.iml
atlassian-ide-plugin.xml
target
28 changes: 28 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,34 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

<!--WebJars-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.5</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>2.1.4</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/guru/springframework/SpringmvcApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class SpringmvcApplication {

public static void main(String[] args) {
SpringApplication.run(SpringmvcApplication.class, args);
ApplicationContext ctx = SpringApplication.run(SpringmvcApplication.class, args);

// for (String name : ctx.getBeanDefinitionNames()){
// System.out.println(name);
// }
// System.out.println("******* Bean Count *******");
// System.out.println(ctx.getBeanDefinitionCount());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package guru.springframework.controllers;

import guru.springframework.domain.Customer;
import guru.springframework.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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
* Created by jt on 11/15/15.
*/
@RequestMapping("/customer")
@Controller
public class CustomerController {

private CustomerService customerService;

@Autowired
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}

@RequestMapping({"/list", "/"})
public String listCustomers(Model model){
model.addAttribute("customers", customerService.listAll());
return "customer/list";
}

@RequestMapping("/show/{id}")
public String showCustomer(@PathVariable Integer id, Model model){
model.addAttribute("customer", customerService.getById(id));
return "customer/show";
}

@RequestMapping("/edit/{id}")
public String edit(@PathVariable Integer id, Model model){
model.addAttribute("customer", customerService.getById(id));
return "customer/customerform";
}

@RequestMapping("/new")
public String newCustomer(Model model){
model.addAttribute("customer", new Customer());
return "customer/customerform";
}

@RequestMapping(method = RequestMethod.POST)
public String saveOrUpdate(Customer customer){
Customer newCustomer = customerService.saveOrUpdate(customer);
return "redirect:customer/show/" + newCustomer.getId();
}

@RequestMapping("/delete/{id}")
public String delete(@PathVariable Integer id){
customerService.delete(id);
return "redirect:/customer/list";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package guru.springframework.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
* Created by jt on 11/6/15.
*/
@Controller
public class IndexController {

@RequestMapping({"/", ""})
public String index(){
return "index";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package guru.springframework.controllers;

import guru.springframework.domain.Product;
import guru.springframework.services.ProductService;
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;

/**
* Created by jt on 11/6/15.
*/
@Controller
public class ProductController {

private ProductService productService;

@Autowired
public void setProductService(ProductService productService) {
this.productService = productService;
}

@RequestMapping("/product/list")
public String listProducts(Model model){
model.addAttribute("products", productService.listAll());
return "product/list";
}

@RequestMapping("/product/show/{id}")
public String getProduct(@PathVariable Integer id, Model model){
model.addAttribute("product", productService.getById(id));
return "product/show";
}

@RequestMapping("product/edit/{id}")
public String edit(@PathVariable Integer id, Model model){
model.addAttribute("product", productService.getById(id));
return "product/productform";
}

@RequestMapping("/product/new")
public String newProduct(Model model){
model.addAttribute("product", new Product());
return "product/productform";
}

@RequestMapping(value = "/product", method = RequestMethod.POST)
public String saveOrUpdateProduct(Product product){
Product savedProduct = productService.saveOrUpdate(product);
return "redirect:/product/show/" + savedProduct.getId();
}

@RequestMapping("/product/delete/{id}")
public String delete(@PathVariable Integer id){
productService.delete(id);
return "redirect:/product/list";
}
}
100 changes: 100 additions & 0 deletions src/main/java/guru/springframework/domain/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package guru.springframework.domain;

/**
* Created by jt on 11/14/15.
*/
public class Customer implements DomainObject {

private Integer id;
private String firstName;
private String lastName;
private String email;
private String phoneNumber;
private String addressLine1;
private String addressLine2;
private String city;
private String state;
private String zipCode;

@Override
public Integer getId() {
return id;
}

@Override
public void setId(Integer 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 getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPhoneNumber() {
return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}

public String getAddressLine1() {
return addressLine1;
}

public void setAddressLine1(String addressLine1) {
this.addressLine1 = addressLine1;
}

public String getAddressLine2() {
return addressLine2;
}

public void setAddressLine2(String addressLine2) {
this.addressLine2 = addressLine2;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getZipCode() {
return zipCode;
}

public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
}
11 changes: 11 additions & 0 deletions src/main/java/guru/springframework/domain/DomainObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package guru.springframework.domain;

/**
* Created by jt on 11/14/15.
*/
public interface DomainObject {

Integer getId();

void setId(Integer id);
}
62 changes: 62 additions & 0 deletions src/main/java/guru/springframework/domain/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package guru.springframework.domain;

import javax.persistence.*;
import java.math.BigDecimal;

/**
* Created by jt on 11/6/15.
*/
@Entity
public class Product implements DomainObject{

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@Version
private Integer version;

private String description;
private BigDecimal price;
private String imageUrl;

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;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public BigDecimal getPrice() {
return price;
}

public void setPrice(BigDecimal price) {
this.price = price;
}

public String getImageUrl() {
return imageUrl;
}

public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
Loading