├── README.md ├── pom.xml └── src ├── main ├── java │ └── guru │ │ └── springframework │ │ ├── SpringBootWebApplication.java │ │ ├── bootstrap │ │ └── ProductLoader.java │ │ ├── configuration │ │ ├── SecurityConfiguration.java │ │ ├── WebConfig.java │ │ └── WebConfiguration.java │ │ ├── controllers │ │ ├── IndexController.java │ │ ├── PageWrapper.java │ │ └── ProductController.java │ │ ├── domain │ │ └── Product.java │ │ ├── repositories │ │ └── ProductRepository.java │ │ └── services │ │ ├── ProductService.java │ │ └── ProductServiceImpl.java └── resources │ ├── application.properties │ ├── static │ ├── css │ │ └── guru.css │ └── images │ │ ├── FBcover1200x628.png │ │ └── NewBannerBOOTS_2.png │ └── templates │ ├── fragments │ ├── header.html │ └── headerinc.html │ ├── index.html │ ├── productform.html │ ├── products.html │ └── productshow.html └── test └── java └── guru └── springframework ├── SpringBootWebApplicationTests.java ├── configuration └── RepositoryConfiguration.java └── repositories └── ProductRepositoryTest.java /README.md: -------------------------------------------------------------------------------- 1 | # Spring Boot Web Application, support data jpa and pageable, using thymeleaf 2 | 3 | In this part of the tutorial series, I show how to setup a Spring MVC controller to suport CRUD operations, a Spring service facad over a Spring Data JPA repository with pagination support, and Thymeleaf templates for the web application. -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | guru.springframework 7 | spring-boot-web 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | Spring Boot Web Application 12 | Spring Boot Web Application 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.2.7.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | 1.8 24 | 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-data-jpa 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter-security 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-thymeleaf 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-starter-web 42 | 43 | 44 | 45 | 46 | org.webjars 47 | bootstrap 48 | 3.3.4 49 | 50 | 51 | org.webjars 52 | jquery 53 | 2.1.4 54 | 55 | 56 | 57 | com.h2database 58 | h2 59 | 60 | 61 | org.springframework.boot 62 | spring-boot-starter-test 63 | test 64 | 65 | 66 | 67 | 68 | 69 | 70 | org.springframework.boot 71 | spring-boot-maven-plugin 72 | 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/SpringBootWebApplication.java: -------------------------------------------------------------------------------- 1 | package guru.springframework; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootWebApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootWebApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/bootstrap/ProductLoader.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.bootstrap; 2 | 3 | import guru.springframework.domain.Product; 4 | import guru.springframework.repositories.ProductRepository; 5 | import org.apache.log4j.Logger; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.context.ApplicationListener; 8 | import org.springframework.context.event.ContextRefreshedEvent; 9 | import org.springframework.stereotype.Component; 10 | 11 | import java.math.BigDecimal; 12 | 13 | @Component 14 | public class ProductLoader implements ApplicationListener { 15 | 16 | private ProductRepository productRepository; 17 | 18 | private Logger log = Logger.getLogger(ProductLoader.class); 19 | 20 | @Autowired 21 | public void setProductRepository(ProductRepository productRepository) { 22 | this.productRepository = productRepository; 23 | } 24 | 25 | @Override 26 | public void onApplicationEvent(ContextRefreshedEvent event) { 27 | 28 | Product shirt = new Product(); 29 | shirt.setDescription("Spring Framework Guru Shirt"); 30 | shirt.setPrice(new BigDecimal("18.95")); 31 | shirt.setImageUrl("https://springframework.guru/wp-content/uploads/2015/04/spring_framework_guru_shirt-rf412049699c14ba5b68bb1c09182bfa2_8nax2_512.jpg"); 32 | shirt.setProductId("235268845711068308"); 33 | productRepository.save(shirt); 34 | 35 | log.info("Saved Shirt - id: " + shirt.getId()); 36 | 37 | Product mug = new Product(); 38 | mug.setDescription("Spring Framework Guru Mug"); 39 | mug.setImageUrl("https://springframework.guru/wp-content/uploads/2015/04/spring_framework_guru_coffee_mug-r11e7694903c348e1a667dfd2f1474d95_x7j54_8byvr_512.jpg"); 40 | mug.setProductId("168639393495335947"); 41 | mug.setPrice(new BigDecimal("11.95")); 42 | productRepository.save(mug); 43 | 44 | log.info("Saved Mug - id:" + mug.getId()); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/configuration/SecurityConfiguration.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.configuration; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 5 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 6 | 7 | @Configuration 8 | public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 9 | 10 | @Override 11 | protected void configure(HttpSecurity httpSecurity) throws Exception { 12 | httpSecurity.authorizeRequests().antMatchers("/").permitAll().and() 13 | .authorizeRequests().antMatchers("/console/**").permitAll(); 14 | 15 | httpSecurity.csrf().disable(); 16 | httpSecurity.headers().frameOptions().disable(); 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /src/main/java/guru/springframework/configuration/WebConfig.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.configuration; 2 | 3 | import org.springframework.context.annotation.ComponentScan; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.data.domain.PageRequest; 6 | import org.springframework.web.method.support.HandlerMethodArgumentResolver; 7 | import org.springframework.web.servlet.config.annotation.EnableWebMvc; 8 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 9 | import org.springframework.web.servlet.mvc.method.annotation.ServletWebArgumentResolverAdapter; 10 | import org.springframework.data.web.PageableHandlerMethodArgumentResolver; 11 | import java.util.List; 12 | 13 | /** 14 | * Created by tiansha on 2015/11/3. 15 | */ 16 | @Configuration 17 | //@EnableWebMvc 18 | //@ComponentScan(basePackages = "guru.springframework") 19 | public class WebConfig extends WebMvcConfigurerAdapter { 20 | 21 | @Override 22 | public void addArgumentResolvers(List argumentResolvers) { 23 | PageableHandlerMethodArgumentResolver resolver = new PageableHandlerMethodArgumentResolver(); 24 | resolver.setFallbackPageable(new PageRequest(0, 5)); 25 | argumentResolvers.add(resolver); 26 | super.addArgumentResolvers(argumentResolvers); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/configuration/WebConfiguration.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.configuration; 2 | 3 | import org.h2.server.web.WebServlet; 4 | import org.springframework.boot.context.embedded.ServletRegistrationBean; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | import org.springframework.data.web.config.EnableSpringDataWebSupport; 8 | 9 | @Configuration 10 | @EnableSpringDataWebSupport 11 | public class WebConfiguration { 12 | @Bean 13 | ServletRegistrationBean h2servletRegistration(){ 14 | ServletRegistrationBean registrationBean = new ServletRegistrationBean( new WebServlet()); 15 | registrationBean.addUrlMappings("/console/*"); 16 | return registrationBean; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/controllers/IndexController.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.controllers; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | 6 | @Controller 7 | public class IndexController { 8 | @RequestMapping("/") 9 | String index(){ 10 | return "index"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/controllers/PageWrapper.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.controllers; 2 | 3 | import org.springframework.data.domain.Page; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | /** 9 | * Created by tiansha on 2015/11/3. 10 | */ 11 | public class PageWrapper { 12 | public static final int MAX_PAGE_ITEM_DISPLAY = 5; 13 | private Page page; 14 | private List items; 15 | private int currentNumber; 16 | private String url; 17 | 18 | public String getUrl() { 19 | return url; 20 | } 21 | 22 | public void setUrl(String url) { 23 | this.url = url; 24 | } 25 | 26 | public PageWrapper(Page page, String url){ 27 | this.page = page; 28 | this.url = url; 29 | items = new ArrayList(); 30 | 31 | currentNumber = page.getNumber() + 1; //start from 1 to match page.page 32 | 33 | int start, size; 34 | if (page.getTotalPages() <= MAX_PAGE_ITEM_DISPLAY){ 35 | start = 1; 36 | size = page.getTotalPages(); 37 | } else { 38 | if (currentNumber <= MAX_PAGE_ITEM_DISPLAY - MAX_PAGE_ITEM_DISPLAY/2){ 39 | start = 1; 40 | size = MAX_PAGE_ITEM_DISPLAY; 41 | } else if (currentNumber >= page.getTotalPages() - MAX_PAGE_ITEM_DISPLAY/2){ 42 | start = page.getTotalPages() - MAX_PAGE_ITEM_DISPLAY + 1; 43 | size = MAX_PAGE_ITEM_DISPLAY; 44 | } else { 45 | start = currentNumber - MAX_PAGE_ITEM_DISPLAY/2; 46 | size = MAX_PAGE_ITEM_DISPLAY; 47 | } 48 | } 49 | 50 | for (int i = 0; i getItems(){ 56 | return items; 57 | } 58 | 59 | public int getNumber(){ 60 | return currentNumber; 61 | } 62 | 63 | public List getContent(){ 64 | return page.getContent(); 65 | } 66 | 67 | public int getSize(){ 68 | return page.getSize(); 69 | } 70 | 71 | public int getTotalPages(){ 72 | return page.getTotalPages(); 73 | } 74 | 75 | public boolean isFirstPage(){ 76 | return page.isFirst(); 77 | } 78 | 79 | public boolean isLastPage(){ 80 | return page.isLast(); 81 | } 82 | 83 | public boolean isHasPreviousPage(){ 84 | return page.hasPrevious(); 85 | } 86 | 87 | public boolean isHasNextPage(){ 88 | return page.hasNext(); 89 | } 90 | 91 | public class PageItem { 92 | private int number; 93 | private boolean current; 94 | public PageItem(int number, boolean current){ 95 | this.number = number; 96 | this.current = current; 97 | } 98 | 99 | public int getNumber(){ 100 | return this.number; 101 | } 102 | 103 | public boolean isCurrent(){ 104 | return this.current; 105 | } 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/controllers/ProductController.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.controllers; 2 | 3 | import guru.springframework.domain.Product; 4 | import guru.springframework.services.ProductService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.data.domain.Page; 7 | import org.springframework.data.domain.Pageable; 8 | import org.springframework.stereotype.Controller; 9 | import org.springframework.ui.Model; 10 | import org.springframework.web.bind.annotation.PathVariable; 11 | import org.springframework.web.bind.annotation.RequestMapping; 12 | import org.springframework.web.bind.annotation.RequestMethod; 13 | 14 | @Controller 15 | public class ProductController { 16 | 17 | private ProductService productService; 18 | 19 | @Autowired 20 | public void setProductService(ProductService productService) { 21 | this.productService = productService; 22 | } 23 | 24 | @RequestMapping(value = "/products", method = RequestMethod.GET) 25 | public String list(Model model, Pageable pageable){ 26 | Page productPage = productService.findAll(pageable); 27 | PageWrapper page = new PageWrapper(productPage, "/products"); 28 | model.addAttribute("products", page.getContent()); 29 | model.addAttribute("page", page); 30 | return "products"; 31 | } 32 | 33 | @RequestMapping("product/{id}") 34 | public String showProduct(@PathVariable Integer id, Model model){ 35 | model.addAttribute("product", productService.getProductById(id)); 36 | return "productshow"; 37 | } 38 | 39 | @RequestMapping("product/edit/{id}") 40 | public String edit(@PathVariable Integer id, Model model){ 41 | model.addAttribute("product", productService.getProductById(id)); 42 | return "productform"; 43 | } 44 | 45 | @RequestMapping("product/new") 46 | public String newProduct(Model model){ 47 | model.addAttribute("product", new Product()); 48 | return "productform"; 49 | } 50 | 51 | @RequestMapping(value = "product", method = RequestMethod.POST) 52 | public String saveProduct(Product product){ 53 | productService.saveProduct(product); 54 | return "redirect:/product/" + product.getId(); 55 | } 56 | 57 | @RequestMapping("product/delete/{id}") 58 | public String delete(@PathVariable Integer id){ 59 | productService.deleteProduct(id); 60 | return "redirect:/products"; 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/domain/Product.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.domain; 2 | 3 | import javax.persistence.*; 4 | import java.math.BigDecimal; 5 | 6 | @Entity 7 | public class Product { 8 | @Id 9 | @GeneratedValue(strategy = GenerationType.AUTO) 10 | private Integer id; 11 | 12 | @Version 13 | private Integer version; 14 | 15 | private String productId; 16 | private String description; 17 | private String imageUrl; 18 | private BigDecimal price; 19 | 20 | public String getDescription() { 21 | return description; 22 | } 23 | 24 | public void setDescription(String description) { 25 | this.description = description; 26 | } 27 | 28 | public Integer getVersion() { 29 | return version; 30 | } 31 | 32 | public void setVersion(Integer version) { 33 | this.version = version; 34 | } 35 | 36 | public Integer getId() { 37 | return id; 38 | } 39 | 40 | public void setId(Integer id) { 41 | this.id = id; 42 | } 43 | 44 | public String getProductId() { 45 | return productId; 46 | } 47 | 48 | public void setProductId(String productId) { 49 | this.productId = productId; 50 | } 51 | 52 | public String getImageUrl() { 53 | return imageUrl; 54 | } 55 | 56 | public void setImageUrl(String imageUrl) { 57 | this.imageUrl = imageUrl; 58 | } 59 | 60 | public BigDecimal getPrice() { 61 | return price; 62 | } 63 | 64 | public void setPrice(BigDecimal price) { 65 | this.price = price; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/repositories/ProductRepository.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.repositories; 2 | 3 | import guru.springframework.domain.Product; 4 | import org.springframework.data.domain.Page; 5 | import org.springframework.data.domain.Pageable; 6 | import org.springframework.data.repository.CrudRepository; 7 | 8 | public interface ProductRepository extends CrudRepository{ 9 | Page findAll(Pageable pageable); 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/services/ProductService.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.services; 2 | 3 | 4 | import guru.springframework.domain.Product; 5 | import org.springframework.data.domain.Page; 6 | import org.springframework.data.domain.Pageable; 7 | 8 | public interface ProductService { 9 | Iterable listAllProducts(); 10 | 11 | Product getProductById(Integer id); 12 | 13 | Product saveProduct(Product product); 14 | 15 | void deleteProduct(Integer id); 16 | 17 | Page findAll(Pageable pageable); 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/services/ProductServiceImpl.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.services; 2 | 3 | import guru.springframework.domain.Product; 4 | import guru.springframework.repositories.ProductRepository; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.data.domain.Page; 7 | import org.springframework.data.domain.Pageable; 8 | import org.springframework.stereotype.Service; 9 | 10 | @Service 11 | public class ProductServiceImpl implements ProductService { 12 | private ProductRepository productRepository; 13 | 14 | @Autowired 15 | public void setProductRepository(ProductRepository productRepository) { 16 | this.productRepository = productRepository; 17 | } 18 | 19 | @Override 20 | public Iterable listAllProducts() { 21 | return productRepository.findAll(); 22 | } 23 | 24 | @Override 25 | public Product getProductById(Integer id) { 26 | return productRepository.findOne(id); 27 | } 28 | 29 | @Override 30 | public Product saveProduct(Product product) { 31 | return productRepository.save(product); 32 | } 33 | 34 | @Override 35 | public void deleteProduct(Integer id) { 36 | productRepository.delete(id); 37 | } 38 | 39 | @Override 40 | public Page findAll(Pageable pageable) { 41 | return productRepository.findAll(pageable); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #logging.level.org.h2.server: DEBUG 2 | -------------------------------------------------------------------------------- /src/main/resources/static/css/guru.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mtiger2k/pageableSpringBootDataJPA/5e6342047e3f7b226afc8a0d600c1eae86e3595c/src/main/resources/static/css/guru.css -------------------------------------------------------------------------------- /src/main/resources/static/images/FBcover1200x628.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mtiger2k/pageableSpringBootDataJPA/5e6342047e3f7b226afc8a0d600c1eae86e3595c/src/main/resources/static/images/FBcover1200x628.png -------------------------------------------------------------------------------- /src/main/resources/static/images/NewBannerBOOTS_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mtiger2k/pageableSpringBootDataJPA/5e6342047e3f7b226afc8a0d600c1eae86e3595c/src/main/resources/static/images/NewBannerBOOTS_2.png -------------------------------------------------------------------------------- /src/main/resources/templates/fragments/header.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | Home 14 | 15 | Products 16 | Create Product 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | Spring Framework Guru 27 | 28 | Spring Boot Web App 29 | 30 | 31 | 32 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /src/main/resources/templates/fragments/headerinc.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 8 | 9 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/main/resources/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Spring Framework Guru 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/main/resources/templates/productform.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Spring Framework Guru 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | Product Details 14 | 15 | 16 | 17 | 18 | 19 | Description: 20 | 21 | 22 | 23 | 24 | 25 | Price: 26 | 27 | 28 | 29 | 30 | 31 | Image Url: 32 | 33 | 34 | 35 | 36 | 37 | Submit 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /src/main/resources/templates/products.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Spring Framework Guru 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | Product List 14 | 15 | 16 | Id 17 | Product Id 18 | Description 19 | Price 20 | View 21 | Edit 22 | Delete 23 | 24 | 25 | Id 26 | Product Id 27 | descirption 28 | price 29 | View 30 | Edit 31 | Delete 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | ← First 43 | ← First 44 | 45 | 46 | « 47 | « 48 | 49 | 50 | 1 51 | 1 52 | 53 | 54 | » 55 | » 56 | 57 | 58 | Last → 59 | Last → 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /src/main/resources/templates/productshow.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Spring Framework Guru 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | Product Details 14 | 15 | 16 | 17 | Product Id: 18 | 19 | Product Id 20 | 21 | 22 | Description: 23 | 24 | description 25 | 26 | 27 | 28 | Price: 29 | 30 | Priceaddd 31 | 32 | 33 | 34 | Image Url: 35 | 36 | url.... 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /src/test/java/guru/springframework/SpringBootWebApplicationTests.java: -------------------------------------------------------------------------------- 1 | package guru.springframework; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.test.context.web.WebAppConfiguration; 6 | import org.springframework.boot.test.SpringApplicationConfiguration; 7 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 8 | 9 | @RunWith(SpringJUnit4ClassRunner.class) 10 | @SpringApplicationConfiguration(classes = SpringBootWebApplication.class) 11 | @WebAppConfiguration 12 | public class SpringBootWebApplicationTests { 13 | 14 | @Test 15 | public void contextLoads() { 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/guru/springframework/configuration/RepositoryConfiguration.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.configuration; 2 | 3 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 4 | import org.springframework.boot.orm.jpa.EntityScan; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 7 | import org.springframework.transaction.annotation.EnableTransactionManagement; 8 | 9 | @Configuration 10 | @EnableAutoConfiguration 11 | @EntityScan(basePackages = {"guru.springframework.domain"}) 12 | @EnableJpaRepositories(basePackages = {"guru.springframework.repositories"}) 13 | @EnableTransactionManagement 14 | public class RepositoryConfiguration { 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/guru/springframework/repositories/ProductRepositoryTest.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.repositories; 2 | 3 | import guru.springframework.configuration.RepositoryConfiguration; 4 | import guru.springframework.domain.Product; 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.boot.test.SpringApplicationConfiguration; 9 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 10 | 11 | import java.math.BigDecimal; 12 | 13 | import static org.junit.Assert.assertEquals; 14 | import static org.junit.Assert.assertNotNull; 15 | import static org.junit.Assert.assertNull; 16 | 17 | @RunWith(SpringJUnit4ClassRunner.class) 18 | @SpringApplicationConfiguration(classes = {RepositoryConfiguration.class}) 19 | public class ProductRepositoryTest { 20 | 21 | private ProductRepository productRepository; 22 | 23 | @Autowired 24 | public void setProductRepository(ProductRepository productRepository) { 25 | this.productRepository = productRepository; 26 | } 27 | 28 | @Test 29 | public void testSaveProduct(){ 30 | //setup product 31 | Product product = new Product(); 32 | product.setDescription("Spring Framework Guru Shirt"); 33 | product.setPrice(new BigDecimal("18.95")); 34 | product.setProductId("1234"); 35 | 36 | //save product, verify has ID value after save 37 | assertNull(product.getId()); //null before save 38 | productRepository.save(product); 39 | assertNotNull(product.getId()); //not null after save 40 | 41 | //fetch from DB 42 | Product fetchedProduct = productRepository.findOne(product.getId()); 43 | 44 | //should not be null 45 | assertNotNull(fetchedProduct); 46 | 47 | //should equal 48 | assertEquals(product.getId(), fetchedProduct.getId()); 49 | assertEquals(product.getDescription(), fetchedProduct.getDescription()); 50 | 51 | //update description and save 52 | fetchedProduct.setDescription("New Description"); 53 | productRepository.save(fetchedProduct); 54 | 55 | //get from DB, should be updated 56 | Product fetchedUpdatedProduct = productRepository.findOne(fetchedProduct.getId()); 57 | assertEquals(fetchedProduct.getDescription(), fetchedUpdatedProduct.getDescription()); 58 | 59 | //verify count of products in DB 60 | long productCount = productRepository.count(); 61 | assertEquals(productCount, 1); 62 | 63 | //get all products, list should only have one 64 | Iterable products = productRepository.findAll(); 65 | 66 | int count = 0; 67 | 68 | for(Product p : products){ 69 | count++; 70 | } 71 | 72 | assertEquals(count, 1); 73 | } 74 | } 75 | --------------------------------------------------------------------------------
Product Id
description
Priceaddd
url....