├── .gitignore ├── LICENSE.txt ├── README.md ├── download-demo ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── zhengxl │ │ │ └── downloaddemo │ │ │ ├── DownloadDemoApplication.java │ │ │ ├── controller │ │ │ ├── DownloadController.java │ │ │ └── UploadController.java │ │ │ ├── repository │ │ │ └── FileRepository.java │ │ │ └── service │ │ │ ├── FileService.java │ │ │ └── impl │ │ │ └── FileServiceImpl.java │ └── resources │ │ ├── application.yml │ │ └── templates │ │ ├── error.html │ │ ├── index.html │ │ └── upload-success.html │ └── test │ └── java │ └── com │ └── zhengxl │ └── downloaddemo │ └── DownloadDemoApplicationTests.java ├── error-handle-demo ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── zhengxl │ │ │ └── errorhandledemo │ │ │ ├── ErrorHandleDemoApplication.java │ │ │ └── controller │ │ │ ├── CustomErrorController.java │ │ │ └── HelloWorldController.java │ └── resources │ │ ├── application.yml │ │ └── templates │ │ ├── error.html │ │ ├── hello.html │ │ ├── my401.html │ │ ├── my404.html │ │ └── my500.html │ └── test │ └── java │ └── com │ └── zhengxl │ └── errorhandledemo │ └── ErrorHandleDemoApplicationTests.java ├── filter-demo ├── .idea │ ├── $CACHE_FILE$ │ ├── $PRODUCT_WORKSPACE_FILE$ │ ├── .gitignore │ ├── compiler.xml │ ├── encodings.xml │ ├── inspectionProfiles │ │ └── Project_Default.xml │ ├── libraries │ │ ├── Maven__ch_qos_logback_logback_classic_1_2_3.xml │ │ ├── Maven__ch_qos_logback_logback_core_1_2_3.xml │ │ ├── Maven__com_fasterxml_jackson_core_jackson_annotations_2_11_0.xml │ │ ├── Maven__com_fasterxml_jackson_core_jackson_core_2_11_0.xml │ │ ├── Maven__com_fasterxml_jackson_core_jackson_databind_2_11_0.xml │ │ ├── Maven__com_fasterxml_jackson_datatype_jackson_datatype_jdk8_2_11_0.xml │ │ ├── Maven__com_fasterxml_jackson_datatype_jackson_datatype_jsr310_2_11_0.xml │ │ ├── Maven__com_fasterxml_jackson_module_jackson_module_parameter_names_2_11_0.xml │ │ ├── Maven__com_jayway_jsonpath_json_path_2_4_0.xml │ │ ├── Maven__com_vaadin_external_google_android_json_0_0_20131108_vaadin1.xml │ │ ├── Maven__jakarta_activation_jakarta_activation_api_1_2_2.xml │ │ ├── Maven__jakarta_annotation_jakarta_annotation_api_1_3_5.xml │ │ ├── Maven__jakarta_xml_bind_jakarta_xml_bind_api_2_3_3.xml │ │ ├── Maven__net_bytebuddy_byte_buddy_1_10_11.xml │ │ ├── Maven__net_bytebuddy_byte_buddy_agent_1_10_11.xml │ │ ├── Maven__net_minidev_accessors_smart_1_2.xml │ │ ├── Maven__net_minidev_json_smart_2_3.xml │ │ ├── Maven__org_apache_logging_log4j_log4j_api_2_13_3.xml │ │ ├── Maven__org_apache_logging_log4j_log4j_to_slf4j_2_13_3.xml │ │ ├── Maven__org_apache_tomcat_embed_tomcat_embed_core_9_0_36.xml │ │ ├── Maven__org_apache_tomcat_embed_tomcat_embed_websocket_9_0_36.xml │ │ ├── Maven__org_apiguardian_apiguardian_api_1_1_0.xml │ │ ├── Maven__org_assertj_assertj_core_3_16_1.xml │ │ ├── Maven__org_glassfish_jakarta_el_3_0_3.xml │ │ ├── Maven__org_hamcrest_hamcrest_2_2.xml │ │ ├── Maven__org_junit_jupiter_junit_jupiter_5_6_2.xml │ │ ├── Maven__org_junit_jupiter_junit_jupiter_api_5_6_2.xml │ │ ├── Maven__org_junit_jupiter_junit_jupiter_engine_5_6_2.xml │ │ ├── Maven__org_junit_jupiter_junit_jupiter_params_5_6_2.xml │ │ ├── Maven__org_junit_platform_junit_platform_commons_1_6_2.xml │ │ ├── Maven__org_junit_platform_junit_platform_engine_1_6_2.xml │ │ ├── Maven__org_mockito_mockito_core_3_3_3.xml │ │ ├── Maven__org_mockito_mockito_junit_jupiter_3_3_3.xml │ │ ├── Maven__org_objenesis_objenesis_2_6.xml │ │ ├── Maven__org_opentest4j_opentest4j_1_2_0.xml │ │ ├── Maven__org_ow2_asm_asm_5_0_4.xml │ │ ├── Maven__org_skyscreamer_jsonassert_1_5_0.xml │ │ ├── Maven__org_slf4j_jul_to_slf4j_1_7_30.xml │ │ ├── Maven__org_slf4j_slf4j_api_1_7_30.xml │ │ ├── Maven__org_springframework_boot_spring_boot_2_3_1_RELEASE.xml │ │ ├── Maven__org_springframework_boot_spring_boot_autoconfigure_2_3_1_RELEASE.xml │ │ ├── Maven__org_springframework_boot_spring_boot_starter_2_3_1_RELEASE.xml │ │ ├── Maven__org_springframework_boot_spring_boot_starter_json_2_3_1_RELEASE.xml │ │ ├── Maven__org_springframework_boot_spring_boot_starter_logging_2_3_1_RELEASE.xml │ │ ├── Maven__org_springframework_boot_spring_boot_starter_test_2_3_1_RELEASE.xml │ │ ├── Maven__org_springframework_boot_spring_boot_starter_tomcat_2_3_1_RELEASE.xml │ │ ├── Maven__org_springframework_boot_spring_boot_starter_web_2_3_1_RELEASE.xml │ │ ├── Maven__org_springframework_boot_spring_boot_test_2_3_1_RELEASE.xml │ │ ├── Maven__org_springframework_boot_spring_boot_test_autoconfigure_2_3_1_RELEASE.xml │ │ ├── Maven__org_springframework_spring_aop_5_2_7_RELEASE.xml │ │ ├── Maven__org_springframework_spring_beans_5_2_7_RELEASE.xml │ │ ├── Maven__org_springframework_spring_context_5_2_7_RELEASE.xml │ │ ├── Maven__org_springframework_spring_core_5_2_7_RELEASE.xml │ │ ├── Maven__org_springframework_spring_expression_5_2_7_RELEASE.xml │ │ ├── Maven__org_springframework_spring_jcl_5_2_7_RELEASE.xml │ │ ├── Maven__org_springframework_spring_test_5_2_7_RELEASE.xml │ │ ├── Maven__org_springframework_spring_web_5_2_7_RELEASE.xml │ │ ├── Maven__org_springframework_spring_webmvc_5_2_7_RELEASE.xml │ │ ├── Maven__org_xmlunit_xmlunit_core_2_7_0.xml │ │ └── Maven__org_yaml_snakeyaml_1_26.xml │ ├── misc.xml │ ├── modules.xml │ └── vcs.xml ├── filter-demo.iml ├── pom.xml ├── src │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── zhengxl │ │ │ │ └── filterdemo │ │ │ │ ├── FilterDemoApplication.java │ │ │ │ ├── config │ │ │ │ └── FilterConfig.java │ │ │ │ ├── controller │ │ │ │ └── HelloWorld.java │ │ │ │ └── filter │ │ │ │ ├── MyAnotherFilter.java │ │ │ │ └── MyFilter.java │ │ └── resources │ │ │ └── application.properties │ └── test │ │ └── java │ │ └── com │ │ └── zhengxl │ │ └── filterdemo │ │ └── FilterDemoApplicationTests.java └── target │ ├── classes │ ├── application.properties │ └── com │ │ └── zhengxl │ │ └── filterdemo │ │ ├── FilterDemoApplication.class │ │ ├── controller │ │ └── HelloWorld.class │ │ └── filter │ │ ├── MyAnotherFilter.class │ │ └── MyFilter.class │ └── test-classes │ └── com │ └── zhengxl │ └── filterdemo │ └── FilterDemoApplicationTests.class ├── freemarker ├── Freemarker.xmind ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── zhengxl │ │ └── freemarker │ │ ├── FreemarkerApplication.java │ │ ├── config │ │ └── FreemarkerConfig.java │ │ ├── controller │ │ ├── IndexController.java │ │ ├── StaticMethodController.java │ │ └── UserDefinedDirectiveController.java │ │ ├── directive │ │ └── MyDirective.java │ │ ├── entity │ │ └── User.java │ │ ├── properties │ │ └── FreeMarkerProperties.java │ │ ├── service │ │ ├── UserService.java │ │ └── impl │ │ │ └── UserServiceImpl.java │ │ └── tools │ │ ├── AnotherStaticTool.java │ │ └── UserStaticTool.java │ └── resources │ ├── META-INF │ └── additional-spring-configuration-metadata.json │ ├── application.yml │ └── templates │ ├── built-ins.ftlh │ ├── directive.ftlh │ ├── footer.ftlh │ ├── index.ftlh │ ├── inner-method.ftlh │ ├── lib │ ├── bbb.ftlh │ ├── common.ftlh │ └── example.ftlh │ └── static-method-view.ftlh ├── interceptor-demo ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── zhengxl │ └── interceptordemo │ ├── InterceptorDemoApplication.java │ ├── config │ └── WebMVCConfig.java │ ├── controller │ └── HelloWorldController.java │ └── interceptor │ ├── AnotherInterceptor.java │ └── MyInterceptor.java ├── mybatis ├── dynamic-multiple-data-sources │ ├── .gitignore │ ├── mvnw │ ├── mvnw.cmd │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── top │ │ │ │ └── javahelper │ │ │ │ └── dynamicmultipledatasources │ │ │ │ ├── DynamicMultipleDataSourcesApplication.java │ │ │ │ ├── aspect │ │ │ │ └── DataSourceAspect.java │ │ │ │ ├── common │ │ │ │ ├── RoutingDataSource.java │ │ │ │ ├── RoutingDataSourceContext.java │ │ │ │ └── WithDataSource.java │ │ │ │ ├── config │ │ │ │ ├── H2DataBaseInit.java │ │ │ │ └── RoutingDataSourcesConfig.java │ │ │ │ ├── controller │ │ │ │ └── UserController.java │ │ │ │ ├── mapper │ │ │ │ └── UserMapper.java │ │ │ │ ├── model │ │ │ │ └── User.java │ │ │ │ └── service │ │ │ │ ├── UserService.java │ │ │ │ └── impl │ │ │ │ └── UserServiceImpl.java │ │ └── resources │ │ │ ├── application.yml │ │ │ └── generatorConfig.xml │ │ └── test │ │ └── java │ │ └── top │ │ └── javahelper │ │ └── dynamicmultipledatasources │ │ ├── DynamicMultipleDataSourcesApplicationTests.java │ │ ├── MBG.java │ │ └── mapper │ │ └── UserMapperTest.java ├── multiple-data-sources │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── top │ │ │ │ └── javahelper │ │ │ │ └── multipledatasources │ │ │ │ ├── MultipleDataSourcesApplication.java │ │ │ │ ├── config │ │ │ │ ├── FirstDataSourceConfig.java │ │ │ │ ├── InitData.java │ │ │ │ └── SecondDataSourceConfig.java │ │ │ │ ├── controller │ │ │ │ └── UserController.java │ │ │ │ ├── entity │ │ │ │ └── User.java │ │ │ │ ├── mapper │ │ │ │ ├── first │ │ │ │ │ └── FirstUserMapper.java │ │ │ │ └── second │ │ │ │ │ └── SecondUserMapper.java │ │ │ │ └── service │ │ │ │ ├── UserService.java │ │ │ │ └── impl │ │ │ │ └── UserServiceImpl.java │ │ └── resources │ │ │ ├── application.yml │ │ │ ├── db1.user.sql │ │ │ ├── db2.user.sql │ │ │ └── mybatis │ │ │ ├── first │ │ │ └── FirstUserMapper.xml │ │ │ └── second │ │ │ └── SecondUserMapper.xml │ │ └── test │ │ └── java │ │ └── top │ │ └── javahelper │ │ └── multipledatasources │ │ ├── MultipleDataSourcesApplicationTests.java │ │ └── mapper │ │ └── UserMapperTest.java ├── mybatis-cache │ ├── .gitignore │ ├── .mvn │ │ └── wrapper │ │ │ ├── MavenWrapperDownloader.java │ │ │ ├── maven-wrapper.jar │ │ │ └── maven-wrapper.properties │ ├── mvnw │ ├── mvnw.cmd │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── zhengxl │ │ │ │ └── mybatiscache │ │ │ │ ├── MybatiscacheApplication.java │ │ │ │ ├── controller │ │ │ │ └── Hello.java │ │ │ │ ├── dto │ │ │ │ └── UserDTO.java │ │ │ │ ├── entity │ │ │ │ ├── User.java │ │ │ │ └── UserOrder.java │ │ │ │ └── mapper │ │ │ │ ├── UserMapper.java │ │ │ │ └── UserOrderMapper.java │ │ └── resources │ │ │ ├── application.yml │ │ │ ├── data.sql │ │ │ ├── mybatis │ │ │ ├── userMapper.xml │ │ │ └── userOrderMapper.xml │ │ │ └── schema.sql │ │ └── test │ │ └── java │ │ └── com │ │ └── zhengxl │ │ └── mybatiscache │ │ ├── MybatiscacheApplicationTests.java │ │ ├── UserOrderTest.java │ │ └── UserTest.java └── mybatis-sample │ ├── pom.xml │ ├── src │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── zhengxl │ │ │ │ └── mybatissample │ │ │ │ ├── MybatisSampleApplication.java │ │ │ │ ├── config │ │ │ │ └── H2DataBaseInit.java │ │ │ │ ├── controller │ │ │ │ └── MyController.java │ │ │ │ ├── mapper │ │ │ │ └── UserMapper.java │ │ │ │ ├── model │ │ │ │ └── User.java │ │ │ │ └── service │ │ │ │ ├── UserService.java │ │ │ │ └── impl │ │ │ │ └── UserServiceImpl.java │ │ └── resources │ │ │ ├── application.yml │ │ │ ├── generatorConfig.xml │ │ │ └── mybatis │ │ │ └── UserMapper.xml │ └── test │ │ └── java │ │ └── com │ │ └── zhengxl │ │ └── mybatissample │ │ ├── Mbg.java │ │ └── MybatisSampleApplicationTests.java │ └── target │ └── classes │ ├── application.yml │ └── generatorConfig.xml ├── redis-demo └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── zhengxl │ │ │ └── redisdemo │ │ │ ├── config │ │ │ └── RedisConfig.java │ │ │ ├── controller │ │ │ └── UserController.java │ │ │ ├── core │ │ │ └── ObjectRedisTemplate.java │ │ │ └── entity │ │ │ └── User.java │ └── resources │ │ └── application.yml │ └── test │ └── java │ └── com │ └── zhengxl │ └── redisdemo │ └── redis │ └── RedisTest.java ├── redis └── redis-transaction │ ├── .gitignore │ ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties │ ├── mvnw │ ├── mvnw.cmd │ ├── pom.xml │ └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── zhengxl │ │ │ └── redistransaction │ │ │ ├── RedisTransactionApplication.java │ │ │ ├── config │ │ │ └── RedisConfig.java │ │ │ ├── controller │ │ │ └── RedisController.java │ │ │ └── service │ │ │ └── RedisService.java │ └── resources │ │ └── application.yml │ └── test │ └── java │ └── com │ └── zhengxl │ └── redistransaction │ └── RedisTransactionApplicationTests.java ├── slf4j └── logtest │ ├── pom.xml │ └── src │ ├── main │ ├── java │ │ └── zhengxl │ │ │ └── App.java │ └── resources │ │ └── log4j.properties │ └── test │ └── java │ └── zhengxl │ └── AppTest.java ├── thymeleaf-demo ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── zhengxl │ │ └── thymeleafdemo │ │ ├── ThymeleafDemoApplication.java │ │ ├── controller │ │ ├── DownloadController.java │ │ └── UploadController.java │ │ ├── repository │ │ └── FileRepository.java │ │ ├── service │ │ ├── FileService.java │ │ └── impl │ │ │ ├── FileServiceImpl.java │ │ │ └── MyService.java │ │ └── utils │ │ └── HttpUtils.java │ └── resources │ ├── application.yml │ └── templates │ ├── error.html │ ├── index.html │ └── upload-success.html ├── transactional ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── javahelper │ │ │ └── top │ │ │ └── transactional │ │ │ ├── TransactionalApplication.java │ │ │ ├── controller │ │ │ └── UserController.java │ │ │ ├── exception │ │ │ └── RollBackException.java │ │ │ └── service │ │ │ ├── UserService.java │ │ │ └── impl │ │ │ └── UserServiceImpl.java │ └── resources │ │ ├── application.yml │ │ └── schema.sql │ └── test │ └── java │ └── com │ └── javahelper │ └── top │ └── transactional │ └── TransactionalApplicationTests.java ├── unittest-demo └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── zhengxl │ │ │ └── unittestdemo │ │ │ ├── controller │ │ │ └── UserController.java │ │ │ ├── utils │ │ │ └── MathUtils.java │ │ │ └── vo │ │ │ └── UserVO.java │ └── resources │ │ └── application.yml │ └── test │ └── java │ └── com │ └── zhengxl │ └── unittestdemo │ ├── controller │ └── UserControllerTest.java │ └── utils │ └── MathUtilsTest.java └── validation-demo ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── zhengxl │ │ └── validationdemo │ │ ├── ValidationDemoApplication.java │ │ ├── advice │ │ └── GlobalControllerAdvice.java │ │ ├── common │ │ └── ResultInfo.java │ │ ├── controller │ │ └── UserController.java │ │ ├── validator │ │ ├── annotation │ │ │ └── HaveNoBlank.java │ │ ├── constraint │ │ │ └── HaveNoBlankValidator.java │ │ └── validategroup │ │ │ └── Update.java │ │ └── vo │ │ ├── OrderVO.java │ │ └── UserVO.java └── resources │ └── application.yml └── test └── java └── com └── zhengxl └── validationdemo ├── UserControllerTest.java └── ValidationDemoApplicationTests.java /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.iml 3 | 4 | # Compiled class file 5 | *.class 6 | 7 | # Log file 8 | *.log 9 | 10 | # BlueJ files 11 | *.ctxt 12 | 13 | # Mobile Tools for Java (J2ME) 14 | .mtj.tmp/ 15 | 16 | # Package Files # 17 | *.jar 18 | *.war 19 | *.nar 20 | *.ear 21 | *.zip 22 | *.tar.gz 23 | *.rar 24 | 25 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 26 | hs_err_pid* -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 zhengxl5566 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /download-demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.3.2.RELEASE 9 | 10 | 11 | com.zhengxl 12 | download-demo 13 | 0.0.1-SNAPSHOT 14 | download-demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-thymeleaf 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-validation 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-web 33 | 34 | 35 | 36 | commons-io 37 | commons-io 38 | 2.7 39 | 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-starter-test 45 | test 46 | 47 | 48 | org.junit.vintage 49 | junit-vintage-engine 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | org.springframework.boot 59 | spring-boot-maven-plugin 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /download-demo/src/main/java/com/zhengxl/downloaddemo/DownloadDemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.downloaddemo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class DownloadDemoApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(DownloadDemoApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /download-demo/src/main/java/com/zhengxl/downloaddemo/service/FileService.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.downloaddemo.service; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | 6 | /** 7 | * @description: 8 | * @projectName:thymeleaf-demo 9 | * @see:com.zhengxl.thymeleafdemo.service 10 | * @author:郑晓龙 11 | * @createTime:2020/8/8 10:40 12 | * @version:1.0 13 | */ 14 | public interface FileService { 15 | 16 | /** 17 | * @param clientFileName 客户端上传的文件名 18 | * @param inputStream 输入流 19 | * @return java.lang.String 服务端保存的文件名 20 | * @description 21 | * @author 郑晓龙 22 | * @createTime 2020/8/10 16:05 23 | **/ 24 | String saveInputStream(String clientFileName, InputStream inputStream) throws IOException; 25 | 26 | /** 27 | * @param serverFileName 服务端文件名 28 | * @return 文件输入流 29 | * @description 根据文件名获取文件 30 | * @author 郑晓龙 31 | * @createTime 2020/8/8 10:44 32 | **/ 33 | InputStream getInputStream(String serverFileName) throws IOException; 34 | 35 | /** 36 | * @param serverFileName 服务端文件名 37 | * @return java.lang.String 客户端文件名 38 | * @description 根据服务端文件名获取客户端文件名 39 | * @author 郑晓龙 40 | * @createTime 2020/8/10 16:09 41 | **/ 42 | String getClientFileName(String serverFileName); 43 | 44 | } 45 | -------------------------------------------------------------------------------- /download-demo/src/main/java/com/zhengxl/downloaddemo/service/impl/FileServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.downloaddemo.service.impl; 2 | 3 | 4 | import com.zhengxl.downloaddemo.repository.FileRepository; 5 | import com.zhengxl.downloaddemo.service.FileService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import java.io.IOException; 10 | import java.io.InputStream; 11 | 12 | /** 13 | * @description: 14 | * @projectName:thymeleaf-demo 15 | * @see:com.zhengxl.thymeleafdemo.service 16 | * @author:郑晓龙 17 | * @createTime:2020/8/7 13:47 18 | * @version:1.0 19 | */ 20 | @Service 21 | public class FileServiceImpl implements FileService { 22 | 23 | @Autowired 24 | FileRepository fileRepository; 25 | 26 | @Override 27 | public String saveInputStream(String clientFileName, InputStream inputStream) throws IOException { 28 | return fileRepository.saveFile(clientFileName, inputStream); 29 | 30 | } 31 | 32 | @Override 33 | public InputStream getInputStream(String serverFileName) throws IOException { 34 | return fileRepository.getInputStream(serverFileName); 35 | } 36 | 37 | @Override 38 | public String getClientFileName(String serverFileName) { 39 | return fileRepository.getClientFileName(serverFileName); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /download-demo/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8080 3 | spring: 4 | thymeleaf: 5 | cache: false 6 | servlet: 7 | multipart: 8 | enabled: true 9 | max-file-size: 20MB 10 | max-request-size: 20MB 11 | file: 12 | storage: 13 | # windows D:/upload/ 14 | #linux /home/upload/ 15 | path: "D:/upload/" -------------------------------------------------------------------------------- /download-demo/src/main/resources/templates/error.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | Document 9 | 10 | 11 |

