├── .idea ├── .name ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── modules.xml ├── libraries │ ├── Maven__ognl_ognl_3_0_8.xml │ ├── Maven__junit_junit_4_12.xml │ ├── Maven__antlr_antlr_2_7_7.xml │ ├── Maven__dom4j_dom4j_1_6_1.xml │ ├── Maven__com_alibaba_druid_1_0_11.xml │ ├── Maven__org_yaml_snakeyaml_1_14.xml │ ├── Maven__redis_clients_jedis_2_5_2.xml │ ├── Maven__xml_apis_xml_apis_1_0_b2.xml │ ├── Maven__org_objenesis_objenesis_2_1.xml │ ├── Maven__aopalliance_aopalliance_1_0.xml │ ├── Maven__org_aspectj_aspectjrt_1_8_6.xml │ ├── Maven__org_slf4j_slf4j_api_1_7_12.xml │ ├── Maven__com_fasterxml_classmate_1_0_0.xml │ ├── Maven__org_jboss_jandex_1_1_0_Final.xml │ ├── Maven__org_hamcrest_hamcrest_core_1_3.xml │ ├── Maven__org_slf4j_jul_to_slf4j_1_7_12.xml │ ├── Maven__org_aspectj_aspectjweaver_1_8_6.xml │ ├── Maven__ch_qos_logback_logback_core_1_1_3.xml │ ├── Maven__org_mockito_mockito_core_1_10_19.xml │ ├── Maven__org_slf4j_jcl_over_slf4j_1_7_12.xml │ ├── Maven__org_hamcrest_hamcrest_library_1_3.xml │ ├── Maven__org_javassist_javassist_3_18_1_GA.xml │ ├── Maven__org_apache_commons_commons_pool2_2_2.xml │ ├── Maven__org_apache_tomcat_tomcat_jdbc_8_0_23.xml │ ├── Maven__org_apache_tomcat_tomcat_juli_8_0_23.xml │ ├── Maven__org_slf4j_log4j_over_slf4j_1_7_12.xml │ ├── Maven__ch_qos_logback_logback_classic_1_1_3.xml │ ├── Maven__mysql_mysql_connector_java_5_1_35.xml │ ├── Maven__org_thymeleaf_thymeleaf_2_1_4_RELEASE.xml │ ├── Maven__org_unbescape_unbescape_1_1_0_RELEASE.xml │ ├── Maven__org_jboss_logging_jboss_logging_3_1_3_GA.xml │ ├── Maven__com_fasterxml_jackson_core_jackson_core_2_4_6.xml │ ├── Maven__org_hibernate_hibernate_core_4_3_10_Final.xml │ ├── Maven__org_springframework_spring_tx_4_1_7_RELEASE.xml │ ├── Maven__javax_transaction_javax_transaction_api_1_2.xml │ ├── Maven__javax_validation_validation_api_1_1_0_Final.xml │ ├── Maven__org_apache_tomcat_embed_tomcat_embed_el_8_0_23.xml │ ├── Maven__org_springframework_spring_aop_4_1_7_RELEASE.xml │ ├── Maven__org_springframework_spring_orm_4_1_7_RELEASE.xml │ ├── Maven__org_springframework_spring_web_4_1_7_RELEASE.xml │ ├── Maven__org_springframework_spring_core_4_1_7_RELEASE.xml │ ├── Maven__org_springframework_spring_jdbc_4_1_7_RELEASE.xml │ ├── Maven__org_springframework_spring_test_4_1_7_RELEASE.xml │ ├── Maven__com_fasterxml_jackson_core_jackson_databind_2_4_6.xml │ ├── Maven__org_springframework_spring_beans_4_1_7_RELEASE.xml │ ├── Maven__org_apache_tomcat_embed_tomcat_embed_core_8_0_23.xml │ ├── Maven__org_springframework_spring_webmvc_4_1_7_RELEASE.xml │ ├── Maven__org_hibernate_hibernate_validator_5_1_3_Final.xml │ ├── Maven__org_thymeleaf_thymeleaf_spring4_2_1_4_RELEASE.xml │ ├── Maven__org_springframework_boot_spring_boot_1_2_5_RELEASE.xml │ ├── Maven__org_springframework_spring_aspects_4_1_7_RELEASE.xml │ ├── Maven__org_springframework_spring_context_4_1_7_RELEASE.xml │ ├── Maven__com_fasterxml_jackson_core_jackson_annotations_2_4_6.xml │ ├── Maven__org_springframework_spring_expression_4_1_7_RELEASE.xml │ ├── Maven__org_apache_tomcat_embed_tomcat_embed_websocket_8_0_23.xml │ ├── Maven__nz_net_ultraq_thymeleaf_thymeleaf_layout_dialect_1_2_9.xml │ ├── Maven__org_hibernate_hibernate_entitymanager_4_3_10_Final.xml │ ├── Maven__org_springframework_data_spring_data_jpa_1_7_3_RELEASE.xml │ ├── Maven__org_apache_tomcat_embed_tomcat_embed_logging_juli_8_0_23.xml │ ├── Maven__org_springframework_data_spring_data_redis_1_4_3_RELEASE.xml │ ├── Maven__org_jboss_logging_jboss_logging_annotations_1_2_0_Beta1.xml │ ├── Maven__org_springframework_boot_spring_boot_starter_1_2_5_RELEASE.xml │ ├── Maven__org_springframework_data_spring_data_commons_1_9_3_RELEASE.xml │ ├── Maven__org_springframework_spring_context_support_4_1_7_RELEASE.xml │ ├── Maven__org_springframework_boot_spring_boot_starter_aop_1_2_5_RELEASE.xml │ ├── Maven__org_springframework_boot_spring_boot_starter_web_1_2_5_RELEASE.xml │ ├── Maven__org_hibernate_javax_persistence_hibernate_jpa_2_1_api_1_0_0_Final.xml │ ├── Maven__org_springframework_boot_spring_boot_starter_jdbc_1_2_5_RELEASE.xml │ ├── Maven__org_springframework_boot_spring_boot_starter_test_1_2_5_RELEASE.xml │ ├── Maven__org_hibernate_common_hibernate_commons_annotations_4_0_5_Final.xml │ ├── Maven__org_springframework_boot_spring_boot_autoconfigure_1_2_5_RELEASE.xml │ ├── Maven__org_springframework_boot_spring_boot_starter_redis_1_2_5_RELEASE.xml │ ├── Maven__org_springframework_boot_spring_boot_starter_tomcat_1_2_5_RELEASE.xml │ ├── Maven__org_springframework_boot_spring_boot_starter_logging_1_2_5_RELEASE.xml │ ├── Maven__org_springframework_boot_spring_boot_starter_data_jpa_1_2_5_RELEASE.xml │ └── Maven__org_springframework_boot_spring_boot_starter_thymeleaf_1_2_5_RELEASE.xml ├── compiler.xml └── misc.xml ├── src ├── main │ ├── resources │ │ ├── application.properties │ │ ├── templates │ │ │ ├── testredis.html │ │ │ ├── result.html │ │ │ ├── listall.html │ │ │ ├── index.html │ │ │ ├── input.html │ │ │ └── listpage.html │ │ └── application-dev.properties │ └── java │ │ └── com │ │ └── jvmhub │ │ └── springboot │ │ ├── repository │ │ ├── PostRepository.java │ │ └── ThingRepository.java │ │ ├── form │ │ └── Post.java │ │ ├── service │ │ ├── CacheDemoService.java │ │ └── impl │ │ │ └── CacheDemoServiceImpl.java │ │ ├── validate │ │ ├── Money.java │ │ └── MoneyValidator.java │ │ ├── SpringBootThymeleafApplication.java │ │ ├── domain │ │ ├── PostEntity.java │ │ ├── Post.java │ │ └── Thing.java │ │ ├── aspect │ │ └── ExecutionTimeLogger.java │ │ ├── redis │ │ ├── RedisConfig.java │ │ └── RedisUtil.java │ │ ├── druid │ │ └── DruidConfiguration.java │ │ └── controller │ │ └── Home.java └── test │ └── java │ └── com │ └── jvmhub │ └── springboot │ └── SpringBootThymeleafApplicationTests.java ├── bootdemo.log ├── README.md ├── .project ├── pom.xml ├── pom.xml.bak ├── .classpath └── spring-boot-thymeleaf-jpa.iml /.idea/.name: -------------------------------------------------------------------------------- 1 | spring-boot-thymeleaf-jpa -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.profiles.active=dev -------------------------------------------------------------------------------- /src/main/resources/templates/testredis.html: -------------------------------------------------------------------------------- 1 | hello 2 |
-------------------------------------------------------------------------------- /bootdemo.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiangzhongke/spring-boot-thymeleaf-jpa/HEAD/bootdemo.log -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Spring-Boot-with-Thymeleaf-part-3-Spring-Data-JPA 2 | Spring Boot with Thymeleaf tutorial, part 3 - Spring Data JPA All tutorial you can find on jvmhub.com -> http://jvmhub.com/2015/08/09/spring-boot-with-thymeleaf-tutorial-part-3-spring-data-jpa/ 3 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/main/java/com/jvmhub/springboot/repository/PostRepository.java: -------------------------------------------------------------------------------- 1 | package com.jvmhub.springboot.repository; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.cache.annotation.Cacheable; 6 | import org.springframework.data.repository.CrudRepository; 7 | 8 | import com.jvmhub.springboot.domain.Post; 9 | 10 | 11 | 12 | public interface PostRepository extends CrudRepository { 13 | List findByTitle(String title); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__ognl_ognl_3_0_8.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__junit_junit_4_12.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__antlr_antlr_2_7_7.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__dom4j_dom4j_1_6_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | spring-boot-thymeleaf-jpa 4 | SpringBoot Portal. NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse. 5 | 6 | 7 | 8 | org.eclipse.jdt.core.javabuilder 9 | 10 | 11 | 12 | org.eclipse.jdt.core.javanature 13 | 14 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__com_alibaba_druid_1_0_11.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_yaml_snakeyaml_1_14.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__redis_clients_jedis_2_5_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__xml_apis_xml_apis_1_0_b2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_objenesis_objenesis_2_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__aopalliance_aopalliance_1_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_aspectj_aspectjrt_1_8_6.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_slf4j_slf4j_api_1_7_12.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__com_fasterxml_classmate_1_0_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_jboss_jandex_1_1_0_Final.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/main/java/com/jvmhub/springboot/form/Post.java: -------------------------------------------------------------------------------- 1 | package com.jvmhub.springboot.form; 2 | 3 | import javax.validation.constraints.Size; 4 | 5 | public class Post { 6 | 7 | @Size(min=4, max=35) 8 | private String title; 9 | 10 | @Size(min=30, max= 1000) 11 | private String content; 12 | 13 | public String getTitle() { 14 | return title; 15 | } 16 | 17 | public void setTitle(String title) { 18 | this.title = title; 19 | } 20 | 21 | public String getContent() { 22 | return content; 23 | } 24 | 25 | public void setContent(String content) { 26 | this.content = content; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_slf4j_jul_to_slf4j_1_7_12.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_aspectj_aspectjweaver_1_8_6.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__ch_qos_logback_logback_core_1_1_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_mockito_mockito_core_1_10_19.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_slf4j_jcl_over_slf4j_1_7_12.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_hamcrest_hamcrest_library_1_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_javassist_javassist_3_18_1_GA.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_apache_commons_commons_pool2_2_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_apache_tomcat_tomcat_jdbc_8_0_23.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_apache_tomcat_tomcat_juli_8_0_23.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_slf4j_log4j_over_slf4j_1_7_12.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__ch_qos_logback_logback_classic_1_1_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__mysql_mysql_connector_java_5_1_35.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/main/resources/templates/result.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Spring Boot and Thymeleaf example 5 | 6 | 7 | 8 |

