Spring data JPA Filter Data

This commit is contained in:
Chantha
2020-05-15 17:20:27 +07:00
commit addf2882af
29 changed files with 1479 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
package com.chantha.jdbc
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
@SpringBootApplication
class JdbcApplication
fun main(args: Array<String>) {
runApplication<JdbcApplication>(*args)
}

View File

@@ -0,0 +1,18 @@
package com.chantha.jdbc.controller
import com.chantha.jdbc.jpa.model.Customer
import com.chantha.jdbc.jpa.service.customer.CustomerService
import org.apache.catalina.User
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
@Suppress("UNCHECKED_CAST")
@RestController
class CustomerController @Autowired constructor(private val customerService: CustomerService) {
@GetMapping("/customer")
fun customer():List<Customer>{
return customerService.findAll().toList()
}
}

View File

@@ -0,0 +1,16 @@
package com.chantha.jdbc.controller
import com.chantha.jdbc.jpa.model.Order
import com.chantha.jdbc.jpa.service.order.OrderService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
@RestController
class OrderController @Autowired constructor(private val orderService: OrderService) {
@GetMapping("/")
fun findAllOrder():List<Order>{
return orderService.findAll().toList()
}
}

View File

@@ -0,0 +1,4 @@
package com.chantha.jdbc.jpa;
public class Test {
}

View File

@@ -0,0 +1,76 @@
package com.chantha.jdbc.jpa.model;
import com.chantha.jdbc.jpa.view.Views;
import com.chantha.jdbc.utils.CustomGenderSerializer;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import javax.persistence.*;
import java.io.Serializable;
import java.util.List;
@Entity
@Table(name = "tbCustomer")
public class Customer implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long Id;
private String cusName;
private enum GENDER{MALE,FEMALE,OTHER};
@Enumerated(EnumType.STRING)
// @JsonSerialize(using = CustomGenderSerializer.class)
private Gender gender;
public Customer() {
}
public Customer(Long Id,String cusName,Gender gender,List<Order> orders) {
this.Id=Id;
this.cusName=cusName;
this.gender=gender;
this.orders=orders;
}
public Long getId() {
return Id;
}
public void setId(Long id) {
Id = id;
}
public String getCusName() {
return cusName;
}
public void setCusName(String cusName) {
this.cusName = cusName;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public List<Order> getOrders() {
return orders;
}
public void setOrders(List<Order> orders) {
this.orders = orders;
}
@JsonManagedReference
@OneToMany(mappedBy = "customer")
private List<Order> orders;
}

View File

@@ -0,0 +1,5 @@
package com.chantha.jdbc.jpa.model
enum class Gender{
MALE,FEMALE,OTHER
}

View File

@@ -0,0 +1,89 @@
package com.chantha.jdbc.jpa.model;
import com.chantha.jdbc.utils.CustomDateDeserializer;
import com.chantha.jdbc.utils.CustomDateSerializer;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
@Entity
@Table(name = "tbOrder")
public class Order implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long orderId;
@JsonSerialize(using = CustomDateSerializer.class)
@JsonDeserialize(using = CustomDateDeserializer.class)
private Date orderDate;
private double amount;
@ManyToOne(fetch = FetchType.LAZY)
@JsonBackReference
@JoinColumn(name = "cusId",nullable = false,referencedColumnName = "id")
private Customer customer;
@JsonManagedReference
@OneToMany(mappedBy = "order")
private List<OrderDetail> orderDetails;
public Long getOrderId() {
return orderId;
}
public void setOrderId(Long orderId) {
this.orderId = orderId;
}
public Date getOrderDate() {
return orderDate;
}
public void setOrderDate(Date orderDate) {
this.orderDate = orderDate;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
@JsonIgnore
public List<OrderDetail> getOrderDetails() {
return orderDetails;
}
public void setOrderDetails(List<OrderDetail> orderDetails) {
this.orderDetails = orderDetails;
}
public Order() {
}
public Order(Long orderId,Date orderDate,double amount,Customer customer,List<OrderDetail> orderDetails) {
this.orderId=orderId;
this.orderDate=orderDate;
this.amount=amount;
this.orderDetails=orderDetails;
}
}