操作失败!

12 | 13 | 返回 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /download-demo/src/main/resources/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 文件上传 7 | 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 | -------------------------------------------------------------------------------- /download-demo/src/main/resources/templates/upload-success.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | Document 9 | 15 | 16 | 17 |

操作成功!

18 |
19 | 点此下载刚刚上传的附件 20 |
21 |
22 | 返回 23 |
24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /download-demo/src/test/java/com/zhengxl/downloaddemo/DownloadDemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.downloaddemo; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class DownloadDemoApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /error-handle-demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.3.3.RELEASE 9 | 10 | 11 | com.zhengxl 12 | error-handle-demo 13 | 0.0.1-SNAPSHOT 14 | error-handle-demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-thymeleaf 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter-test 34 | test 35 | 36 | 37 | org.junit.vintage 38 | junit-vintage-engine 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /error-handle-demo/src/main/java/com/zhengxl/errorhandledemo/ErrorHandleDemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.errorhandledemo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class ErrorHandleDemoApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(ErrorHandleDemoApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /error-handle-demo/src/main/java/com/zhengxl/errorhandledemo/controller/CustomErrorController.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.errorhandledemo.controller; 2 | 3 | import org.springframework.boot.web.servlet.error.ErrorController; 4 | import org.springframework.http.HttpStatus; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | 8 | import javax.servlet.RequestDispatcher; 9 | import javax.servlet.http.HttpServletRequest; 10 | 11 | /** 12 | * @description: 13 | * @projectName:error-handle-demo 14 | * @see:com.zhengxl.errorhandledemo.controller 15 | * @author:郑晓龙 16 | * @createTime:2020/8/24 18:08 17 | * @version:1.0 18 | */ 19 | @Controller 20 | public class CustomErrorController implements ErrorController { 21 | 22 | @RequestMapping("/error") 23 | public String handleError(HttpServletRequest request) { 24 | Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE); 25 | if (status != null) { 26 | Integer statusCode = Integer.valueOf(status.toString()); 27 | 28 | if (statusCode == HttpStatus.NOT_FOUND.value()) { 29 | return "my404"; 30 | } else if (statusCode == HttpStatus.UNAUTHORIZED.value()) { 31 | return "my401"; 32 | } else if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) { 33 | return "my500"; 34 | } 35 | // 这里后期可以扩展其他错误页面 36 | } 37 | return "my404"; 38 | } 39 | 40 | @Override 41 | public String getErrorPath() { 42 | return null; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /error-handle-demo/src/main/java/com/zhengxl/errorhandledemo/controller/HelloWorldController.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.errorhandledemo.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | 6 | /** 7 | * @description: 8 | * @projectName:error-handle-demo 9 | * @see:com.zhengxl.errorhandledemo.controller 10 | * @author:郑晓龙 11 | * @createTime:2020/8/24 17:05 12 | * @version:1.0 13 | */ 14 | @Controller 15 | public class HelloWorldController { 16 | @GetMapping("hello") 17 | public String hello() { 18 | // 制造一个 500 错误 19 | // int error= 1/0; 20 | return "hello"; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /error-handle-demo/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | error: 3 | # 使用默认的 /error 4 | path: /error 5 | whitelabel: 6 | enabled: false 7 | spring: 8 | thymeleaf: 9 | cache: false -------------------------------------------------------------------------------- /error-handle-demo/src/main/resources/templates/error.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |

My Error Page

5 | 6 | -------------------------------------------------------------------------------- /error-handle-demo/src/main/resources/templates/hello.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | helloPage 6 | 7 | 8 |

Hello,World!

9 | 10 | -------------------------------------------------------------------------------- /error-handle-demo/src/main/resources/templates/my401.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |

My 401 Unauthorized Page

5 | 6 | -------------------------------------------------------------------------------- /error-handle-demo/src/main/resources/templates/my404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |

My 404 Not Found Page

5 | 6 | -------------------------------------------------------------------------------- /error-handle-demo/src/main/resources/templates/my500.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |

My 500 Internal Server Error Page

5 | 6 | -------------------------------------------------------------------------------- /error-handle-demo/src/test/java/com/zhengxl/errorhandledemo/ErrorHandleDemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.errorhandledemo; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class ErrorHandleDemoApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /filter-demo/.idea/$CACHE_FILE$: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Ali-Check 10 | 11 | 12 | Java 13 | 14 | 15 | Serialization issuesJava 16 | 17 | 18 | 19 | 20 | SerializableHasSerialVersionUIDField 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /filter-demo/.idea/$PRODUCT_WORKSPACE_FILE$: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1.8 8 | 9 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /filter-demo/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /workspace.xml -------------------------------------------------------------------------------- /filter-demo/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 20 | 21 | -------------------------------------------------------------------------------- /filter-demo/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /filter-demo/.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 36 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__ch_qos_logback_logback_classic_1_2_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__ch_qos_logback_logback_core_1_2_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_annotations_2_11_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_core_2_11_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_databind_2_11_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__com_fasterxml_jackson_datatype_jackson_datatype_jdk8_2_11_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__com_fasterxml_jackson_datatype_jackson_datatype_jsr310_2_11_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__com_fasterxml_jackson_module_jackson_module_parameter_names_2_11_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__com_jayway_jsonpath_json_path_2_4_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__com_vaadin_external_google_android_json_0_0_20131108_vaadin1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__jakarta_activation_jakarta_activation_api_1_2_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__jakarta_annotation_jakarta_annotation_api_1_3_5.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__jakarta_xml_bind_jakarta_xml_bind_api_2_3_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__net_bytebuddy_byte_buddy_1_10_11.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__net_bytebuddy_byte_buddy_agent_1_10_11.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__net_minidev_accessors_smart_1_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__net_minidev_json_smart_2_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_apache_logging_log4j_log4j_api_2_13_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_apache_logging_log4j_log4j_to_slf4j_2_13_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_apache_tomcat_embed_tomcat_embed_core_9_0_36.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_apache_tomcat_embed_tomcat_embed_websocket_9_0_36.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_apiguardian_apiguardian_api_1_1_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_assertj_assertj_core_3_16_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_glassfish_jakarta_el_3_0_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_hamcrest_hamcrest_2_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_5_6_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_api_5_6_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_engine_5_6_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_params_5_6_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_junit_platform_junit_platform_commons_1_6_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_junit_platform_junit_platform_engine_1_6_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_mockito_mockito_core_3_3_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_mockito_mockito_junit_jupiter_3_3_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_objenesis_objenesis_2_6.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_opentest4j_opentest4j_1_2_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_ow2_asm_asm_5_0_4.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_skyscreamer_jsonassert_1_5_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_slf4j_jul_to_slf4j_1_7_30.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_slf4j_slf4j_api_1_7_30.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_springframework_boot_spring_boot_2_3_1_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_springframework_boot_spring_boot_autoconfigure_2_3_1_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_2_3_1_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_json_2_3_1_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_logging_2_3_1_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_test_2_3_1_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_tomcat_2_3_1_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_web_2_3_1_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_springframework_boot_spring_boot_test_2_3_1_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_springframework_boot_spring_boot_test_autoconfigure_2_3_1_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_springframework_spring_aop_5_2_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_springframework_spring_beans_5_2_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_springframework_spring_context_5_2_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_springframework_spring_core_5_2_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_springframework_spring_expression_5_2_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_springframework_spring_jcl_5_2_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_springframework_spring_test_5_2_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_springframework_spring_web_5_2_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_springframework_spring_webmvc_5_2_7_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_xmlunit_xmlunit_core_2_7_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/libraries/Maven__org_yaml_snakeyaml_1_26.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /filter-demo/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /filter-demo/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /filter-demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.3.1.RELEASE 9 | 10 | 11 | com.zhengxl 12 | filter-demo 13 | 0.0.1-SNAPSHOT 14 | filter-demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-web 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-test 30 | test 31 | 32 | 33 | org.junit.vintage 34 | junit-vintage-engine 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-maven-plugin 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /filter-demo/src/main/java/com/zhengxl/filterdemo/FilterDemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.filterdemo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.web.servlet.ServletComponentScan; 6 | 7 | @SpringBootApplication 8 | @ServletComponentScan("com.zhengxl.filterdemo.filter") 9 | public class FilterDemoApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(FilterDemoApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /filter-demo/src/main/java/com/zhengxl/filterdemo/config/FilterConfig.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.filterdemo.config; 2 | 3 | import com.zhengxl.filterdemo.filter.MyAnotherFilter; 4 | import com.zhengxl.filterdemo.filter.MyFilter; 5 | import org.springframework.boot.web.servlet.FilterRegistrationBean; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | 9 | @Configuration 10 | public class FilterConfig { 11 | @Bean 12 | public FilterRegistrationBean registerMyFilter(){ 13 | FilterRegistrationBean bean = new FilterRegistrationBean<>(); 14 | bean.setOrder(1); 15 | bean.setFilter(new MyFilter()); 16 | // 匹配"/hello/"下面的所有url 17 | bean.addUrlPatterns("/hello/*"); 18 | return bean; 19 | } 20 | @Bean 21 | public FilterRegistrationBean registerMyAnotherFilter(){ 22 | FilterRegistrationBean bean = new FilterRegistrationBean<>(); 23 | bean.setOrder(2); 24 | bean.setFilter(new MyAnotherFilter()); 25 | // 匹配所有url 26 | bean.addUrlPatterns("/*"); 27 | return bean; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /filter-demo/src/main/java/com/zhengxl/filterdemo/controller/HelloWorld.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.filterdemo.controller; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | @RestController 8 | public class HelloWorld { 9 | @GetMapping("hello") 10 | public String helloWorld(){ 11 | return "Hello world!"; 12 | } 13 | @GetMapping("hi") 14 | public String helloChina(){ 15 | return "Hi world!"; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /filter-demo/src/main/java/com/zhengxl/filterdemo/filter/MyAnotherFilter.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.filterdemo.filter; 2 | 3 | import org.springframework.core.annotation.Order; 4 | import org.springframework.stereotype.Component; 5 | 6 | import javax.servlet.*; 7 | import javax.servlet.annotation.WebFilter; 8 | import java.io.IOException; 9 | 10 | //@Component 11 | //@Order(2) 12 | //@WebFilter(urlPatterns = "/*") 13 | public class MyAnotherFilter implements Filter { 14 | @Override 15 | public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { 16 | System.out.println("my another filter in"); 17 | filterChain.doFilter(servletRequest,servletResponse); 18 | System.out.println("my another filter out"); 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /filter-demo/src/main/java/com/zhengxl/filterdemo/filter/MyFilter.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.filterdemo.filter; 2 | 3 | import org.springframework.core.annotation.Order; 4 | import org.springframework.stereotype.Component; 5 | 6 | import javax.servlet.*; 7 | import javax.servlet.annotation.WebFilter; 8 | import java.io.IOException; 9 | 10 | //@Component 11 | //@Order(1) 12 | //@WebFilter(urlPatterns = "/hello/*") 13 | public class MyFilter implements Filter { 14 | @Override 15 | public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { 16 | System.out.println("my filter in"); 17 | filterChain.doFilter(servletRequest,servletResponse); 18 | System.out.println("my filter out"); 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /filter-demo/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /filter-demo/src/test/java/com/zhengxl/filterdemo/FilterDemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.filterdemo; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class FilterDemoApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /filter-demo/target/classes/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /filter-demo/target/classes/com/zhengxl/filterdemo/FilterDemoApplication.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengxl5566/springboot-demo/5d27efdf123773e3df033d1e545ec57009e3a7ac/filter-demo/target/classes/com/zhengxl/filterdemo/FilterDemoApplication.class -------------------------------------------------------------------------------- /filter-demo/target/classes/com/zhengxl/filterdemo/controller/HelloWorld.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengxl5566/springboot-demo/5d27efdf123773e3df033d1e545ec57009e3a7ac/filter-demo/target/classes/com/zhengxl/filterdemo/controller/HelloWorld.class -------------------------------------------------------------------------------- /filter-demo/target/classes/com/zhengxl/filterdemo/filter/MyAnotherFilter.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengxl5566/springboot-demo/5d27efdf123773e3df033d1e545ec57009e3a7ac/filter-demo/target/classes/com/zhengxl/filterdemo/filter/MyAnotherFilter.class -------------------------------------------------------------------------------- /filter-demo/target/classes/com/zhengxl/filterdemo/filter/MyFilter.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengxl5566/springboot-demo/5d27efdf123773e3df033d1e545ec57009e3a7ac/filter-demo/target/classes/com/zhengxl/filterdemo/filter/MyFilter.class -------------------------------------------------------------------------------- /filter-demo/target/test-classes/com/zhengxl/filterdemo/FilterDemoApplicationTests.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengxl5566/springboot-demo/5d27efdf123773e3df033d1e545ec57009e3a7ac/filter-demo/target/test-classes/com/zhengxl/filterdemo/FilterDemoApplicationTests.class -------------------------------------------------------------------------------- /freemarker/Freemarker.xmind: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengxl5566/springboot-demo/5d27efdf123773e3df033d1e545ec57009e3a7ac/freemarker/Freemarker.xmind -------------------------------------------------------------------------------- /freemarker/src/main/java/com/zhengxl/freemarker/FreemarkerApplication.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.freemarker; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cache.annotation.EnableCaching; 6 | 7 | @SpringBootApplication 8 | public class FreemarkerApplication { 9 | 10 | public static void main(String[] args) { 11 | SpringApplication.run(FreemarkerApplication.class, args); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /freemarker/src/main/java/com/zhengxl/freemarker/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.freemarker.controller; 2 | 3 | import com.zhengxl.freemarker.service.UserService; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.ui.Model; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | 9 | import java.util.ArrayList; 10 | import java.util.Date; 11 | import java.util.List; 12 | 13 | /** 14 | * @description: 15 | * @projectName:freemarker 16 | * @see:com.zhengxl.freemarker.controller 17 | * @author:郑晓龙 18 | * @createTime:2020/12/17 14:34 19 | * @version:1.0 20 | */ 21 | @Controller 22 | public class IndexController { 23 | @Autowired 24 | private UserService userService; 25 | 26 | @GetMapping(value={"/","index"}) 27 | public String index(Model model) { 28 | // 放入单个值 29 | model.addAttribute("userName","Java 课代表"); 30 | 31 | // 放入集合 32 | model.addAttribute("allUsers",userService.getAllUser()); 33 | 34 | // 放入日期 35 | model.addAttribute("currentTime",new Date()); 36 | 37 | 38 | List xs = new ArrayList<>(); 39 | xs.add("1"); 40 | xs.add("2"); 41 | xs.add("3"); 42 | xs.add(null); 43 | model.addAttribute("xs",xs); 44 | 45 | // 返回模板名称 46 | return "index"; 47 | 48 | } 49 | 50 | @GetMapping("built-ins") 51 | public String builtIns(){ 52 | return "built-ins"; 53 | } 54 | 55 | } 56 | 57 | // model.addAttribute("user",new User("zhengxl5566",18)); 58 | // 59 | // model.addAttribute("userService",userService); 60 | 61 | // model.addAttribute("timeStamp",1608535755L); 62 | // System.out.println("executing" + name); 63 | // String content = "<p>2222222222222222222</p>"; 64 | // String content2 = "

2222222222222222222

"; -------------------------------------------------------------------------------- /freemarker/src/main/java/com/zhengxl/freemarker/controller/StaticMethodController.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.freemarker.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | 6 | /** 7 | * @description:展示FTL中调用Java静态方法的用法 8 | * @projectName:freemarker 9 | * @see:com.zhengxl.freemarker.controller 10 | * @author:郑晓龙 11 | * @createTime:2020/12/28 14:13 12 | * @version:1.0 13 | */ 14 | @Controller 15 | public class StaticMethodController { 16 | @GetMapping("static") 17 | public String staticMethodView(){ 18 | return "static-method-view"; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /freemarker/src/main/java/com/zhengxl/freemarker/controller/UserDefinedDirectiveController.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.freemarker.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | 6 | /** 7 | * @description: 展示用户自定义指令的用法 8 | * @projectName:freemarker 9 | * @see:com.zhengxl.freemarker.controller 10 | * @author:郑晓龙 11 | * @createTime:2020/12/28 14:14 12 | * @version:1.0 13 | */ 14 | @Controller 15 | public class UserDefinedDirectiveController { 16 | @GetMapping("directive") 17 | public String userDefinedDirective(){ 18 | return "directive"; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /freemarker/src/main/java/com/zhengxl/freemarker/directive/MyDirective.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.freemarker.directive; 2 | 3 | import com.zhengxl.freemarker.service.UserService; 4 | import freemarker.core.Environment; 5 | import freemarker.template.*; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Component; 8 | 9 | import java.io.IOException; 10 | import java.util.Iterator; 11 | import java.util.Map; 12 | 13 | /** 14 | * @description: 15 | * @projectName:freemarker 16 | * @see:com.zhengxl.freemarker.directive 17 | * @author:郑晓龙 18 | * @createTime:2020/12/28 15:47 19 | * @version:1.0 20 | */ 21 | @Component 22 | public class MyDirective implements TemplateDirectiveModel { 23 | @Autowired 24 | private UserService userService; 25 | 26 | @Override 27 | public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { 28 | 29 | Iterator iterator = params.entrySet().iterator(); 30 | while (iterator.hasNext()) { 31 | Map.Entry entry = (Map.Entry) iterator.next(); 32 | String paramName = (String) entry.getKey(); 33 | String paramValue = entry.getValue().toString(); 34 | if (paramName.equals("method")) { 35 | if(paramValue.equals("getAllUsers")){ 36 | DefaultObjectWrapper build = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_30).build(); 37 | TemplateModel wrap = build.wrap(userService.getAllUser()); 38 | loopVars[0] = wrap; 39 | } 40 | if(paramValue.equals("getUser")){ 41 | DefaultObjectWrapper build = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_30).build(); 42 | TemplateModel wrap = build.wrap(userService.getUser(params.get("userName").toString())); 43 | loopVars[0] = wrap; 44 | } 45 | } 46 | } 47 | 48 | if (body != null) { 49 | body.render(env.getOut()); 50 | } 51 | 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /freemarker/src/main/java/com/zhengxl/freemarker/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.freemarker.entity; 2 | 3 | /** 4 | * @description: 5 | * @projectName:freemarker 6 | * @see:com.zhengxl.freemarker.entity 7 | * @author:郑晓龙 8 | * @createTime:2020/12/17 16:31 9 | * @version:1.0 10 | */ 11 | public class User { 12 | private String name; 13 | private Integer age; 14 | 15 | public User(String name, Integer age) { 16 | this.name = name; 17 | this.age = age; 18 | } 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public void setName(String name) { 25 | this.name = name; 26 | } 27 | 28 | public Integer getAge() { 29 | return age; 30 | } 31 | 32 | public void setAge(Integer age) { 33 | this.age = age; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /freemarker/src/main/java/com/zhengxl/freemarker/properties/FreeMarkerProperties.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.freemarker.properties; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | import org.springframework.context.annotation.Primary; 5 | import org.springframework.stereotype.Component; 6 | 7 | /** 8 | * @description: 9 | * @projectName:freemarker 10 | * @see:com.zhengxl.freemarker.properties 11 | * @author:郑晓龙 12 | * @createTime:2021/1/5 14:57 13 | * @version:1.0 14 | */ 15 | @Component 16 | @Primary 17 | @ConfigurationProperties(prefix = "spring.freemarker") 18 | public class FreeMarkerProperties extends org.springframework.boot.autoconfigure.freemarker.FreeMarkerProperties{ 19 | /** 20 | * 需要引入的静态工具类的包路径 21 | */ 22 | private String[] staticToolPackagePath; 23 | 24 | public String[] getStaticToolPackagePath() { 25 | return staticToolPackagePath; 26 | } 27 | 28 | public void setStaticToolPackagePath(String[] staticToolPackagePath) { 29 | this.staticToolPackagePath = staticToolPackagePath; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /freemarker/src/main/java/com/zhengxl/freemarker/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.freemarker.service; 2 | 3 | import com.zhengxl.freemarker.entity.User; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @description: 9 | * @projectName:freemarker 10 | * @see:com.zhengxl.freemarker.service 11 | * @author:郑晓龙 12 | * @createTime:2020/12/17 16:31 13 | * @version:1.0 14 | */ 15 | public interface UserService { 16 | List getAllUser(); 17 | 18 | Integer insert(User user); 19 | 20 | User getUser(String userName); 21 | } 22 | -------------------------------------------------------------------------------- /freemarker/src/main/java/com/zhengxl/freemarker/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.freemarker.service.impl; 2 | 3 | import com.zhengxl.freemarker.entity.User; 4 | import com.zhengxl.freemarker.service.UserService; 5 | import org.springframework.stereotype.Service; 6 | 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | /** 11 | * @description: 12 | * @projectName:freemarker 13 | * @see:com.zhengxl.freemarker.service.impl 14 | * @author:郑晓龙 15 | * @createTime:2020/12/17 16:33 16 | * @version:1.0 17 | */ 18 | @Service 19 | public class UserServiceImpl implements UserService { 20 | @Override 21 | public List getAllUser() { 22 | List users = new ArrayList<>(); 23 | users.add(new User("zxl", 18)); 24 | users.add(new User("ls", 19)); 25 | users.add(new User("zs", 16)); 26 | return users; 27 | } 28 | 29 | @Override 30 | public Integer insert(User user) { 31 | System.out.println("Insert OK!"); 32 | return 1; 33 | } 34 | 35 | @Override 36 | public User getUser(String userName) { 37 | return new User(userName, 20); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /freemarker/src/main/java/com/zhengxl/freemarker/tools/AnotherStaticTool.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.freemarker.tools; 2 | 3 | /** 4 | * @description: 5 | * @projectName:freemarker 6 | * @see:com.zhengxl.freemarker.tools 7 | * @author:郑晓龙 8 | * @createTime:2021/1/5 14:50 9 | * @version:1.0 10 | */ 11 | public class AnotherStaticTool { 12 | public static String sayHello(){ 13 | return "hello!"; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /freemarker/src/main/java/com/zhengxl/freemarker/tools/UserStaticTool.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.freemarker.tools; 2 | 3 | import com.google.common.collect.Maps; 4 | import com.zhengxl.freemarker.entity.User; 5 | 6 | import java.util.Map; 7 | 8 | /** 9 | * @description: 10 | * @projectName:freemarker 11 | * @see:com.zhengxl.freemarker.tools 12 | * @author:郑晓龙 13 | * @createTime:2020/12/24 16:56 14 | * @version:1.0 15 | */ 16 | public class UserStaticTool { 17 | public static User getUser(String userName){ 18 | Map userMap = Maps.newHashMap(); 19 | userMap.put("zhangsan",new User("zhangsan", 18)); 20 | userMap.put("lisi",new User("lisi", 19)); 21 | userMap.put("wangwu",new User("wangwu", 20)); 22 | return userMap.get(userName); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /freemarker/src/main/resources/META-INF/additional-spring-configuration-metadata.json: -------------------------------------------------------------------------------- 1 | { 2 | "properties": [ 3 | { 4 | "name": "spring.freemarker.staticToolPackagePath", 5 | "type": "java.lang.String", 6 | "description": "the class path of static method that you want to expose to the FTL." 7 | } 8 | ] } -------------------------------------------------------------------------------- /freemarker/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | freemarker: 3 | cache: false 4 | suffix: .ftlh 5 | staticToolPackagePath: 6 | - com.zhengxl.freemarker.tools.UserStaticTool 7 | - com.zhengxl.freemarker.tools.AnotherStaticTool 8 | server: 9 | port: 8080 -------------------------------------------------------------------------------- /freemarker/src/main/resources/templates/built-ins.ftlh: -------------------------------------------------------------------------------- 1 | <#--字符串类型内置函数样例--> 2 | <#--创建字符串--> 3 | <#assign myStr = "Java 课代表"> 4 | <#--首字母小写--> 5 | ${myStr?uncap_first}
6 | <#--保留指定字符后面的字符串--> 7 | ${myStr?keep_after("Java")}
8 | <#--替换指定字符--> 9 | ${myStr?replace("Java","Freemarker")}
10 | 11 | <#--时间类型内置函数样例--> 12 | <#--获取当前时间(如果是后端将时间传入data-model,只需要传Date类型即可)--> 13 | <#assign currentDateTime = .now> 14 | <#--展示日期部分--> 15 | ${currentDateTime?date}
16 | <#--展示时间部分--> 17 | ${currentDateTime?time}
18 | <#--展示日期和时间部分--> 19 | ${currentDateTime?datetime}
20 | <#--按指定格式展示时间日期--> 21 | ${currentDateTime?string("yyyy-MM-dd HH:mm a")}
22 | 23 | <#--序列类型内置函数样例--> 24 | <#assign mySequence = ["Java 课代表","张三","李四","王五"]> 25 | <#--将所有元素以指定分隔符分割,输出字符串--> 26 | ${mySequence?join(",")}
27 | <#--取序列中的第一个元素--> 28 | ${mySequence?first}
29 | <#--将序列排序后使用逗号分割输出为字符串--> 30 | ${mySequence?sort?join(",")}
31 | 32 | <#global g = 123> -------------------------------------------------------------------------------- /freemarker/src/main/resources/templates/directive.ftlh: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | Document 9 | 10 | 11 | 12 | <#--返回值为集合的自定义指令--> 13 | <@myDirective method="getAllUsers" ;users> 14 | <#list users as user> 15 | ${user.name} 16 | 17 | 18 |
19 | <#--演示带参数的自定义指令--> 20 | <@myDirective method="getUser" userName="zhengxl5566";user> 21 | ${user.name} ${user.age} 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /freemarker/src/main/resources/templates/footer.ftlh: -------------------------------------------------------------------------------- 1 |
2 |

这里是 footer

-------------------------------------------------------------------------------- /freemarker/src/main/resources/templates/inner-method.ftlh: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | inner-method 9 | 10 | 11 | <#--内部方法调用展示--> 12 | 13 | <#--String 内部方法调用展示--> 14 | <#assign myString = "this is a very long string"> 15 | 长度:${myString?length}
16 | 首字母大写:${myString?cap_first}
17 | 全部大写:${myString?upper_case}
18 | 查找指定字符串:${myString?index_of("long")}
19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /freemarker/src/main/resources/templates/lib/bbb.ftlh: -------------------------------------------------------------------------------- 1 | <#assign bbb = "bbb"> -------------------------------------------------------------------------------- /freemarker/src/main/resources/templates/lib/common.ftlh: -------------------------------------------------------------------------------- 1 | <#macro ulList seq> 2 | <#list seq> 3 |
    4 | <#items as item> 5 |
  • 6 | ${item} 7 |
  • 8 | 9 |
10 | 11 | 12 | 13 | 14 | <#macro olList seq> 15 | <#list seq> 16 |
    17 | <#items as item> 18 |
  1. 19 | ${item} 20 |
  2. 21 | 22 |
23 | 24 | 25 | 26 | <#macro myFooter> 27 |
28 |

这里是 footer

29 | -------------------------------------------------------------------------------- /freemarker/src/main/resources/templates/lib/example.ftlh: -------------------------------------------------------------------------------- 1 | <#macro copyright date> 2 |

Copyright (C) ${date} Someone. All rights reserved.

3 | 4 | 5 | <#assign mail = "user@example.com"> -------------------------------------------------------------------------------- /freemarker/src/main/resources/templates/static-method-view.ftlh: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | static-method-view 9 | 10 | 11 | <#--展示模板中调用java静态方法--> 12 | <#--UserStaticTool 已通过配置类加载到共享变量中,可查看 FreemarkerConfig 相关配置--> 13 | <#assign user = UserStaticTool.getUser("zhangsan")> 14 | 姓名:${user.name} 年龄:${user.age} 15 | 16 |
17 | <#assign res = AnotherStaticTool.sayHello()> 18 |

${res}

19 | 20 | 21 | -------------------------------------------------------------------------------- /interceptor-demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.3.1.RELEASE 9 | 10 | 11 | com.zhengxl 12 | interceptor-demo 13 | 0.0.1-SNAPSHOT 14 | interceptor-demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-web 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-test 30 | test 31 | 32 | 33 | org.junit.vintage 34 | junit-vintage-engine 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-maven-plugin 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /interceptor-demo/src/main/java/com/zhengxl/interceptordemo/InterceptorDemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.interceptordemo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.web.servlet.ServletComponentScan; 6 | 7 | @SpringBootApplication 8 | public class InterceptorDemoApplication { 9 | 10 | public static void main(String[] args) { 11 | SpringApplication.run(InterceptorDemoApplication.class, args); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /interceptor-demo/src/main/java/com/zhengxl/interceptordemo/config/WebMVCConfig.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.interceptordemo.config; 2 | 3 | import com.zhengxl.interceptordemo.interceptor.AnotherInterceptor; 4 | import com.zhengxl.interceptordemo.interceptor.MyInterceptor; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.context.annotation.Configuration; 7 | import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 8 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 9 | 10 | /** 11 | * @description: 12 | * @projectName:interceptor-demo 13 | * @see:com.zhengxl.interceptordemo.config.CustomWebMVCConfigurer 14 | * @author:郑晓龙 15 | * @createTime:2020/9/26 10:04 16 | * @version:1.0 17 | */ 18 | @Configuration 19 | public class WebMVCConfig implements WebMvcConfigurer { 20 | @Autowired 21 | MyInterceptor myInterceptor; 22 | @Autowired 23 | AnotherInterceptor anotherInterceptor; 24 | 25 | @Override 26 | public void addInterceptors(InterceptorRegistry registry) { 27 | registry.addInterceptor(myInterceptor); 28 | registry.addInterceptor(anotherInterceptor); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /interceptor-demo/src/main/java/com/zhengxl/interceptordemo/controller/HelloWorldController.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.interceptordemo.controller; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | /** 10 | * @description: 11 | * @projectName:interceptor-demo 12 | * @see:com.zhengxl.interceptordemo.controller 13 | * @author:郑晓龙 14 | * @createTime:2020/9/26 10:04 15 | * @version:1.0 16 | */ 17 | @RestController 18 | public class HelloWorldController { 19 | private static Logger logger = LoggerFactory.getLogger(HelloWorldController.class); 20 | 21 | @GetMapping("hello") 22 | public String Hello(){ 23 | logger.info("hello world controller!"); 24 | return "hello,world!"; 25 | } 26 | } -------------------------------------------------------------------------------- /interceptor-demo/src/main/java/com/zhengxl/interceptordemo/interceptor/AnotherInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.interceptordemo.interceptor; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.lang.Nullable; 6 | import org.springframework.stereotype.Component; 7 | import org.springframework.web.servlet.HandlerInterceptor; 8 | import org.springframework.web.servlet.ModelAndView; 9 | 10 | import javax.servlet.http.HttpServletRequest; 11 | import javax.servlet.http.HttpServletResponse; 12 | 13 | /** 14 | * @description: 15 | * @projectName:interceptor-demo 16 | * @see:com.zhengxl.interceptordemo.interceptor 17 | * @author:郑晓龙 18 | * @createTime:2020/9/26 11:19 19 | * @version:1.0 20 | */ 21 | @Component 22 | public class AnotherInterceptor implements HandlerInterceptor { 23 | private static Logger logger = LoggerFactory.getLogger(AnotherInterceptor.class); 24 | 25 | @Override 26 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) 27 | throws Exception { 28 | logger.info("AnotherInterceptor preHandle"); 29 | return true; 30 | } 31 | 32 | @Override 33 | public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, 34 | @Nullable ModelAndView modelAndView) throws Exception { 35 | logger.info("AnotherInterceptor postHandle"); 36 | } 37 | @Override 38 | public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, 39 | @Nullable Exception ex) throws Exception { 40 | logger.info("AnotherInterceptor afterCompletion"); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /interceptor-demo/src/main/java/com/zhengxl/interceptordemo/interceptor/MyInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.interceptordemo.interceptor; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.stereotype.Component; 6 | import org.springframework.web.servlet.HandlerInterceptor; 7 | import org.springframework.web.servlet.ModelAndView; 8 | 9 | import javax.servlet.http.HttpServletRequest; 10 | import javax.servlet.http.HttpServletResponse; 11 | 12 | /** 13 | * @description: 14 | * @projectName:interceptor-demo 15 | * @see:com.zhengxl.interceptordemo.interceptor 16 | * @author:郑晓龙 17 | * @createTime:2020/9/26 10:06 18 | * @version:1.0 19 | */ 20 | @Component 21 | public class MyInterceptor implements HandlerInterceptor { 22 | private static Logger logger = LoggerFactory.getLogger(MyInterceptor.class); 23 | @Override 24 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) 25 | throws Exception { 26 | logger.info("MyInterceptor preHandle"); 27 | return true; 28 | 29 | } 30 | 31 | @Override 32 | public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { 33 | logger.info("MyInterceptor postHandle"); 34 | } 35 | 36 | @Override 37 | public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { 38 | logger.info("MyInterceptor afterCompletion"); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /mybatis/dynamic-multiple-data-sources/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | -------------------------------------------------------------------------------- /mybatis/dynamic-multiple-data-sources/src/main/java/top/javahelper/dynamicmultipledatasources/DynamicMultipleDataSourcesApplication.java: -------------------------------------------------------------------------------- 1 | package top.javahelper.dynamicmultipledatasources; 2 | 3 | import org.mybatis.spring.annotation.MapperScan; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | 7 | @SpringBootApplication 8 | @MapperScan(value = "top.javahelper.dynamicmultipledatasources.mapper") 9 | public class DynamicMultipleDataSourcesApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(DynamicMultipleDataSourcesApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /mybatis/dynamic-multiple-data-sources/src/main/java/top/javahelper/dynamicmultipledatasources/aspect/DataSourceAspect.java: -------------------------------------------------------------------------------- 1 | package top.javahelper.dynamicmultipledatasources.aspect; 2 | 3 | import org.aspectj.lang.ProceedingJoinPoint; 4 | import org.aspectj.lang.annotation.Around; 5 | import org.aspectj.lang.annotation.Aspect; 6 | import org.springframework.core.Ordered; 7 | import org.springframework.core.annotation.Order; 8 | import org.springframework.stereotype.Component; 9 | import top.javahelper.dynamicmultipledatasources.common.RoutingDataSourceContext; 10 | import top.javahelper.dynamicmultipledatasources.common.WithDataSource; 11 | 12 | /** 13 | * 数据源切面 14 | * 根据注解 @WithDataSource 的 value 切换数据源 15 | * 16 | * @author :Java课代表 17 | */ 18 | @Aspect 19 | @Component 20 | // 指定优先级高于@Transactional 21 | // 从而保证先切换数据源再进行事务操作 22 | @Order(Ordered.LOWEST_PRECEDENCE - 1) 23 | public class DataSourceAspect { 24 | 25 | @Around("@annotation(withDataSource)") 26 | public Object switchDataSource(ProceedingJoinPoint pjp, WithDataSource withDataSource) throws Throwable { 27 | 28 | // 1.获取 @WithDataSource 注解中指定的数据源 29 | String routingKey = withDataSource.value(); 30 | // 2.设置数据源上下文 31 | RoutingDataSourceContext.setRoutingKey(routingKey); 32 | // 3.使用设定好的数据源处理业务 33 | try { 34 | return pjp.proceed(); 35 | } finally { 36 | // 4.清空数据源上下文 37 | RoutingDataSourceContext.reset(); 38 | } 39 | } 40 | } 41 | 42 | -------------------------------------------------------------------------------- /mybatis/dynamic-multiple-data-sources/src/main/java/top/javahelper/dynamicmultipledatasources/common/RoutingDataSource.java: -------------------------------------------------------------------------------- 1 | package top.javahelper.dynamicmultipledatasources.common; 2 | 3 | import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; 4 | 5 | /** 6 | * 支持动态切换的数据源 7 | * 通过重写 determineCurrentLookupKey 实现数据源切换 8 | * @author :Java课代表 9 | */ 10 | public class RoutingDataSource extends AbstractRoutingDataSource { 11 | 12 | @Override 13 | protected Object determineCurrentLookupKey() { 14 | return RoutingDataSourceContext.getRoutingKey(); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /mybatis/dynamic-multiple-data-sources/src/main/java/top/javahelper/dynamicmultipledatasources/common/RoutingDataSourceContext.java: -------------------------------------------------------------------------------- 1 | package top.javahelper.dynamicmultipledatasources.common; 2 | 3 | /** 4 | * 数据源 key 上下文 5 | * 通过控制 ThreadLocal变量 LOOKUP_KEY_HOLDER 的值用于控制数据源切换 6 | * @see RoutingDataSource 7 | * @author :Java课代表 8 | */ 9 | public class RoutingDataSourceContext { 10 | 11 | private static final ThreadLocal LOOKUP_KEY_HOLDER = new ThreadLocal<>(); 12 | 13 | public static void setRoutingKey(String routingKey) { 14 | LOOKUP_KEY_HOLDER.set(routingKey); 15 | } 16 | 17 | public static String getRoutingKey() { 18 | String key = LOOKUP_KEY_HOLDER.get(); 19 | // 默认返回 key 为 first 的数据源 20 | return key == null ? "first" : key; 21 | } 22 | 23 | public static void reset() { 24 | LOOKUP_KEY_HOLDER.remove(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /mybatis/dynamic-multiple-data-sources/src/main/java/top/javahelper/dynamicmultipledatasources/common/WithDataSource.java: -------------------------------------------------------------------------------- 1 | package top.javahelper.dynamicmultipledatasources.common; 2 | 3 | import java.lang.annotation.*; 4 | 5 | /** 6 | * @author :Java课代表 7 | */ 8 | @Target({ElementType.TYPE, ElementType.METHOD}) 9 | @Retention(RetentionPolicy.RUNTIME) 10 | @Documented 11 | public @interface WithDataSource { 12 | String value() default ""; 13 | } 14 | -------------------------------------------------------------------------------- /mybatis/dynamic-multiple-data-sources/src/main/java/top/javahelper/dynamicmultipledatasources/config/H2DataBaseInit.java: -------------------------------------------------------------------------------- 1 | package top.javahelper.dynamicmultipledatasources.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.ComponentScan; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.jdbc.core.JdbcTemplate; 7 | import top.javahelper.dynamicmultipledatasources.common.RoutingDataSourceContext; 8 | 9 | import javax.annotation.PostConstruct; 10 | 11 | /** 12 | * 初始化两个数据源的表结构 13 | * 为了演示方便,使用了内存数据库 H2,所以需要初始化一下 14 | * 这里也演示了手工切换数据源的基本用法 15 | * @author :Java课代表 16 | */ 17 | @Configuration 18 | @ComponentScan() 19 | public class H2DataBaseInit { 20 | @Autowired 21 | JdbcTemplate jdbcTemplate; 22 | 23 | @PostConstruct 24 | public void init() { 25 | // 手工切换为数据源 first,初始化表 26 | RoutingDataSourceContext.setRoutingKey("first"); 27 | createTableUser(); 28 | RoutingDataSourceContext.reset(); 29 | 30 | // 手工切换为数据源 second,初始化表 31 | RoutingDataSourceContext.setRoutingKey("second"); 32 | createTableUser(); 33 | RoutingDataSourceContext.reset(); 34 | 35 | } 36 | 37 | private void createTableUser() { 38 | jdbcTemplate.update("CREATE TABLE user\n" + 39 | " (\n" + 40 | " id VARCHAR(50) NOT NULL AUTO_INCREMENT,\n" + 41 | " name VARCHAR(50) NOT NULL,\n" + 42 | " PRIMARY KEY (id)\n" + 43 | " )"); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /mybatis/dynamic-multiple-data-sources/src/main/java/top/javahelper/dynamicmultipledatasources/config/RoutingDataSourcesConfig.java: -------------------------------------------------------------------------------- 1 | package top.javahelper.dynamicmultipledatasources.config; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | import org.springframework.boot.jdbc.DataSourceBuilder; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | import org.springframework.context.annotation.Primary; 8 | import top.javahelper.dynamicmultipledatasources.common.RoutingDataSource; 9 | 10 | import javax.sql.DataSource; 11 | import java.util.HashMap; 12 | import java.util.Map; 13 | 14 | /** 15 | * 数据源配置 16 | * 把多个数据源,装配到一个 RoutingDataSource 里 17 | * @author :Java课代表 18 | */ 19 | @Configuration 20 | public class RoutingDataSourcesConfig { 21 | 22 | @Bean 23 | @ConfigurationProperties(prefix = "spring.datasource.first") 24 | public DataSource firstDataSource() { 25 | return DataSourceBuilder.create().build(); 26 | } 27 | 28 | @Bean 29 | @ConfigurationProperties(prefix = "spring.datasource.second") 30 | public DataSource secondDataSource() { 31 | return DataSourceBuilder.create().build(); 32 | } 33 | 34 | @Primary 35 | @Bean 36 | public RoutingDataSource routingDataSource() { 37 | RoutingDataSource routingDataSource = new RoutingDataSource(); 38 | routingDataSource.setDefaultTargetDataSource(firstDataSource()); 39 | Map dataSourceMap = new HashMap<>(); 40 | dataSourceMap.put("first", firstDataSource()); 41 | dataSourceMap.put("second", secondDataSource()); 42 | routingDataSource.setTargetDataSources(dataSourceMap); 43 | return routingDataSource; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /mybatis/dynamic-multiple-data-sources/src/main/java/top/javahelper/dynamicmultipledatasources/model/User.java: -------------------------------------------------------------------------------- 1 | package top.javahelper.dynamicmultipledatasources.model; 2 | 3 | public class User { 4 | /** 5 | * 6 | * This field was generated by MyBatis Generator. 7 | * This field corresponds to the database column USER.ID 8 | * 9 | * @mbg.generated Wed Dec 15 10:20:55 CST 2021 10 | */ 11 | private String id; 12 | 13 | /** 14 | * 15 | * This field was generated by MyBatis Generator. 16 | * This field corresponds to the database column USER.NAME 17 | * 18 | * @mbg.generated Wed Dec 15 10:20:55 CST 2021 19 | */ 20 | private String name; 21 | 22 | /** 23 | * This method was generated by MyBatis Generator. 24 | * This method returns the value of the database column USER.ID 25 | * 26 | * @return the value of USER.ID 27 | * 28 | * @mbg.generated Wed Dec 15 10:20:55 CST 2021 29 | */ 30 | public String getId() { 31 | return id; 32 | } 33 | 34 | /** 35 | * This method was generated by MyBatis Generator. 36 | * This method sets the value of the database column USER.ID 37 | * 38 | * @param id the value for USER.ID 39 | * 40 | * @mbg.generated Wed Dec 15 10:20:55 CST 2021 41 | */ 42 | public void setId(String id) { 43 | this.id = id; 44 | } 45 | 46 | /** 47 | * This method was generated by MyBatis Generator. 48 | * This method returns the value of the database column USER.NAME 49 | * 50 | * @return the value of USER.NAME 51 | * 52 | * @mbg.generated Wed Dec 15 10:20:55 CST 2021 53 | */ 54 | public String getName() { 55 | return name; 56 | } 57 | 58 | /** 59 | * This method was generated by MyBatis Generator. 60 | * This method sets the value of the database column USER.NAME 61 | * 62 | * @param name the value for USER.NAME 63 | * 64 | * @mbg.generated Wed Dec 15 10:20:55 CST 2021 65 | */ 66 | public void setName(String name) { 67 | this.name = name; 68 | } 69 | } -------------------------------------------------------------------------------- /mybatis/dynamic-multiple-data-sources/src/main/java/top/javahelper/dynamicmultipledatasources/service/UserService.java: -------------------------------------------------------------------------------- 1 | package top.javahelper.dynamicmultipledatasources.service; 2 | 3 | import top.javahelper.dynamicmultipledatasources.model.User; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @author :Java课代表 9 | */ 10 | public interface UserService { 11 | 12 | void insert(User user); 13 | 14 | void insertWithTx(User user); 15 | 16 | List selectAll(); 17 | } 18 | -------------------------------------------------------------------------------- /mybatis/dynamic-multiple-data-sources/src/main/java/top/javahelper/dynamicmultipledatasources/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package top.javahelper.dynamicmultipledatasources.service.impl; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | import top.javahelper.dynamicmultipledatasources.mapper.UserMapper; 6 | import top.javahelper.dynamicmultipledatasources.model.User; 7 | import top.javahelper.dynamicmultipledatasources.service.UserService; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * @author :Java课代表 13 | */ 14 | @Service 15 | public class UserServiceImpl implements UserService { 16 | @Autowired 17 | UserMapper userMapper; 18 | 19 | @Override 20 | public void insert(User user) { 21 | userMapper.insert(user); 22 | } 23 | 24 | @Override 25 | public void insertWithTx(User user) { 26 | userMapper.insert(user); 27 | throw new RuntimeException("just for rollBack"); 28 | } 29 | 30 | 31 | @Override 32 | public List selectAll() { 33 | return userMapper.selectAll(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /mybatis/dynamic-multiple-data-sources/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | first: 4 | driver-class-name: org.h2.Driver 5 | jdbc-url: jdbc:h2:mem:db1 6 | username: sa 7 | password: 8 | second: 9 | driver-class-name: org.h2.Driver 10 | jdbc-url: jdbc:h2:mem:db2 11 | username: sa 12 | password: 13 | 14 | logging: 15 | level: 16 | top.javahelper.dynamicmultipledatasources.mapper: debug -------------------------------------------------------------------------------- /mybatis/dynamic-multiple-data-sources/src/main/resources/generatorConfig.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 |
20 |
-------------------------------------------------------------------------------- /mybatis/dynamic-multiple-data-sources/src/test/java/top/javahelper/dynamicmultipledatasources/DynamicMultipleDataSourcesApplicationTests.java: -------------------------------------------------------------------------------- 1 | package top.javahelper.dynamicmultipledatasources; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class DynamicMultipleDataSourcesApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /mybatis/dynamic-multiple-data-sources/src/test/java/top/javahelper/dynamicmultipledatasources/MBG.java: -------------------------------------------------------------------------------- 1 | package top.javahelper.dynamicmultipledatasources; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.mybatis.generator.api.MyBatisGenerator; 5 | import org.mybatis.generator.config.Configuration; 6 | import org.mybatis.generator.config.xml.ConfigurationParser; 7 | import org.mybatis.generator.exception.InvalidConfigurationException; 8 | import org.mybatis.generator.exception.XMLParserException; 9 | import org.mybatis.generator.internal.DefaultShellCallback; 10 | import org.springframework.boot.test.context.SpringBootTest; 11 | 12 | import java.io.IOException; 13 | import java.sql.SQLException; 14 | import java.util.ArrayList; 15 | import java.util.List; 16 | 17 | /** 18 | * @description: 19 | * @projectName:dynamic-multiple-data-sources 20 | * @see:top.javahelper.dynamicmultipledatasources 21 | * @author:郑晓龙 22 | * @createTime:2021/12/1 17:34 23 | * @version:1.0 24 | */ 25 | @SpringBootTest 26 | public class MBG { 27 | @Test 28 | public void generate() throws XMLParserException, IOException, InvalidConfigurationException, SQLException, InterruptedException { 29 | List warnings = new ArrayList<>(); 30 | ConfigurationParser cp = new ConfigurationParser(warnings); 31 | Configuration config = cp.parseConfiguration( 32 | this.getClass().getResourceAsStream("/generatorConfig.xml")); 33 | DefaultShellCallback callback = new DefaultShellCallback(true); 34 | MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); 35 | myBatisGenerator.generate(null); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /mybatis/dynamic-multiple-data-sources/src/test/java/top/javahelper/dynamicmultipledatasources/mapper/UserMapperTest.java: -------------------------------------------------------------------------------- 1 | package top.javahelper.dynamicmultipledatasources.mapper; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import top.javahelper.dynamicmultipledatasources.model.User; 7 | 8 | /** 9 | * @author:郑晓龙 10 | */ 11 | @SpringBootTest 12 | public class UserMapperTest { 13 | @Autowired 14 | private UserMapper userMapper; 15 | @Test 16 | public void test(){ 17 | User user = new User(); 18 | user.setName("zxl"); 19 | userMapper.insert(user); 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /mybatis/multiple-data-sources/src/main/java/top/javahelper/multipledatasources/MultipleDataSourcesApplication.java: -------------------------------------------------------------------------------- 1 | package top.javahelper.multipledatasources; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | 7 | @SpringBootApplication 8 | public class MultipleDataSourcesApplication { 9 | 10 | public static void main(String[] args) { 11 | SpringApplication.run(MultipleDataSourcesApplication.class, args); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /mybatis/multiple-data-sources/src/main/java/top/javahelper/multipledatasources/config/InitData.java: -------------------------------------------------------------------------------- 1 | package top.javahelper.multipledatasources.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Configuration; 5 | import top.javahelper.multipledatasources.mapper.first.FirstUserMapper; 6 | import top.javahelper.multipledatasources.mapper.second.SecondUserMapper; 7 | 8 | import javax.annotation.PostConstruct; 9 | 10 | /** 11 | * @description: 12 | * @author:Java课代表 13 | * @createTime:2021/11/3 23:13 14 | */ 15 | @Configuration 16 | public class InitData { 17 | @Autowired 18 | FirstUserMapper firstUserMapper; 19 | @Autowired 20 | SecondUserMapper secondUserMapper; 21 | 22 | @PostConstruct 23 | public void init() { 24 | firstUserMapper.init(); 25 | secondUserMapper.init(); 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /mybatis/multiple-data-sources/src/main/java/top/javahelper/multipledatasources/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package top.javahelper.multipledatasources.controller; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Controller; 5 | import org.springframework.web.bind.annotation.*; 6 | import top.javahelper.multipledatasources.entity.User; 7 | import top.javahelper.multipledatasources.service.UserService; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * @description: 13 | * @author:Java课代表 14 | * @createTime:2021/11/29 23:22 15 | */ 16 | @RestController 17 | public class UserController { 18 | @Autowired 19 | UserService userService; 20 | 21 | @GetMapping("allUsersFromFirst") 22 | public List allUsersFromFirst() { 23 | return userService.getAllFirstDBUsers(); 24 | 25 | } 26 | @GetMapping("allUsersFromSecond") 27 | public List allUsersFromSecond() { 28 | return userService.getAllSecondDBUsers(); 29 | } 30 | @PostMapping("insertTwoDB") 31 | public void insertTwoDB(@RequestParam String name){ 32 | 33 | userService.insertTwoDBWithTX(name); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /mybatis/multiple-data-sources/src/main/java/top/javahelper/multipledatasources/entity/User.java: -------------------------------------------------------------------------------- 1 | package top.javahelper.multipledatasources.entity; 2 | 3 | /** 4 | * @description: 5 | * @author:Java课代表 6 | * @createTime:2021/11/3 23:13 7 | */ 8 | public class User { 9 | private Long id; 10 | private String name; 11 | 12 | public Long getId() { 13 | return id; 14 | } 15 | 16 | public void setId(Long id) { 17 | this.id = id; 18 | } 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public void setName(String name) { 25 | this.name = name; 26 | } 27 | 28 | @Override 29 | public String toString() { 30 | return "User{" + 31 | "id=" + id + 32 | ", name='" + name + '\'' + 33 | '}'; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /mybatis/multiple-data-sources/src/main/java/top/javahelper/multipledatasources/mapper/first/FirstUserMapper.java: -------------------------------------------------------------------------------- 1 | package top.javahelper.multipledatasources.mapper.first; 2 | 3 | import top.javahelper.multipledatasources.entity.User; 4 | 5 | import java.util.List; 6 | 7 | 8 | public interface FirstUserMapper { 9 | 10 | void init(); 11 | 12 | /** 13 | * This method was generated by MyBatis Generator. 14 | * This method corresponds to the database table user_info 15 | * 16 | * @mbg.generated 17 | */ 18 | int deleteByPrimaryKey(Integer id); 19 | 20 | /** 21 | * This method was generated by MyBatis Generator. 22 | * This method corresponds to the database table user_info 23 | * 24 | * @mbg.generated 25 | */ 26 | int insert(User record); 27 | 28 | /** 29 | * This method was generated by MyBatis Generator. 30 | * This method corresponds to the database table user_info 31 | * 32 | * @mbg.generated 33 | */ 34 | User selectByPrimaryKey(Integer id); 35 | 36 | /** 37 | * This method was generated by MyBatis Generator. 38 | * This method corresponds to the database table user_info 39 | * 40 | * @mbg.generated 41 | */ 42 | List selectAll(); 43 | 44 | /** 45 | * This method was generated by MyBatis Generator. 46 | * This method corresponds to the database table user_info 47 | * 48 | * @mbg.generated 49 | */ 50 | int updateByPrimaryKey(User record); 51 | } 52 | -------------------------------------------------------------------------------- /mybatis/multiple-data-sources/src/main/java/top/javahelper/multipledatasources/mapper/second/SecondUserMapper.java: -------------------------------------------------------------------------------- 1 | package top.javahelper.multipledatasources.mapper.second; 2 | 3 | import top.javahelper.multipledatasources.entity.User; 4 | 5 | import java.util.List; 6 | 7 | 8 | public interface SecondUserMapper { 9 | 10 | void init(); 11 | /** 12 | * This method was generated by MyBatis Generator. 13 | * This method corresponds to the database table user_info 14 | * 15 | * @mbg.generated 16 | */ 17 | int deleteByPrimaryKey(Integer id); 18 | 19 | /** 20 | * This method was generated by MyBatis Generator. 21 | * This method corresponds to the database table user_info 22 | * 23 | * @mbg.generated 24 | */ 25 | int insert(User record); 26 | 27 | /** 28 | * This method was generated by MyBatis Generator. 29 | * This method corresponds to the database table user_info 30 | * 31 | * @mbg.generated 32 | */ 33 | User selectByPrimaryKey(Integer id); 34 | 35 | /** 36 | * This method was generated by MyBatis Generator. 37 | * This method corresponds to the database table user_info 38 | * 39 | * @mbg.generated 40 | */ 41 | List selectAll(); 42 | 43 | /** 44 | * This method was generated by MyBatis Generator. 45 | * This method corresponds to the database table user_info 46 | * 47 | * @mbg.generated 48 | */ 49 | int updateByPrimaryKey(User record); 50 | } 51 | -------------------------------------------------------------------------------- /mybatis/multiple-data-sources/src/main/java/top/javahelper/multipledatasources/service/UserService.java: -------------------------------------------------------------------------------- 1 | package top.javahelper.multipledatasources.service; 2 | 3 | import top.javahelper.multipledatasources.entity.User; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @description: 9 | * @author:Java课代表 10 | * @createTime:2021/11/29 23:49 11 | */ 12 | public interface UserService { 13 | 14 | void insertTwoDBWithTX(String name); 15 | 16 | List getAllFirstDBUsers(); 17 | 18 | List getAllSecondDBUsers(); 19 | 20 | 21 | } 22 | -------------------------------------------------------------------------------- /mybatis/multiple-data-sources/src/main/java/top/javahelper/multipledatasources/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package top.javahelper.multipledatasources.service.impl; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | import org.springframework.transaction.annotation.Transactional; 6 | import top.javahelper.multipledatasources.entity.User; 7 | import top.javahelper.multipledatasources.mapper.first.FirstUserMapper; 8 | import top.javahelper.multipledatasources.mapper.second.SecondUserMapper; 9 | import top.javahelper.multipledatasources.service.UserService; 10 | 11 | import java.util.List; 12 | 13 | /** 14 | * @description: 15 | * @author:Java课代表 16 | * @createTime:2021/11/29 23:23 17 | */ 18 | @Service 19 | public class UserServiceImpl implements UserService { 20 | @Autowired 21 | FirstUserMapper firstUserMapper; 22 | @Autowired 23 | SecondUserMapper secondUserMapper; 24 | 25 | @Override 26 | // 不显式指定参数 transactionManager 则会使用设置为 Primary 的 firstTransactionManager 27 | // 如下代码只会回滚 firstUserMapper.insert, secondUserMapper.insert(user2);会正常插入 28 | @Transactional(rollbackFor = Throwable.class,transactionManager = "firstTransactionManager") 29 | public void insertTwoDBWithTX(String name) { 30 | User user = new User(); 31 | user.setName(name); 32 | // 会回滚 33 | firstUserMapper.insert(user); 34 | // 不会回滚 35 | secondUserMapper.insert(user); 36 | 37 | // 主动触发回滚 38 | // int i = 1/0; 39 | } 40 | 41 | @Override 42 | public List getAllFirstDBUsers() { 43 | return firstUserMapper.selectAll(); 44 | } 45 | 46 | @Override 47 | public List getAllSecondDBUsers() { 48 | return secondUserMapper.selectAll(); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /mybatis/multiple-data-sources/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8080 3 | spring: 4 | datasource: 5 | first: 6 | driver-class-name: org.h2.Driver 7 | jdbc-url: jdbc:h2:mem:db1 8 | username: sa 9 | password: 10 | second: 11 | driver-class-name: org.h2.Driver 12 | jdbc-url: jdbc:h2:mem:db2 13 | username: sa 14 | password: 15 | # autoconfigure: 16 | # exclude: 17 | # - org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration 18 | # - org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration 19 | # - org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration 20 | # - org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration 21 | 22 | #management: 23 | # endpoints: 24 | # web: 25 | # exposure: 26 | # include: beans 27 | -------------------------------------------------------------------------------- /mybatis/multiple-data-sources/src/main/resources/db1.user.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Premium Data Transfer 3 | 4 | Source Server : MyDB 5 | Source Server Type : MySQL 6 | Source Server Version : 80027 7 | Source Host : 123.57.211.17:3306 8 | Source Schema : db2 9 | 10 | Target Server Type : MySQL 11 | Target Server Version : 80027 12 | File Encoding : 65001 13 | 14 | Date: 05/11/2021 09:18:16 15 | */ 16 | 17 | SET NAMES utf8mb4; 18 | SET 19 | FOREIGN_KEY_CHECKS = 0; 20 | 21 | -- ---------------------------- 22 | -- Table structure for user 23 | -- ---------------------------- 24 | DROP TABLE IF EXISTS `user`; 25 | CREATE TABLE `user` 26 | ( 27 | `id` int(0) NOT NULL AUTO_INCREMENT COMMENT '自增主键', 28 | `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '姓名', 29 | PRIMARY KEY (`id`) USING BTREE 30 | ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic; 31 | 32 | -- ---------------------------- 33 | -- Records of user 34 | -- ---------------------------- 35 | INSERT INTO `user` 36 | VALUES (1, 'java课代表'); 37 | 38 | SET 39 | FOREIGN_KEY_CHECKS = 1; 40 | -------------------------------------------------------------------------------- /mybatis/multiple-data-sources/src/main/resources/db2.user.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Premium Data Transfer 3 | 4 | Source Server : MyDB 5 | Source Server Type : MySQL 6 | Source Server Version : 80027 7 | Source Host : 123.57.211.17:3306 8 | Source Schema : db2 9 | 10 | Target Server Type : MySQL 11 | Target Server Version : 80027 12 | File Encoding : 65001 13 | 14 | Date: 05/11/2021 09:18:16 15 | */ 16 | 17 | SET NAMES utf8mb4; 18 | SET 19 | FOREIGN_KEY_CHECKS = 0; 20 | 21 | -- ---------------------------- 22 | -- Table structure for user 23 | -- ---------------------------- 24 | DROP TABLE IF EXISTS `user`; 25 | CREATE TABLE `user` 26 | ( 27 | `id` int(0) NOT NULL AUTO_INCREMENT COMMENT '自增主键', 28 | `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '姓名', 29 | PRIMARY KEY (`id`) USING BTREE 30 | ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic; 31 | 32 | -- ---------------------------- 33 | -- Records of user 34 | -- ---------------------------- 35 | INSERT INTO `user` 36 | VALUES (1, 'zhengxl'); 37 | 38 | SET 39 | FOREIGN_KEY_CHECKS = 1; 40 | -------------------------------------------------------------------------------- /mybatis/multiple-data-sources/src/test/java/top/javahelper/multipledatasources/MultipleDataSourcesApplicationTests.java: -------------------------------------------------------------------------------- 1 | package top.javahelper.multipledatasources; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class MultipleDataSourcesApplicationTests { 8 | @Test 9 | void contextLoads() { 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /mybatis/multiple-data-sources/src/test/java/top/javahelper/multipledatasources/mapper/UserMapperTest.java: -------------------------------------------------------------------------------- 1 | package top.javahelper.multipledatasources.mapper; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.util.Assert; 7 | import top.javahelper.multipledatasources.entity.User; 8 | import top.javahelper.multipledatasources.mapper.first.FirstUserMapper; 9 | import top.javahelper.multipledatasources.mapper.second.SecondUserMapper; 10 | 11 | import java.util.List; 12 | 13 | @SpringBootTest 14 | public class UserMapperTest { 15 | @Autowired 16 | FirstUserMapper firstUserMapper; 17 | @Autowired 18 | SecondUserMapper secondUserMapper; 19 | 20 | @Test 21 | public void test() { 22 | User user1 = new User(); 23 | user1.setName("Java 课代表"); 24 | 25 | firstUserMapper.insert(user1); 26 | List firstUsers = firstUserMapper.selectAll(); 27 | 28 | System.out.println(firstUsers); 29 | 30 | User user2 = new User(); 31 | user2.setName("zhengxl"); 32 | 33 | secondUserMapper.insert(user2); 34 | List secondUsers = secondUserMapper.selectAll(); 35 | 36 | System.out.println(secondUsers); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /mybatis/mybatis-cache/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/** 5 | !**/src/test/** 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | 30 | ### VS Code ### 31 | .vscode/ 32 | -------------------------------------------------------------------------------- /mybatis/mybatis-cache/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengxl5566/springboot-demo/5d27efdf123773e3df033d1e545ec57009e3a7ac/mybatis/mybatis-cache/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /mybatis/mybatis-cache/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.2/apache-maven-3.6.2-bin.zip 2 | wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.5/maven-wrapper-0.5.5.jar 3 | -------------------------------------------------------------------------------- /mybatis/mybatis-cache/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.1.RELEASE 9 | 10 | 11 | com.zhengxl 12 | mybatiscache 13 | 0.0.1-SNAPSHOT 14 | mybatiscache 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-web 25 | 26 | 27 | org.mybatis.spring.boot 28 | mybatis-spring-boot-starter 29 | 2.1.1 30 | 31 | 32 | 33 | com.h2database 34 | h2 35 | runtime 36 | 37 | 38 | org.projectlombok 39 | lombok 40 | true 41 | 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-starter-test 46 | test 47 | 48 | 49 | org.junit.vintage 50 | junit-vintage-engine 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | org.springframework.boot 60 | spring-boot-maven-plugin 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /mybatis/mybatis-cache/src/main/java/com/zhengxl/mybatiscache/MybatiscacheApplication.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.mybatiscache; 2 | 3 | import org.mybatis.spring.annotation.MapperScan; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | 7 | @SpringBootApplication 8 | @MapperScan({"com.zhengxl.mybatiscache.mapper"}) 9 | public class MybatiscacheApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(MybatiscacheApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /mybatis/mybatis-cache/src/main/java/com/zhengxl/mybatiscache/controller/Hello.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.mybatiscache.controller; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | /** 7 | * @description: 8 | * @projectName:mybatiscache 9 | * @see:com.zhengxl.mybatiscache.controller 10 | * @author:郑晓龙 11 | * @createTime:2019/11/16 15:48 12 | * @version:1.0 13 | */ 14 | @RestController 15 | public class Hello { 16 | @GetMapping("/hello") 17 | public String hello(){ 18 | return "hello"; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /mybatis/mybatis-cache/src/main/java/com/zhengxl/mybatiscache/dto/UserDTO.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.mybatiscache.dto; 2 | 3 | import lombok.Data; 4 | 5 | import java.io.Serializable; 6 | 7 | /** 8 | * @description: 9 | * @projectName:mybatiscache 10 | * @see:com.zhengxl.mybatiscache 11 | * @author:郑晓龙 12 | * @createTime:2019/11/16 22:10 13 | * @version:1.0 14 | */ 15 | @Data 16 | public class UserDTO implements Serializable { 17 | private static final long serialVersionUID = 8031791326949440737L; 18 | /** 19 | * 用户Id自增主键 20 | **/ 21 | Integer id; 22 | /** 23 | * 姓名 24 | **/ 25 | String name; 26 | /** 27 | * 密码 28 | **/ 29 | String passWord; 30 | /** 31 | * 货物名称 32 | **/ 33 | String goodName; 34 | } 35 | -------------------------------------------------------------------------------- /mybatis/mybatis-cache/src/main/java/com/zhengxl/mybatiscache/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.mybatiscache.entity; 2 | 3 | import lombok.Data; 4 | 5 | import java.io.Serializable; 6 | 7 | /** 8 | * @description: 9 | * @projectName:mybatiscache 10 | * @see:com.zhengxl.mybatiscache.entity 11 | * @author:郑晓龙 12 | * @createTime:2019/11/15 16:01 13 | * @version:1.0 14 | */ 15 | @Data 16 | public class User implements Serializable { 17 | /** 18 | * 用户Id自增主键 19 | **/ 20 | Integer id; 21 | /** 22 | * 姓名 23 | **/ 24 | String name; 25 | /** 26 | * 密码 27 | **/ 28 | String passWord; 29 | } 30 | -------------------------------------------------------------------------------- /mybatis/mybatis-cache/src/main/java/com/zhengxl/mybatiscache/entity/UserOrder.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.mybatiscache.entity; 2 | 3 | import lombok.Data; 4 | 5 | import java.io.Serializable; 6 | 7 | /** 8 | * @description: 9 | * @projectName:mybatiscache 10 | * @see:com.zhengxl.mybatiscache.entity 11 | * @author:郑晓龙 12 | * @createTime:2019/11/16 16:31 13 | * @version:1.0 14 | */ 15 | @Data 16 | public class UserOrder implements Serializable { 17 | private static final long serialVersionUID = 6179013399637503174L; 18 | /** 19 | * 订单Id自增主键 20 | **/ 21 | Integer id; 22 | /** 23 | * 用户Id 24 | **/ 25 | Integer userId; 26 | /** 27 | * 货物名称 28 | **/ 29 | String goodName; 30 | } 31 | -------------------------------------------------------------------------------- /mybatis/mybatis-cache/src/main/java/com/zhengxl/mybatiscache/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.mybatiscache.mapper; 2 | 3 | import com.zhengxl.mybatiscache.entity.User; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @description: 9 | * @projectName:mybatiscache 10 | * @see:com.zhengxl.mybatiscache.mapper 11 | * @author:郑晓龙 12 | * @createTime:2019/11/15 16:14 13 | * @version:1.0 14 | */ 15 | public interface UserMapper { 16 | /** 17 | * description 根据用户Id查询用户 18 | **/ 19 | User selectById(Integer id); 20 | /** 21 | * description 联查user_order表 22 | **/ 23 | List selectJoin(); 24 | /** 25 | * description 添加用户 26 | **/ 27 | Integer saveUser(User user); 28 | } 29 | -------------------------------------------------------------------------------- /mybatis/mybatis-cache/src/main/java/com/zhengxl/mybatiscache/mapper/UserOrderMapper.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.mybatiscache.mapper; 2 | 3 | import com.zhengxl.mybatiscache.entity.User; 4 | import com.zhengxl.mybatiscache.entity.UserOrder; 5 | 6 | /** 7 | * @description: 8 | * @projectName:mybatiscache 9 | * @see:com.zhengxl.mybatiscache.mapper 10 | * @author:郑晓龙 11 | * @createTime:2019/11/15 16:14 12 | * @version:1.0 13 | */ 14 | public interface UserOrderMapper { 15 | /** 16 | * description 根据用户Id查询用户 17 | **/ 18 | UserOrder selectById(Integer id); 19 | 20 | Integer saveOrder(UserOrder userOrder); 21 | } 22 | -------------------------------------------------------------------------------- /mybatis/mybatis-cache/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 80 3 | spring: 4 | h2: 5 | console: 6 | #H2数据库页面地址:http://localhost/h2-console 7 | path: /h2-console 8 | settings: 9 | web-allow-others: true 10 | enabled: true 11 | datasource: 12 | url: jdbc:h2:mem:mydb 13 | username: sa 14 | password: 15 | driver-class-name: org.h2.Driver 16 | schema: classpath*:schema.sql 17 | data: classpath*:data.sql 18 | application: 19 | name: MybatisCache 20 | 21 | mybatis: 22 | enable: true 23 | mapper-locations: classpath:mybatis/*.xml 24 | type-aliases-package: com.zhengxl.mybatiscache.entity 25 | configuration: 26 | #自动将表字段的下划线命名映射为驼峰命名 27 | map-underscore-to-camel-case: true 28 | #二级缓存总开关,mybatis默认开启二级缓存 29 | cache-enabled: true 30 | local-cache-scope: session 31 | 32 | logging: 33 | level: 34 | com.zhengxl.mybatiscache.mapper: debug 35 | -------------------------------------------------------------------------------- /mybatis/mybatis-cache/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO `user` VALUES (1, 'zhengxl', '123456'); 2 | INSERT INTO `user` VALUES (2, 'qiqi', '123'); 3 | INSERT INTO `user` VALUES (3, 'renbin', '123'); 4 | INSERT INTO `user_order` VALUES (1, 1, '123'); 5 | INSERT INTO `user_order` VALUES (2,1,'books'); 6 | INSERT INTO `user_order` VALUES (3,1,'foods'); 7 | INSERT INTO `user_order` VALUES (4,2,'foods'); -------------------------------------------------------------------------------- /mybatis/mybatis-cache/src/main/resources/mybatis/userMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | `id`,`name`,`pass_word` 12 | 13 | 17 | 22 | 23 | 24 | insert into user 25 | ( 26 | `name`, 27 | `pass_word` 28 | ) 29 | values 30 | ( 31 | #{name}, 32 | '9' 33 | ) 34 | 35 | -------------------------------------------------------------------------------- /mybatis/mybatis-cache/src/main/resources/mybatis/userOrderMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | `id`,`user_id`,`good_name` 9 | 10 | 14 | 15 | 16 | insert into user_order 17 | ( 18 | `user_id`, 19 | `good_name` 20 | ) 21 | values 22 | ( 23 | #{userId}, 24 | #{goodName} 25 | ) 26 | 27 | 28 | -------------------------------------------------------------------------------- /mybatis/mybatis-cache/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE user (ID INT IDENTITY, name VARCHAR(64),pass_word VARCHAR(64)); 2 | CREATE TABLE user_order (ID INT IDENTITY, user_id INT ,good_name VARCHAR(64)); -------------------------------------------------------------------------------- /mybatis/mybatis-cache/src/test/java/com/zhengxl/mybatiscache/MybatiscacheApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.mybatiscache; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | import org.springframework.transaction.annotation.Transactional; 6 | 7 | @SpringBootTest 8 | class MybatiscacheApplicationTests { 9 | 10 | @Test 11 | void contextLoads() { 12 | } 13 | 14 | 15 | } 16 | -------------------------------------------------------------------------------- /mybatis/mybatis-cache/src/test/java/com/zhengxl/mybatiscache/UserOrderTest.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.mybatiscache; 2 | 3 | import com.zhengxl.mybatiscache.mapper.UserOrderMapper; 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.test.context.SpringBootTest; 7 | 8 | /** 9 | * @description: 10 | * @projectName:mybatiscache 11 | * @see:com.zhengxl.mybatiscache 12 | * @author:郑晓龙 13 | * @createTime:2019/11/16 16:35 14 | * @version:1.0 15 | */ 16 | @SpringBootTest 17 | public class UserOrderTest { 18 | @Autowired 19 | UserOrderMapper userOrderMapper; 20 | @Test 21 | public void selectById(){ 22 | System.out.println(userOrderMapper.selectById(1)); 23 | System.out.println(userOrderMapper.selectById(1)); 24 | System.out.println(userOrderMapper.selectById(1)); 25 | } 26 | @Test 27 | public void saveOrder(){ 28 | //System.out.println(userOrderMapper.saveOrder("myGood")); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /mybatis/mybatis-cache/src/test/java/com/zhengxl/mybatiscache/UserTest.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.mybatiscache; 2 | 3 | import com.zhengxl.mybatiscache.entity.User; 4 | import com.zhengxl.mybatiscache.entity.UserOrder; 5 | import com.zhengxl.mybatiscache.mapper.UserMapper; 6 | import com.zhengxl.mybatiscache.mapper.UserOrderMapper; 7 | import org.junit.jupiter.api.Test; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.boot.test.context.SpringBootTest; 10 | import org.springframework.transaction.annotation.Transactional; 11 | 12 | /** 13 | * @description: 14 | * @projectName:mybatiscache 15 | * @see:com.zhengxl.mybatiscache 16 | * @author:郑晓龙 17 | * @createTime:2019/11/15 16:20 18 | * @version:1.0 19 | */ 20 | @SpringBootTest 21 | public class UserTest { 22 | @Autowired 23 | UserMapper userMapper; 24 | @Autowired 25 | UserOrderMapper userOrderMapper; 26 | /** 27 | * 开启事务,测试一级缓存效果 28 | * 缓存命中顺序:二级缓存---> 一级缓存---> 数据库 29 | **/ 30 | @Test 31 | @Transactional(rollbackFor = Throwable.class) 32 | public void testFistCache(){ 33 | // 第一次查询,缓存到一级缓存 34 | System.out.println(userMapper.selectById(1)); 35 | // 第二次查询,直接读取一级缓存 36 | System.out.println(userMapper.selectById(1)); 37 | 38 | } 39 | /** 40 | * 测试二级缓存效果 41 | * 需要*Mapper.xml开启二级缓存 42 | **/ 43 | @Test 44 | public void testSecondCache(){ 45 | userMapper.selectById(1); 46 | userMapper.selectById(1); 47 | } 48 | 49 | /** 50 | * 测试多表联查的二级缓存效果 51 | * 需要*Mapper.xml设定引用空间 52 | **/ 53 | @Test 54 | public void testJoin(){ 55 | System.out.println(userMapper.selectJoin()); 56 | System.out.println(userMapper.selectJoin()); 57 | UserOrder userOrder = new UserOrder(); 58 | userOrder.setGoodName("myGoods"); 59 | userOrder.setUserId(1); 60 | userOrderMapper.saveOrder(userOrder); 61 | System.out.println(userMapper.selectJoin()); 62 | 63 | } 64 | 65 | @Test 66 | public void saveUser(){ 67 | User user = new User(); 68 | user.setName("zhangxin"); 69 | user.setPassWord("123456"); 70 | userMapper.saveUser(user); 71 | System.out.println(user.getId()); 72 | System.out.println(user.getPassWord()); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /mybatis/mybatis-sample/src/main/java/com/zhengxl/mybatissample/MybatisSampleApplication.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.mybatissample; 2 | 3 | import org.mybatis.spring.annotation.MapperScan; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | 7 | @SpringBootApplication 8 | @MapperScan("com.zhengxl.mybatissample.mapper") 9 | public class MybatisSampleApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(MybatisSampleApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /mybatis/mybatis-sample/src/main/java/com/zhengxl/mybatissample/config/H2DataBaseInit.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.mybatissample.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.jdbc.core.JdbcTemplate; 6 | 7 | import javax.annotation.PostConstruct; 8 | 9 | /** 10 | * @description: 初始化内存数据库的表结构,用于自动生成代码 11 | * @projectName:mybatis-sample 12 | * @see:com.zhengxl.mybatissample.controller 13 | * @author:郑晓龙 14 | * @createTime:2021/12/12 21:11 15 | * @version:1.0 16 | */ 17 | @Configuration 18 | public class H2DataBaseInit { 19 | @Autowired 20 | JdbcTemplate jdbcTemplate; 21 | 22 | @PostConstruct 23 | public void init() { 24 | createTableUser(); 25 | } 26 | 27 | private void createTableUser() { 28 | jdbcTemplate.update("CREATE TABLE user\n" + 29 | " (\n" + 30 | " id VARCHAR(50) NOT NULL AUTO_INCREMENT,\n" + 31 | " name VARCHAR(50) NOT NULL,\n" + 32 | " PRIMARY KEY (id)\n" + 33 | " )"); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /mybatis/mybatis-sample/src/main/java/com/zhengxl/mybatissample/controller/MyController.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.mybatissample.controller; 2 | 3 | import com.zhengxl.mybatissample.model.User; 4 | import com.zhengxl.mybatissample.service.UserService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.*; 7 | 8 | import java.util.List; 9 | 10 | /** 11 | * @description: 12 | * @projectName:mybatis-sample 13 | * @see:com.zhengxl.mybatissample.controller 14 | * @author:郑晓龙 15 | * @createTime:2020/1/15 14:39 16 | * @version:1.0 17 | */ 18 | @RestController 19 | public class MyController { 20 | @Autowired 21 | UserService userService; 22 | 23 | @GetMapping("/hello") 24 | public String hello(){ 25 | return "hello!"; 26 | } 27 | 28 | @GetMapping("all") 29 | public List getAll(){ 30 | return userService.queryAll(); 31 | } 32 | 33 | @PostMapping("add") 34 | public int add(@RequestBody User user){ 35 | return userService.create(user); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /mybatis/mybatis-sample/src/main/java/com/zhengxl/mybatissample/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.mybatissample.mapper; 2 | 3 | import com.zhengxl.mybatissample.model.User; 4 | import java.util.List; 5 | 6 | public interface UserMapper { 7 | /** 8 | * This method was generated by MyBatis Generator. 9 | * This method corresponds to the database table USER 10 | * 11 | * @mbg.generated Mon Dec 13 10:57:56 CST 2021 12 | */ 13 | int deleteByPrimaryKey(String id); 14 | 15 | /** 16 | * This method was generated by MyBatis Generator. 17 | * This method corresponds to the database table USER 18 | * 19 | * @mbg.generated Mon Dec 13 10:57:56 CST 2021 20 | */ 21 | int insert(User record); 22 | 23 | /** 24 | * This method was generated by MyBatis Generator. 25 | * This method corresponds to the database table USER 26 | * 27 | * @mbg.generated Mon Dec 13 10:57:56 CST 2021 28 | */ 29 | User selectByPrimaryKey(String id); 30 | 31 | /** 32 | * This method was generated by MyBatis Generator. 33 | * This method corresponds to the database table USER 34 | * 35 | * @mbg.generated Mon Dec 13 10:57:56 CST 2021 36 | */ 37 | List selectAll(); 38 | 39 | /** 40 | * This method was generated by MyBatis Generator. 41 | * This method corresponds to the database table USER 42 | * 43 | * @mbg.generated Mon Dec 13 10:57:56 CST 2021 44 | */ 45 | int updateByPrimaryKey(User record); 46 | } -------------------------------------------------------------------------------- /mybatis/mybatis-sample/src/main/java/com/zhengxl/mybatissample/model/User.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.mybatissample.model; 2 | 3 | public class User { 4 | /** 5 | * 6 | * This field was generated by MyBatis Generator. 7 | * This field corresponds to the database column USER.ID 8 | * 9 | * @mbg.generated Mon Dec 13 10:57:56 CST 2021 10 | */ 11 | private String id; 12 | 13 | /** 14 | * 15 | * This field was generated by MyBatis Generator. 16 | * This field corresponds to the database column USER.NAME 17 | * 18 | * @mbg.generated Mon Dec 13 10:57:56 CST 2021 19 | */ 20 | private String name; 21 | 22 | /** 23 | * This method was generated by MyBatis Generator. 24 | * This method returns the value of the database column USER.ID 25 | * 26 | * @return the value of USER.ID 27 | * 28 | * @mbg.generated Mon Dec 13 10:57:56 CST 2021 29 | */ 30 | public String getId() { 31 | return id; 32 | } 33 | 34 | /** 35 | * This method was generated by MyBatis Generator. 36 | * This method sets the value of the database column USER.ID 37 | * 38 | * @param id the value for USER.ID 39 | * 40 | * @mbg.generated Mon Dec 13 10:57:56 CST 2021 41 | */ 42 | public void setId(String id) { 43 | this.id = id; 44 | } 45 | 46 | /** 47 | * This method was generated by MyBatis Generator. 48 | * This method returns the value of the database column USER.NAME 49 | * 50 | * @return the value of USER.NAME 51 | * 52 | * @mbg.generated Mon Dec 13 10:57:56 CST 2021 53 | */ 54 | public String getName() { 55 | return name; 56 | } 57 | 58 | /** 59 | * This method was generated by MyBatis Generator. 60 | * This method sets the value of the database column USER.NAME 61 | * 62 | * @param name the value for USER.NAME 63 | * 64 | * @mbg.generated Mon Dec 13 10:57:56 CST 2021 65 | */ 66 | public void setName(String name) { 67 | this.name = name; 68 | } 69 | } -------------------------------------------------------------------------------- /mybatis/mybatis-sample/src/main/java/com/zhengxl/mybatissample/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.mybatissample.service; 2 | 3 | import com.zhengxl.mybatissample.model.User; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @description: 9 | * @author:Java课代表 10 | * @createTime:2021/12/12 21:25 11 | */ 12 | public interface UserService { 13 | int create(User User); 14 | List queryAll(); 15 | } 16 | -------------------------------------------------------------------------------- /mybatis/mybatis-sample/src/main/java/com/zhengxl/mybatissample/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.mybatissample.service.impl; 2 | 3 | import com.zhengxl.mybatissample.mapper.UserMapper; 4 | import com.zhengxl.mybatissample.model.User; 5 | import com.zhengxl.mybatissample.service.UserService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * @description: 13 | * @author:Java课代表 14 | * @createTime:2021/12/12 21:26 15 | */ 16 | @Service 17 | public class UserServiceImpl implements UserService { 18 | @Autowired 19 | UserMapper userMapper; 20 | 21 | @Override 22 | public int create(User user) { 23 | int i = userMapper.insert(user); 24 | return i; 25 | } 26 | 27 | @Override 28 | public List queryAll() { 29 | return userMapper.selectAll(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /mybatis/mybatis-sample/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | #数据源配置 3 | datasource: 4 | driver-class-name: org.h2.Driver 5 | url: jdbc:h2:mem:db1 6 | username: sa 7 | password: 8 | 9 | mybatis: 10 | mapper-locations: classpath:mybatis/*.xml -------------------------------------------------------------------------------- /mybatis/mybatis-sample/src/main/resources/generatorConfig.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 |
20 |
-------------------------------------------------------------------------------- /mybatis/mybatis-sample/src/test/java/com/zhengxl/mybatissample/Mbg.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.mybatissample; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.mybatis.generator.api.MyBatisGenerator; 5 | import org.mybatis.generator.config.Configuration; 6 | import org.mybatis.generator.config.xml.ConfigurationParser; 7 | import org.mybatis.generator.exception.InvalidConfigurationException; 8 | import org.mybatis.generator.exception.XMLParserException; 9 | import org.mybatis.generator.internal.DefaultShellCallback; 10 | import org.springframework.boot.test.context.SpringBootTest; 11 | 12 | import java.io.IOException; 13 | import java.sql.SQLException; 14 | import java.util.ArrayList; 15 | import java.util.List; 16 | 17 | /** 18 | * @description: 19 | * @projectName:mybatis-sample 20 | * @see:com.zhengxl.mybatissample 21 | * @author:郑晓龙 22 | * @createTime:2020/1/15 15:09 23 | * @version:1.0 24 | */ 25 | @SpringBootTest 26 | public class Mbg { 27 | @Test 28 | public void test() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException { 29 | List warnings = new ArrayList<>(); 30 | ConfigurationParser cp = new ConfigurationParser(warnings); 31 | Configuration config = cp.parseConfiguration( 32 | this.getClass().getResourceAsStream("/generatorConfig.xml")); 33 | DefaultShellCallback callback = new DefaultShellCallback(true); 34 | MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); 35 | myBatisGenerator.generate(null); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /mybatis/mybatis-sample/src/test/java/com/zhengxl/mybatissample/MybatisSampleApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.mybatissample; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class MybatisSampleApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /mybatis/mybatis-sample/target/classes/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | #数据源配置 3 | datasource: 4 | driver-class-name: org.h2.Driver 5 | url: jdbc:h2:mem:db1 6 | username: sa 7 | password: 8 | 9 | mybatis: 10 | mapper-locations: classpath:mybatis/*.xml -------------------------------------------------------------------------------- /mybatis/mybatis-sample/target/classes/generatorConfig.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 |
20 |
-------------------------------------------------------------------------------- /redis-demo/src/main/java/com/zhengxl/redisdemo/config/RedisConfig.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.redisdemo.config; 2 | 3 | import com.zhengxl.redisdemo.core.ObjectRedisTemplate; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.data.redis.connection.RedisConnectionFactory; 7 | 8 | /** 9 | * @description: 10 | * @projectName:redis-demo 11 | * @see:com.zhengxl.redisdemo.config 12 | * @author:郑晓龙 13 | * @createTime:2020/10/27 14:16 14 | * @version:1.0 15 | */ 16 | @Configuration 17 | public class RedisConfig { 18 | @Bean 19 | public ObjectRedisTemplate objectRedisTemplate(RedisConnectionFactory redisConnectionFactory) { 20 | return new ObjectRedisTemplate(redisConnectionFactory); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /redis-demo/src/main/java/com/zhengxl/redisdemo/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.redisdemo.controller; 2 | 3 | import com.zhengxl.redisdemo.core.ObjectRedisTemplate; 4 | import com.zhengxl.redisdemo.entity.User; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | /** 10 | * @description: 11 | * @projectName:redis-demo 12 | * @see:com.zhengxl.redisdemo.controller 13 | * @author:郑晓龙 14 | * @createTime:2020/10/27 11:47 15 | * @version:1.0 16 | */ 17 | @RestController 18 | public class UserController { 19 | 20 | @Autowired 21 | ObjectRedisTemplate objectRedisTemplate; 22 | 23 | @GetMapping("string") 24 | public String string(String value){ 25 | objectRedisTemplate.opsForValue().set("zxl",value); 26 | return (String)objectRedisTemplate.opsForValue().get("zxl"); 27 | } 28 | 29 | @GetMapping("object") 30 | public User object(User user){ 31 | objectRedisTemplate.opsForValue().set("user",user); 32 | return (User)objectRedisTemplate.opsForValue().get("user"); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /redis-demo/src/main/java/com/zhengxl/redisdemo/core/ObjectRedisTemplate.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.redisdemo.core; 2 | 3 | import org.springframework.data.redis.connection.RedisConnectionFactory; 4 | import org.springframework.data.redis.core.RedisTemplate; 5 | import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; 6 | import org.springframework.data.redis.serializer.StringRedisSerializer; 7 | 8 | /** 9 | * @description: 10 | * @projectName:redis-demo 11 | * @see:com.zhengxl.redisdemo.config 12 | * @author:郑晓龙 13 | * @createTime:2020/10/27 14:34 14 | * @version:1.0 15 | */ 16 | public class ObjectRedisTemplate extends RedisTemplate { 17 | 18 | public ObjectRedisTemplate(RedisConnectionFactory connectionFactory) { 19 | setKeySerializer(new StringRedisSerializer()); 20 | setValueSerializer(new GenericJackson2JsonRedisSerializer()); 21 | setHashKeySerializer(new StringRedisSerializer()); 22 | setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); 23 | 24 | setConnectionFactory(connectionFactory); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /redis-demo/src/main/java/com/zhengxl/redisdemo/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.redisdemo.entity; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * @description: 7 | * @projectName:redis-demo 8 | * @see:com.zhengxl.redisdemo.entity 9 | * @author:郑晓龙 10 | * @createTime:2020/10/27 14:13 11 | * @version:1.0 12 | */ 13 | public class User implements Serializable { 14 | 15 | private static final long serialVersionUID = -509010839673409655L; 16 | private String name; 17 | private Integer age; 18 | 19 | public User() { 20 | } 21 | 22 | public User(String name, Integer age) { 23 | this.name = name; 24 | this.age = age; 25 | } 26 | 27 | public String getName() { 28 | return name; 29 | } 30 | 31 | public void setName(String name) { 32 | this.name = name; 33 | } 34 | 35 | public Integer getAge() { 36 | return age; 37 | } 38 | 39 | public void setAge(Integer age) { 40 | this.age = age; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /redis-demo/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /redis-demo/src/test/java/com/zhengxl/redisdemo/redis/RedisTest.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.redisdemo.redis; 2 | 3 | import com.zhengxl.redisdemo.entity.User; 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.test.context.SpringBootTest; 7 | import org.springframework.data.redis.core.RedisTemplate; 8 | import org.springframework.data.redis.core.StringRedisTemplate; 9 | 10 | /** 11 | * @description: 12 | * @projectName:redis-demo 13 | * @see:com.zhengxl.redisdemo.redis 14 | * @author:郑晓龙 15 | * @createTime:2020/10/27 14:10 16 | * @version:1.0 17 | */ 18 | @SpringBootTest 19 | public class RedisTest { 20 | @Autowired 21 | StringRedisTemplate stringRedisTemplate; 22 | @Autowired 23 | RedisTemplate redisTemplate; 24 | @Test 25 | public void redisTemplateTest(){ 26 | redisTemplate.opsForValue().set("user",new User("zhengxl",10)); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /redis/redis-transaction/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/** 5 | !**/src/test/** 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | 30 | ### VS Code ### 31 | .vscode/ 32 | -------------------------------------------------------------------------------- /redis/redis-transaction/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengxl5566/springboot-demo/5d27efdf123773e3df033d1e545ec57009e3a7ac/redis/redis-transaction/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /redis/redis-transaction/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.2/apache-maven-3.6.2-bin.zip 2 | wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.5/maven-wrapper-0.5.5.jar 3 | -------------------------------------------------------------------------------- /redis/redis-transaction/src/main/java/com/zhengxl/redistransaction/RedisTransactionApplication.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.redistransaction; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class RedisTransactionApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(RedisTransactionApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /redis/redis-transaction/src/main/java/com/zhengxl/redistransaction/config/RedisConfig.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.redistransaction.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.data.redis.connection.RedisConnectionFactory; 6 | import org.springframework.data.redis.core.StringRedisTemplate; 7 | import org.springframework.jdbc.datasource.DataSourceTransactionManager; 8 | import org.springframework.transaction.PlatformTransactionManager; 9 | import org.springframework.transaction.annotation.EnableTransactionManagement; 10 | 11 | import javax.sql.DataSource; 12 | 13 | /** 14 | * @description: 15 | * @projectName:redis-transaction 16 | * @see:com.zhengxl.redistransaction.config 17 | * @author:郑晓龙 18 | * @createTime:2019/12/12 16:12 19 | * @version:1.0 20 | */ 21 | @Configuration 22 | public class RedisConfig { 23 | @Bean 24 | public StringRedisTemplate StringRedisTemplate(RedisConnectionFactory factory) { 25 | StringRedisTemplate template = new StringRedisTemplate(factory); 26 | /** 27 | * description 开启redis事务(仅支持单机,不支持cluster) 28 | **/ 29 | template.setEnableTransactionSupport(true); 30 | return template; 31 | } 32 | 33 | /** 34 | * description 配置事务管理器 35 | **/ 36 | @Bean 37 | public PlatformTransactionManager transactionManager(DataSource dataSource){ 38 | return new DataSourceTransactionManager(dataSource); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /redis/redis-transaction/src/main/java/com/zhengxl/redistransaction/controller/RedisController.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.redistransaction.controller; 2 | 3 | import com.zhengxl.redistransaction.service.RedisService; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.data.redis.core.StringRedisTemplate; 6 | import org.springframework.transaction.annotation.Transactional; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | /** 12 | * @description: 13 | * @projectName:redis-transaction 14 | * @see:com.zhengxl.redistransaction.controller 15 | * @author:郑晓龙 16 | * @createTime:2019/12/12 16:05 17 | * @version:1.0 18 | */ 19 | @RestController 20 | @RequestMapping("redis") 21 | public class RedisController { 22 | @Autowired 23 | RedisService redisService; 24 | 25 | /** 26 | * description hello world! 27 | * return java.lang.String 28 | * author 郑晓龙 29 | * createTime 2019/12/12 16:36 30 | **/ 31 | @GetMapping("hello") 32 | public String hello() { 33 | return "hello,SpringBoot!"; 34 | } 35 | 36 | /** 37 | * description 不带事务set 38 | * return java.lang.String 39 | * author 郑晓龙 40 | * createTime 2019/12/12 16:36 41 | **/ 42 | @GetMapping("put") 43 | public void put(String key, String value) { 44 | redisService.put(key, value); 45 | } 46 | 47 | /** 48 | * description 带事务set 49 | * return java.lang.String 50 | * author 郑晓龙 51 | * createTime 2019/12/12 16:36 52 | **/ 53 | @GetMapping("putWithTx") 54 | public void putWithTx(String key, String value) { 55 | redisService.putWithTx(key, value); 56 | } 57 | 58 | /** 59 | * description 调用带事务方法不生效的情况 60 | * return java.lang.String 61 | * author 郑晓龙 62 | * createTime 2019/12/12 16:36 63 | **/ 64 | @GetMapping("invokeWithPutTx") 65 | public void invokeWithPutTx(String key, String value) { 66 | redisService.invokePutWithTx(key, value); 67 | } 68 | 69 | /** 70 | * description 调用带事务方法生效的情况 71 | * return java.lang.String 72 | * author 郑晓龙 73 | * createTime 2019/12/12 16:36 74 | **/ 75 | @GetMapping("invokeWithPutTx2") 76 | public void invokeWithPutTx2(String key, String value) { 77 | redisService.invokePutWithTx2(key, value); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /redis/redis-transaction/src/main/java/com/zhengxl/redistransaction/service/RedisService.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.redistransaction.service; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.data.redis.core.StringRedisTemplate; 5 | import org.springframework.stereotype.Service; 6 | import org.springframework.transaction.annotation.Transactional; 7 | 8 | /** 9 | * @description: 10 | * @projectName:redis-transaction 11 | * @see:com.zhengxl.redistransaction.service 12 | * @author:郑晓龙 13 | * @createTime:2019/12/12 16:27 14 | * @version:1.0 15 | */ 16 | @Service 17 | public class RedisService { 18 | @Autowired 19 | StringRedisTemplate stringRedisTemplate; 20 | @Autowired 21 | RedisService redisService; 22 | /** 23 | * description 不带事务写入 24 | **/ 25 | public void put(String key,String value){ 26 | stringRedisTemplate.opsForValue().set(key,value); 27 | } 28 | /** 29 | * description 带事务写入 30 | **/ 31 | @Transactional(rollbackFor = Throwable.class) 32 | public void putWithTx(String key,String value){ 33 | stringRedisTemplate.opsForValue().set(key,value); 34 | } 35 | /** 36 | * description 该方法演示内部调用带事务方法 事务不生效的情况 37 | **/ 38 | public void invokePutWithTx(String key,String value){ 39 | putWithTx(key,value); 40 | } 41 | /** 42 | * description 该方法演示内部调用带事务方法 事务生效的情况 43 | **/ 44 | public void invokePutWithTx2(String key,String value){ 45 | redisService.putWithTx(key,value); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /redis/redis-transaction/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8080 3 | 4 | spring: 5 | redis: 6 | port: 6379 7 | host: localhost 8 | # 9 | #logging: 10 | # level: debug 11 | # 12 | -------------------------------------------------------------------------------- /redis/redis-transaction/src/test/java/com/zhengxl/redistransaction/RedisTransactionApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.redistransaction; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class RedisTransactionApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /slf4j/logtest/src/main/java/zhengxl/App.java: -------------------------------------------------------------------------------- 1 | package zhengxl; 2 | 3 | 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | 7 | /** 8 | * Hello world! 9 | * 10 | */ 11 | public class App 12 | { 13 | private static final Logger logger = LoggerFactory.getLogger(App.class); 14 | 15 | public static void main( String[] args ) 16 | { 17 | logger.info("log test"); 18 | 19 | System.out.println( "Hello World!" ); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /slf4j/logtest/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=TRACE, STDOUT, ERRLOG 2 | log4j.appender.encoding=UTF-8 3 | 4 | #log to console 5 | log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender 6 | log4j.appender.STDOUT.Threshold=DEBUG 7 | log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout 8 | log4j.appender.STDOUT.layout.ConversionPattern=[%d][%-5p] (%F:%L) - %m%n 9 | 10 | #log to file 11 | log4j.appender.ERRLOG=org.apache.log4j.DailyRollingFileAppender 12 | log4j.appender.ERRLOG.Threshold=TRACE 13 | log4j.appender.ERRLOG.layout=org.apache.log4j.PatternLayout 14 | log4j.appender.ERRLOG.layout.ConversionPattern=[%d][%-5p] (%F:%L) - %m%n 15 | log4j.appender.ERRLOG.file=logs/error.log 16 | log4j.appender.ERRLOG.DatePattern=-yyyyMMdd -------------------------------------------------------------------------------- /slf4j/logtest/src/test/java/zhengxl/AppTest.java: -------------------------------------------------------------------------------- 1 | package zhengxl; 2 | 3 | import static org.junit.Assert.assertTrue; 4 | 5 | import org.junit.Test; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | { 12 | /** 13 | * Rigorous Test :-) 14 | */ 15 | @Test 16 | public void shouldAnswerWithTrue() 17 | { 18 | assertTrue( true ); 19 | } 20 | @Test 21 | public void test(){ 22 | 23 | System.out.println("hi"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /thymeleaf-demo/src/main/java/com/zhengxl/thymeleafdemo/ThymeleafDemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.thymeleafdemo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class ThymeleafDemoApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(ThymeleafDemoApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /thymeleaf-demo/src/main/java/com/zhengxl/thymeleafdemo/service/FileService.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.thymeleafdemo.service; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | 6 | /** 7 | * @description: 8 | * @projectName:thymeleaf-demo 9 | * @see:com.zhengxl.thymeleafdemo.service 10 | * @author:郑晓龙 11 | * @createTime:2020/8/8 10:40 12 | * @version:1.0 13 | */ 14 | public interface FileService { 15 | 16 | /** 17 | * @param clientFileName 客户端上传的文件名 18 | * @param inputStream 输入流 19 | * @return java.lang.String 服务端保存的文件名 20 | * @description 21 | * @author 郑晓龙 22 | * @createTime 2020/8/10 16:05 23 | **/ 24 | String saveInputStream(String clientFileName, InputStream inputStream) throws IOException; 25 | 26 | /** 27 | * @param serverFileName 服务端文件名 28 | * @return 文件输入流 29 | * @description 根据文件名获取文件 30 | * @author 郑晓龙 31 | * @createTime 2020/8/8 10:44 32 | **/ 33 | InputStream getInputStream(String serverFileName) throws IOException; 34 | 35 | /** 36 | * @param serverFileName 服务端文件名 37 | * @return java.lang.String 客户端文件名 38 | * @description 根据服务端文件名获取客户端文件名 39 | * @author 郑晓龙 40 | * @createTime 2020/8/10 16:09 41 | **/ 42 | String getClientFileName(String serverFileName); 43 | 44 | } 45 | -------------------------------------------------------------------------------- /thymeleaf-demo/src/main/java/com/zhengxl/thymeleafdemo/service/impl/FileServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.thymeleafdemo.service.impl; 2 | 3 | import com.zhengxl.thymeleafdemo.repository.FileRepository; 4 | import com.zhengxl.thymeleafdemo.service.FileService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | import java.io.IOException; 9 | import java.io.InputStream; 10 | 11 | /** 12 | * @description: 13 | * @projectName:thymeleaf-demo 14 | * @see:com.zhengxl.thymeleafdemo.service 15 | * @author:郑晓龙 16 | * @createTime:2020/8/7 13:47 17 | * @version:1.0 18 | */ 19 | @Service 20 | public class FileServiceImpl implements FileService { 21 | 22 | @Autowired 23 | FileRepository fileRepository; 24 | 25 | @Override 26 | public String saveInputStream(String clientFileName, InputStream inputStream) throws IOException { 27 | return fileRepository.saveFile(clientFileName,inputStream); 28 | 29 | } 30 | 31 | @Override 32 | public InputStream getInputStream(String serverFileName) throws IOException { 33 | return fileRepository.getInputStream(serverFileName); 34 | } 35 | 36 | @Override 37 | public String getClientFileName(String serverFileName) { 38 | return fileRepository.getClientFileName(serverFileName); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /thymeleaf-demo/src/main/java/com/zhengxl/thymeleafdemo/service/impl/MyService.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.thymeleafdemo.service.impl; 2 | 3 | import org.springframework.stereotype.Service; 4 | 5 | /** 6 | * @description: 7 | * @projectName:thymeleaf-demo 8 | * @see:com.zhengxl.thymeleafdemo.service.impl 9 | * @author:郑晓龙 10 | * @createTime:2020/12/22 9:40 11 | * @version:1.0 12 | */ 13 | @Service 14 | public class MyService { 15 | public boolean test(){ 16 | return false; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /thymeleaf-demo/src/main/java/com/zhengxl/thymeleafdemo/utils/HttpUtils.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.thymeleafdemo.utils; 2 | 3 | import javax.servlet.http.HttpServletRequest; 4 | import java.io.UnsupportedEncodingException; 5 | import java.net.URLEncoder; 6 | import java.nio.charset.StandardCharsets; 7 | 8 | /** 9 | * @description: 10 | * @projectName:thymeleaf-demo 11 | * @see:com.zhengxl.thymeleafdemo.utils 12 | * @author:郑晓龙 13 | * @createTime:2020/8/8 14:00 14 | * @version:1.0 15 | */ 16 | public class HttpUtils { 17 | public static String getDownloadFileName(HttpServletRequest request, String fileName) throws UnsupportedEncodingException { 18 | // 获取 agent 信息,统一转换为小写方便后续对比 19 | String agent = request.getHeader("USER-AGENT").toLowerCase(); 20 | String downLoadFileName; 21 | // 微软浏览器 22 | if (agent.contains("msie") || agent.contains("trident") || agent.contains("edge")) { 23 | downLoadFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString()) 24 | .replaceAll("\\+", "%20"); 25 | } 26 | // 火狐浏览器 27 | else if (agent.contains("firefox")) { 28 | downLoadFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString()) 29 | .replaceAll("\\+", "%20"); 30 | } 31 | // safari 浏览器 32 | else if (agent.contains("firefox")) { 33 | downLoadFileName = ""; 34 | } 35 | // Chrome 等其他浏览器 36 | else { 37 | downLoadFileName = new String(fileName.getBytes(), StandardCharsets.ISO_8859_1); 38 | } 39 | 40 | return downLoadFileName; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /thymeleaf-demo/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8080 3 | spring: 4 | thymeleaf: 5 | cache: false 6 | servlet: 7 | multipart: 8 | enabled: true 9 | max-file-size: 20MB 10 | max-request-size: 20MB 11 | 12 | file: 13 | storage: 14 | path: "d:/upload" -------------------------------------------------------------------------------- /thymeleaf-demo/src/main/resources/templates/error.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | Document 9 | 10 | 11 |

操作失败!

12 | 13 | 返回 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /thymeleaf-demo/src/main/resources/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 文件上传 7 | 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 | -------------------------------------------------------------------------------- /thymeleaf-demo/src/main/resources/templates/upload-success.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | Document 9 | 15 | 16 | 17 |

操作成功!

18 | 21 |
22 | 返回 23 |
24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /transactional/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | -------------------------------------------------------------------------------- /transactional/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhengxl5566/springboot-demo/5d27efdf123773e3df033d1e545ec57009e3a7ac/transactional/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /transactional/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.1/apache-maven-3.8.1-bin.zip 2 | wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar 3 | -------------------------------------------------------------------------------- /transactional/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.5.4 9 | 10 | 11 | com.javahelper.top 12 | transactional 13 | 0.0.1-SNAPSHOT 14 | transactional 15 | shows the usage of @Transactional with h2 database 16 | 17 | 1.8 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-web 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-jdbc 28 | 29 | 30 | 31 | com.h2database 32 | h2 33 | runtime 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-test 38 | test 39 | 40 | 41 | 42 | 43 | 44 | 45 | org.springframework.boot 46 | spring-boot-maven-plugin 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /transactional/src/main/java/com/javahelper/top/transactional/TransactionalApplication.java: -------------------------------------------------------------------------------- 1 | package com.javahelper.top.transactional; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class TransactionalApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(TransactionalApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /transactional/src/main/java/com/javahelper/top/transactional/exception/RollBackException.java: -------------------------------------------------------------------------------- 1 | package com.javahelper.top.transactional.exception; 2 | 3 | /** 4 | * @description: 5 | * @projectName:transactional 6 | * @see:com.javahelper.top.transactional.exception 7 | * @author:郑晓龙 8 | * @createTime:2021/8/23 15:05 9 | * @version:1.0 10 | */ 11 | public class RollBackException extends Exception { 12 | public RollBackException(String message) { 13 | super(message); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /transactional/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8080 3 | spring: 4 | datasource: 5 | driver-class-name: org.h2.Driver 6 | url: jdbc:h2:mem:test 7 | h2: 8 | console: 9 | enabled: true 10 | -------------------------------------------------------------------------------- /transactional/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE USER (ID INT IDENTITY, NAME VARCHAR(64)); 2 | CREATE TABLE CITY (ID INT IDENTITY, ADDRESS VARCHAR(64)); 3 | -------------------------------------------------------------------------------- /transactional/src/test/java/com/javahelper/top/transactional/TransactionalApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.javahelper.top.transactional; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class TransactionalApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /unittest-demo/src/main/java/com/zhengxl/unittestdemo/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.unittestdemo.controller; 2 | 3 | import com.zhengxl.unittestdemo.vo.UserVO; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | /** 12 | * @description: 13 | * @projectName:unittest-demo 14 | * @see:com.zhengxl.unittestdemo.controller 15 | * @author:郑晓龙 16 | * @createTime:2020/9/3 9:08 17 | * @version:1.0 18 | */ 19 | @RestController 20 | @RequestMapping("/user") 21 | public class UserController { 22 | @GetMapping("/list") 23 | public List listAllUsers() throws InterruptedException { 24 | UserVO userVO1 = new UserVO(); 25 | userVO1.setAge(10); 26 | userVO1.setName("name1"); 27 | 28 | UserVO userVO2 = new UserVO(); 29 | userVO2.setAge(20); 30 | userVO2.setName("name2"); 31 | 32 | List resList = new ArrayList<>(); 33 | resList.add(userVO1); 34 | resList.add(userVO2); 35 | return resList; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /unittest-demo/src/main/java/com/zhengxl/unittestdemo/utils/MathUtils.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.unittestdemo.utils; 2 | 3 | /** 4 | * @description: 5 | * @projectName:unittest-demo 6 | * @see:com.zhengxl.unittestdemo.utils 7 | * @author:郑晓龙 8 | * @createTime:2020/9/7 16:11 9 | * @version:1.0 10 | */ 11 | public class MathUtils { 12 | public static Long factorial(Long n) { 13 | if (n < 0) { 14 | throw new IllegalArgumentException(); 15 | } 16 | long result = 1; 17 | for (long i = 1; i <= n; i++) { 18 | result = result * i; 19 | } 20 | return result; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /unittest-demo/src/main/java/com/zhengxl/unittestdemo/vo/UserVO.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.unittestdemo.vo; 2 | 3 | import lombok.Data; 4 | 5 | /** 6 | * @description: 7 | * @projectName:unittest-demo 8 | * @see:com.zhengxl.unittestdemo.vo 9 | * @author:郑晓龙 10 | * @createTime:2020/9/3 9:09 11 | * @version:1.0 12 | */ 13 | @Data 14 | public class UserVO { 15 | private String name; 16 | private Integer age; 17 | } 18 | -------------------------------------------------------------------------------- /unittest-demo/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | tomcat: 3 | threads: 4 | max: 5 5 | 6 | accept-count: 1 -------------------------------------------------------------------------------- /unittest-demo/src/test/java/com/zhengxl/unittestdemo/controller/UserControllerTest.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.unittestdemo.controller; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.BeforeAll; 5 | import org.junit.jupiter.api.BeforeEach; 6 | import org.junit.jupiter.api.Test; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | 9 | import static org.junit.jupiter.api.Assertions.*; 10 | 11 | /** 12 | * @description: 13 | * @projectName:unittest-demo 14 | * @see:com.zhengxl.unittestdemo.controller 15 | * @author:郑晓龙 16 | * @createTime:2020/9/3 9:25 17 | * @version:1.0 18 | */ 19 | @SpringBootTest 20 | public class UserControllerTest { 21 | 22 | @BeforeAll 23 | public static void beforeAll(){ 24 | System.out.println("beforeAll"); 25 | } 26 | 27 | @BeforeEach 28 | public void beforeEach(){ 29 | System.out.println("before each"); 30 | } 31 | 32 | 33 | @Test 34 | public void test1(){ 35 | System.out.println(111); 36 | assertEquals(111,Integer.valueOf(111)); 37 | } 38 | 39 | @Test 40 | public void test2(){ 41 | System.out.println(22); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /unittest-demo/src/test/java/com/zhengxl/unittestdemo/utils/MathUtilsTest.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.unittestdemo.utils; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | 7 | /** 8 | * @description: 9 | * @projectName:unittest-demo 10 | * @see:com.zhengxl.unittestdemo.utils 11 | * @author:郑晓龙 12 | * @createTime:2020/9/7 16:20 13 | * @version:1.0 14 | */ 15 | @SpringBootTest 16 | public class MathUtilsTest { 17 | @Test 18 | public void test(){ 19 | Assertions.assertEquals(6L,MathUtils.factorial(3L)); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /validation-demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.3.1.RELEASE 9 | 10 | 11 | com.zhengxl 12 | validation-demo 13 | 0.0.1-SNAPSHOT 14 | validation-demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-validation 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-web 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-test 36 | test 37 | 38 | 39 | org.junit.vintage 40 | junit-vintage-engine 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | org.springframework.boot 50 | spring-boot-maven-plugin 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /validation-demo/src/main/java/com/zhengxl/validationdemo/ValidationDemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.validationdemo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class ValidationDemoApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(ValidationDemoApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /validation-demo/src/main/java/com/zhengxl/validationdemo/common/ResultInfo.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.validationdemo.common; 2 | 3 | import org.springframework.http.HttpStatus; 4 | 5 | import java.io.Serializable; 6 | 7 | /** 8 | * @description: 封装返回结果 9 | * @projectName:validation-demo 10 | * @see:com.zhengxl.validationdemo.controller 11 | * @author:郑晓龙 12 | * @createTime:2020/7/24 17:32 13 | * @version:1.0 14 | */ 15 | public class ResultInfo implements Serializable { 16 | 17 | private static final long serialVersionUID = 6793350277616007958L; 18 | 19 | private Integer status; 20 | private String message; 21 | private T response; 22 | 23 | public ResultInfo success() { 24 | this.status = HttpStatus.OK.value(); 25 | this.message = "OK"; 26 | return this; 27 | } 28 | 29 | public ResultInfo success(Integer status, String message) { 30 | this.status = status; 31 | this.message = message; 32 | return this; 33 | } 34 | 35 | public ResultInfo success(T response) { 36 | this.status = HttpStatus.OK.value(); 37 | this.message = "OK"; 38 | this.response = response; 39 | return this; 40 | } 41 | 42 | public ResultInfo success(Integer status, String message, T response) { 43 | this.status = status; 44 | this.message = message; 45 | this.response = response; 46 | return this; 47 | } 48 | 49 | public ResultInfo error(String message) { 50 | this.status = HttpStatus.INTERNAL_SERVER_ERROR.value(); 51 | this.message = message; 52 | return this; 53 | } 54 | 55 | public ResultInfo error(Integer status, String message) { 56 | this.status = status; 57 | this.message = message; 58 | return this; 59 | } 60 | 61 | public Integer getStatus() { 62 | return status; 63 | } 64 | 65 | public void setStatus(Integer status) { 66 | this.status = status; 67 | } 68 | 69 | public String getMessage() { 70 | return message; 71 | } 72 | 73 | public void setMessage(String message) { 74 | this.message = message; 75 | } 76 | 77 | public T getResponse() { 78 | return response; 79 | } 80 | 81 | public void setResponse(T response) { 82 | this.response = response; 83 | } 84 | } 85 | 86 | -------------------------------------------------------------------------------- /validation-demo/src/main/java/com/zhengxl/validationdemo/validator/annotation/HaveNoBlank.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.validationdemo.validator.annotation; 2 | 3 | import com.zhengxl.validationdemo.validator.constraint.HaveNoBlankValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import javax.validation.constraints.NotBlank; 8 | import java.lang.annotation.Documented; 9 | import java.lang.annotation.Retention; 10 | import java.lang.annotation.Target; 11 | 12 | import static java.lang.annotation.ElementType.*; 13 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 14 | 15 | /** 16 | * @description: 校验字符串中不能有空格 17 | * @projectName:validation-demo 18 | * @see:com.zhengxl.validationdemo.validator.annotation 19 | * @author:郑晓龙 20 | * @createTime:2020/7/29 11:27 21 | * @version:1.0 22 | */ 23 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER}) 24 | @Retention(RUNTIME) 25 | @Documented 26 | @Constraint(validatedBy = {HaveNoBlankValidator.class})// 标明由哪个类执行校验逻辑 27 | public @interface HaveNoBlank { 28 | 29 | // 校验出错时默认返回的消息 30 | String message() default "字符串中不能含有空格"; 31 | 32 | Class[] groups() default {}; 33 | 34 | Class[] payload() default {}; 35 | 36 | /** 37 | * 同一个元素上指定多个该注解时使用 38 | */ 39 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 40 | @Retention(RUNTIME) 41 | @Documented 42 | public @interface List { 43 | NotBlank[] value(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /validation-demo/src/main/java/com/zhengxl/validationdemo/validator/constraint/HaveNoBlankValidator.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.validationdemo.validator.constraint; 2 | 3 | import com.zhengxl.validationdemo.validator.annotation.HaveNoBlank; 4 | 5 | import javax.validation.ConstraintValidator; 6 | import javax.validation.ConstraintValidatorContext; 7 | 8 | /** 9 | * @description: 10 | * @projectName:validation-demo 11 | * @see:com.zhengxl.validationdemo.validator.constraint 12 | * @author:郑晓龙 13 | * @createTime:2020/7/29 11:30 14 | * @version:1.0 15 | */ 16 | public class HaveNoBlankValidator implements ConstraintValidator { 17 | @Override 18 | public boolean isValid(String value, ConstraintValidatorContext context) { 19 | // null 不做检验 20 | if (value == null) { 21 | return true; 22 | } 23 | if (value.contains(" ")) { 24 | // 校验失败 25 | return false; 26 | } 27 | // 校验成功 28 | return true; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /validation-demo/src/main/java/com/zhengxl/validationdemo/validator/validategroup/Update.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.validationdemo.validator.validategroup; 2 | 3 | import javax.validation.groups.Default; 4 | 5 | /** 6 | * @description: 7 | * @projectName:validation-demo 8 | * @see:com.zhengxl.validationdemo.validate 9 | * @author:郑晓龙 10 | * @createTime:2020/7/27 15:35 11 | * @version:1.0 12 | */ 13 | public interface Update extends Default { 14 | } 15 | -------------------------------------------------------------------------------- /validation-demo/src/main/java/com/zhengxl/validationdemo/vo/OrderVO.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.validationdemo.vo; 2 | 3 | import javax.validation.constraints.NotBlank; 4 | import javax.validation.constraints.NotNull; 5 | 6 | /** 7 | * @description: 8 | * @projectName:validation-demo 9 | * @see:com.zhengxl.validationdemo.vo 10 | * @author:郑晓龙 11 | * @createTime:2020/7/29 12:43 12 | * @version:1.0 13 | */ 14 | public class OrderVO { 15 | @NotNull(message = "id 不能为空") 16 | private Long id; 17 | @NotBlank(message = "itemName 不能为空") 18 | private String itemName; 19 | 20 | public Long getId() { 21 | return id; 22 | } 23 | 24 | public void setId(Long id) { 25 | this.id = id; 26 | } 27 | 28 | public String getItemName() { 29 | return itemName; 30 | } 31 | 32 | public void setItemName(String itemName) { 33 | this.itemName = itemName; 34 | } 35 | 36 | @Override 37 | public String toString() { 38 | return "OrderVO{" + 39 | "id=" + id + 40 | ", itemName='" + itemName + '\'' + 41 | '}'; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /validation-demo/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8080 -------------------------------------------------------------------------------- /validation-demo/src/test/java/com/zhengxl/validationdemo/UserControllerTest.java: -------------------------------------------------------------------------------- 1 | //package com.zhengxl.validationdemo; 2 | // 3 | //import com.sun.xml.internal.ws.spi.db.DatabindingException; 4 | //import com.zhengxl.validationdemo.vo.UserVO; 5 | //import org.junit.jupiter.api.DisplayNameGenerator; 6 | //import org.junit.jupiter.api.Test; 7 | //import org.springframework.beans.factory.annotation.Autowired; 8 | //import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; 9 | //import org.springframework.boot.test.context.SpringBootTest; 10 | //import org.springframework.http.MediaType; 11 | //import org.springframework.test.web.servlet.MockMvc; 12 | //import org.springframework.test.web.servlet.MvcResult; 13 | // 14 | //import java.nio.charset.StandardCharsets; 15 | //import java.util.Date; 16 | // 17 | //import static org.hamcrest.Matchers.containsString; 18 | //import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; 19 | //import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; 20 | // 21 | ///** 22 | // * @description: 23 | // * @projectName:validation-demo 24 | // * @see:com.zhengxl.validationdemo 25 | // * @author:郑晓龙 26 | // * @createTime:2020/8/22 10:14 27 | // * @version:1.0 28 | // */ 29 | //@SpringBootTest 30 | //@AutoConfigureMockMvc 31 | //public class UserControllerTest { 32 | // @Autowired 33 | // private MockMvc mockMvc; 34 | // 35 | // @Test 36 | // public void userControllerTest() throws Exception { 37 | // MvcResult mvcResult = this.mockMvc.perform(post("/user/add2", new UserVO()).accept(MediaType.APPLICATION_JSON)) 38 | // .andExpect(content().encoding(StandardCharsets.UTF_8.toString())) 39 | // .andReturn(); 40 | // System.out.println(new String(mvcResult.getResponse().getContentAsByteArray(), StandardCharsets.UTF_8)); 41 | // 42 | // } 43 | //} 44 | -------------------------------------------------------------------------------- /validation-demo/src/test/java/com/zhengxl/validationdemo/ValidationDemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.zhengxl.validationdemo; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class ValidationDemoApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | --------------------------------------------------------------------------------