Spring Boot and Thymeleaf, part 3 - SPRING DATA JPA

9 |

10 |

ID:

11 |
12 |

Title:

13 |
14 |

Content:

15 |
16 |
---------------------------------------------------------
17 |

18 | 19 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_thymeleaf_thymeleaf_2_1_4_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_unbescape_unbescape_1_1_0_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_jboss_logging_jboss_logging_3_1_3_GA.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/main/java/com/jvmhub/springboot/service/CacheDemoService.java: -------------------------------------------------------------------------------- 1 | package com.jvmhub.springboot.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.data.domain.Page; 6 | import org.springframework.data.domain.Pageable; 7 | 8 | import com.jvmhub.springboot.domain.Thing; 9 | 10 | 11 | 12 | public interface CacheDemoService { 13 | public void create(Thing thing); 14 | public Thing findById(Long id); 15 | public Iterable findAll(); 16 | public Iterable findByTitle(String title); 17 | public Thing update(Thing thing); 18 | public void delete(Long id); 19 | 20 | public Page findByPage(Pageable pageable); 21 | } 22 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_core_2_4_6.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_hibernate_hibernate_core_4_3_10_Final.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_tx_4_1_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__javax_transaction_javax_transaction_api_1_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__javax_validation_validation_api_1_1_0_Final.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_apache_tomcat_embed_tomcat_embed_el_8_0_23.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_aop_4_1_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_orm_4_1_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_web_4_1_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_core_4_1_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_jdbc_4_1_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_test_4_1_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/main/java/com/jvmhub/springboot/validate/Money.java: -------------------------------------------------------------------------------- 1 | package com.jvmhub.springboot.validate; 2 | 3 | 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | import javax.validation.Constraint; 10 | import javax.validation.Payload; 11 | 12 | 13 | @Target({ElementType.FIELD, ElementType.METHOD}) 14 | @Retention(RetentionPolicy.RUNTIME) 15 | @Constraint(validatedBy=MoneyValidator.class) 16 | public @interface Money { 17 | 18 | String message() default"不是金额形式"; 19 | 20 | Class[] groups() default {}; 21 | 22 | Class[] payload() default {}; 23 | 24 | } -------------------------------------------------------------------------------- /src/main/resources/templates/listall.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Spring Boot and Thymeleaf example 5 | 6 | 7 | 8 |