View File

@@ -0,0 +1,82 @@
package com.chantha.jdbc.jpa.model;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import javax.persistence.*;
@Entity
public class OrderDetail {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long orderDetailId;
private int qty;
private double price;
@JsonManagedReference
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "orderId",nullable = false)
private Order order;
@JsonManagedReference
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "productId",nullable = false)
private Product product;
public OrderDetail() {
}
public Long getOrderDetailId() {
return orderDetailId;
}
public void setOrderDetailId(Long orderDetailId) {
this.orderDetailId = orderDetailId;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@JsonBackReference
public Order getOrder() {
return order;
}
@JsonBackReference
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public void setOrder(Order order) {
this.order = order;
}
public OrderDetail(Long orderDetailId, int qty, double price, Order order,Product product) {
this.order=order;
this.orderDetailId=orderDetailId;
this.price=price;
this.qty=qty;
this.product=product;
}
}

View File

@@ -0,0 +1,65 @@
package com.chantha.jdbc.jpa.model;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import javax.persistence.*;
import java.util.List;
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long productId;
private String productName;
private double price;
public Product() {
}
public Long getProductId() {
return productId;
}
public void setProductId(Long productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@JsonBackReference
public List<OrderDetail> getOrderDetails() {
return orderDetails;
}
public void setOrderDetails(List<OrderDetail> orderDetails) {
this.orderDetails = orderDetails;
}
public Product(Long productId, String productName, double price, List<OrderDetail> orderDetails) {
this.productId=productId;
this.productName=productName;
this.price=price;
this.orderDetails=orderDetails;
}
@JsonManagedReference
@OneToMany(fetch = FetchType.EAGER,mappedBy = "product")
private List<OrderDetail> orderDetails;
}

View File

@@ -0,0 +1,15 @@
package com.chantha.jdbc.jpa.repo;
import com.chantha.jdbc.jpa.model.Customer;
import com.chantha.jdbc.jpa.model.Order;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
public interface CustomerRepo extends CrudRepository<Customer,Long> {
@Query(value = "SELECT * \n" +
"FROM tb_order o \n" +
"INNER JOIN tb_customer c\n" +
"ON (o.cus_id=c.id)",nativeQuery = true)
@Override
Iterable<Customer> findAll();
}

View File

@@ -0,0 +1,16 @@
package com.chantha.jdbc.jpa.repo;
import com.chantha.jdbc.jpa.model.Order;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface OrderRepo extends CrudRepository<Order,Long> {
@Query(value = "SELECT * \n" +
"FROM tb_order o \n" +
"INNER JOIN tb_customer c\n" +
"ON (o.cus_id=c.id)",nativeQuery = true)
@Override
Iterable<Order> findAll();
}

View File

@@ -0,0 +1,4 @@
package com.chantha.jdbc.jpa.service;
public class Test {
}

View File

@@ -0,0 +1,6 @@
package com.chantha.jdbc.jpa.service.customer;
import com.chantha.jdbc.jpa.repo.CustomerRepo;
public interface CustomerService extends CustomerRepo {
}

View File

@@ -0,0 +1,62 @@
package com.chantha.jdbc.jpa.service.customer;
import com.chantha.jdbc.jpa.model.Customer;
import java.util.Optional;
public class CustomerServiceImpl implements CustomerService {
@Override
public <S extends Customer> S save(S s) {
return null;
}
@Override
public <S extends Customer> Iterable<S> saveAll(Iterable<S> iterable) {
return null;
}
@Override
public Optional<Customer> findById(Long aLong) {
return Optional.empty();
}
@Override
public boolean existsById(Long aLong) {
return false;
}
@Override
public Iterable<Customer> findAll() {
return null;
}
@Override
public Iterable<Customer> findAllById(Iterable<Long> iterable) {
return null;
}
@Override
public long count() {
return 0;
}
@Override
public void deleteById(Long aLong) {
}
@Override
public void delete(Customer customer) {
}
@Override
public void deleteAll(Iterable<? extends Customer> iterable) {
}
@Override
public void deleteAll() {
}
}

