From a9ab80c75ebcc997e93119a4e95626eb5e33a31a Mon Sep 17 00:00:00 2001 From: Ankur Gupta Date: Fri, 10 Apr 2020 03:57:55 +0530 Subject: [PATCH] Example code for using Multiple Cache Manager in SpringBoot --- .../HexagonaljavaApplication.java | 12 ---- .../controller/StudentResultController.java | 28 -------- .../hexagonaljava/entity/Student.java | 36 ---------- .../repository/StudentResultJdbcRepoImpl.java | 72 ------------------- .../repository/StudentResultRepo.java | 10 --- .../repository/StudentResultRepoImpl.java | 24 ------- .../service/StudentResultService.java | 10 --- .../service/StudentResultServiceImpl.java | 30 -------- hexagonaljava/src/main/resources/schema.sql | 11 --- .../pom.xml | 41 ++++++----- .../MultiplecachemanagerApplication.java | 45 ++++++++++++ .../bo/CustomerDetailBO.java | 28 ++++++++ .../bo/OrderDetailBO.java | 25 +++++++ .../config/MultipleCacheResolver.java | 38 ++++++++++ .../MultipleCacheManagerController.java | 43 +++++++++++ .../multiplecachemanager/entity/Customer.java | 24 +++++++ .../multiplecachemanager/entity/Item.java | 34 +++++++++ .../multiplecachemanager/entity/Order.java | 44 ++++++++++++ .../repository/CustomerDetailRepository.java | 49 +++++++++++++ .../repository/OrderDetailRepository.java | 53 ++++++++++++++ .../src/main/resources/application.properties | 7 +- .../src/main/resources/data.sql | 7 ++ .../src/main/resources/schema.sql | 19 +++++ .../MultiplecachemanagerApplicationTests.java | 5 +- 24 files changed, 443 insertions(+), 252 deletions(-) delete mode 100644 hexagonaljava/src/main/java/com/baeldung/hexagonaljava/HexagonaljavaApplication.java delete mode 100644 hexagonaljava/src/main/java/com/baeldung/hexagonaljava/controller/StudentResultController.java delete mode 100644 hexagonaljava/src/main/java/com/baeldung/hexagonaljava/entity/Student.java delete mode 100644 hexagonaljava/src/main/java/com/baeldung/hexagonaljava/repository/StudentResultJdbcRepoImpl.java delete mode 100644 hexagonaljava/src/main/java/com/baeldung/hexagonaljava/repository/StudentResultRepo.java delete mode 100644 hexagonaljava/src/main/java/com/baeldung/hexagonaljava/repository/StudentResultRepoImpl.java delete mode 100644 hexagonaljava/src/main/java/com/baeldung/hexagonaljava/service/StudentResultService.java delete mode 100644 hexagonaljava/src/main/java/com/baeldung/hexagonaljava/service/StudentResultServiceImpl.java delete mode 100644 hexagonaljava/src/main/resources/schema.sql rename {hexagonaljava => multiplecachemanager}/pom.xml (64%) create mode 100644 multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/MultiplecachemanagerApplication.java create mode 100644 multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/bo/CustomerDetailBO.java create mode 100644 multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/bo/OrderDetailBO.java create mode 100644 multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/config/MultipleCacheResolver.java create mode 100644 multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/controller/MultipleCacheManagerController.java create mode 100644 multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/entity/Customer.java create mode 100644 multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/entity/Item.java create mode 100644 multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/entity/Order.java create mode 100644 multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/repository/CustomerDetailRepository.java create mode 100644 multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/repository/OrderDetailRepository.java rename {hexagonaljava => multiplecachemanager}/src/main/resources/application.properties (56%) create mode 100644 multiplecachemanager/src/main/resources/data.sql create mode 100644 multiplecachemanager/src/main/resources/schema.sql rename hexagonaljava/src/test/java/com/baeldung/hexagonaljava/HexagonaljavaApplicationTests.java => multiplecachemanager/src/test/java/com/baeldung/multiplecachemanager/MultiplecachemanagerApplicationTests.java (62%) diff --git a/hexagonaljava/src/main/java/com/baeldung/hexagonaljava/HexagonaljavaApplication.java b/hexagonaljava/src/main/java/com/baeldung/hexagonaljava/HexagonaljavaApplication.java deleted file mode 100644 index 89bcfb6510..0000000000 --- a/hexagonaljava/src/main/java/com/baeldung/hexagonaljava/HexagonaljavaApplication.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.baeldung.hexagonaljava; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -@SpringBootApplication -public class HexagonaljavaApplication { - - public static void main(String[] args) { - SpringApplication.run(HexagonaljavaApplication.class, args); - } -} diff --git a/hexagonaljava/src/main/java/com/baeldung/hexagonaljava/controller/StudentResultController.java b/hexagonaljava/src/main/java/com/baeldung/hexagonaljava/controller/StudentResultController.java deleted file mode 100644 index 7a5ba70c51..0000000000 --- a/hexagonaljava/src/main/java/com/baeldung/hexagonaljava/controller/StudentResultController.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.hexagonaljava.controller; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RestController; - -import com.baeldung.hexagonaljava.entity.Student; -import com.baeldung.hexagonaljava.service.StudentResultService; - -@RestController -public class StudentResultController { - - @Autowired - private StudentResultService studentResultService; - - @PostMapping(value = "/save") - public void saveStudent(@RequestBody Student student) { - studentResultService.save(student); - } - - @GetMapping(value = "/getTotalMarks/{id}") - public Double getTotalMarks(@PathVariable Integer id) { - return studentResultService.getTotalMarks(id); - } -} diff --git a/hexagonaljava/src/main/java/com/baeldung/hexagonaljava/entity/Student.java b/hexagonaljava/src/main/java/com/baeldung/hexagonaljava/entity/Student.java deleted file mode 100644 index 040faa03a7..0000000000 --- a/hexagonaljava/src/main/java/com/baeldung/hexagonaljava/entity/Student.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.baeldung.hexagonaljava.entity; - -import java.util.Map; - -public class Student { - - private Integer id; - - private String name; - - private Map marks; - - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Map getMarks() { - return marks; - } - - public void setMarks(Map marks) { - this.marks = marks; - } -} diff --git a/hexagonaljava/src/main/java/com/baeldung/hexagonaljava/repository/StudentResultJdbcRepoImpl.java b/hexagonaljava/src/main/java/com/baeldung/hexagonaljava/repository/StudentResultJdbcRepoImpl.java deleted file mode 100644 index 7b970462e3..0000000000 --- a/hexagonaljava/src/main/java/com/baeldung/hexagonaljava/repository/StudentResultJdbcRepoImpl.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.baeldung.hexagonaljava.repository; - -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.HashMap; -import java.util.Map; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Primary; -import org.springframework.jdbc.core.BatchPreparedStatementSetter; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.core.RowCallbackHandler; -import org.springframework.stereotype.Component; - -import com.baeldung.hexagonaljava.entity.Student; - -@Primary -@Component -public class StudentResultJdbcRepoImpl implements StudentResultRepo { - - @Autowired - JdbcTemplate jdbcTemplate; - - @Override - public void save(Student student) { - jdbcTemplate.update("insert into student (id, name) " + "values(?, ?)", new Object[] { student.getId(), student.getName() }); - insertResult(student); - } - - public void insertResult(Student student) { - String insertQuery = "insert into " + "studentresult " + "(subject,marks,id) " + "values " + "(?,?,?)"; - for (final Map.Entry entry : student.getMarks() - .entrySet()) { - this.jdbcTemplate.batchUpdate(insertQuery, new BatchPreparedStatementSetter() { - @Override - public void setValues(final PreparedStatement ps, final int i) throws SQLException { - ps.setString(1, entry.getKey()); - ps.setDouble(2, entry.getValue()); - ps.setInt(3, student.getId()); - } - - public int getBatchSize() { - return student.getMarks() - .size(); - } - }); - } - } - - @Override - public Student getStudent(Integer id) { - String selectQuery = "select * from ( select * from student where id = ? ) s left join studentresult on s.id = studentresult.id"; - Student student = new Student(); - jdbcTemplate.query(selectQuery, new Object[] { id }, new RowCallbackHandler() { - public void processRow(ResultSet rs) throws SQLException { - while (rs.next()) { - if (student.getId() == null) { - student.setId(rs.getInt("id")); - student.setName(rs.getString("name")); - student.setMarks(new HashMap()); - } - String subject = rs.getString("subject"); - Double marks = rs.getDouble("marks"); - student.getMarks() - .put(subject, marks); - } - } - }); - return student; - } -} diff --git a/hexagonaljava/src/main/java/com/baeldung/hexagonaljava/repository/StudentResultRepo.java b/hexagonaljava/src/main/java/com/baeldung/hexagonaljava/repository/StudentResultRepo.java deleted file mode 100644 index 96cf105600..0000000000 --- a/hexagonaljava/src/main/java/com/baeldung/hexagonaljava/repository/StudentResultRepo.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.baeldung.hexagonaljava.repository; - -import com.baeldung.hexagonaljava.entity.Student; - -public interface StudentResultRepo { - - void save(Student student); - - Student getStudent(Integer id); -} diff --git a/hexagonaljava/src/main/java/com/baeldung/hexagonaljava/repository/StudentResultRepoImpl.java b/hexagonaljava/src/main/java/com/baeldung/hexagonaljava/repository/StudentResultRepoImpl.java deleted file mode 100644 index 46fd37a5f2..0000000000 --- a/hexagonaljava/src/main/java/com/baeldung/hexagonaljava/repository/StudentResultRepoImpl.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.baeldung.hexagonaljava.repository; - -import java.util.HashMap; -import java.util.Map; - -import org.springframework.stereotype.Repository; - -import com.baeldung.hexagonaljava.entity.Student; - -@Repository -public class StudentResultRepoImpl implements StudentResultRepo { - - private Map studentsMap = new HashMap(); - - @Override - public void save(Student student) { - studentsMap.put(student.getId(), student); - } - - @Override - public Student getStudent(Integer id) { - return studentsMap.get(id); - } -} diff --git a/hexagonaljava/src/main/java/com/baeldung/hexagonaljava/service/StudentResultService.java b/hexagonaljava/src/main/java/com/baeldung/hexagonaljava/service/StudentResultService.java deleted file mode 100644 index 91a25f3809..0000000000 --- a/hexagonaljava/src/main/java/com/baeldung/hexagonaljava/service/StudentResultService.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.baeldung.hexagonaljava.service; - -import com.baeldung.hexagonaljava.entity.Student; - -public interface StudentResultService { - - void save(Student student); - - Double getTotalMarks(Integer id); -} diff --git a/hexagonaljava/src/main/java/com/baeldung/hexagonaljava/service/StudentResultServiceImpl.java b/hexagonaljava/src/main/java/com/baeldung/hexagonaljava/service/StudentResultServiceImpl.java deleted file mode 100644 index f8c163fdf4..0000000000 --- a/hexagonaljava/src/main/java/com/baeldung/hexagonaljava/service/StudentResultServiceImpl.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.baeldung.hexagonaljava.service; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import com.baeldung.hexagonaljava.entity.Student; -import com.baeldung.hexagonaljava.repository.StudentResultRepo; - -@Component -public class StudentResultServiceImpl implements StudentResultService { - - @Autowired - private StudentResultRepo studentResultRepo; - - @Override - public void save(Student student) { - studentResultRepo.save(student); - } - - @Override - public Double getTotalMarks(Integer id) { - Student student = studentResultRepo.getStudent(id); - double totalMarks = 0; - for (double marks : student.getMarks() - .values()) { - totalMarks += marks; - } - return totalMarks; - } -} diff --git a/hexagonaljava/src/main/resources/schema.sql b/hexagonaljava/src/main/resources/schema.sql deleted file mode 100644 index 40ba01ec02..0000000000 --- a/hexagonaljava/src/main/resources/schema.sql +++ /dev/null @@ -1,11 +0,0 @@ -CREATE TABLE STUDENT ( - id INT PRIMARY KEY, - name VARCHAR(250) NOT NULL -); - -CREATE TABLE STUDENTRESULT( -subject VARCHAR(250) NOT NULL, -marks NUMERIC(8,2), -id VARCHAR(250), -FOREIGN KEY (id) references STUDENT(id) -); diff --git a/hexagonaljava/pom.xml b/multiplecachemanager/pom.xml similarity index 64% rename from hexagonaljava/pom.xml rename to multiplecachemanager/pom.xml index d9ca173888..22daf6e87d 100644 --- a/hexagonaljava/pom.xml +++ b/multiplecachemanager/pom.xml @@ -5,25 +5,45 @@ org.springframework.boot spring-boot-starter-parent - 2.2.5.RELEASE + 2.2.6.RELEASE - com.baeldung. - hexagonaljava + com.baeldung + multiplecachemanager 0.0.1-SNAPSHOT - hexagonaljava - Demo project for Spring Boot + multiplecachemanager + sample project for configuring multiple cache managers 1.8 + + org.springframework.boot + spring-boot-starter-jdbc + org.springframework.boot spring-boot-starter-web + + com.h2database + h2 + runtime + + + + org.springframework.boot + spring-boot-starter-cache + + + + com.github.ben-manes.caffeine + caffeine + + org.springframework.boot spring-boot-starter-test @@ -35,17 +55,6 @@ - - - org.springframework.boot - spring-boot-starter-jdbc - - - - com.h2database - h2 - runtime - diff --git a/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/MultiplecachemanagerApplication.java b/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/MultiplecachemanagerApplication.java new file mode 100644 index 0000000000..4c7efc98bd --- /dev/null +++ b/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/MultiplecachemanagerApplication.java @@ -0,0 +1,45 @@ +package com.baeldung.multiplecachemanager; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.CachingConfigurerSupport; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.cache.caffeine.CaffeineCacheManager; +import org.springframework.cache.concurrent.ConcurrentMapCacheManager; +import org.springframework.cache.interceptor.CacheResolver; +import org.springframework.context.annotation.Bean; + +import com.baeldung.multiplecachemanager.config.MultipleCacheResolver; +import com.github.benmanes.caffeine.cache.Caffeine; + +@SpringBootApplication +@EnableCaching +public class MultiplecachemanagerApplication extends CachingConfigurerSupport { + + public static void main(String[] args) { + SpringApplication.run(MultiplecachemanagerApplication.class, args); + } + + @Bean + // @Primary + public CacheManager cacheManager() { + CaffeineCacheManager cacheManager = new CaffeineCacheManager("customers", "orders"); + cacheManager.setCaffeine(Caffeine.newBuilder() + .initialCapacity(200) + .maximumSize(500) + .weakKeys() + .recordStats()); + return cacheManager; + } + + @Bean + public CacheManager alternateCacheManager() { + return new ConcurrentMapCacheManager("customerOrders", "orderprice"); + } + + @Bean + public CacheResolver cacheResolver() { + return new MultipleCacheResolver(alternateCacheManager(), cacheManager()); + } +} diff --git a/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/bo/CustomerDetailBO.java b/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/bo/CustomerDetailBO.java new file mode 100644 index 0000000000..3da4c23e28 --- /dev/null +++ b/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/bo/CustomerDetailBO.java @@ -0,0 +1,28 @@ +package com.baeldung.multiplecachemanager.bo; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Component; + +import com.baeldung.multiplecachemanager.entity.Customer; +import com.baeldung.multiplecachemanager.entity.Order; +import com.baeldung.multiplecachemanager.repository.CustomerDetailRepository; + +@Component +public class CustomerDetailBO { + + @Autowired + private CustomerDetailRepository customerDetailRepository; + + @Cacheable(cacheNames = "customers") + public Customer getCustomerDetail(Integer customerId) { + return customerDetailRepository.getCustomerDetail(customerId); + } + + @Cacheable(cacheNames = "customerOrders", cacheManager = "alternateCacheManager") + public List getCustomerOrders(Integer customerId) { + return customerDetailRepository.getCustomerOrders(customerId); + } +} diff --git a/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/bo/OrderDetailBO.java b/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/bo/OrderDetailBO.java new file mode 100644 index 0000000000..cb9e301fcb --- /dev/null +++ b/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/bo/OrderDetailBO.java @@ -0,0 +1,25 @@ +package com.baeldung.multiplecachemanager.bo; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Component; + +import com.baeldung.multiplecachemanager.entity.Order; +import com.baeldung.multiplecachemanager.repository.OrderDetailRepository; + +@Component +public class OrderDetailBO { + + @Autowired + private OrderDetailRepository orderDetailRepository; + + @Cacheable(cacheNames = "orders", cacheResolver = "cacheResolver") + public Order getOrderDetail(Integer orderId) { + return orderDetailRepository.getOrderDetail(orderId); + } + + @Cacheable(cacheNames = "orderprice", cacheResolver = "cacheResolver") + public double getOrderPrice(Integer orderId) { + return orderDetailRepository.getOrderPrice(orderId); + } +} diff --git a/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/config/MultipleCacheResolver.java b/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/config/MultipleCacheResolver.java new file mode 100644 index 0000000000..1bd869d98e --- /dev/null +++ b/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/config/MultipleCacheResolver.java @@ -0,0 +1,38 @@ +package com.baeldung.multiplecachemanager.config; + +import java.util.ArrayList; +import java.util.Collection; + +import org.springframework.cache.Cache; +import org.springframework.cache.CacheManager; +import org.springframework.cache.interceptor.CacheOperationInvocationContext; +import org.springframework.cache.interceptor.CacheResolver; + +public class MultipleCacheResolver implements CacheResolver { + + private final CacheManager simpleCacheManager; + + private final CacheManager caffeineCacheManager; + + private static final String ORDER_CACHE = "orders"; + + private static final String ORDER_PRICE_CACHE = "orderprice"; + + public MultipleCacheResolver(CacheManager simpleCacheManager, CacheManager caffeineCacheManager) { + this.simpleCacheManager = simpleCacheManager; + this.caffeineCacheManager = caffeineCacheManager; + + } + + @Override + public Collection resolveCaches(CacheOperationInvocationContext context) { + Collection caches = new ArrayList(); + if ("getOrderDetail".equals(context.getMethod() + .getName())) { + caches.add(caffeineCacheManager.getCache(ORDER_CACHE)); + } else { + caches.add(simpleCacheManager.getCache(ORDER_PRICE_CACHE)); + } + return caches; + } +} diff --git a/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/controller/MultipleCacheManagerController.java b/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/controller/MultipleCacheManagerController.java new file mode 100644 index 0000000000..17a73bb27a --- /dev/null +++ b/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/controller/MultipleCacheManagerController.java @@ -0,0 +1,43 @@ +package com.baeldung.multiplecachemanager.controller; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.multiplecachemanager.bo.CustomerDetailBO; +import com.baeldung.multiplecachemanager.bo.OrderDetailBO; +import com.baeldung.multiplecachemanager.entity.Customer; +import com.baeldung.multiplecachemanager.entity.Order; + +@RestController +public class MultipleCacheManagerController { + + @Autowired + private CustomerDetailBO customerDetailBO; + + @Autowired + private OrderDetailBO orderDetailBO; + + @GetMapping(value = "/getCustomer/{customerid}") + public Customer getCustomer(@PathVariable Integer customerid) { + return customerDetailBO.getCustomerDetail(customerid); + } + + @GetMapping(value = "/getCustomerOrders/{customerid}") + public List getCustomerOrders(@PathVariable Integer customerid) { + return customerDetailBO.getCustomerOrders(customerid); + } + + @GetMapping(value = "/getOrder/{orderid}") + public Order getOrder(@PathVariable Integer orderid) { + return orderDetailBO.getOrderDetail(orderid); + } + + @GetMapping(value = "/getOrderPrice/{orderid}") + public double getOrderPrice(@PathVariable Integer orderid) { + return orderDetailBO.getOrderPrice(orderid); + } +} diff --git a/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/entity/Customer.java b/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/entity/Customer.java new file mode 100644 index 0000000000..cfae15f4e9 --- /dev/null +++ b/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/entity/Customer.java @@ -0,0 +1,24 @@ +package com.baeldung.multiplecachemanager.entity; + +public class Customer { + + private int customerId; + + private String customerName; + + public int getCustomerId() { + return customerId; + } + + public void setCustomerId(int customerId) { + this.customerId = customerId; + } + + public String getCustomerName() { + return customerName; + } + + public void setCustomerName(String customerName) { + this.customerName = customerName; + } +} diff --git a/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/entity/Item.java b/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/entity/Item.java new file mode 100644 index 0000000000..4131464981 --- /dev/null +++ b/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/entity/Item.java @@ -0,0 +1,34 @@ +package com.baeldung.multiplecachemanager.entity; + +public class Item { + + private int itemId; + + private String itemDesc; + + private double itemPrice; + + public int getItemId() { + return itemId; + } + + public void setItemId(int itemId) { + this.itemId = itemId; + } + + public String getItemDesc() { + return itemDesc; + } + + public void setItemDesc(String itemDesc) { + this.itemDesc = itemDesc; + } + + public double getItemPrice() { + return itemPrice; + } + + public void setItemPrice(double itemPrice) { + this.itemPrice = itemPrice; + } +} diff --git a/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/entity/Order.java b/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/entity/Order.java new file mode 100644 index 0000000000..15da60d6ea --- /dev/null +++ b/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/entity/Order.java @@ -0,0 +1,44 @@ +package com.baeldung.multiplecachemanager.entity; + +public class Order { + + private int orderId; + + private int itemId; + + private int quantity; + + private int customerId; + + public int getOrderId() { + return orderId; + } + + public void setOrderId(int orderId) { + this.orderId = orderId; + } + + public int getItemId() { + return itemId; + } + + public void setItemId(int itemId) { + this.itemId = itemId; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public int getCustomerId() { + return customerId; + } + + public void setCustomerId(int customerId) { + this.customerId = customerId; + } +} diff --git a/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/repository/CustomerDetailRepository.java b/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/repository/CustomerDetailRepository.java new file mode 100644 index 0000000000..aab011427d --- /dev/null +++ b/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/repository/CustomerDetailRepository.java @@ -0,0 +1,49 @@ +package com.baeldung.multiplecachemanager.repository; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowCallbackHandler; +import org.springframework.stereotype.Repository; + +import com.baeldung.multiplecachemanager.entity.Customer; +import com.baeldung.multiplecachemanager.entity.Order; + +@Repository +public class CustomerDetailRepository { + + @Autowired + private JdbcTemplate jdbcTemplate; + + public Customer getCustomerDetail(Integer customerId) { + String customerQuery = "select * from customer where customerid = ? "; + Customer customer = new Customer(); + jdbcTemplate.query(customerQuery, new Object[] { customerId }, new RowCallbackHandler() { + public void processRow(ResultSet rs) throws SQLException { + customer.setCustomerId(rs.getInt("customerid")); + customer.setCustomerName(rs.getString("customername")); + } + }); + return customer; + } + + public List getCustomerOrders(Integer customerId) { + String customerOrderQuery = "select * from orderdetail where customerid = ? "; + List orders = new ArrayList(); + jdbcTemplate.query(customerOrderQuery, new Object[] { customerId }, new RowCallbackHandler() { + public void processRow(ResultSet rs) throws SQLException { + Order order = new Order(); + order.setCustomerId(rs.getInt("customerid")); + order.setItemId(rs.getInt("orderid")); + order.setOrderId(rs.getInt("orderid")); + order.setQuantity(rs.getInt("quantity")); + orders.add(order); + } + }); + return orders; + } +} diff --git a/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/repository/OrderDetailRepository.java b/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/repository/OrderDetailRepository.java new file mode 100644 index 0000000000..58c0968e48 --- /dev/null +++ b/multiplecachemanager/src/main/java/com/baeldung/multiplecachemanager/repository/OrderDetailRepository.java @@ -0,0 +1,53 @@ +package com.baeldung.multiplecachemanager.repository; + +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowCallbackHandler; +import org.springframework.stereotype.Repository; + +import com.baeldung.multiplecachemanager.entity.Item; +import com.baeldung.multiplecachemanager.entity.Order; + +@Repository +public class OrderDetailRepository { + + @Autowired + private JdbcTemplate jdbcTemplate; + + public Order getOrderDetail(Integer orderId) { + String orderDetailQuery = "select * from orderdetail where orderid = ? "; + Order order = new Order(); + jdbcTemplate.query(orderDetailQuery, new Object[] { orderId }, new RowCallbackHandler() { + public void processRow(ResultSet rs) throws SQLException { + order.setCustomerId(rs.getInt("customerid")); + order.setOrderId(rs.getInt("orderid")); + order.setItemId(rs.getInt("itemid")); + order.setQuantity(rs.getInt("quantity")); + } + }); + return order; + } + + public double getOrderPrice(Integer orderId) { + + String orderItemJoinQuery = "select * from ( select * from orderdetail where orderid = ? ) o left join item on o.itemid = ITEM.itemid"; + Order order = new Order(); + Item item = new Item(); + + jdbcTemplate.query(orderItemJoinQuery, new Object[] { orderId }, new RowCallbackHandler() { + public void processRow(ResultSet rs) throws SQLException { + order.setCustomerId(rs.getInt("customerid")); + order.setOrderId(rs.getInt("orderid")); + order.setItemId(rs.getInt("itemid")); + order.setQuantity(rs.getInt("quantity")); + item.setItemDesc("itemdesc"); + item.setItemId(rs.getInt("itemid")); + item.setItemPrice(rs.getDouble("price")); + } + }); + return order.getQuantity() * item.getItemPrice(); + } +} diff --git a/hexagonaljava/src/main/resources/application.properties b/multiplecachemanager/src/main/resources/application.properties similarity index 56% rename from hexagonaljava/src/main/resources/application.properties rename to multiplecachemanager/src/main/resources/application.properties index 5952b7eb01..53a3ac93b4 100644 --- a/hexagonaljava/src/main/resources/application.properties +++ b/multiplecachemanager/src/main/resources/application.properties @@ -2,4 +2,9 @@ spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password= -spring.h2.console.enabled=false +#spring.h2.console.enabled=false + + +# Enabling H2 Console +spring.h2.console.enabled=true +spring.h2.console.path=/h2 diff --git a/multiplecachemanager/src/main/resources/data.sql b/multiplecachemanager/src/main/resources/data.sql new file mode 100644 index 0000000000..e4165ae71f --- /dev/null +++ b/multiplecachemanager/src/main/resources/data.sql @@ -0,0 +1,7 @@ +INSERT INTO CUSTOMER VALUES(1001,'BAELDUNG'); + +INSERT INTO ITEM VALUES(10001,'ITEM1',50.0); +INSERT INTO ITEM VALUES(10002,'ITEM2',100.0); + +INSERT INTO ORDERDETAIL VALUES(300001,1001,10001,2); +INSERT INTO ORDERDETAIL VALUES(300002,1001,10002,5); \ No newline at end of file diff --git a/multiplecachemanager/src/main/resources/schema.sql b/multiplecachemanager/src/main/resources/schema.sql new file mode 100644 index 0000000000..5862499bc0 --- /dev/null +++ b/multiplecachemanager/src/main/resources/schema.sql @@ -0,0 +1,19 @@ +CREATE TABLE CUSTOMER( + CUSTOMERID INT PRIMARY KEY, + CUSTOMERNAME VARCHAR(250) NOT NULL +); + +CREATE TABLE ITEM( +ITEMID INT PRIMARY KEY, +ITEMDESC VARCHAR(250), +PRICE DOUBLE +); + +CREATE TABLE ORDERDETAIL( +ORDERID INT PRIMARY KEY, +CUSTOMERID INT NOT NULL, +ITEMID INT NOT NULL, +QUANTITY INT, +FOREIGN KEY (customerid) references CUSTOMER(customerid), +FOREIGN KEY (itemid) references ITEM(itemid) +); \ No newline at end of file diff --git a/hexagonaljava/src/test/java/com/baeldung/hexagonaljava/HexagonaljavaApplicationTests.java b/multiplecachemanager/src/test/java/com/baeldung/multiplecachemanager/MultiplecachemanagerApplicationTests.java similarity index 62% rename from hexagonaljava/src/test/java/com/baeldung/hexagonaljava/HexagonaljavaApplicationTests.java rename to multiplecachemanager/src/test/java/com/baeldung/multiplecachemanager/MultiplecachemanagerApplicationTests.java index eed2f7e6a3..adf69309f2 100644 --- a/hexagonaljava/src/test/java/com/baeldung/hexagonaljava/HexagonaljavaApplicationTests.java +++ b/multiplecachemanager/src/test/java/com/baeldung/multiplecachemanager/MultiplecachemanagerApplicationTests.java @@ -1,12 +1,13 @@ -package com.baeldung.hexagonaljava; +package com.baeldung.multiplecachemanager; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest -class HexagonaljavaApplicationTests { +class MultiplecachemanagerApplicationTests { @Test void contextLoads() { } + }