Spring Boot and Thymeleaf, part 3 - SPRING DATA JPA

9 |

10 |

ID:

11 |
12 |

Title:

13 |
14 |

Content:

15 |
16 | 删除 17 |
---------------------------------------------------------
18 |

19 | 20 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_databind_2_4_6.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_beans_4_1_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_apache_tomcat_embed_tomcat_embed_core_8_0_23.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_webmvc_4_1_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_hibernate_hibernate_validator_5_1_3_Final.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_thymeleaf_thymeleaf_spring4_2_1_4_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_boot_spring_boot_1_2_5_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_aspects_4_1_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_context_4_1_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_annotations_2_4_6.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_expression_4_1_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_apache_tomcat_embed_tomcat_embed_websocket_8_0_23.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__nz_net_ultraq_thymeleaf_thymeleaf_layout_dialect_1_2_9.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_hibernate_hibernate_entitymanager_4_3_10_Final.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_data_spring_data_jpa_1_7_3_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_apache_tomcat_embed_tomcat_embed_logging_juli_8_0_23.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_data_spring_data_redis_1_4_3_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_jboss_logging_jboss_logging_annotations_1_2_0_Beta1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_1_2_5_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_data_spring_data_commons_1_9_3_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_context_support_4_1_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_aop_1_2_5_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_web_1_2_5_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_hibernate_javax_persistence_hibernate_jpa_2_1_api_1_0_0_Final.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_jdbc_1_2_5_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_test_1_2_5_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_hibernate_common_hibernate_commons_annotations_4_0_5_Final.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_boot_spring_boot_autoconfigure_1_2_5_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_redis_1_2_5_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_tomcat_1_2_5_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_logging_1_2_5_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_data_jpa_1_2_5_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_thymeleaf_1_2_5_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/main/java/com/jvmhub/springboot/validate/MoneyValidator.java: -------------------------------------------------------------------------------- 1 | package com.jvmhub.springboot.validate; 2 | 3 | 4 | import java.math.BigDecimal; 5 | import java.util.regex.Pattern; 6 | 7 | import javax.validation.ConstraintValidator; 8 | import javax.validation.ConstraintValidatorContext; 9 | public class MoneyValidator implements ConstraintValidator { 10 | 11 | private String moneyReg = "^\\d+(\\.\\d{1,2})?$";//表示金额的正则表达式 12 | private Pattern moneyPattern = Pattern.compile(moneyReg); 13 | 14 | public void initialize(Money money) { 15 | // TODO Auto-generated method stub 16 | 17 | } 18 | 19 | public boolean isValid(BigDecimal value, ConstraintValidatorContext arg1) { 20 | // TODO Auto-generated method stub 21 | if (value == null) 22 | return true; 23 | return moneyPattern.matcher(value.toString()).matches(); 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /src/main/java/com/jvmhub/springboot/SpringBootThymeleafApplication.java: -------------------------------------------------------------------------------- 1 | package com.jvmhub.springboot; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.context.annotation.ComponentScan; 7 | import org.springframework.context.annotation.Configuration; 8 | import org.springframework.context.annotation.EnableAspectJAutoProxy; 9 | import org.springframework.context.annotation.ImportResource; 10 | import org.springframework.scheduling.annotation.EnableScheduling; 11 | 12 | @SpringBootApplication 13 | @EnableAspectJAutoProxy 14 | public class SpringBootThymeleafApplication { 15 | 16 | public static void main(String[] args) { 17 | SpringApplication.run(SpringBootThymeleafApplication.class, args); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/resources/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Spring Boot and Thymeleaf example 5 | 6 | 7 | 8 |

Spring Boot and Thymeleaf, part 2 - forms

9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 |
Title:Title error message
Content:Content error message
25 |
26 | 27 | -------------------------------------------------------------------------------- /src/main/resources/templates/input.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Spring Boot and Thymeleaf example 5 | 6 | 7 | 8 |

Spring Boot and Thymeleaf, part 2 - forms

9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 |
Title:Title error message
Content:Content error message
25 |
26 | 27 | -------------------------------------------------------------------------------- /src/main/resources/templates/listpage.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Spring Boot and Thymeleaf example 5 | 6 | 7 | 8 |

Spring Boot and Thymeleaf, part 3 - SPRING DATA JPA

9 | 10 |
11 |
12 |
13 |
14 |
15 |
16 | 17 | 18 | 19 |

20 |

ID:

21 |
22 |

Title:

23 |
24 |

Content:

25 |
26 | 删除 27 |
---------------------------------------------------------
28 |

29 | 30 | -------------------------------------------------------------------------------- /src/main/java/com/jvmhub/springboot/domain/PostEntity.java: -------------------------------------------------------------------------------- 1 | package com.jvmhub.springboot.domain; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.GeneratedValue; 5 | import javax.persistence.GenerationType; 6 | import javax.persistence.Id; 7 | 8 | @Entity 9 | public class PostEntity { 10 | 11 | public PostEntity() {} 12 | 13 | public PostEntity(String title, String content) { 14 | this.title = title; 15 | this.content = content; 16 | } 17 | 18 | @Id 19 | @GeneratedValue(strategy = GenerationType.AUTO) 20 | private int id; 21 | 22 | private String title; 23 | 24 | private String content; 25 | 26 | public int getId() { 27 | return id; 28 | } 29 | public void setId(int id) { 30 | this.id = id; 31 | } 32 | public String getTitle() { 33 | return title; 34 | } 35 | public void setTitle(String title) { 36 | this.title = title; 37 | } 38 | public String getContent() { 39 | return content; 40 | } 41 | public void setContent(String content) { 42 | this.content = content; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/jvmhub/springboot/domain/Post.java: -------------------------------------------------------------------------------- 1 | package com.jvmhub.springboot.domain; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.GeneratedValue; 5 | import javax.persistence.GenerationType; 6 | import javax.persistence.Id; 7 | import javax.validation.constraints.Size; 8 | 9 | @Entity 10 | public class Post { 11 | 12 | public Post() {} 13 | 14 | public Post(String title, String content) { 15 | this.title = title; 16 | this.content = content; 17 | } 18 | 19 | @Id 20 | @GeneratedValue(strategy = GenerationType.AUTO) 21 | private int id; 22 | 23 | @Size(min=4, max=35) 24 | private String title; 25 | 26 | @Size(min=30, max= 1000) 27 | private String content; 28 | 29 | public int getId() { 30 | return id; 31 | } 32 | public void setId(int id) { 33 | this.id = id; 34 | } 35 | public String getTitle() { 36 | return title; 37 | } 38 | public void setTitle(String title) { 39 | this.title = title; 40 | } 41 | public String getContent() { 42 | return content; 43 | } 44 | public void setContent(String content) { 45 | this.content = content; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/jvmhub/springboot/repository/ThingRepository.java: -------------------------------------------------------------------------------- 1 | package com.jvmhub.springboot.repository; 2 | 3 | import java.util.Date; 4 | import java.util.Iterator; 5 | import java.util.List; 6 | 7 | import org.springframework.data.domain.Page; 8 | import org.springframework.data.domain.Pageable; 9 | import org.springframework.data.jpa.repository.JpaRepository; 10 | import org.springframework.data.jpa.repository.Query; 11 | import org.springframework.data.repository.CrudRepository; 12 | import org.springframework.data.repository.PagingAndSortingRepository; 13 | 14 | import com.jvmhub.springboot.domain.Thing; 15 | 16 | public interface ThingRepository extends JpaRepository { 17 | @Query("select u from Thing u where u.title like ?1%") 18 | Iterable findByTitle(String title); 19 | //自動生成,無序實現 20 | List findByContentLike(String content); 21 | List findByDatefieldGreaterThan(Date datefield); 22 | List findByDatefieldBetween(Date begindate,Date enddate); 23 | 24 | //返回部分字段 25 | @Query(value="select new Thing(u.id,u.title) from Thing u") 26 | Page findPartFields(Pageable pageable); 27 | 28 | 29 | } 30 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /src/main/java/com/jvmhub/springboot/aspect/ExecutionTimeLogger.java: -------------------------------------------------------------------------------- 1 | package com.jvmhub.springboot.aspect; 2 | 3 | import org.aspectj.lang.ProceedingJoinPoint; 4 | import org.aspectj.lang.annotation.After; 5 | import org.aspectj.lang.annotation.Around; 6 | import org.aspectj.lang.annotation.Aspect; 7 | import org.aspectj.lang.annotation.Before; 8 | import org.aspectj.lang.annotation.Pointcut; 9 | import org.slf4j.Logger; 10 | import org.slf4j.LoggerFactory; 11 | import org.springframework.stereotype.Component; 12 | import org.springframework.util.StopWatch; 13 | 14 | @Aspect 15 | @Component 16 | public class ExecutionTimeLogger { 17 | 18 | private Logger logger; 19 | 20 | public ExecutionTimeLogger() { 21 | logger = LoggerFactory.getLogger(getClass()); 22 | logger.info("HEY"); 23 | } 24 | 25 | //@Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)") 26 | //public void requestMapping() {} 27 | 28 | //@Pointcut("execution(* com.jvmhub.springboot.service.impl.*ServiceImpl.*(..))") 29 | @Pointcut("execution(* com.jvmhub.springboot.repository..*.*(..))") 30 | public void methodPointcut() {} 31 | 32 | 33 | //@Around("requestMapping() && methodPointcut()") 34 | @Around("methodPointcut()") 35 | public Object profile(ProceedingJoinPoint pjp) throws Throwable { 36 | StopWatch sw = new StopWatch(); 37 | String name = pjp.getSignature().getName(); 38 | try { 39 | sw.start(); 40 | return pjp.proceed(); 41 | } finally { 42 | sw.stop(); 43 | logger.info("STOPWATCH: " + sw.getTotalTimeMillis() + " - " + name); 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.jvmhub 7 | spring-boot-thymeleaf-jpa 8 | 0.0.1 9 | jar 10 | 11 | bootportal 12 | SpringBoot Portal 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.2.5.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | 1.7 24 | 25 | 1.0.11 26 | 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-thymeleaf 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-test 36 | test 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-data-jpa 41 | 42 | 43 | 44 | mysql 45 | mysql-connector-java 46 | 47 | 48 | 49 | org.springframework.boot 50 | spring-boot-starter-redis 51 | 52 | 53 | com.alibaba 54 | druid 55 | ${druid.version} 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | org.springframework.boot 64 | spring-boot-maven-plugin 65 | 66 | 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /pom.xml.bak: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.jvmhub 7 | spring-boot-thymeleaf-jpa 8 | 0.0.1 9 | jar 10 | 11 | bootportal 12 | SpringBoot Portal 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.2.5.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | 1.8 24 | 25 | 1.0.11 26 | 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-thymeleaf 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-test 36 | test 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-data-jpa 41 | 42 | 43 | 44 | mysql 45 | mysql-connector-java 46 | 47 | 48 | 49 | org.springframework.boot 50 | spring-boot-starter-redis 51 | 52 | 53 | com.alibaba 54 | druid 55 | ${druid.version} 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | org.springframework.boot 64 | spring-boot-maven-plugin 65 | 66 | 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 1.7 37 | 38 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /src/main/java/com/jvmhub/springboot/service/impl/CacheDemoServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.jvmhub.springboot.service.impl; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.cache.annotation.CacheEvict; 7 | import org.springframework.cache.annotation.CachePut; 8 | import org.springframework.cache.annotation.Cacheable; 9 | import org.springframework.data.domain.Page; 10 | import org.springframework.data.domain.Pageable; 11 | import org.springframework.stereotype.Service; 12 | 13 | import com.jvmhub.springboot.domain.Thing; 14 | import com.jvmhub.springboot.repository.ThingRepository; 15 | import com.jvmhub.springboot.service.CacheDemoService; 16 | 17 | @Service 18 | public class CacheDemoServiceImpl implements CacheDemoService { 19 | 20 | /** 21 | * 缓存的key 22 | */ 23 | public static final String THING_ALL_KEY = "\"thing_all\""; 24 | /** 25 | * value属性表示使用哪个缓存策略,缓存策略在ehcache.xml 26 | */ 27 | public static final String DEMO_CACHE_NAME = "demo"; 28 | 29 | @Autowired 30 | public ThingRepository data; 31 | 32 | @CacheEvict(value = DEMO_CACHE_NAME, key = THING_ALL_KEY) 33 | @Override 34 | public void create(Thing thing) { 35 | data.save(thing); 36 | } 37 | 38 | @Cacheable(value = DEMO_CACHE_NAME, key = "#thing.getId()+'thing'") 39 | @Override 40 | public Thing findById(Long id) { 41 | System.err.println("没有走缓存!" + id); 42 | return data.findOne(id); 43 | } 44 | 45 | @Cacheable(value = DEMO_CACHE_NAME, key = THING_ALL_KEY) 46 | @Override 47 | public Iterable findAll() { 48 | return data.findAll(); 49 | } 50 | 51 | //@Cacheable(value = DEMO_CACHE_NAME, key = THING_ALL_KEY) 52 | @Override 53 | public Iterable findByTitle(String title) { 54 | return data.findByTitle(title); 55 | } 56 | 57 | @Override 58 | @CachePut(value = DEMO_CACHE_NAME, key = "#thing.getId()+'thing'") 59 | @CacheEvict(value = DEMO_CACHE_NAME, key = THING_ALL_KEY) 60 | public Thing update(Thing thing) { 61 | System.out.println(thing); 62 | data.save(thing); 63 | 64 | return thing; 65 | } 66 | 67 | @CacheEvict(value = DEMO_CACHE_NAME,key = THING_ALL_KEY ) 68 | @Override 69 | public void delete(Long id) { 70 | data.delete(id); 71 | 72 | } 73 | 74 | public Page findByPage(Pageable pageable){ 75 | return data.findAll(pageable); 76 | } 77 | 78 | 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/main/resources/application-dev.properties: -------------------------------------------------------------------------------- 1 | #spring data source config 2 | spring.datasource.url=jdbc:mysql://localhost:3306/test 3 | spring.datasource.username=root 4 | spring.datasource.password=admin 5 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver 6 | 7 | spring.datasource.initialsize=10 8 | # Number of ms to wait before throwing an exception if no connection is available. 9 | spring.datasource.max-wait=10 10 | # Maximum number of active connections that can be allocated from this pool at the same time. 11 | spring.datasource.max-active=50 12 | # Validate the connection before borrowing it from the pool. 13 | spring.datasource.test-on-borrow=true 14 | 15 | # Specify the DBMS 16 | spring.jpa.database = MYSQL 17 | spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect 18 | # Show or not log for each sql query 19 | spring.jpa.show-sql = true 20 | # Hibernate ddl auto (create, create-drop, update) 21 | spring.jpa.hibernate.ddl-auto = update 22 | # Naming strategy 23 | spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy 24 | # stripped before adding them to the entity manager) 25 | spring.jpa.properties.hibernate.dialect =org.hibernate.dialect.MySQL5Dialect 26 | 27 | 28 | # REDIS (RedisProperties) 29 | #spring.redis.database= 30 | spring.redis.host= 31 | spring.redis.password= 32 | spring.redis.port=6379 33 | spring.redis.pool.max-idle=100 34 | spring.redis.pool.min-idle=1 35 | spring.redis.pool.max-active=1000 36 | spring.redis.pool.max-wait=-1 37 | # name of Redis server 38 | #spring.redis.sentinel.master= 39 | # comma-separated list of host:port pairs 40 | #spring.redis.sentinel.nodes= localhost:6379 41 | #spring.data.mongodb.uri=mongodb://test:test@localhost:27017/test 42 | #web servlet config 43 | server.port=8088 44 | 45 | #logging 46 | #currently we use sl4j,Sentry is a better choice,if we want to realtime log 47 | #TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF 48 | logging.level.org.springframework.web=DEBUG 49 | logging.level.org.hibernate=ERROR 50 | logging.path=d:/logs 51 | logging.file=bootdemo.log 52 | #logging.config= # location of config file (default classpath:logback.xml for logback) 53 | #endpoints 54 | #endpoints.sensitive=true 55 | #endpoints.info.sensitive=false 56 | 57 | info.build.artifact=springbootdemo 58 | info.build.name=springboot 59 | info.build.description=springboot description 60 | info.build.version=version-1.3 61 | 62 | -------------------------------------------------------------------------------- /src/test/java/com/jvmhub/springboot/SpringBootThymeleafApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.jvmhub.springboot; 2 | 3 | import java.math.BigDecimal; 4 | import java.util.ArrayList; 5 | import java.util.Date; 6 | import java.util.List; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.springframework.test.context.web.WebAppConfiguration; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.boot.test.SpringApplicationConfiguration; 13 | import org.springframework.data.domain.Page; 14 | import org.springframework.data.domain.PageRequest; 15 | import org.springframework.data.domain.Pageable; 16 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 17 | 18 | import com.jvmhub.springboot.domain.Thing; 19 | import com.jvmhub.springboot.repository.ThingRepository; 20 | 21 | import static org.junit.Assert.*; 22 | 23 | @RunWith(SpringJUnit4ClassRunner.class) 24 | @SpringApplicationConfiguration(classes = SpringBootThymeleafApplication.class) 25 | @WebAppConfiguration 26 | public class SpringBootThymeleafApplicationTests { 27 | 28 | @Autowired 29 | ThingRepository thingrepo; 30 | 31 | @Test 32 | public void contextLoads() { 33 | System.out.println(thingrepo.count()); 34 | /* 35 | List vals = thingrepo.findByContentLike("abcd"); 36 | assertEquals(vals.size(),0); 37 | 38 | List vals2 = thingrepo.findByContentLike("%123456789%"); 39 | assertNotEquals(vals2.size(),0); 40 | 41 | List vals3 = thingrepo.findByDatefieldGreaterThan(new Date("2016/06/03 00:00:00")); 42 | assertNotEquals(vals3.size(),0); 43 | 44 | List vals4 = thingrepo.findByDatefieldBetween(new Date("2016/06/03 00:00:00"),new Date("2016/06/05 00:00:00")); 45 | assertNotEquals(vals4.size(),0); 46 | 47 | Page tmppage1 = thingrepo.findPartFields(new PageRequest(0,1)); 48 | assertNotEquals(tmppage1.getContent().size(),0); 49 | */ 50 | Thing tmpthing = new Thing("a1234","a1012345678901234567890123456789"); 51 | tmpthing.setPhone("32432432432"); 52 | tmpthing.setMoney(new BigDecimal("2")); 53 | thingrepo.save(tmpthing); 54 | //批量保存测试 55 | List tmpthinglist = new ArrayList(); 56 | tmpthinglist.add(new Thing("new1_4","newcontent1_012345678901234567890123456789")); 57 | tmpthinglist.add(new Thing("new2_4","newcontent2_012345678901234567890123456789")); 58 | Listtmpresultlist = thingrepo.save(tmpthinglist); 59 | assertNotEquals(tmpresultlist.size(),0); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/com/jvmhub/springboot/redis/RedisConfig.java: -------------------------------------------------------------------------------- 1 | package com.jvmhub.springboot.redis; 2 | 3 | import com.fasterxml.jackson.annotation.JsonAutoDetect; 4 | import com.fasterxml.jackson.annotation.PropertyAccessor; 5 | import com.fasterxml.jackson.databind.ObjectMapper; 6 | 7 | import org.springframework.cache.CacheManager; 8 | import org.springframework.cache.annotation.CachingConfigurerSupport; 9 | import org.springframework.cache.annotation.EnableCaching; 10 | import org.springframework.cache.interceptor.KeyGenerator; 11 | import org.springframework.context.annotation.Bean; 12 | import org.springframework.context.annotation.Configuration; 13 | import org.springframework.data.redis.cache.RedisCacheManager; 14 | import org.springframework.data.redis.connection.RedisConnectionFactory; 15 | import org.springframework.data.redis.core.RedisTemplate; 16 | import org.springframework.data.redis.core.StringRedisTemplate; 17 | import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; 18 | 19 | import java.lang.reflect.Method; 20 | 21 | 22 | @Configuration 23 | @EnableCaching 24 | public class RedisConfig extends CachingConfigurerSupport{ 25 | 26 | @Bean 27 | public KeyGenerator wiselyKeyGenerator(){ 28 | return new KeyGenerator() { 29 | @Override 30 | public Object generate(Object target, Method method, Object... params) { 31 | StringBuilder sb = new StringBuilder(); 32 | sb.append(target.getClass().getName()); 33 | sb.append(method.getName()); 34 | for (Object obj : params) { 35 | sb.append(obj.toString()); 36 | } 37 | return sb.toString(); 38 | } 39 | }; 40 | 41 | } 42 | 43 | @Bean 44 | public CacheManager cacheManager( 45 | @SuppressWarnings("rawtypes") RedisTemplate redisTemplate) { 46 | return new RedisCacheManager(redisTemplate); 47 | } 48 | 49 | @Bean 50 | public RedisTemplate redisTemplate( 51 | RedisConnectionFactory factory) { 52 | StringRedisTemplate template = new StringRedisTemplate(factory); 53 | Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); 54 | ObjectMapper om = new ObjectMapper(); 55 | om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 56 | om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); 57 | jackson2JsonRedisSerializer.setObjectMapper(om); 58 | template.setValueSerializer(jackson2JsonRedisSerializer); 59 | template.afterPropertiesSet(); 60 | return template; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/com/jvmhub/springboot/redis/RedisUtil.java: -------------------------------------------------------------------------------- 1 | package com.jvmhub.springboot.redis; 2 | 3 | import java.io.Serializable; 4 | import java.util.Set; 5 | import java.util.concurrent.TimeUnit; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.data.redis.core.RedisTemplate; 8 | import org.springframework.data.redis.core.ValueOperations; 9 | import org.springframework.stereotype.Component; 10 | 11 | /** 12 | * redicache 工具类 13 | * 14 | */ 15 | @SuppressWarnings("unchecked") 16 | @Component 17 | public class RedisUtil { 18 | @SuppressWarnings("rawtypes") 19 | @Autowired 20 | private RedisTemplate redisTemplate; 21 | 22 | /** 23 | * 批量删除对应的value 24 | * 25 | * @param keys 26 | */ 27 | public void remove(final String... keys) { 28 | for (String key : keys) { 29 | remove(key); 30 | } 31 | } 32 | 33 | /** 34 | * 批量删除key 35 | * 36 | * @param pattern 37 | */ 38 | public void removePattern(final String pattern) { 39 | Set keys = redisTemplate.keys(pattern); 40 | if (keys.size() > 0) 41 | redisTemplate.delete(keys); 42 | } 43 | 44 | /** 45 | * 删除对应的value 46 | * 47 | * @param key 48 | */ 49 | public void remove(final String key) { 50 | if (exists(key)) { 51 | redisTemplate.delete(key); 52 | } 53 | } 54 | 55 | /** 56 | * 判断缓存中是否有对应的value 57 | * 58 | * @param key 59 | * @return 60 | */ 61 | public boolean exists(final String key) { 62 | return redisTemplate.hasKey(key); 63 | } 64 | 65 | /** 66 | * 读取缓存 67 | * 68 | * @param key 69 | * @return 70 | */ 71 | public Object get(final String key) { 72 | Object result = null; 73 | ValueOperations operations = redisTemplate 74 | .opsForValue(); 75 | result = operations.get(key); 76 | return result; 77 | } 78 | 79 | /** 80 | * 写入缓存 81 | * 82 | * @param key 83 | * @param value 84 | * @return 85 | */ 86 | public boolean set(final String key, Object value) { 87 | boolean result = false; 88 | try { 89 | ValueOperations operations = redisTemplate 90 | .opsForValue(); 91 | operations.set(key, value); 92 | result = true; 93 | } catch (Exception e) { 94 | e.printStackTrace(); 95 | } 96 | return result; 97 | } 98 | 99 | /** 100 | * 写入缓存 101 | * 102 | * @param key 103 | * @param value 104 | * @return 105 | */ 106 | public boolean set(final String key, Object value, Long expireTime) { 107 | boolean result = false; 108 | try { 109 | ValueOperations operations = redisTemplate 110 | .opsForValue(); 111 | operations.set(key, value); 112 | redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); 113 | result = true; 114 | } catch (Exception e) { 115 | e.printStackTrace(); 116 | } 117 | return result; 118 | } 119 | } -------------------------------------------------------------------------------- /src/main/java/com/jvmhub/springboot/druid/DruidConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.jvmhub.springboot.druid; 2 | 3 | 4 | import com.alibaba.druid.pool.DruidDataSource; 5 | import com.alibaba.druid.support.http.StatViewServlet; 6 | import com.alibaba.druid.support.http.WebStatFilter; 7 | import org.springframework.beans.factory.annotation.Value; 8 | import org.springframework.boot.context.embedded.FilterRegistrationBean; 9 | import org.springframework.boot.context.embedded.ServletRegistrationBean; 10 | import org.springframework.context.annotation.Bean; 11 | import org.springframework.context.annotation.Configuration; 12 | 13 | import javax.sql.DataSource; 14 | import java.sql.SQLException; 15 | 16 | @Configuration 17 | public class DruidConfiguration { 18 | 19 | @Bean 20 | public ServletRegistrationBean druidServlet() { 21 | ServletRegistrationBean reg = new ServletRegistrationBean(); 22 | reg.setServlet(new StatViewServlet()); 23 | reg.addUrlMappings("/druid/*"); 24 | //reg.addInitParameter("allow", "127.0.0.1"); 25 | //reg.addInitParameter("deny",""); 26 | reg.addInitParameter("loginUsername", "admin"); 27 | reg.addInitParameter("loginPassword", "admin"); 28 | return reg; 29 | } 30 | 31 | @Bean 32 | public FilterRegistrationBean filterRegistrationBean() { 33 | FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); 34 | filterRegistrationBean.setFilter(new WebStatFilter()); 35 | filterRegistrationBean.addUrlPatterns("/*"); 36 | filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); 37 | return filterRegistrationBean; 38 | } 39 | 40 | 41 | @Bean 42 | public DataSource druidDataSource(@Value("${spring.datasource.driver-class-name}") String driver, 43 | @Value("${spring.datasource.url}") String url, 44 | @Value("${spring.datasource.username}") String username, 45 | @Value("${spring.datasource.password}") String password, 46 | @Value("${spring.datasource.initialsize}") int initialSize, 47 | @Value("${spring.datasource.max-wait}") int maxwait, 48 | @Value("${spring.datasource.max-active}") int maxactive, 49 | @Value("${spring.datasource.test-on-borrow}") boolean testonborrow 50 | ) { 51 | DruidDataSource druidDataSource = new DruidDataSource(); 52 | druidDataSource.setDriverClassName(driver); 53 | druidDataSource.setUrl(url); 54 | druidDataSource.setUsername(username); 55 | druidDataSource.setPassword(password); 56 | druidDataSource.setMaxWait(maxwait); 57 | druidDataSource.setMaxActive(maxactive); 58 | druidDataSource.setInitialSize(initialSize); 59 | druidDataSource.setTestOnBorrow(testonborrow); 60 | 61 | try { 62 | druidDataSource.setFilters("stat, wall"); 63 | } catch (SQLException e) { 64 | e.printStackTrace(); 65 | } 66 | return druidDataSource; 67 | } 68 | } -------------------------------------------------------------------------------- /src/main/java/com/jvmhub/springboot/domain/Thing.java: -------------------------------------------------------------------------------- 1 | package com.jvmhub.springboot.domain; 2 | 3 | import java.io.Serializable; 4 | import java.math.BigDecimal; 5 | import java.util.Date; 6 | 7 | import javax.persistence.Basic; 8 | import javax.persistence.Column; 9 | import javax.persistence.Entity; 10 | import javax.persistence.FetchType; 11 | import javax.persistence.GeneratedValue; 12 | import javax.persistence.GenerationType; 13 | import javax.persistence.Id; 14 | import javax.persistence.Index; 15 | import javax.persistence.Lob; 16 | import javax.persistence.Table; 17 | import javax.persistence.Temporal; 18 | import javax.persistence.TemporalType; 19 | import javax.persistence.Transient; 20 | import javax.validation.constraints.NotNull; 21 | import javax.validation.constraints.Pattern; 22 | import javax.validation.constraints.Size; 23 | 24 | import org.hibernate.validator.constraints.NotBlank; 25 | 26 | import com.jvmhub.springboot.validate.Money; 27 | 28 | 29 | @Entity 30 | @Table(name = "thing") 31 | public class Thing{ 32 | 33 | public Thing() {} 34 | public Thing(Long id,String title){ 35 | this.id = id; 36 | this.title = title; 37 | } 38 | public Thing(String title, String content) { 39 | this.title = title; 40 | this.content = content; 41 | } 42 | 43 | @Id 44 | @GeneratedValue(strategy = GenerationType.AUTO) 45 | private Long id; 46 | 47 | @NotBlank(message = "标题不能为空") 48 | @Column(name="title",unique=true,nullable=false) 49 | private String title; 50 | 51 | 52 | @Temporal(TemporalType.DATE) 53 | private Date datefield; 54 | 55 | @Temporal(TemporalType.TIME) 56 | private Date timefield; 57 | 58 | @Temporal(TemporalType.TIMESTAMP) 59 | private Date createtime; 60 | 61 | 62 | @Size(min=30, max= 1000) 63 | @Lob 64 | @Basic(fetch = FetchType.LAZY) 65 | private String content; 66 | 67 | 68 | @Pattern(regexp = "1([\\d]{10})|((\\+[0-9]{2,4})?\\(?[0-9]+\\)?-?)?[0-9]{7,8}") 69 | private String phone; 70 | 71 | 72 | @Money(message="标准的金额形式为xxx.xx") 73 | private BigDecimal money; 74 | 75 | public Date getDatefield() { 76 | return datefield; 77 | } 78 | public void setDatefield(Date datefield) { 79 | this.datefield = datefield; 80 | } 81 | public Date getTimefield() { 82 | return timefield; 83 | } 84 | public void setTimefield(Date timefield) { 85 | this.timefield = timefield; 86 | } 87 | public Date getCreatetime() { 88 | return createtime; 89 | } 90 | public void setCreatetime(Date createtime) { 91 | this.createtime = createtime; 92 | } 93 | public String getPhone() { 94 | return phone; 95 | } 96 | public void setPhone(String phone) { 97 | this.phone = phone; 98 | } 99 | public BigDecimal getMoney() { 100 | return money; 101 | } 102 | public void setMoney(BigDecimal money) { 103 | this.money = money; 104 | } 105 | public String getTransfield() { 106 | return transfield; 107 | } 108 | public void setTransfield(String transfield) { 109 | this.transfield = transfield; 110 | } 111 | 112 | @Transient 113 | private String transfield; 114 | 115 | public Long getId() { 116 | return id; 117 | } 118 | public void setId(Long id) { 119 | this.id = id; 120 | } 121 | public String getTitle() { 122 | return title; 123 | } 124 | public void setTitle(String title) { 125 | this.title = title; 126 | } 127 | public String getContent() { 128 | return content; 129 | } 130 | public void setContent(String content) { 131 | this.content = content; 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /src/main/java/com/jvmhub/springboot/controller/Home.java: -------------------------------------------------------------------------------- 1 | package com.jvmhub.springboot.controller; 2 | 3 | import javax.validation.Valid; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.data.domain.Pageable; 7 | import org.springframework.data.domain.Sort.Direction; 8 | import org.springframework.data.redis.core.RedisTemplate; 9 | import org.springframework.data.web.PageableDefault; 10 | import org.springframework.stereotype.Controller; 11 | import org.springframework.ui.Model; 12 | import org.springframework.validation.BindingResult; 13 | import org.springframework.web.bind.annotation.PathVariable; 14 | import org.springframework.web.bind.annotation.RequestMapping; 15 | import org.springframework.web.bind.annotation.RequestMethod; 16 | 17 | import com.jvmhub.springboot.domain.Post; 18 | import com.jvmhub.springboot.domain.Thing; 19 | import com.jvmhub.springboot.redis.RedisUtil; 20 | import com.jvmhub.springboot.repository.PostRepository; 21 | import com.jvmhub.springboot.service.CacheDemoService; 22 | 23 | @Controller 24 | public class Home { 25 | 26 | @Autowired 27 | private PostRepository postRepository; 28 | 29 | @Autowired 30 | private RedisUtil redisUtil; 31 | 32 | @Autowired 33 | private CacheDemoService demoService; 34 | 35 | 36 | @RequestMapping(value="/", method=RequestMethod.GET) 37 | public String index(Post post) { 38 | //model.addAttribute("post", new PostEntity()); 39 | return "index"; 40 | } 41 | 42 | @RequestMapping(value="/testredis", method=RequestMethod.GET) 43 | public String testredis(Model model) { 44 | Object val = redisUtil.get("testredis"); 45 | if(val==null){ 46 | 47 | redisUtil.set("testredis", "abcd"); 48 | } 49 | model.addAttribute("val", val); 50 | 51 | return "testredis"; 52 | } 53 | 54 | @RequestMapping(value = "/", method = RequestMethod.POST) 55 | public String addNewPost(@Valid Post post, BindingResult bindingResult, Model model) { 56 | if (bindingResult.hasErrors()) { 57 | return "index"; 58 | } 59 | postRepository.save(post);//new Post(post.getTitle(), post.getContent())); 60 | model.addAttribute("posts", postRepository.findAll()); 61 | return "redirect:result"; 62 | } 63 | 64 | @RequestMapping(value = "/result", method = RequestMethod.GET) 65 | public String showAllPosts(Model model) { 66 | model.addAttribute("posts", postRepository.findAll()); 67 | return "result"; 68 | } 69 | 70 | 71 | @RequestMapping(value="/input", method=RequestMethod.GET) 72 | public String addThingInput(Thing thing) { 73 | return "input"; 74 | } 75 | 76 | @RequestMapping(value = "/input", method = RequestMethod.POST) 77 | public String addThing(@Valid Thing thing, BindingResult bindingResult, Model model) { 78 | if (bindingResult.hasErrors()) { 79 | return "input"; 80 | } 81 | demoService.create(thing);//new Thing(thing.getTitle(), thing.getContent())); 82 | model.addAttribute("thing", demoService.findAll()); 83 | return "redirect:listall"; 84 | } 85 | 86 | @RequestMapping(value = "/update", method = RequestMethod.POST) 87 | public String updateThing(@Valid Thing thing, BindingResult bindingResult, Model model) { 88 | if (bindingResult.hasErrors()) { 89 | return "update"; 90 | } 91 | demoService.create(thing);//new Thing(thing.getTitle(), thing.getContent())); 92 | model.addAttribute("thing", demoService.findAll()); 93 | return "redirect:listall"; 94 | } 95 | 96 | @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET) 97 | public String deleteThing(@PathVariable Long id,Model model) { 98 | demoService.delete(id); 99 | model.addAttribute("thing", demoService.findAll()); 100 | return "redirect:../listall"; 101 | } 102 | @RequestMapping(value = "/search", method = RequestMethod.GET) 103 | public String searchThing(String title,Model model) { 104 | Iterable vals = demoService.findByTitle(title); 105 | model.addAttribute("things", vals); 106 | 107 | return "listall"; 108 | } 109 | 110 | @RequestMapping(value = "/listall", method = RequestMethod.GET) 111 | public String showAllThings(Model model) { 112 | model.addAttribute("things", demoService.findAll()); 113 | return "listall"; 114 | } 115 | 116 | @RequestMapping(value = "/listpage", method = RequestMethod.GET) 117 | public String listPage( @PageableDefault(value = 15, sort = { "id" }, direction = Direction.DESC) Pageable pageable,Model model) { 118 | /*page,第几页,从0开始,默认为第0页 119 | size,每一页的大小,默认为20 120 | sort,排序相关的信息,以property,property(,ASC|DESC)的方式组织,例如sort=firstname&sort=lastname,desc表示在按firstname正序排列基础上按lastname倒序排列 */ 121 | model.addAttribute("page", demoService.findByPage(pageable)); 122 | 123 | return "listpage"; 124 | } 125 | 126 | } 127 | -------------------------------------------------------------------------------- /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /spring-boot-thymeleaf-jpa.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | --------------------------------------------------------------------------------