View File

@@ -0,0 +1,8 @@
package com.chantha.jdbc.jpa.service.order;
import com.chantha.jdbc.jpa.repo.OrderRepo;
import org.springframework.stereotype.Service;
@Service
public interface OrderService extends OrderRepo {
}

View File

@@ -0,0 +1,63 @@
package com.chantha.jdbc.jpa.service.order;
import com.chantha.jdbc.jpa.model.Order;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
public class OrderServiceImpl implements OrderService{
@Override
public <S extends Order> S save(S s) {
return null;
}
@Override
public <S extends Order> Iterable<S> saveAll(Iterable<S> iterable) {
return null;
}
@Override
public Optional<Order> findById(Long aLong) {
return Optional.empty();
}
@Override
public boolean existsById(Long aLong) {
return false;
}
@Override
public Iterable<Order> findAll() {
return null;
}
@Override
public Iterable<Order> findAllById(Iterable<Long> iterable) {
return null;
}
@Override
public long count() {
return 0;
}
@Override
public void deleteById(Long aLong) {
}
@Override
public void delete(Order order) {
}
@Override
public void deleteAll(Iterable<? extends Order> iterable) {
}
@Override
public void deleteAll() {
}
}

View File

@@ -0,0 +1,78 @@
package com.chantha.jdbc.jpa.view;
import com.chantha.jdbc.jpa.model.Gender;
import java.util.Date;
public class Views {
public static class Public{
private Long Id;
private String cusName;
private Gender gender;
private int orderId;
private double amount;
private Date orderDate;
public Long getId() {
return Id;
}
public void setId(Long id) {
Id = id;
}
public String getCusName() {
return cusName;
}
public void setCusName(String cusName) {
this.cusName = cusName;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public Date getOrderDate() {
return orderDate;
}
public void setOrderDate(Date orderDate) {
this.orderDate = orderDate;
}
public Public() {
}
public Public(Long id, String cusName, Gender gender, int orderId, double amount, Date orderDate) {
Id = id;
this.cusName = cusName;
this.gender = gender;
this.orderId = orderId;
this.amount = amount;
this.orderDate = orderDate;
}
}
}

View File

@@ -0,0 +1,37 @@
package com.chantha.jdbc.utils;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CustomDateDeserializer extends StdDeserializer<Date> {
private static SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
public CustomDateDeserializer(){
this(null);
}
public CustomDateDeserializer(Class c){
super(c);
}
@Override
public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
String date = jsonParser.getText();
try {
return simpleDateFormat.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -0,0 +1,26 @@
package com.chantha.jdbc.utils;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CustomDateSerializer extends StdSerializer<Date> {
private static SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("dd-MM-yyyy hh:mm:ss aa");
public CustomDateSerializer(){
this(null);
}
public CustomDateSerializer(Class<Date> t) {
super(t);
}
@Override
public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString(simpleDateFormat.format(date));
}
}

View File

@@ -0,0 +1,27 @@
package com.chantha.jdbc.utils;
import com.chantha.jdbc.jpa.model.Gender;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CustomGenderSerializer extends StdSerializer<Gender> {
public CustomGenderSerializer(){
this(null);
}
public CustomGenderSerializer(Class<Gender> t) {
super(t);
}
@Override
public void serialize(Gender gender, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
// jsonGenerator.writeString(gender.n);
}
}

View File

@@ -0,0 +1,11 @@
spring.datasource.url=jdbc:postgresql://localhost:5432/dbproduct?
spring.datasource.username=postgres
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
spring.jpa.open-in-view=true

View File

@@ -0,0 +1,13 @@
package com.chantha.jdbc
import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest
@SpringBootTest
class JdbcApplicationTests {
@Test
fun contextLoads() {
}
}