├── README.md ├── activemq-demo ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── demo │ │ │ ├── DemoApplication.java │ │ │ ├── config │ │ │ └── ActivemqConfig.java │ │ │ └── controller │ │ │ ├── QueueCustomerController.java │ │ │ ├── SendController.java │ │ │ └── TopicCustomerController.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── example │ └── demo │ └── DemoApplicationTests.java ├── exception-demo ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── demo │ │ │ ├── ExceptionDemoApplication.java │ │ │ ├── controller │ │ │ └── DemoController.java │ │ │ └── util │ │ │ ├── GlobleExceptionHandler.java │ │ │ └── Result.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── example │ └── demo │ └── ExceptionDemoApplicationTests.java ├── filter-demo ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── demo │ │ │ ├── DemoApplication.java │ │ │ ├── config │ │ │ └── WebConfig.java │ │ │ ├── controller │ │ │ └── DemoController.java │ │ │ └── filter │ │ │ ├── MyFilter1.java │ │ │ └── MyFilter2.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── example │ └── demo │ └── DemoApplicationTests.java ├── jpa-demo ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── demo │ │ │ ├── SpringBootStudyJpaApplication.java │ │ │ ├── controller │ │ │ └── UserController.java │ │ │ ├── entity │ │ │ └── User.java │ │ │ ├── repository │ │ │ └── UserRepository.java │ │ │ └── service │ │ │ ├── UserService.java │ │ │ └── impl │ │ │ └── UserServiceImpl.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── example │ └── demo │ └── SpringBootStudyJpaApplicationTests.java ├── pom.xml ├── qrcode ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── demo │ │ │ ├── QrcodeApplication.java │ │ │ ├── controller │ │ │ └── QrCodeController.java │ │ │ └── util │ │ │ └── QRCodeUtil.java │ └── resources │ │ ├── application.properties │ │ └── templates │ │ └── logo.jpg │ └── test │ └── java │ └── com │ └── example │ └── demo │ └── QrcodeApplicationTests.java ├── sendmail ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── example │ │ └── mail │ │ └── sendmail │ │ ├── SendMailApplication.java │ │ ├── controller │ │ └── MailController.java │ │ ├── dto │ │ └── Email.java │ │ └── service │ │ ├── MailService.java │ │ └── impl │ │ └── MailServiceImpl.java │ └── resources │ └── application.properties ├── spirng-boot-kaptcha ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── demo │ │ │ ├── DemoApplication.java │ │ │ ├── config │ │ │ ├── KaptchaConfig.java │ │ │ └── WebConfig.java │ │ │ └── controller │ │ │ └── KaptchaController.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── example │ └── demo │ └── DemoApplicationTests.java ├── spring-boot-encry ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── example │ │ └── springbootencry │ │ ├── SpringBootEncryApplication.java │ │ ├── advice │ │ ├── DecryptRequestBodyAdvice.java │ │ └── EncryResponseBodyAdvice.java │ │ ├── controller │ │ └── TestController.java │ │ └── util │ │ ├── DesUtil.java │ │ └── Result.java │ └── resources │ └── application.yml ├── spring-boot-init └── src │ ├── main │ └── java │ │ └── com │ │ └── example │ │ └── springbootinit │ │ └── component │ │ ├── ApplicationRunnerImpl.java │ │ ├── CommandLineRunnerImpl.java │ │ ├── PostConstructInit.java │ │ └── TestService.java │ └── test │ └── java │ └── com │ └── example │ └── springbootinit │ └── Test.java ├── spring-boot-jwt └── src │ └── main │ ├── java │ └── com │ │ └── example │ │ └── springbootjwt │ │ ├── controller │ │ ├── LoginController.java │ │ └── SecureController.java │ │ ├── entity │ │ └── User.java │ │ ├── filter │ │ └── JwtFilter.java │ │ └── util │ │ └── JwtUtil.java │ └── resources │ └── application.yml ├── spring-boot-log ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springbootlog │ │ │ ├── SpringBootLogApplication.java │ │ │ ├── config │ │ │ └── InterceptorConfig.java │ │ │ ├── controller │ │ │ └── TestLogController.java │ │ │ └── interceptor │ │ │ └── SessionInterceptor.java │ └── resources │ │ ├── application.yml │ │ └── logback-spring.xml │ └── test │ └── java │ └── com │ └── example │ └── springbootlog │ └── SpringBootLogApplicationTests.java ├── spring-boot-mybatis-annotation └── src │ └── main │ ├── java │ └── com │ │ └── example │ │ └── springboot │ │ └── mybatisannotation │ │ ├── config │ │ └── MybatisConfig.java │ │ ├── controller │ │ └── UserController.java │ │ ├── dao │ │ └── mapper │ │ │ ├── UserMapper.java │ │ │ └── UserRoleMapper.java │ │ ├── entity │ │ ├── User.java │ │ └── UserRole.java │ │ └── service │ │ ├── UserRoleService.java │ │ ├── UserService.java │ │ └── impl │ │ ├── UserRoleServiceImpl.java │ │ └── UserServiceImpl.java │ └── resources │ └── application.yml ├── spring-boot-mybatis-generator ├── .gitignore ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springbootmybatis │ │ │ └── generator │ │ │ ├── SpringBootMybatisGeneratorApplication.java │ │ │ ├── dao │ │ │ ├── UserMapper.java │ │ │ └── UserRoleMapper.java │ │ │ └── entity │ │ │ ├── User.java │ │ │ └── UserRole.java │ └── resources │ │ ├── application.properties │ │ ├── mapper │ │ ├── UserMapper.xml │ │ └── UserRoleMapper.xml │ │ └── mybatis-generator.xml │ └── test │ └── java │ └── com │ └── example │ └── springbootmybatis │ └── generator │ └── SpringBootMybatisGeneratorApplicationTests.java ├── spring-boot-mybatis-xml ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springboot │ │ │ └── mybatisxml │ │ │ ├── SpringBootMybatisXmlApplication.java │ │ │ ├── controller │ │ │ └── UserController.java │ │ │ ├── dao │ │ │ └── mapper │ │ │ │ └── UserMapper.java │ │ │ ├── entity │ │ │ └── User.java │ │ │ └── service │ │ │ ├── UserService.java │ │ │ └── impl │ │ │ └── UserServiceImpl.java │ └── resources │ │ ├── application.yml │ │ ├── mapper │ │ └── UserMapper.xml │ │ └── mybatis-config.xml │ └── test │ └── java │ └── com │ └── example │ └── springboot │ └── mybatisxml │ └── SpringBootMybatisXmlApplicationTests.java ├── spring-boot-read-yml ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springbootreadyml │ │ │ ├── SpringBootReadYmlApplication.java │ │ │ ├── config │ │ │ └── WxMpProperties.java │ │ │ └── controller │ │ │ └── TestReadYmlController.java │ └── resources │ │ └── application.yml │ └── test │ └── java │ └── com │ └── example │ └── springbootreadyml │ └── SpringBootReadYmlApplicationTests.java ├── spring-boot-security ├── .gitignore ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springboot │ │ │ └── security │ │ │ ├── SpringBootSecurityApplication.java │ │ │ ├── config │ │ │ ├── MyPasswordEncoder.java │ │ │ └── WebSecurityConfig.java │ │ │ └── controller │ │ │ ├── AdminController.java │ │ │ ├── AppController.java │ │ │ └── UserController.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── example │ └── springboot │ └── security │ └── SpringBootSecurityApplicationTests.java ├── spring-boot-study-base ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── study │ └── day01 │ ├── Config.java │ ├── LinuxCondition.java │ ├── LinuxService.java │ ├── ListService.java │ ├── Main01.java │ ├── WindowsCondition.java │ └── WindowsService.java ├── spring-boot-websocket ├── .gitignore ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springboot │ │ │ └── websocket │ │ │ ├── SpringBootWebsocketApplication.java │ │ │ ├── config │ │ │ └── WebSocketConfig.java │ │ │ ├── controller │ │ │ └── WebSocketController.java │ │ │ ├── dto │ │ │ └── SocketMessage.java │ │ │ └── util │ │ │ └── WebSocketServer.java │ └── resources │ │ ├── application.properties │ │ └── static │ │ └── index.html │ └── test │ └── java │ └── com │ └── example │ └── springboot │ └── websocket │ └── SpringBootWebsocketApplicationTests.java ├── spring-cache-demo ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── demo │ │ │ ├── DemoApplication.java │ │ │ ├── aop │ │ │ └── LogRecordAspect.java │ │ │ ├── config │ │ │ └── WebConfig.java │ │ │ ├── controller │ │ │ └── UserController.java │ │ │ ├── entity │ │ │ └── User.java │ │ │ ├── repository │ │ │ └── UserRepository.java │ │ │ └── service │ │ │ ├── UserService.java │ │ │ └── impl │ │ │ └── UserServiceImpl.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── example │ └── demo │ └── DemoApplicationTests.java ├── spring-cloud-consumer-fegin-hystrix ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springcloudconsumerfeginhystrix │ │ │ ├── SpringCloudConsumerFeginHystrixApplication.java │ │ │ ├── controller │ │ │ └── FeginHystrixController.java │ │ │ └── service │ │ │ ├── UserFeginService.java │ │ │ └── impl │ │ │ └── UserFeginFailBackImpl.java │ └── resources │ │ └── application.yml │ └── test │ └── java │ └── com │ └── example │ └── springcloudconsumerfeginhystrix │ └── SpringCloudConsumerFeginHystrixApplicationTests.java ├── spring-cloud-consumer-fegin ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springcloudconsumerfegin │ │ │ ├── SpringCloudConsumerFeginApplication.java │ │ │ ├── controller │ │ │ └── FeginController.java │ │ │ └── service │ │ │ └── IFeginService.java │ └── resources │ │ └── application.yml │ └── test │ └── java │ └── com │ └── example │ └── springcloudconsumerfegin │ └── SpringCloudConsumerFeginApplicationTests.java ├── spring-cloud-consumer-ribbon ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── demo │ │ │ ├── RibbonConsumerApplication.java │ │ │ └── controller │ │ │ ├── HystrixConsumerController.java │ │ │ └── RibbonConsumerController.java │ └── resources │ │ └── application.yml │ └── test │ └── java │ └── com │ └── example │ └── demo │ └── RibbonConsumerApplicationTests.java ├── spring-cloud-eureka ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── demo │ │ │ └── SpringCloudEurekaApplication.java │ └── resources │ │ └── application.yml │ └── test │ └── java │ └── com │ └── example │ └── demo │ └── SpringCloudEurekaApplicationTests.java ├── spring-cloud-user-consumer ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── demo │ │ │ ├── SpringCloudUserConsumerApplication.java │ │ │ └── controller │ │ │ └── ConsumerController.java │ └── resources │ │ └── application.yml │ └── test │ └── java │ └── com │ └── example │ └── demo │ └── SpringCloudUserConsumerApplicationTests.java ├── spring-cloud-user-service ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── demo │ │ │ ├── UserServiceDemoApplication.java │ │ │ ├── controller │ │ │ └── UserController.java │ │ │ └── entity │ │ │ └── User.java │ └── resources │ │ └── application.yml │ └── test │ └── java │ └── com │ └── example │ └── demo │ ├── DemoApplicationTests.java │ └── Test.java └── spring-mybatis-generator └── src └── main └── resources ├── application.yml └── mybatis-generator.xml /README.md: -------------------------------------------------------------------------------- 1 | #### 本项目记录 SpringBoot和SpringCloud 学习过程中遇到的问题,下面的是我的公众号二维码图片,欢迎关注,欢迎留言,一起学习,一起进步。 2 | 3 | ![](https://mmbiz.qpic.cn/mmbiz_jpg/OXnTwHqwxnYc8Qxiappy2M1MnJcc3MLI9R1PorLeibiczcODP9IAYGdrB2HOTWW24NX5rfeB1ZJ8G4jPfKRnah2WQ/0?wx_fmt=jpeg) 4 | 5 | 6 | 7 | ### 近期文章 8 | 9 | 10 | [一分钟带你学会利用mybatis-generator自动生成代码!](https://mp.weixin.qq.com/s/HSlmvdSsQikfJ8M99MYGNg) 11 | 12 | [SpringBoot系列-整合Mybatis(注解方式)](https://mp.weixin.qq.com/s/toH_rCF5Sf3sPK-NPp5OhQ) 13 | 14 | [SpringBoot系列-整合Mybatis(XML配置方式)](https://mp.weixin.qq.com/s/Ad8aKvyTgjFlzjOcadO1xQ) 15 | 16 | [利用SpringBoot+Logback手写一个简单的链路追踪](https://mp.weixin.qq.com/s/9ozbOBxhW9yreBWH9UTFMQ) -------------------------------------------------------------------------------- /activemq-demo/.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 | -------------------------------------------------------------------------------- /activemq-demo/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suisui2019/springboot-study/13083fe6fa2e748097a0b98c78bc76cb6f2ab98f/activemq-demo/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /activemq-demo/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /activemq-demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.8.RELEASE 9 | 10 | 11 | com.example 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-activemq 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | 32 | org.projectlombok 33 | lombok 34 | true 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-test 39 | test 40 | 41 | 42 | 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-maven-plugin 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /activemq-demo/src/main/java/com/example/demo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.demo; 2 | 3 | import org.apache.activemq.command.ActiveMQQueue; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.context.annotation.Bean; 7 | 8 | import javax.jms.Queue; 9 | 10 | @SpringBootApplication 11 | public class DemoApplication { 12 | 13 | 14 | 15 | 16 | public static void main(String[] args) { 17 | SpringApplication.run(DemoApplication.class, args); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /activemq-demo/src/main/java/com/example/demo/config/ActivemqConfig.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.config; 2 | 3 | import org.apache.activemq.command.ActiveMQQueue; 4 | import org.apache.activemq.command.ActiveMQTopic; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | 8 | import javax.jms.Queue; 9 | import javax.jms.Topic; 10 | 11 | /** 12 | * ActivemqConfig配置类 13 | */ 14 | @Configuration 15 | public class ActivemqConfig { 16 | 17 | /** 18 | * 点对点 19 | */ 20 | @Bean 21 | public Queue queue() { 22 | return new ActiveMQQueue("active.queue"); 23 | } 24 | 25 | /** 26 | * 发布/订阅 27 | */ 28 | @Bean 29 | public Topic topic() { 30 | return new ActiveMQTopic("active.topic"); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /activemq-demo/src/main/java/com/example/demo/controller/QueueCustomerController.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.controller; 2 | 3 | import org.springframework.jms.annotation.JmsListener; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | @RestController 7 | public class QueueCustomerController { 8 | /* 9 | * 监听和接收 队列消息 10 | */ 11 | @JmsListener(destination="active.queue") 12 | public void readActiveQueue(String message) { 13 | System.out.println("接受到:" + message); 14 | } 15 | } -------------------------------------------------------------------------------- /activemq-demo/src/main/java/com/example/demo/controller/SendController.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.controller; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.jms.core.JmsMessagingTemplate; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | import javax.jms.Queue; 9 | import javax.jms.Topic; 10 | import java.util.UUID; 11 | 12 | /** 13 | * 消息生产者 14 | */ 15 | @RestController 16 | public class SendController { 17 | 18 | @Autowired 19 | private JmsMessagingTemplate jmsMessagingTemplate; 20 | 21 | @Autowired 22 | private Queue queue; 23 | 24 | @Autowired 25 | private Topic topic; 26 | 27 | /* 28 | * 发送 队列消息 29 | */ 30 | @RequestMapping("/sendQueue") 31 | public String sendQueue() { 32 | String message = UUID.randomUUID().toString(); 33 | // 指定消息发送的目的地及内容 34 | this.jmsMessagingTemplate.convertAndSend(this.queue, message); 35 | return "消息发送成功!message=" + message; 36 | } 37 | 38 | /* 39 | * 发送 主题消息 40 | */ 41 | @RequestMapping("/sendTopic") 42 | public String sendTopic() { 43 | String message = UUID.randomUUID().toString(); 44 | // 指定消息发送的目的地及内容 45 | this.jmsMessagingTemplate.convertAndSend(this.topic, message); 46 | return "消息发送成功!message=" + message; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /activemq-demo/src/main/java/com/example/demo/controller/TopicCustomerController.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.controller; 2 | 3 | import org.springframework.jms.annotation.JmsListener; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | @RestController 7 | public class TopicCustomerController { 8 | /* 9 | * 监听和接收 主题消息 10 | */ 11 | @JmsListener(destination = "active.topic") 12 | public void readActiveTopic1(String message) { 13 | System.out.println("Customer1接受到:" + message); 14 | } 15 | 16 | /* 17 | * 监听和接收 主题消息 18 | */ 19 | @JmsListener(destination = "active.topic") 20 | public void readActiveTopic2(String message) { 21 | System.out.println("Customer2接受到:" + message); 22 | } 23 | } -------------------------------------------------------------------------------- /activemq-demo/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suisui2019/springboot-study/13083fe6fa2e748097a0b98c78bc76cb6f2ab98f/activemq-demo/src/main/resources/application.properties -------------------------------------------------------------------------------- /activemq-demo/src/test/java/com/example/demo/DemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.demo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class DemoApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /exception-demo/.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 | -------------------------------------------------------------------------------- /exception-demo/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suisui2019/springboot-study/13083fe6fa2e748097a0b98c78bc76cb6f2ab98f/exception-demo/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /exception-demo/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /exception-demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.7.RELEASE 9 | 10 | 11 | com.example 12 | demo 13 | 0.0.1-SNAPSHOT 14 | exception-demo 15 | 统计处理异常 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-web 25 | 26 | 27 | 28 | org.projectlombok 29 | lombok 30 | true 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-test 35 | test 36 | 37 | 38 | 39 | 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-maven-plugin 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /exception-demo/src/main/java/com/example/demo/ExceptionDemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class ExceptionDemoApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(ExceptionDemoApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /exception-demo/src/main/java/com/example/demo/controller/DemoController.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.controller; 2 | 3 | import com.example.demo.util.Result; 4 | import org.springframework.stereotype.Controller; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.ResponseBody; 7 | 8 | 9 | @Controller 10 | public class DemoController { 11 | 12 | /** 13 | * 异常情况 14 | */ 15 | @ResponseBody 16 | @RequestMapping(value = "/testException") 17 | public Result testException() { 18 | Result result = new Result(Result.RESULT_SUCCESS, "SUCCESS"); 19 | //异常 20 | Integer.parseInt("abc"); 21 | return result; 22 | } 23 | 24 | /** 25 | *正常情况 26 | */ 27 | @ResponseBody 28 | @RequestMapping(value = "/testRight") 29 | public Result testRight() { 30 | Result result = new Result(Result.RESULT_SUCCESS, "SUCCESS"); 31 | return result; 32 | } 33 | 34 | } 35 | 36 | -------------------------------------------------------------------------------- /exception-demo/src/main/java/com/example/demo/util/GlobleExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.util; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.web.bind.annotation.ControllerAdvice; 5 | import org.springframework.web.bind.annotation.ExceptionHandler; 6 | import org.springframework.web.bind.annotation.ResponseBody; 7 | 8 | import javax.servlet.http.HttpServletResponse; 9 | 10 | /** 11 | * 异常全局处理类 12 | * 13 | * @author 14 | **/ 15 | @Slf4j 16 | @ControllerAdvice 17 | public class GlobleExceptionHandler { 18 | 19 | @ExceptionHandler(Exception.class) 20 | @ResponseBody 21 | public Result handlerException(HttpServletResponse response, Exception e) throws Exception { 22 | log.info("异常信息", e); 23 | Result result = new Result(Result.RESULT_FAIL, "error :" + e.getMessage()); 24 | return result; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /exception-demo/src/main/java/com/example/demo/util/Result.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.util; 2 | 3 | import lombok.Data; 4 | 5 | import java.io.Serializable; 6 | 7 | /** 8 | * 封装返回类 9 | */ 10 | @Data 11 | public class Result implements Serializable { 12 | 13 | private static final long serialVersionUID = 1L; 14 | 15 | public static final int RESULT_FAIL = 0; 16 | public static final int RESULT_SUCCESS = 1; 17 | 18 | //返回代码 19 | private Integer code; 20 | 21 | //返回消息 22 | private String message; 23 | 24 | //返回对象 25 | private Object data; 26 | 27 | public Result(Integer code, String message) { 28 | this.code = code; 29 | this.message = message; 30 | } 31 | 32 | public Result(Integer code, String message, Object object) { 33 | this.code = code; 34 | this.message = message; 35 | this.data = object; 36 | } 37 | } -------------------------------------------------------------------------------- /exception-demo/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /exception-demo/src/test/java/com/example/demo/ExceptionDemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.demo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class ExceptionDemoApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /filter-demo/.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 | -------------------------------------------------------------------------------- /filter-demo/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suisui2019/springboot-study/13083fe6fa2e748097a0b98c78bc76cb6f2ab98f/filter-demo/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /filter-demo/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /filter-demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.7.RELEASE 9 | 10 | 11 | com.example 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-web 30 | 31 | 32 | 33 | org.projectlombok 34 | lombok 35 | true 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-test 41 | test 42 | 43 | 44 | 45 | 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-maven-plugin 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /filter-demo/src/main/java/com/example/demo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.demo; 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(basePackages = "com.example.demo.filter") 9 | public class DemoApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(DemoApplication.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /filter-demo/src/main/java/com/example/demo/config/WebConfig.java: -------------------------------------------------------------------------------- 1 | //package com.example.demo.config; 2 | // 3 | //import com.example.demo.filter.MyFilter2; 4 | //import org.springframework.boot.web.servlet.FilterRegistrationBean; 5 | //import org.springframework.context.annotation.Bean; 6 | //import org.springframework.context.annotation.Configuration; 7 | // 8 | ///** 9 | // * 10 | // */ 11 | //@Configuration 12 | //public class WebConfig { 13 | // 14 | // @Bean 15 | // public FilterRegistrationBean testFilterRegistration() { 16 | // FilterRegistrationBean registration = new FilterRegistrationBean(new MyFilter2()); 17 | // registration.addUrlPatterns("/test"); // 18 | // registration.setName("myFilter2"); 19 | // return registration; 20 | // } 21 | //} 22 | -------------------------------------------------------------------------------- /filter-demo/src/main/java/com/example/demo/controller/DemoController.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.ResponseBody; 6 | 7 | 8 | @Controller 9 | public class DemoController { 10 | 11 | 12 | /** 13 | * test 14 | */ 15 | @ResponseBody 16 | @RequestMapping(value = "/test") 17 | public String test() { 18 | return "OK"; 19 | } 20 | 21 | } 22 | 23 | -------------------------------------------------------------------------------- /filter-demo/src/main/java/com/example/demo/filter/MyFilter1.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.filter; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | 5 | import javax.servlet.*; 6 | import javax.servlet.annotation.WebFilter; 7 | 8 | @Slf4j 9 | @WebFilter(filterName = "myFilter1", urlPatterns = "/test") 10 | public class MyFilter1 implements Filter { 11 | @Override 12 | public void init(FilterConfig filterConfig) throws ServletException { 13 | log.info(filterConfig.getFilterName() + " init"); 14 | } 15 | 16 | @Override 17 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) { 18 | log.info("myFilter1 begin"); 19 | try { 20 | log.info("业务方法执行"); 21 | chain.doFilter(request, response); 22 | } catch (Exception e) { 23 | log.error("error!", e); 24 | } 25 | log.info("myFilter1 end"); 26 | } 27 | 28 | @Override 29 | public void destroy() { 30 | } 31 | } -------------------------------------------------------------------------------- /filter-demo/src/main/java/com/example/demo/filter/MyFilter2.java: -------------------------------------------------------------------------------- 1 | //package com.example.demo.filter; 2 | // 3 | //import lombok.extern.slf4j.Slf4j; 4 | // 5 | //import javax.servlet.*; 6 | //import javax.servlet.annotation.WebFilter; 7 | //@Slf4j 8 | //public class MyFilter2 implements Filter { 9 | // @Override 10 | // public void init(FilterConfig filterConfig) throws ServletException { 11 | // log.info(filterConfig.getFilterName() + " init"); 12 | // } 13 | // 14 | // @Override 15 | // public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) { 16 | // log.info("myFilter2 begin"); 17 | // try { 18 | // log.info("业务方法执行"); 19 | // chain.doFilter(request, response); 20 | // } catch (Exception e) { 21 | // log.error("error!", e); 22 | // } 23 | // log.info("myFilter2 end"); 24 | // } 25 | // 26 | // @Override 27 | // public void destroy() { 28 | // } 29 | //} -------------------------------------------------------------------------------- /filter-demo/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /filter-demo/src/test/java/com/example/demo/DemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.demo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class DemoApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /jpa-demo/.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 | -------------------------------------------------------------------------------- /jpa-demo/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suisui2019/springboot-study/13083fe6fa2e748097a0b98c78bc76cb6f2ab98f/jpa-demo/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /jpa-demo/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /jpa-demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.7.RELEASE 9 | 10 | 11 | com.example 12 | demo 13 | 0.0.1-SNAPSHOT 14 | spring-boot-study-jpa 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-data-jpa 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | 32 | mysql 33 | mysql-connector-java 34 | runtime 35 | 36 | 37 | org.projectlombok 38 | lombok 39 | true 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-starter-test 44 | test 45 | 46 | 47 | 48 | 49 | 50 | 51 | org.springframework.boot 52 | spring-boot-maven-plugin 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /jpa-demo/src/main/java/com/example/demo/SpringBootStudyJpaApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootStudyJpaApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootStudyJpaApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /jpa-demo/src/main/java/com/example/demo/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.controller; 2 | 3 | import com.example.demo.entity.User; 4 | import com.example.demo.service.UserService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.PathVariable; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | @RestController 11 | public class UserController { 12 | @Autowired 13 | UserService service; 14 | 15 | /** 16 | * 根据id 查询 17 | */ 18 | @RequestMapping("/getUser/{id}") 19 | public User getUser(@PathVariable("id") Integer id) { 20 | return service.getUser(id); 21 | } 22 | 23 | /** 24 | * 根据id 删除 25 | */ 26 | @RequestMapping("/deleteUser/{id}") 27 | public String deleteUser(@PathVariable("id") Integer id) { 28 | service.deleteUser(id); 29 | return "删除成功!"; 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /jpa-demo/src/main/java/com/example/demo/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.entity; 2 | 3 | import javax.persistence.*; 4 | 5 | @Entity 6 | @Table(name = "user") 7 | public class User { 8 | 9 | @Id 10 | @GeneratedValue(strategy = GenerationType.AUTO) 11 | private Integer id; 12 | private String name; 13 | private String password; 14 | 15 | public Integer getId() { 16 | return id; 17 | } 18 | 19 | public void setId(Integer id) { 20 | this.id = id; 21 | } 22 | 23 | public String getName() { 24 | return name; 25 | } 26 | 27 | public void setName(String name) { 28 | this.name = name; 29 | } 30 | 31 | public String getPassword() { 32 | return password; 33 | } 34 | 35 | public void setPassword(String password) { 36 | this.password = password; 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /jpa-demo/src/main/java/com/example/demo/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.repository; 2 | 3 | import com.example.demo.entity.User; 4 | import org.springframework.data.jpa.repository.Query; 5 | import org.springframework.data.repository.CrudRepository; 6 | import org.springframework.data.repository.query.Param; 7 | 8 | /** 9 | * 注意: 10 | * 1.这里这里是interface,不是class 11 | * 2.CrudRepository里面的泛型,第一个是实体类,第二个是主键的类型 12 | */ 13 | public interface UserRepository extends CrudRepository { 14 | 15 | @Query("from User where id =:id ") 16 | public User getUser(@Param("id") Integer id); 17 | } -------------------------------------------------------------------------------- /jpa-demo/src/main/java/com/example/demo/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.service; 2 | 3 | 4 | import com.example.demo.entity.User; 5 | 6 | public interface UserService { 7 | public User getUser(Integer id); 8 | 9 | public void deleteUser(Integer id); 10 | } -------------------------------------------------------------------------------- /jpa-demo/src/main/java/com/example/demo/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.service.impl; 2 | 3 | import com.example.demo.entity.User; 4 | import com.example.demo.repository.UserRepository; 5 | import com.example.demo.service.UserService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | 10 | @Service 11 | public class UserServiceImpl implements UserService { 12 | 13 | @Autowired 14 | UserRepository repository; 15 | 16 | @Override 17 | public User getUser(Integer id) { 18 | //有两种方式: 19 | //1.调用crudRepository的接口 20 | // return repository.findOne(id); 21 | //2.调用我们自己写的接口 22 | return repository.getUser(id); 23 | } 24 | 25 | @Override 26 | public void deleteUser(Integer id) { 27 | repository.deleteById(id); 28 | } 29 | 30 | 31 | } -------------------------------------------------------------------------------- /jpa-demo/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suisui2019/springboot-study/13083fe6fa2e748097a0b98c78bc76cb6f2ab98f/jpa-demo/src/main/resources/application.properties -------------------------------------------------------------------------------- /jpa-demo/src/test/java/com/example/demo/SpringBootStudyJpaApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.demo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringBootStudyJpaApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | com.example 6 | spring-boot-study 7 | pom 8 | 0.0.1-SNAPSHOT 9 | spring-boot-study 10 | Demo project for Spring Boot 11 | 12 | 13 | spring-boot-study-base 14 | sendmail 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /qrcode/.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 | -------------------------------------------------------------------------------- /qrcode/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suisui2019/springboot-study/13083fe6fa2e748097a0b98c78bc76cb6f2ab98f/qrcode/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /qrcode/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /qrcode/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.7.RELEASE 9 | 10 | 11 | com.example 12 | demo 13 | 0.0.1-SNAPSHOT 14 | qrcode 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 | 34 | 35 | com.google.zxing 36 | core 37 | 3.2.0 38 | 39 | 40 | com.google.zxing 41 | javase 42 | 3.2.0 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | org.springframework.boot 52 | spring-boot-maven-plugin 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /qrcode/src/main/java/com/example/demo/QrcodeApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class QrcodeApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(QrcodeApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /qrcode/src/main/java/com/example/demo/controller/QrCodeController.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.controller; 2 | 3 | import com.example.demo.util.QRCodeUtil; 4 | import org.springframework.stereotype.Controller; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | 7 | import javax.servlet.ServletOutputStream; 8 | import javax.servlet.http.HttpServletResponse; 9 | import java.io.File; 10 | 11 | 12 | @Controller 13 | public class QrCodeController { 14 | 15 | 16 | /** 17 | * 根据 url 生成 普通二维码 18 | */ 19 | @RequestMapping(value = "/createCommonQRCode") 20 | public void createCommonQRCode(HttpServletResponse response, String url) throws Exception { 21 | ServletOutputStream stream = null; 22 | try { 23 | stream = response.getOutputStream(); 24 | //使用工具类生成二维码 25 | QRCodeUtil.encode(url, stream); 26 | } catch (Exception e) { 27 | e.getStackTrace(); 28 | } finally { 29 | if (stream != null) { 30 | stream.flush(); 31 | stream.close(); 32 | } 33 | } 34 | } 35 | 36 | /** 37 | * 根据 url 生成 带有logo二维码 38 | */ 39 | @RequestMapping(value = "/createLogoQRCode") 40 | public void createLogoQRCode(HttpServletResponse response, String url) throws Exception { 41 | ServletOutputStream stream = null; 42 | try { 43 | stream = response.getOutputStream(); 44 | String logoPath = Thread.currentThread().getContextClassLoader().getResource("").getPath() 45 | + "templates" + File.separator + "logo.jpg"; 46 | //使用工具类生成二维码 47 | QRCodeUtil.encode(url, logoPath, stream, true); 48 | } catch (Exception e) { 49 | e.getStackTrace(); 50 | } finally { 51 | if (stream != null) { 52 | stream.flush(); 53 | stream.close(); 54 | } 55 | } 56 | } 57 | 58 | 59 | } 60 | 61 | -------------------------------------------------------------------------------- /qrcode/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /qrcode/src/main/resources/templates/logo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suisui2019/springboot-study/13083fe6fa2e748097a0b98c78bc76cb6f2ab98f/qrcode/src/main/resources/templates/logo.jpg -------------------------------------------------------------------------------- /qrcode/src/test/java/com/example/demo/QrcodeApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.demo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class QrcodeApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /sendmail/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.6.RELEASE 9 | 10 | 11 | com.example.mail 12 | sendmail 13 | 0.0.1-SNAPSHOT 14 | sendmail 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 | 34 | org.springframework.boot 35 | spring-boot-starter-mail 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-maven-plugin 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /sendmail/src/main/java/com/example/mail/sendmail/SendMailApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.mail.sendmail; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | 7 | @SpringBootApplication 8 | 9 | public class SendMailApplication { 10 | 11 | public static void main(String[] args) { 12 | //java mail发邮件是附件名过长默认会被截断,附件名显示【tcmime.29121.29517.50430.bin】,主动设为false可正常显示附件名 13 | System.setProperty("mail.mime.splitlongparameters", "false"); 14 | SpringApplication.run(SendMailApplication.class, args); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /sendmail/src/main/java/com/example/mail/sendmail/controller/MailController.java: -------------------------------------------------------------------------------- 1 | package com.example.mail.sendmail.controller; 2 | 3 | import com.example.mail.sendmail.dto.Email; 4 | import com.example.mail.sendmail.service.MailService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Controller; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | /** 13 | * @Auther: java碎碎念 14 | * @Description: 15 | */ 16 | @Controller 17 | public class MailController { 18 | 19 | @Autowired 20 | private MailService mailService; 21 | 22 | 23 | @RequestMapping(value = "/sendMail") 24 | public String sendEmail() { 25 | Email email_email = new Email(); 26 | List addressList = new ArrayList(); 27 | addressList.add("xxx@163.com"); 28 | email_email.setToAddress(addressList); 29 | email_email.setSubject("java碎碎念-主题测试");// 主题 30 | email_email.setContent("你好!

测试邮件发送成功!"); 31 | // 发送邮件 32 | mailService.sendMail(email_email); 33 | 34 | return "ok"; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /sendmail/src/main/java/com/example/mail/sendmail/dto/Email.java: -------------------------------------------------------------------------------- 1 | package com.example.mail.sendmail.dto; 2 | 3 | import java.io.File; 4 | import java.util.List; 5 | 6 | /** 7 | * Email实体类 8 | * 9 | * @Author: lifq 10 | */ 11 | public class Email { 12 | 13 | /** 14 | * 发件人地址(为空时自动调用配置文件中设置的发件人) 15 | */ 16 | // private String fromAddress; 17 | 18 | /** 19 | * 收件人地址 20 | */ 21 | private List toAddress; 22 | 23 | /** 24 | * 抄送人地址 25 | */ 26 | private List ccAddress; 27 | 28 | /** 29 | * 密送人地址 30 | */ 31 | private List bccAddress; 32 | 33 | /** 34 | * 邮件主题 35 | */ 36 | private String subject; 37 | 38 | /** 39 | * 邮件内容 40 | */ 41 | private String content; 42 | 43 | /** 44 | * 附件 45 | */ 46 | private List attachments; 47 | 48 | 49 | public List getToAddress() { 50 | 51 | return toAddress; 52 | } 53 | 54 | public void setToAddress(List toAddress) { 55 | 56 | this.toAddress = toAddress; 57 | } 58 | 59 | public List getCcAddress() { 60 | 61 | return ccAddress; 62 | } 63 | 64 | public void setCcAddress(List ccAddress) { 65 | 66 | this.ccAddress = ccAddress; 67 | } 68 | 69 | public List getBccAddress() { 70 | 71 | return bccAddress; 72 | } 73 | 74 | public void setBccAddress(List bccAddress) { 75 | 76 | this.bccAddress = bccAddress; 77 | } 78 | 79 | public String getSubject() { 80 | 81 | return subject; 82 | } 83 | 84 | public void setSubject(String subject) { 85 | 86 | this.subject = subject; 87 | } 88 | 89 | public String getContent() { 90 | 91 | return content; 92 | } 93 | 94 | public void setContent(String content) { 95 | 96 | this.content = content; 97 | } 98 | 99 | public List getAttachments() { 100 | 101 | return attachments; 102 | } 103 | 104 | public void setAttachments(List attachments) { 105 | 106 | this.attachments = attachments; 107 | } 108 | 109 | } 110 | -------------------------------------------------------------------------------- /sendmail/src/main/java/com/example/mail/sendmail/service/MailService.java: -------------------------------------------------------------------------------- 1 | package com.example.mail.sendmail.service; 2 | 3 | 4 | import com.example.mail.sendmail.dto.Email; 5 | 6 | public interface MailService { 7 | 8 | 9 | /** 10 | * 发送邮件 11 | * 12 | * @Author: lifq 13 | */ 14 | public void sendMail(Email email); 15 | 16 | } -------------------------------------------------------------------------------- /sendmail/src/main/java/com/example/mail/sendmail/service/impl/MailServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.example.mail.sendmail.service.impl; 2 | 3 | import com.example.mail.sendmail.dto.Email; 4 | import com.example.mail.sendmail.service.MailService; 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.beans.factory.annotation.Value; 9 | import org.springframework.core.io.FileSystemResource; 10 | import org.springframework.mail.javamail.JavaMailSender; 11 | import org.springframework.mail.javamail.MimeMessageHelper; 12 | import org.springframework.stereotype.Service; 13 | 14 | import javax.mail.internet.MimeMessage; 15 | import javax.mail.internet.MimeUtility; 16 | import java.io.File; 17 | 18 | /** 19 | * 邮件实现类 20 | * 21 | * @Auther: java碎碎念 22 | * @Description: 23 | */ 24 | @Service 25 | public class MailServiceImpl implements MailService { 26 | 27 | 28 | @Autowired 29 | private JavaMailSender javaMailSender; 30 | 31 | @Value("${spring.mail.username}") 32 | private String mailUserName; 33 | 34 | Logger log = LoggerFactory.getLogger(MailServiceImpl.class); 35 | 36 | 37 | /** 38 | * 发送邮件 39 | * 40 | * @Author: java碎碎念 41 | */ 42 | public void sendMail(Email email) { 43 | long start = System.currentTimeMillis(); 44 | try { 45 | MimeMessage mimeMessage = javaMailSender.createMimeMessage(); 46 | //true表示需要创建一个multipart message 47 | MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8"); 48 | helper.setFrom(mailUserName); 49 | helper.setTo(email.getToAddress().toArray(new String[email.getToAddress().size()])); 50 | helper.setSubject(email.getSubject()); 51 | helper.setText(email.getContent(), true); 52 | 53 | if (null != email.getAttachments() && email.getAttachments().size() > 0) { 54 | for (File curFile : email.getAttachments()) { 55 | FileSystemResource file = new FileSystemResource(curFile); 56 | helper.addAttachment(MimeUtility.encodeWord(file.getFilename(), "utf-8", "B"), file); 57 | } 58 | } 59 | log.info("邮件开始发送"); 60 | javaMailSender.send(mimeMessage); 61 | long sendMillTimes = System.currentTimeMillis() - start; 62 | log.info("邮件发送成功,sendTimes=" + sendMillTimes); 63 | } catch (Exception e) { 64 | log.error("发送html邮件时发生异常!", e); 65 | } 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /sendmail/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.mail.host = smtp.163.com 2 | spring.mail.port = 465 3 | spring.mail.username = xxx@163.com 4 | spring.mail.password = xxx 5 | spring.mail.properties.mail.smtp.auth = true 6 | spring.mail.properties.mail.smtp.timeout = 25000 7 | spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory 8 | spring.mail.properties.mail.smtp.starttls.enable = true 9 | spring.mail.properties.mail.smtp.starttls.required = true -------------------------------------------------------------------------------- /spirng-boot-kaptcha/.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 | -------------------------------------------------------------------------------- /spirng-boot-kaptcha/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suisui2019/springboot-study/13083fe6fa2e748097a0b98c78bc76cb6f2ab98f/spirng-boot-kaptcha/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /spirng-boot-kaptcha/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /spirng-boot-kaptcha/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.7.RELEASE 9 | 10 | 11 | com.example 12 | demo 13 | 0.0.1-SNAPSHOT 14 | 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.projectlombok 29 | lombok 30 | true 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-test 35 | test 36 | 37 | 38 | 39 | 40 | com.github.penggle 41 | kaptcha 42 | 2.3.2 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | org.springframework.boot 52 | spring-boot-maven-plugin 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /spirng-boot-kaptcha/src/main/java/com/example/demo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class DemoApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(DemoApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spirng-boot-kaptcha/src/main/java/com/example/demo/config/KaptchaConfig.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.config; 2 | 3 | 4 | import com.google.code.kaptcha.Constants; 5 | import com.google.code.kaptcha.impl.DefaultKaptcha; 6 | import com.google.code.kaptcha.util.Config; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.Configuration; 9 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 10 | 11 | import java.util.Properties; 12 | 13 | /** 14 | * KaptchaConfig 15 | */ 16 | @Configuration 17 | public class KaptchaConfig implements WebMvcConfigurer { 18 | 19 | /** 20 | * 验证码 生成的bean 21 | */ 22 | @Bean 23 | public DefaultKaptcha captchaProducer() { 24 | DefaultKaptcha captchaProducer = new DefaultKaptcha(); 25 | Properties properties = new Properties(); 26 | properties.setProperty(Constants.KAPTCHA_IMAGE_WIDTH, "100"); 27 | properties.setProperty(Constants.KAPTCHA_IMAGE_HEIGHT, "30"); 28 | properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_SIZE, "22"); 29 | properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4"); 30 | properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_SPACE, "6"); 31 | properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR, "black"); 32 | properties.setProperty(Constants.KAPTCHA_BORDER_COLOR, "LIGHT_GRAY"); 33 | properties.setProperty(Constants.KAPTCHA_BACKGROUND_CLR_FROM, "WHITE"); 34 | properties.setProperty(Constants.KAPTCHA_NOISE_IMPL, "com.google.code.kaptcha.impl.NoNoise"); 35 | properties.setProperty(Constants.KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy"); 36 | properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_STRING, "0123456789"); 37 | properties.setProperty(Constants.KAPTCHA_SESSION_CONFIG_KEY, "checkCode"); 38 | Config config = new Config(properties); 39 | captchaProducer.setConfig(config); 40 | return captchaProducer; 41 | } 42 | 43 | } -------------------------------------------------------------------------------- /spirng-boot-kaptcha/src/main/java/com/example/demo/config/WebConfig.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suisui2019/springboot-study/13083fe6fa2e748097a0b98c78bc76cb6f2ab98f/spirng-boot-kaptcha/src/main/java/com/example/demo/config/WebConfig.java -------------------------------------------------------------------------------- /spirng-boot-kaptcha/src/main/java/com/example/demo/controller/KaptchaController.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.controller; 2 | 3 | import com.google.code.kaptcha.Constants; 4 | import com.google.code.kaptcha.Producer; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Controller; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.servlet.ModelAndView; 10 | 11 | import javax.imageio.ImageIO; 12 | import javax.servlet.ServletOutputStream; 13 | import javax.servlet.http.HttpServletRequest; 14 | import javax.servlet.http.HttpServletResponse; 15 | import java.awt.image.BufferedImage; 16 | 17 | @Slf4j 18 | @Controller 19 | @RequestMapping("/kaptcha") 20 | public class KaptchaController { 21 | 22 | @Autowired 23 | private Producer captchaProducer; 24 | 25 | @RequestMapping("/getCode") 26 | public ModelAndView getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception { 27 | response.setDateHeader("Expires", 0); 28 | // Set standard HTTP/1.1 no-cache headers. 29 | response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate"); 30 | // Set IE extended HTTP/1.1 no-cache headers (use addHeader). 31 | response.addHeader("Cache-Control", "post-check=0, pre-check=0"); 32 | // Set standard HTTP/1.0 no-cache header. 33 | response.setHeader("Pragma", "no-cache"); 34 | response.setContentType("image/jpeg"); 35 | // create the text for the image 36 | String capText = captchaProducer.createText(); 37 | log.info("******************验证码是: " + capText + "******************"); 38 | // store the text in the session 39 | request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText); 40 | // create the image with the text 41 | BufferedImage bi = captchaProducer.createImage(capText); 42 | ServletOutputStream out = response.getOutputStream(); 43 | // write the data out 44 | ImageIO.write(bi, "jpg", out); 45 | try { 46 | out.flush(); 47 | } finally { 48 | out.close(); 49 | } 50 | return null; 51 | } 52 | } 53 | 54 | -------------------------------------------------------------------------------- /spirng-boot-kaptcha/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8081 2 | -------------------------------------------------------------------------------- /spirng-boot-kaptcha/src/test/java/com/example/demo/DemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.demo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class DemoApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-boot-encry/.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 | -------------------------------------------------------------------------------- /spring-boot-encry/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suisui2019/springboot-study/13083fe6fa2e748097a0b98c78bc76cb6f2ab98f/spring-boot-encry/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /spring-boot-encry/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /spring-boot-encry/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.0.RELEASE 9 | 10 | 11 | com.example 12 | spring-boot-encry 13 | 0.0.1-SNAPSHOT 14 | spring-boot-encry 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.projectlombok 29 | lombok 30 | true 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-test 35 | test 36 | 37 | 38 | org.junit.vintage 39 | junit-vintage-engine 40 | 41 | 42 | 43 | 44 | 45 | com.alibaba 46 | fastjson 47 | 1.2.60 48 | 49 | 50 | 51 | 52 | 53 | 54 | org.springframework.boot 55 | spring-boot-maven-plugin 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /spring-boot-encry/src/main/java/com/example/springbootencry/SpringBootEncryApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootencry; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootEncryApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootEncryApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-encry/src/main/java/com/example/springbootencry/advice/DecryptRequestBodyAdvice.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootencry.advice; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.example.springbootencry.util.DesUtil; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.springframework.core.MethodParameter; 7 | import org.springframework.http.HttpInputMessage; 8 | import org.springframework.http.converter.HttpMessageConverter; 9 | import org.springframework.lang.Nullable; 10 | import org.springframework.stereotype.Component; 11 | import org.springframework.web.bind.annotation.ControllerAdvice; 12 | import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice; 13 | 14 | import java.io.IOException; 15 | import java.lang.reflect.Type; 16 | import java.util.Map; 17 | 18 | /** 19 | * 请求参数 解密操作 20 | * 21 | * @Author: Java碎碎念 22 | * @Date: 2019/10/24 21:31 23 | * 24 | */ 25 | @Component 26 | @ControllerAdvice(basePackages = "com.example.springbootencry.controller") 27 | @Slf4j 28 | public class DecryptRequestBodyAdvice implements RequestBodyAdvice { 29 | 30 | 31 | @Override 32 | public boolean supports(MethodParameter methodParameter, Type targetType, Class> converterType) { 33 | return true; 34 | } 35 | 36 | @Override 37 | public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter methodParameter, Type targetType, Class> selectedConverterType) throws IOException { 38 | return inputMessage; 39 | } 40 | 41 | @Override 42 | public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class> converterType) { 43 | String dealData = null; 44 | try { 45 | //解密操作 46 | Map dataMap = (Map)body; 47 | log.info("接收到原始请求数据={}", JSON.toJSONString(dataMap)); 48 | String srcData = dataMap.get("data"); 49 | dealData = DesUtil.decrypt(srcData); 50 | log.info("解密后数据={}",dealData); 51 | } catch (Exception e) { 52 | log.error("异常!", e); 53 | } 54 | return dealData; 55 | } 56 | 57 | 58 | @Override 59 | public Object handleEmptyBody(@Nullable Object var1, HttpInputMessage var2, MethodParameter var3, Type var4, Class> var5) { 60 | return var1; 61 | } 62 | 63 | 64 | } -------------------------------------------------------------------------------- /spring-boot-encry/src/main/java/com/example/springbootencry/advice/EncryResponseBodyAdvice.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootencry.advice; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.example.springbootencry.util.DesUtil; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.springframework.core.MethodParameter; 7 | import org.springframework.http.MediaType; 8 | import org.springframework.http.converter.HttpMessageConverter; 9 | import org.springframework.http.server.ServerHttpRequest; 10 | import org.springframework.http.server.ServerHttpResponse; 11 | import org.springframework.http.server.ServletServerHttpRequest; 12 | import org.springframework.stereotype.Component; 13 | import org.springframework.web.bind.annotation.ControllerAdvice; 14 | import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; 15 | 16 | import javax.servlet.http.HttpServletRequest; 17 | 18 | /** 19 | * 请求参数 解密操作 20 | * 21 | * @Author: Java碎碎念 22 | * @Date: 2019/10/24 21:31 23 | * 24 | */ 25 | @Component 26 | @ControllerAdvice(basePackages = "com.example.springbootencry.controller") 27 | @Slf4j 28 | public class EncryResponseBodyAdvice implements ResponseBodyAdvice { 29 | 30 | 31 | @Override 32 | public boolean supports(MethodParameter returnType, Class> converterType) { 33 | return true; 34 | } 35 | 36 | @Override 37 | public Object beforeBodyWrite(Object obj, MethodParameter returnType, MediaType selectedContentType, 38 | Class> selectedConverterType, ServerHttpRequest serverHttpRequest, 39 | ServerHttpResponse serverHttpResponse) { 40 | //通过 ServerHttpRequest的实现类ServletServerHttpRequest 获得HttpServletRequest 41 | ServletServerHttpRequest sshr = (ServletServerHttpRequest) serverHttpRequest; 42 | //此处获取到request 是为了取到在拦截器里面设置的一个对象 是我项目需要,可以忽略 43 | HttpServletRequest request = sshr.getServletRequest(); 44 | 45 | String returnStr = ""; 46 | 47 | try { 48 | //添加encry header,告诉前端数据已加密 49 | serverHttpResponse.getHeaders().add("encry", "true"); 50 | String srcData = JSON.toJSONString(obj); 51 | //加密 52 | returnStr = DesUtil.encrypt(srcData); 53 | log.info("接口={},原始数据={},加密后数据={}", request.getRequestURI(), srcData, returnStr); 54 | 55 | } catch (Exception e) { 56 | log.error("异常!", e); 57 | } 58 | return returnStr; 59 | } 60 | 61 | 62 | } -------------------------------------------------------------------------------- /spring-boot-encry/src/main/java/com/example/springbootencry/controller/TestController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootencry.controller; 2 | 3 | import com.example.springbootencry.util.Result; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | import org.springframework.web.bind.annotation.RequestBody; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | /** 11 | * @Author: Java碎碎念 12 | * @Date: 2019/10/24 21:40 13 | */ 14 | @RestController 15 | public class TestController { 16 | 17 | Logger log = LoggerFactory.getLogger(getClass()); 18 | 19 | /** 20 | * 响应数据 加密 21 | */ 22 | @RequestMapping(value = "/sendResponseEncryData") 23 | public Result sendResponseEncryData() { 24 | Result result = Result.createResult().setSuccess(true); 25 | result.setDataValue("name", "Java碎碎念"); 26 | result.setDataValue("encry", true); 27 | return result; 28 | } 29 | 30 | /** 31 | * 获取 解密后的 请求参数 32 | */ 33 | @RequestMapping(value = "/getRequestData") 34 | public Result getRequestData(@RequestBody Object object) { 35 | log.info("controller接收的参数object={}", object.toString()); 36 | Result result = Result.createResult().setSuccess(true); 37 | return result; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /spring-boot-encry/src/main/java/com/example/springbootencry/util/Result.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootencry.util; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class Result implements java.io.Serializable { 7 | private static final long serialVersionUID = 1L; 8 | private boolean success = true; 9 | private Map data; 10 | 11 | public boolean isSuccess() { 12 | return success; 13 | } 14 | 15 | public Result setSuccess(boolean success) { 16 | this.success = success; 17 | return this; 18 | } 19 | 20 | public static Result createResult() { 21 | return new Result(); 22 | } 23 | 24 | public Map getData() { 25 | return data; 26 | } 27 | 28 | public void setData(Map data) { 29 | this.data = data; 30 | } 31 | 32 | public void setDataValue(String key, Object value) { 33 | if (data == null) { 34 | data = new HashMap(); 35 | } 36 | data.put(key, value); 37 | } 38 | 39 | 40 | } 41 | -------------------------------------------------------------------------------- /spring-boot-encry/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8888 3 | -------------------------------------------------------------------------------- /spring-boot-init/src/main/java/com/example/springbootinit/component/ApplicationRunnerImpl.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootinit.component; 2 | 3 | import org.springframework.boot.ApplicationArguments; 4 | import org.springframework.boot.ApplicationRunner; 5 | import org.springframework.core.annotation.Order; 6 | import org.springframework.stereotype.Component; 7 | 8 | @Component 9 | @Order(3) 10 | public class ApplicationRunnerImpl implements ApplicationRunner { 11 | @Override 12 | public void run(ApplicationArguments args) throws Exception { 13 | 14 | hello(); 15 | } 16 | 17 | public void hello(){ 18 | System.out.println("项目又启动了,这次使用的是:继承 ApplicationRunner"); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /spring-boot-init/src/main/java/com/example/springbootinit/component/CommandLineRunnerImpl.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootinit.component; 2 | 3 | import org.springframework.boot.CommandLineRunner; 4 | import org.springframework.core.annotation.Order; 5 | import org.springframework.stereotype.Component; 6 | 7 | @Component 8 | @Order(2) //通过order值的大小来决定启动的顺序 9 | public class CommandLineRunnerImpl implements CommandLineRunner { 10 | 11 | @Override 12 | public void run(String... args) throws Exception { 13 | askForLeave(); 14 | } 15 | 16 | public void askForLeave(){ 17 | System.out.println("项目启动了,执行了方法1111"); 18 | } 19 | } -------------------------------------------------------------------------------- /spring-boot-init/src/main/java/com/example/springbootinit/component/PostConstructInit.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootinit.component; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Component; 5 | 6 | import javax.annotation.PostConstruct; 7 | 8 | /** 9 | */ 10 | 11 | @Component 12 | public class PostConstructInit { 13 | 14 | @Autowired 15 | TestService testService; 16 | 17 | @PostConstruct 18 | public void init() { 19 | System.out.println("PostConstructInit init"); 20 | testService.getTestData(); 21 | System.out.println("PostConstructInit end"); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-boot-init/src/main/java/com/example/springbootinit/component/TestService.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootinit.component; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.stereotype.Component; 5 | 6 | /** 7 | */ 8 | @Slf4j 9 | @Component 10 | public class TestService { 11 | 12 | void getTestData() { 13 | System.out.println("TestService get data"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /spring-boot-jwt/src/main/java/com/example/springbootjwt/controller/LoginController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjwt.controller; 2 | 3 | import com.example.springbootjwt.entity.User; 4 | import com.example.springbootjwt.util.JwtUtil; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.HashMap; 10 | import java.util.Map; 11 | 12 | /** 13 | * 登录Controller 14 | */ 15 | @Slf4j 16 | @RestController 17 | public class LoginController { 18 | 19 | static Map userMap = new HashMap<>(); 20 | 21 | static { 22 | //模拟数据库 23 | User user1 = new User(1, "zhangsan", "张三", "123456"); 24 | userMap.put(1, user1); 25 | User user2 = new User(2, "lisi", "李四", "123123"); 26 | userMap.put(2, user2); 27 | } 28 | 29 | /** 30 | * 模拟用户 登录 31 | */ 32 | @RequestMapping("/login") 33 | public String login(User user) { 34 | for (User dbUser : userMap.values()) { 35 | if (dbUser.getUserName().equals(user.getUserName()) && dbUser.getPassword().equals(user.getPassword())) { 36 | log.info("登录成功!生成token!"); 37 | String token = JwtUtil.createToken(dbUser); 38 | return token; 39 | } 40 | } 41 | return ""; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /spring-boot-jwt/src/main/java/com/example/springbootjwt/controller/SecureController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjwt.controller; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | import javax.servlet.http.HttpServletRequest; 8 | 9 | /** 10 | * 需要登录后才能访问 11 | */ 12 | @Slf4j 13 | @RestController 14 | public class SecureController { 15 | 16 | /** 17 | * 查询 用户信息,登录后才能访问 18 | */ 19 | @RequestMapping("/secure/getUserInfo") 20 | public String login(HttpServletRequest request) { 21 | Integer id = (Integer) request.getAttribute("id"); 22 | String name = request.getAttribute("name").toString(); 23 | String userName = request.getAttribute("userName").toString(); 24 | return "当前用户信息id=" + id + ",name=" + name + ",userName=" + userName; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /spring-boot-jwt/src/main/java/com/example/springbootjwt/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjwt.entity; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | /** 7 | * @Description: 8 | */ 9 | @Data 10 | @AllArgsConstructor 11 | public class User { 12 | 13 | private static final long serialVersionUID = 1L; 14 | 15 | private Integer id; 16 | private String userName; 17 | private String name; 18 | private String password; 19 | } 20 | -------------------------------------------------------------------------------- /spring-boot-jwt/src/main/java/com/example/springbootjwt/filter/JwtFilter.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjwt.filter; 2 | 3 | import com.auth0.jwt.interfaces.Claim; 4 | import com.example.springbootjwt.util.JwtUtil; 5 | import lombok.extern.slf4j.Slf4j; 6 | 7 | import javax.servlet.*; 8 | import javax.servlet.annotation.WebFilter; 9 | import javax.servlet.http.HttpServletRequest; 10 | import javax.servlet.http.HttpServletResponse; 11 | import java.io.IOException; 12 | import java.util.Map; 13 | 14 | /** 15 | * JWT过滤器,拦截 /secure的请求 16 | */ 17 | @Slf4j 18 | @WebFilter(filterName = "JwtFilter", urlPatterns = "/secure/*") 19 | public class JwtFilter implements Filter { 20 | @Override 21 | public void init(FilterConfig filterConfig) throws ServletException { 22 | } 23 | 24 | @Override 25 | public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { 26 | final HttpServletRequest request = (HttpServletRequest) req; 27 | final HttpServletResponse response = (HttpServletResponse) res; 28 | 29 | response.setCharacterEncoding("UTF-8"); 30 | //获取 header里的token 31 | final String token = request.getHeader("authorization"); 32 | 33 | if ("OPTIONS".equals(request.getMethod())) { 34 | response.setStatus(HttpServletResponse.SC_OK); 35 | chain.doFilter(request, response); 36 | } 37 | // Except OPTIONS, other request should be checked by JWT 38 | else { 39 | 40 | if (token == null) { 41 | response.getWriter().write("没有token!"); 42 | return; 43 | } 44 | 45 | Map userData = JwtUtil.verifyToken(token); 46 | if (userData == null) { 47 | response.getWriter().write("token不合法!"); 48 | return; 49 | } 50 | Integer id = userData.get("id").asInt(); 51 | String name = userData.get("name").asString(); 52 | String userName = userData.get("userName").asString(); 53 | //拦截器 拿到用户信息,放到request中 54 | request.setAttribute("id", id); 55 | request.setAttribute("name", name); 56 | request.setAttribute("userName", userName); 57 | chain.doFilter(req, res); 58 | } 59 | } 60 | 61 | @Override 62 | public void destroy() { 63 | } 64 | } -------------------------------------------------------------------------------- /spring-boot-jwt/src/main/java/com/example/springbootjwt/util/JwtUtil.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjwt.util; 2 | 3 | import com.auth0.jwt.JWT; 4 | import com.auth0.jwt.JWTVerifier; 5 | import com.auth0.jwt.algorithms.Algorithm; 6 | import com.auth0.jwt.interfaces.Claim; 7 | import com.auth0.jwt.interfaces.DecodedJWT; 8 | import com.example.springbootjwt.entity.User; 9 | import org.slf4j.Logger; 10 | import org.slf4j.LoggerFactory; 11 | 12 | import java.util.Date; 13 | import java.util.HashMap; 14 | import java.util.Map; 15 | 16 | /** 17 | * @description: Jwt工具类,生成JWT和认证 18 | * @author: Java碎碎念 19 | */ 20 | public class JwtUtil { 21 | 22 | private static final Logger logger = LoggerFactory.getLogger(JwtUtil.class); 23 | /** 24 | * 密钥 25 | */ 26 | private static final String SECRET = "my_secret"; 27 | 28 | /** 29 | * 过期时间 30 | **/ 31 | private static final long EXPIRATION = 1800L;//单位为秒 32 | 33 | /** 34 | * 生成用户token,设置token超时时间 35 | */ 36 | public static String createToken(User user) { 37 | //过期时间 38 | Date expireDate = new Date(System.currentTimeMillis() + EXPIRATION * 1000); 39 | Map map = new HashMap<>(); 40 | map.put("alg", "HS256"); 41 | map.put("typ", "JWT"); 42 | String token = JWT.create() 43 | .withHeader(map)// 添加头部 44 | //可以将基本信息放到claims中 45 | .withClaim("id", user.getId())//userId 46 | .withClaim("userName", user.getUserName())//userName 47 | .withClaim("name", user.getName())//name 48 | .withExpiresAt(expireDate) //超时设置,设置过期的日期 49 | .withIssuedAt(new Date()) //签发时间 50 | .sign(Algorithm.HMAC256(SECRET)); //SECRET加密 51 | return token; 52 | } 53 | 54 | /** 55 | * 校验token并解析token 56 | */ 57 | public static Map verifyToken(String token) { 58 | DecodedJWT jwt = null; 59 | try { 60 | JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SECRET)).build(); 61 | jwt = verifier.verify(token); 62 | } catch (Exception e) { 63 | logger.error(e.getMessage()); 64 | logger.error("token解码异常"); 65 | //解码异常则抛出异常 66 | return null; 67 | } 68 | return jwt.getClaims(); 69 | } 70 | 71 | } -------------------------------------------------------------------------------- /spring-boot-jwt/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /spring-boot-log/.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 | -------------------------------------------------------------------------------- /spring-boot-log/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suisui2019/springboot-study/13083fe6fa2e748097a0b98c78bc76cb6f2ab98f/spring-boot-log/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /spring-boot-log/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /spring-boot-log/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.0.RELEASE 9 | 10 | 11 | com.example 12 | spring-boot-log 13 | 0.0.1-SNAPSHOT 14 | spring-boot-log 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 | -------------------------------------------------------------------------------- /spring-boot-log/src/main/java/com/example/springbootlog/SpringBootLogApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootlog; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootLogApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootLogApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-log/src/main/java/com/example/springbootlog/config/InterceptorConfig.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootlog.config; 2 | 3 | import com.example.springbootlog.interceptor.SessionInterceptor; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 7 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 8 | 9 | 10 | @Configuration 11 | public class InterceptorConfig implements WebMvcConfigurer { 12 | 13 | @Bean 14 | public SessionInterceptor getSessionInterceptor() { 15 | return new SessionInterceptor(); 16 | } 17 | 18 | @Override 19 | public void addInterceptors(InterceptorRegistry registry) { 20 | registry.addInterceptor(getSessionInterceptor()).addPathPatterns("/*"); 21 | } 22 | 23 | 24 | } -------------------------------------------------------------------------------- /spring-boot-log/src/main/java/com/example/springbootlog/controller/TestLogController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootlog.controller; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | /** 9 | * TestLogController 10 | */ 11 | @RestController 12 | public class TestLogController { 13 | 14 | Logger log = LoggerFactory.getLogger(getClass()); 15 | 16 | /** 17 | * 测试登录 18 | */ 19 | @RequestMapping(value = "/testLogin") 20 | public String testLogin() { 21 | log.info("用户登录成功!"); 22 | return "ok"; 23 | } 24 | 25 | /** 26 | * 测试下单 27 | */ 28 | @RequestMapping(value = "/testNewOrder") 29 | public String testNewOrder() { 30 | log.info("用户创建了订单!"); 31 | log.info("请求完成,返回ok!"); 32 | return "ok"; 33 | } 34 | 35 | /** 36 | * 测试购买 37 | */ 38 | @RequestMapping(value = "/testPay") 39 | public String testPay() { 40 | log.info("用户付款!"); 41 | return "ok"; 42 | } 43 | 44 | 45 | } 46 | -------------------------------------------------------------------------------- /spring-boot-log/src/main/java/com/example/springbootlog/interceptor/SessionInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootlog.interceptor; 2 | 3 | import org.slf4j.MDC; 4 | import org.springframework.web.servlet.ModelAndView; 5 | import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; 6 | 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | 10 | public class SessionInterceptor extends HandlerInterceptorAdapter { 11 | /** 12 | * 会话ID 13 | */ 14 | private final static String SESSION_KEY = "sessionId"; 15 | 16 | 17 | @Override 18 | public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, 19 | Object arg2, ModelAndView arg3) throws Exception { 20 | } 21 | 22 | @Override 23 | public boolean preHandle(HttpServletRequest request, 24 | HttpServletResponse response, Object handler) throws Exception { 25 | 26 | // String token = UUID.randomUUID().toString().replaceAll("-",""); 27 | //本例测试使用sessionId,也可以使用UUID等 28 | String token = request.getSession().getId(); 29 | MDC.put(SESSION_KEY, token); 30 | return true; 31 | } 32 | 33 | @Override 34 | public void afterCompletion(HttpServletRequest arg0, 35 | HttpServletResponse arg1, Object arg2, Exception arg3) 36 | throws Exception { 37 | // 删除 38 | MDC.remove(SESSION_KEY); 39 | } 40 | } -------------------------------------------------------------------------------- /spring-boot-log/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8888 -------------------------------------------------------------------------------- /spring-boot-log/src/main/resources/logback-spring.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %date [%thread] [%X{sessionId}] %-5level %logger{80} - %msg%n 8 | 9 | 10 | 11 | 12 | 14 | ${log.base}.log 15 | 16 | ${log.base}.%d{yyyy -MM-dd}.log.zip 17 | 18 | 19 | %date [%thread] [%X{sessionId}] %-5level %logger{80} - %msg%n 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /spring-boot-log/src/test/java/com/example/springbootlog/SpringBootLogApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootlog; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootLogApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-mybatis-annotation/src/main/java/com/example/springboot/mybatisannotation/config/MybatisConfig.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.mybatisannotation.config; 2 | 3 | import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | 7 | /** 8 | * mybatis 注解版 9 | */ 10 | @Configuration 11 | public class MybatisConfig { 12 | 13 | @Bean 14 | public ConfigurationCustomizer configurationCustomizer() { 15 | return new ConfigurationCustomizer() { 16 | 17 | @Override 18 | public void customize(org.apache.ibatis.session.Configuration configuration) { 19 | configuration.setMapUnderscoreToCamelCase(true);//设置驼峰命名规则 20 | } 21 | }; 22 | } 23 | } -------------------------------------------------------------------------------- /spring-boot-mybatis-annotation/src/main/java/com/example/springboot/mybatisannotation/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.mybatisannotation.controller; 2 | 3 | import com.example.springboot.mybatisannotation.entity.User; 4 | import com.example.springboot.mybatisannotation.service.UserService; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.springframework.beans.factory.annotation.Autowired; 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 | import java.util.List; 12 | 13 | /** 14 | * UserController 15 | * 16 | * @Author: java_suisui 17 | */ 18 | @Slf4j 19 | @RestController 20 | @RequestMapping("/user") 21 | public class UserController { 22 | @Autowired 23 | private UserService userService; 24 | 25 | /** 26 | * 查询 所有用户 27 | */ 28 | @GetMapping("/queryAllUsers") 29 | public List queryAllUsers() { 30 | return userService.queryAllUsers(); 31 | } 32 | 33 | @GetMapping("/add") 34 | public User add(User user) { 35 | int num = userService.add(user); 36 | return user; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /spring-boot-mybatis-annotation/src/main/java/com/example/springboot/mybatisannotation/dao/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.mybatisannotation.dao.mapper; 2 | 3 | 4 | import com.example.springboot.mybatisannotation.entity.User; 5 | import org.apache.ibatis.annotations.Insert; 6 | import org.apache.ibatis.annotations.Mapper; 7 | import org.apache.ibatis.annotations.Options; 8 | import org.apache.ibatis.annotations.Select; 9 | 10 | import java.util.List; 11 | 12 | @Mapper 13 | public interface UserMapper { 14 | 15 | /* 16 | * 查询 所有用户 17 | */ 18 | @Select("SELECT * FROM user ") 19 | List queryAllUsers(); 20 | 21 | 22 | /* 23 | * 新增数据,并把主键绑定到User实体类 24 | */ 25 | @Options(useGeneratedKeys = true, keyProperty = "id") 26 | @Insert("insert into User(name,password) values (#{name},#{password})") 27 | int add(User user); 28 | } -------------------------------------------------------------------------------- /spring-boot-mybatis-annotation/src/main/java/com/example/springboot/mybatisannotation/dao/mapper/UserRoleMapper.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.mybatisannotation.dao.mapper; 2 | 3 | 4 | import com.example.springboot.mybatisannotation.entity.UserRole; 5 | import org.apache.ibatis.annotations.Insert; 6 | import org.apache.ibatis.annotations.Mapper; 7 | import org.apache.ibatis.annotations.Options; 8 | 9 | @Mapper 10 | public interface UserRoleMapper { 11 | 12 | 13 | /* 14 | * 新增数据,并把主键绑定到UserRole实体类 15 | */ 16 | @Options(useGeneratedKeys = true, keyProperty = "id") 17 | @Insert("insert into user_role(user_id,role_id) values (#{userId},#{roleId})") 18 | int add(UserRole user); 19 | } -------------------------------------------------------------------------------- /spring-boot-mybatis-annotation/src/main/java/com/example/springboot/mybatisannotation/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.mybatisannotation.entity; 2 | 3 | import lombok.Data; 4 | 5 | /** 6 | * User实体类 7 | * 8 | * @Author: java_suisui 9 | * 10 | */ 11 | @Data 12 | public class User { 13 | private Integer id; 14 | private String name; 15 | private String password; 16 | private Integer sex; 17 | private String des; 18 | 19 | } -------------------------------------------------------------------------------- /spring-boot-mybatis-annotation/src/main/java/com/example/springboot/mybatisannotation/entity/UserRole.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.mybatisannotation.entity; 2 | 3 | import lombok.Data; 4 | 5 | /** 6 | * UserRole实体类 7 | * 8 | * @Author: java_suisui 9 | * 10 | */ 11 | @Data 12 | public class UserRole { 13 | private Integer id; 14 | private Integer userId; 15 | private Integer roleId; 16 | 17 | } -------------------------------------------------------------------------------- /spring-boot-mybatis-annotation/src/main/java/com/example/springboot/mybatisannotation/service/UserRoleService.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.mybatisannotation.service; 2 | 3 | 4 | import com.example.springboot.mybatisannotation.entity.UserRole; 5 | 6 | public interface UserRoleService { 7 | 8 | /** 9 | * 增加用户角色 10 | */ 11 | int add(UserRole userRole); 12 | } -------------------------------------------------------------------------------- /spring-boot-mybatis-annotation/src/main/java/com/example/springboot/mybatisannotation/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.mybatisannotation.service; 2 | 3 | 4 | import com.example.springboot.mybatisannotation.entity.User; 5 | 6 | import java.util.List; 7 | 8 | public interface UserService { 9 | List queryAllUsers(); 10 | 11 | /** 12 | * 增加用户 13 | * 14 | */ 15 | int add(User user); 16 | } -------------------------------------------------------------------------------- /spring-boot-mybatis-annotation/src/main/java/com/example/springboot/mybatisannotation/service/impl/UserRoleServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.mybatisannotation.service.impl; 2 | 3 | import com.example.springboot.mybatisannotation.dao.mapper.UserMapper; 4 | import com.example.springboot.mybatisannotation.dao.mapper.UserRoleMapper; 5 | import com.example.springboot.mybatisannotation.entity.User; 6 | import com.example.springboot.mybatisannotation.entity.UserRole; 7 | import com.example.springboot.mybatisannotation.service.UserRoleService; 8 | import com.example.springboot.mybatisannotation.service.UserService; 9 | import lombok.extern.slf4j.Slf4j; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.stereotype.Service; 12 | import org.springframework.transaction.annotation.Propagation; 13 | import org.springframework.transaction.annotation.Transactional; 14 | 15 | import java.util.List; 16 | 17 | @Slf4j 18 | @Service 19 | public class UserRoleServiceImpl implements UserRoleService { 20 | 21 | @Autowired 22 | private UserRoleMapper userRoleMapper; 23 | 24 | 25 | 26 | /** 27 | * 增加用户角色 28 | */ 29 | @Transactional(propagation = Propagation.NESTED) 30 | @Override 31 | public int add(UserRole userRole) { 32 | return userRoleMapper.add(userRole); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /spring-boot-mybatis-annotation/src/main/java/com/example/springboot/mybatisannotation/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.mybatisannotation.service.impl; 2 | 3 | import com.example.springboot.mybatisannotation.dao.mapper.UserMapper; 4 | import com.example.springboot.mybatisannotation.entity.User; 5 | import com.example.springboot.mybatisannotation.service.UserService; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Service; 9 | import org.springframework.transaction.annotation.Propagation; 10 | import org.springframework.transaction.annotation.Transactional; 11 | 12 | import java.util.List; 13 | 14 | @Slf4j 15 | @Service 16 | public class UserServiceImpl implements UserService { 17 | 18 | @Autowired 19 | private UserMapper userMapper; 20 | 21 | @Override 22 | public List queryAllUsers() { 23 | log.info("/queryAllUsers start..."); 24 | return userMapper.queryAllUsers(); 25 | } 26 | 27 | /** 28 | * 增加用户 29 | */ 30 | @Transactional(propagation = Propagation.NESTED) 31 | @Override 32 | public int add(User user) { 33 | return userMapper.add(user); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /spring-boot-mybatis-annotation/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | #数据源 3 | datasource: 4 | url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull 5 | username: root 6 | password: 123456 7 | driver-class-name: com.mysql.jdbc.Driver 8 | debug: true 9 | -------------------------------------------------------------------------------- /spring-boot-mybatis-generator/.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 | -------------------------------------------------------------------------------- /spring-boot-mybatis-generator/src/main/java/com/example/springbootmybatis/generator/SpringBootMybatisGeneratorApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootmybatis.generator; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootMybatisGeneratorApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootMybatisGeneratorApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-mybatis-generator/src/main/java/com/example/springbootmybatis/generator/dao/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootmybatis.generator.dao; 2 | 3 | import com.example.springbootmybatis.generator.entity.User; 4 | import java.util.List; 5 | 6 | public interface UserMapper { 7 | int deleteByPrimaryKey(Integer id); 8 | 9 | int insert(User record); 10 | 11 | User selectByPrimaryKey(Integer id); 12 | 13 | List selectAll(); 14 | 15 | int updateByPrimaryKey(User record); 16 | } -------------------------------------------------------------------------------- /spring-boot-mybatis-generator/src/main/java/com/example/springbootmybatis/generator/dao/UserRoleMapper.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootmybatis.generator.dao; 2 | 3 | import com.example.springbootmybatis.generator.entity.UserRole; 4 | import java.util.List; 5 | 6 | public interface UserRoleMapper { 7 | int deleteByPrimaryKey(Integer id); 8 | 9 | int insert(UserRole record); 10 | 11 | UserRole selectByPrimaryKey(Integer id); 12 | 13 | List selectAll(); 14 | 15 | int updateByPrimaryKey(UserRole record); 16 | } -------------------------------------------------------------------------------- /spring-boot-mybatis-generator/src/main/java/com/example/springbootmybatis/generator/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootmybatis.generator.entity; 2 | 3 | import java.io.Serializable; 4 | 5 | public class User implements Serializable { 6 | private Integer id; 7 | 8 | private String name; 9 | 10 | private String password; 11 | 12 | private Integer sex; 13 | 14 | private String des; 15 | 16 | private Integer userType; 17 | 18 | private static final long serialVersionUID = 1L; 19 | 20 | public Integer getId() { 21 | return id; 22 | } 23 | 24 | public void setId(Integer id) { 25 | this.id = id; 26 | } 27 | 28 | public String getName() { 29 | return name; 30 | } 31 | 32 | public void setName(String name) { 33 | this.name = name == null ? null : name.trim(); 34 | } 35 | 36 | public String getPassword() { 37 | return password; 38 | } 39 | 40 | public void setPassword(String password) { 41 | this.password = password == null ? null : password.trim(); 42 | } 43 | 44 | public Integer getSex() { 45 | return sex; 46 | } 47 | 48 | public void setSex(Integer sex) { 49 | this.sex = sex; 50 | } 51 | 52 | public String getDes() { 53 | return des; 54 | } 55 | 56 | public void setDes(String des) { 57 | this.des = des == null ? null : des.trim(); 58 | } 59 | 60 | public Integer getUserType() { 61 | return userType; 62 | } 63 | 64 | public void setUserType(Integer userType) { 65 | this.userType = userType; 66 | } 67 | 68 | @Override 69 | public String toString() { 70 | StringBuilder sb = new StringBuilder(); 71 | sb.append(getClass().getSimpleName()); 72 | sb.append(" ["); 73 | sb.append("Hash = ").append(hashCode()); 74 | sb.append(", id=").append(id); 75 | sb.append(", name=").append(name); 76 | sb.append(", password=").append(password); 77 | sb.append(", sex=").append(sex); 78 | sb.append(", des=").append(des); 79 | sb.append(", userType=").append(userType); 80 | sb.append(", serialVersionUID=").append(serialVersionUID); 81 | sb.append("]"); 82 | return sb.toString(); 83 | } 84 | } -------------------------------------------------------------------------------- /spring-boot-mybatis-generator/src/main/java/com/example/springbootmybatis/generator/entity/UserRole.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootmybatis.generator.entity; 2 | 3 | import java.io.Serializable; 4 | 5 | public class UserRole implements Serializable { 6 | private Integer id; 7 | 8 | private Integer userId; 9 | 10 | private Integer roleId; 11 | 12 | private static final long serialVersionUID = 1L; 13 | 14 | public Integer getId() { 15 | return id; 16 | } 17 | 18 | public void setId(Integer id) { 19 | this.id = id; 20 | } 21 | 22 | public Integer getUserId() { 23 | return userId; 24 | } 25 | 26 | public void setUserId(Integer userId) { 27 | this.userId = userId; 28 | } 29 | 30 | public Integer getRoleId() { 31 | return roleId; 32 | } 33 | 34 | public void setRoleId(Integer roleId) { 35 | this.roleId = roleId; 36 | } 37 | 38 | @Override 39 | public String toString() { 40 | StringBuilder sb = new StringBuilder(); 41 | sb.append(getClass().getSimpleName()); 42 | sb.append(" ["); 43 | sb.append("Hash = ").append(hashCode()); 44 | sb.append(", id=").append(id); 45 | sb.append(", userId=").append(userId); 46 | sb.append(", roleId=").append(roleId); 47 | sb.append(", serialVersionUID=").append(serialVersionUID); 48 | sb.append("]"); 49 | return sb.toString(); 50 | } 51 | } -------------------------------------------------------------------------------- /spring-boot-mybatis-generator/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /spring-boot-mybatis-generator/src/main/resources/mapper/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | delete from user 14 | where id = #{id,jdbcType=INTEGER} 15 | 16 | 17 | 18 | SELECT LAST_INSERT_ID() 19 | 20 | insert into user (`name`, `password`, sex, 21 | des, user_type) 22 | values (#{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{sex,jdbcType=INTEGER}, 23 | #{des,jdbcType=VARCHAR}, #{userType,jdbcType=INTEGER}) 24 | 25 | 26 | update user 27 | set `name` = #{name,jdbcType=VARCHAR}, 28 | `password` = #{password,jdbcType=VARCHAR}, 29 | sex = #{sex,jdbcType=INTEGER}, 30 | des = #{des,jdbcType=VARCHAR}, 31 | user_type = #{userType,jdbcType=INTEGER} 32 | where id = #{id,jdbcType=INTEGER} 33 | 34 | 39 | 43 | -------------------------------------------------------------------------------- /spring-boot-mybatis-generator/src/main/resources/mapper/UserRoleMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | delete from user_role 11 | where id = #{id,jdbcType=INTEGER} 12 | 13 | 14 | 15 | SELECT LAST_INSERT_ID() 16 | 17 | insert into user_role (user_id, role_id) 18 | values (#{userId,jdbcType=INTEGER}, #{roleId,jdbcType=INTEGER}) 19 | 20 | 21 | update user_role 22 | set user_id = #{userId,jdbcType=INTEGER}, 23 | role_id = #{roleId,jdbcType=INTEGER} 24 | where id = #{id,jdbcType=INTEGER} 25 | 26 | 31 | 35 | -------------------------------------------------------------------------------- /spring-boot-mybatis-generator/src/test/java/com/example/springbootmybatis/generator/SpringBootMybatisGeneratorApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootmybatis.generator; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootMybatisGeneratorApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-mybatis-xml/.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 | -------------------------------------------------------------------------------- /spring-boot-mybatis-xml/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suisui2019/springboot-study/13083fe6fa2e748097a0b98c78bc76cb6f2ab98f/spring-boot-mybatis-xml/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /spring-boot-mybatis-xml/.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 | -------------------------------------------------------------------------------- /spring-boot-mybatis-xml/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.example 12 | spring-boot-mybatis-xml 13 | 0.0.1-SNAPSHOT 14 | spring-boot-mybatis-xml 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 | 29 | org.mybatis.spring.boot 30 | mybatis-spring-boot-starter 31 | 2.1.1 32 | 33 | 34 | 35 | 36 | mysql 37 | mysql-connector-java 38 | 5.1.39 39 | 40 | 41 | 42 | org.projectlombok 43 | lombok 44 | true 45 | 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-starter-test 50 | test 51 | 52 | 53 | org.junit.vintage 54 | junit-vintage-engine 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | org.springframework.boot 64 | spring-boot-maven-plugin 65 | 66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /spring-boot-mybatis-xml/src/main/java/com/example/springboot/mybatisxml/SpringBootMybatisXmlApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.mybatisxml; 2 | 3 | import org.mybatis.spring.annotation.MapperScan; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | @MapperScan("com.example.springboot.mybatisxml.dao.mapper") 7 | @SpringBootApplication 8 | public class SpringBootMybatisXmlApplication { 9 | 10 | public static void main(String[] args) { 11 | SpringApplication.run(SpringBootMybatisXmlApplication.class, args); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /spring-boot-mybatis-xml/src/main/java/com/example/springboot/mybatisxml/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.mybatisxml.controller; 2 | 3 | import com.example.springboot.mybatisxml.entity.User; 4 | import com.example.springboot.mybatisxml.service.UserService; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.springframework.beans.factory.annotation.Autowired; 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 | import java.util.List; 12 | 13 | /** 14 | * UserController 15 | * 16 | * @Author: java_suisui 17 | * 18 | */ 19 | @Slf4j 20 | @RestController 21 | @RequestMapping("/user") 22 | public class UserController { 23 | @Autowired 24 | private UserService userService; 25 | 26 | /** 27 | * 查询 所有用户 28 | * 29 | */ 30 | @GetMapping("/queryAllUsers") 31 | public List queryAllUsers(){ 32 | return userService.queryAllUsers(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /spring-boot-mybatis-xml/src/main/java/com/example/springboot/mybatisxml/dao/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.mybatisxml.dao.mapper; 2 | 3 | 4 | import com.example.springboot.mybatisxml.entity.User; 5 | 6 | import java.util.List; 7 | 8 | public interface UserMapper { 9 | 10 | List queryAllUsers(); 11 | } -------------------------------------------------------------------------------- /spring-boot-mybatis-xml/src/main/java/com/example/springboot/mybatisxml/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.mybatisxml.entity; 2 | 3 | import lombok.Data; 4 | /** 5 | * User实体类 6 | * 7 | * @Author: java_suisui 8 | * 9 | */ 10 | @Data 11 | public class User { 12 | private Integer id; 13 | private String name; 14 | private String password; 15 | private Integer sex; 16 | private String des; 17 | 18 | } -------------------------------------------------------------------------------- /spring-boot-mybatis-xml/src/main/java/com/example/springboot/mybatisxml/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.mybatisxml.service; 2 | 3 | 4 | import com.example.springboot.mybatisxml.entity.User; 5 | 6 | import java.util.List; 7 | 8 | public interface UserService { 9 | List queryAllUsers(); 10 | } -------------------------------------------------------------------------------- /spring-boot-mybatis-xml/src/main/java/com/example/springboot/mybatisxml/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.mybatisxml.service.impl; 2 | 3 | import com.example.springboot.mybatisxml.dao.mapper.UserMapper; 4 | import com.example.springboot.mybatisxml.entity.User; 5 | import com.example.springboot.mybatisxml.service.UserService; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Service; 9 | 10 | import java.util.List; 11 | 12 | @Slf4j 13 | @Service 14 | public class UserServiceImpl implements UserService { 15 | 16 | @Autowired 17 | private UserMapper userMapper; 18 | 19 | @Override 20 | public List queryAllUsers() { 21 | log.info("/queryAllUsers start..."); 22 | return userMapper.queryAllUsers(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /spring-boot-mybatis-xml/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | #数据源 3 | datasource: 4 | url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull 5 | username: root 6 | password: 123456 7 | driver-class-name: com.mysql.jdbc.Driver 8 | #mybatis配置 9 | mybatis: 10 | typeAliasesPackage: com.example.springboot.mybatisxml.entity 11 | mapperLocations: classpath:mapper/*.xml 12 | config-location: classpath:mybatis-config.xml 13 | -------------------------------------------------------------------------------- /spring-boot-mybatis-xml/src/main/resources/mapper/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 16 | -------------------------------------------------------------------------------- /spring-boot-mybatis-xml/src/main/resources/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /spring-boot-mybatis-xml/src/test/java/com/example/springboot/mybatisxml/SpringBootMybatisXmlApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.mybatisxml; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootMybatisXmlApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-read-yml/.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 | -------------------------------------------------------------------------------- /spring-boot-read-yml/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suisui2019/springboot-study/13083fe6fa2e748097a0b98c78bc76cb6f2ab98f/spring-boot-read-yml/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /spring-boot-read-yml/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /spring-boot-read-yml/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.0.RELEASE 9 | 10 | 11 | com.example 12 | spring-boot-read-yml 13 | 0.0.1-SNAPSHOT 14 | spring-boot-read-yml 15 | 读取yml配置文件 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-web 25 | 26 | 27 | 28 | org.projectlombok 29 | lombok 30 | true 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-test 35 | test 36 | 37 | 38 | org.junit.vintage 39 | junit-vintage-engine 40 | 41 | 42 | 43 | 44 | 45 | com.alibaba 46 | fastjson 47 | 1.2.60 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | org.springframework.boot 56 | spring-boot-maven-plugin 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /spring-boot-read-yml/src/main/java/com/example/springbootreadyml/SpringBootReadYmlApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootreadyml; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootReadYmlApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootReadYmlApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-read-yml/src/main/java/com/example/springbootreadyml/config/WxMpProperties.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootreadyml.config; 2 | 3 | import lombok.Data; 4 | import org.springframework.boot.context.properties.ConfigurationProperties; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * mp properties 10 | */ 11 | @Data 12 | @ConfigurationProperties(prefix = "wechat.mp") 13 | public class WxMpProperties { 14 | 15 | private List configs; 16 | 17 | @Data 18 | public static class MpConfig { 19 | /** 20 | * 设置微信公众号的appid 21 | */ 22 | private String appId; 23 | 24 | /** 25 | * 设置微信公众号的app secret 26 | */ 27 | private String secret; 28 | 29 | /** 30 | * 设置微信公众号的token 31 | */ 32 | private String token; 33 | 34 | /** 35 | * 设置微信公众号的EncodingAESKey 36 | */ 37 | private String aesKey; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /spring-boot-read-yml/src/main/java/com/example/springbootreadyml/controller/TestReadYmlController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootreadyml.controller; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.example.springbootreadyml.config.WxMpProperties; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.beans.factory.annotation.Value; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | /** 12 | * 读取yml文件的controller 13 | * 14 | * @Authro Java碎碎念 15 | */ 16 | @Slf4j 17 | @RestController 18 | public class TestReadYmlController { 19 | 20 | @Value("${server.port}") 21 | private Integer port; 22 | 23 | @Autowired 24 | private WxMpProperties wxMpProperties; 25 | 26 | @RequestMapping("/readYml") 27 | public void readYml() { 28 | log.info("server.port=" + port); 29 | log.info("wxMpProperties=" + JSON.toJSONString(wxMpProperties)); 30 | } 31 | } 32 | 33 | 34 | -------------------------------------------------------------------------------- /spring-boot-read-yml/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | wechat: 2 | mp: 3 | configs: 4 | - appid: appid1 5 | secret: arr1_secret 6 | token: arr1_token 7 | aesKey: arr1_key 8 | msgDataFormat: JSON 9 | 10 | - appid: appid2 11 | secret: arr2_secret 12 | token: arr2_token 13 | aesKey: arr2_key 14 | msgDataFormat: JSON 15 | server: 16 | port: 8888 17 | servlet: 18 | context-path: / -------------------------------------------------------------------------------- /spring-boot-read-yml/src/test/java/com/example/springbootreadyml/SpringBootReadYmlApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootreadyml; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootReadYmlApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-security/.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 | -------------------------------------------------------------------------------- /spring-boot-security/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.example 12 | spring-boot-security 13 | 0.0.1-SNAPSHOT 14 | spring-boot-security 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-security 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 | 50 | org.springframework.boot 51 | spring-boot-maven-plugin 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /spring-boot-security/src/main/java/com/example/springboot/security/SpringBootSecurityApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.security; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootSecurityApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootSecurityApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-security/src/main/java/com/example/springboot/security/config/MyPasswordEncoder.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.security.config; 2 | 3 | import org.springframework.security.crypto.password.PasswordEncoder; 4 | 5 | public class MyPasswordEncoder implements PasswordEncoder { 6 | @Override 7 | public String encode(CharSequence rawPassword) { 8 | return rawPassword.toString(); 9 | } 10 | 11 | @Override 12 | public boolean matches(CharSequence rawPassword, String encodedPassword) { 13 | return encodedPassword.equals(rawPassword); 14 | } 15 | } -------------------------------------------------------------------------------- /spring-boot-security/src/main/java/com/example/springboot/security/config/WebSecurityConfig.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.security.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 5 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 7 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 8 | 9 | /** 10 | * 配置类 11 | * @Author java_suisui 12 | * 13 | */ 14 | @EnableWebSecurity 15 | @Configuration 16 | public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 17 | 18 | @Override 19 | protected void configure(AuthenticationManagerBuilder auth) throws Exception { 20 | //配置内存中的 用户名、密码和角色 21 | auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder()).withUser("user").password("123456").roles("USER"); 22 | auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder()).withUser("admin").password("123456").roles("ADMIN"); 23 | } 24 | 25 | @Override 26 | protected void configure(HttpSecurity http) throws Exception { 27 | http.authorizeRequests() 28 | .antMatchers("/login").permitAll() 29 | .antMatchers("/user").hasRole("USER") //访问 /user这个接口,需要有USER角色 30 | .antMatchers("/admin").hasRole("ADMIN") 31 | .anyRequest().authenticated() //剩余的其他接口,登录之后就能访问 32 | .and() 33 | .formLogin().defaultSuccessUrl("/hello"); 34 | } 35 | } -------------------------------------------------------------------------------- /spring-boot-security/src/main/java/com/example/springboot/security/controller/AdminController.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.security.controller; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | /** 7 | * AdminController 8 | */ 9 | @RestController 10 | public class AdminController { 11 | 12 | @GetMapping("/admin") 13 | public String hello() { 14 | return "Hello,admin!"; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /spring-boot-security/src/main/java/com/example/springboot/security/controller/AppController.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.security.controller; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | /** 7 | * AppController 8 | */ 9 | @RestController 10 | public class AppController { 11 | 12 | @GetMapping("/hello") 13 | public String hello() { 14 | return "Hello,spring security!"; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /spring-boot-security/src/main/java/com/example/springboot/security/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.security.controller; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | /** 7 | * UserController 8 | */ 9 | @RestController 10 | public class UserController { 11 | 12 | @GetMapping("/user") 13 | public String hello() { 14 | return "Hello,user!"; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /spring-boot-security/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.security.user.name=java_suisui 2 | spring.security.user.password=123456 -------------------------------------------------------------------------------- /spring-boot-security/src/test/java/com/example/springboot/security/SpringBootSecurityApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.security; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootSecurityApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-study-base/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | 8 | org.springframework.boot 9 | spring-boot-starter-parent 10 | 2.1.6.RELEASE 11 | 12 | 13 | 14 | com.java.suisui.study 15 | spring-boot-study-base 16 | 1.0-SNAPSHOT 17 | 18 | 19 | 1.8 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-test 31 | test 32 | 33 | 34 | 35 | 36 | org.jsoup 37 | jsoup 38 | 1.12.1 39 | 40 | 41 | 42 | 43 | com.alibaba 44 | fastjson 45 | 1.2.58 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | org.springframework.boot 57 | spring-boot-maven-plugin 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /spring-boot-study-base/src/main/java/com/study/day01/Config.java: -------------------------------------------------------------------------------- 1 | package com.study.day01; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Conditional; 5 | import org.springframework.context.annotation.Configuration; 6 | 7 | /** 8 | * @Auther: lifq 9 | * @Description: 10 | */ 11 | @Configuration 12 | public class Config { 13 | 14 | @Bean 15 | @Conditional(WindowsCondition.class) 16 | public ListService window() { 17 | return new WindowsService(); 18 | } 19 | 20 | @Bean 21 | @Conditional(LinuxCondition.class) 22 | public ListService linux() { 23 | return new LinuxService(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /spring-boot-study-base/src/main/java/com/study/day01/LinuxCondition.java: -------------------------------------------------------------------------------- 1 | package com.study.day01; 2 | 3 | import org.springframework.context.annotation.Condition; 4 | import org.springframework.context.annotation.ConditionContext; 5 | import org.springframework.core.type.AnnotatedTypeMetadata; 6 | 7 | /** 8 | * @Auther: lifq 9 | * @Description: 10 | */ 11 | public class LinuxCondition implements Condition { 12 | public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { 13 | return context.getEnvironment().getProperty("os.name").contains("Linux"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /spring-boot-study-base/src/main/java/com/study/day01/LinuxService.java: -------------------------------------------------------------------------------- 1 | package com.study.day01; 2 | 3 | /** 4 | * @Auther: lifq 5 | * @Description: 6 | */ 7 | public class LinuxService implements ListService { 8 | public String showListCmd() { 9 | return "ls"; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /spring-boot-study-base/src/main/java/com/study/day01/ListService.java: -------------------------------------------------------------------------------- 1 | package com.study.day01; 2 | 3 | import org.springframework.stereotype.Service; 4 | 5 | /** 6 | * @Auther: lifq 7 | * @Description: 8 | */ 9 | public interface ListService { 10 | 11 | public String showListCmd(); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-study-base/src/main/java/com/study/day01/Main01.java: -------------------------------------------------------------------------------- 1 | package com.study.day01; 2 | 3 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 4 | 5 | /** 6 | * @Auther: lifq 7 | * @Description: 8 | */ 9 | public class Main01 { 10 | 11 | public static void main (String []args) { 12 | 13 | AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class); 14 | 15 | ListService listService = applicationContext.getBean(ListService.class); 16 | 17 | System.out.println(applicationContext.getEnvironment().getProperty("os.name") + "系统下的列表命令为:" + listService.showListCmd()); 18 | 19 | 20 | } 21 | 22 | 23 | 24 | } 25 | -------------------------------------------------------------------------------- /spring-boot-study-base/src/main/java/com/study/day01/WindowsCondition.java: -------------------------------------------------------------------------------- 1 | package com.study.day01; 2 | 3 | import org.springframework.context.annotation.Condition; 4 | import org.springframework.context.annotation.ConditionContext; 5 | import org.springframework.core.type.AnnotatedTypeMetadata; 6 | 7 | /** 8 | * @Auther: lifq 9 | * @Description: 10 | */ 11 | public class WindowsCondition implements Condition { 12 | public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { 13 | return context.getEnvironment().getProperty("os.name").contains("Windows"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /spring-boot-study-base/src/main/java/com/study/day01/WindowsService.java: -------------------------------------------------------------------------------- 1 | package com.study.day01; 2 | 3 | /** 4 | * @Auther: lifq 5 | * @Description: 6 | */ 7 | public class WindowsService implements ListService { 8 | public String showListCmd() { 9 | return "dir"; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /spring-boot-websocket/.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 | -------------------------------------------------------------------------------- /spring-boot-websocket/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.example 12 | spring-boot-websocket 13 | 0.0.1-SNAPSHOT 14 | spring-boot-websocket 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-websocket 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-thymeleaf 35 | 36 | 37 | 38 | org.projectlombok 39 | lombok 40 | true 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 | -------------------------------------------------------------------------------- /spring-boot-websocket/src/main/java/com/example/springboot/websocket/SpringBootWebsocketApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.websocket; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootWebsocketApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootWebsocketApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-websocket/src/main/java/com/example/springboot/websocket/config/WebSocketConfig.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.websocket.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.web.socket.server.standard.ServerEndpointExporter; 6 | 7 | 8 | @Configuration 9 | public class WebSocketConfig { 10 | /** 11 | * 给spring容器注入这个ServerEndpointExporter对象 12 | * 相当于xml: 13 | * 14 | * 15 | * 16 | *

17 | * 检测所有带有@serverEndpoint注解的bean并注册他们。 18 | * 19 | * @return 20 | */ 21 | @Bean 22 | public ServerEndpointExporter serverEndpointExporter() { 23 | System.out.println("我被注入了"); 24 | return new ServerEndpointExporter(); 25 | } 26 | } -------------------------------------------------------------------------------- /spring-boot-websocket/src/main/java/com/example/springboot/websocket/controller/WebSocketController.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.websocket.controller; 2 | 3 | import com.example.springboot.websocket.util.WebSocketServer; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RequestMethod; 6 | import org.springframework.web.bind.annotation.RequestParam; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.io.IOException; 10 | 11 | /** 12 | */ 13 | @RestController 14 | @RequestMapping("/api/ws") 15 | public class WebSocketController { 16 | 17 | 18 | /** 19 | * 群发消息内容 20 | * 21 | * @param message 22 | * @return 23 | */ 24 | @RequestMapping(value = "/sendAll", method = RequestMethod.GET) 25 | public String sendAllMessage(@RequestParam(required = true) String message) { 26 | try { 27 | WebSocketServer.BroadCastInfo(message); 28 | } catch (IOException e) { 29 | e.printStackTrace(); 30 | } 31 | return "ok"; 32 | } 33 | 34 | /** 35 | * 指定会话ID发消息 36 | * 37 | * @param message 消息内容 38 | * @param id 连接会话ID 39 | * @return 40 | */ 41 | @RequestMapping(value = "/sendOne", method = RequestMethod.GET) 42 | public String sendOneMessage(@RequestParam(required = true) String message, @RequestParam(required = true) String id) { 43 | try { 44 | WebSocketServer.SendMessage(message, id); 45 | } catch (IOException e) { 46 | e.printStackTrace(); 47 | } 48 | return "ok"; 49 | } 50 | } -------------------------------------------------------------------------------- /spring-boot-websocket/src/main/java/com/example/springboot/websocket/dto/SocketMessage.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.websocket.dto; 2 | 3 | public class SocketMessage { 4 | 5 | public String message; 6 | 7 | public String date; 8 | 9 | } -------------------------------------------------------------------------------- /spring-boot-websocket/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /spring-boot-websocket/src/main/resources/static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | websocket测试 6 | 7 | 12 | 13 | 14 | 15 |

WebSocket测试,客户端接收到的消息如下:

16 | 17 | 20 | 21 | 22 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /spring-boot-websocket/src/test/java/com/example/springboot/websocket/SpringBootWebsocketApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springboot.websocket; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootWebsocketApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-cache-demo/.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 | -------------------------------------------------------------------------------- /spring-cache-demo/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suisui2019/springboot-study/13083fe6fa2e748097a0b98c78bc76cb6f2ab98f/spring-cache-demo/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /spring-cache-demo/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /spring-cache-demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.7.RELEASE 9 | 10 | 11 | com.example 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-data-jpa 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter-cache 34 | 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-data-redis 39 | 40 | 41 | 42 | mysql 43 | mysql-connector-java 44 | runtime 45 | 46 | 47 | org.projectlombok 48 | lombok 49 | true 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-starter-test 54 | test 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | org.springframework.boot 63 | spring-boot-maven-plugin 64 | 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /spring-cache-demo/src/main/java/com/example/demo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cache.annotation.EnableCaching; 6 | /** 7 | * 整合缓存demo 8 | * 9 | * @Date: 2019/8/22 19:33 10 | * 11 | */ 12 | @SpringBootApplication 13 | @EnableCaching 14 | public class DemoApplication { 15 | public static void main(String[] args) { 16 | SpringApplication.run(DemoApplication.class, args); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /spring-cache-demo/src/main/java/com/example/demo/aop/LogRecordAspect.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.aop; 2 | 3 | import org.aspectj.lang.ProceedingJoinPoint; 4 | import org.aspectj.lang.annotation.Around; 5 | import org.aspectj.lang.annotation.Aspect; 6 | import org.aspectj.lang.annotation.Pointcut; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.context.annotation.Configuration; 10 | import org.springframework.web.context.request.RequestAttributes; 11 | import org.springframework.web.context.request.RequestContextHolder; 12 | import org.springframework.web.context.request.ServletRequestAttributes; 13 | 14 | import javax.servlet.http.HttpServletRequest; 15 | 16 | 17 | /** 18 | * 切面 打印请求、返回参数信息 19 | * 20 | * @author lifq 21 | *

22 | * 2018年10月18日 上午10:56:49 23 | */ 24 | @Aspect // 定义一个切面 25 | @Configuration 26 | public class LogRecordAspect { 27 | private static final Logger logger = LoggerFactory.getLogger(LogRecordAspect.class); 28 | 29 | // 定义切点Pointcut 30 | @Pointcut("execution(* com.example.demo.controller.*Controller.*(..))") 31 | public void excudeService() { 32 | } 33 | 34 | @Around("excudeService()") 35 | public Object doAround(ProceedingJoinPoint pjp) throws Throwable { 36 | RequestAttributes ra = RequestContextHolder.getRequestAttributes(); 37 | ServletRequestAttributes sra = (ServletRequestAttributes) ra; 38 | HttpServletRequest request = sra.getRequest(); 39 | 40 | String method = request.getMethod(); 41 | String uri = request.getRequestURI(); 42 | logger.info("***************************************************"); 43 | logger.info("请求开始, URI: {}, method: {}", uri, method); 44 | long start = System.currentTimeMillis(); 45 | 46 | // result的值就是被拦截方法的返回值 47 | Object result = pjp.proceed(); 48 | logger.info("请求结束, URI: {},耗时={}", uri, System.currentTimeMillis() - start); 49 | return result; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /spring-cache-demo/src/main/java/com/example/demo/config/WebConfig.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.config; 2 | 3 | import org.springframework.cache.CacheManager; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.context.annotation.Primary; 7 | import org.springframework.data.redis.cache.RedisCacheConfiguration; 8 | import org.springframework.data.redis.cache.RedisCacheManager; 9 | import org.springframework.data.redis.cache.RedisCacheWriter; 10 | import org.springframework.data.redis.connection.RedisConnectionFactory; 11 | import org.springframework.data.redis.core.RedisTemplate; 12 | import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; 13 | import org.springframework.data.redis.serializer.RedisSerializationContext; 14 | import org.springframework.data.redis.serializer.RedisSerializer; 15 | import org.springframework.data.redis.serializer.StringRedisSerializer; 16 | 17 | import java.time.Duration; 18 | 19 | @Configuration 20 | public class WebConfig { 21 | 22 | @Bean(name = "redisTemplate") 23 | public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) { 24 | 25 | RedisTemplate redisTemplate = new RedisTemplate<>(); 26 | 27 | redisTemplate.setConnectionFactory(redisConnectionFactory); 28 | redisTemplate.setKeySerializer(keySerializer()); 29 | redisTemplate.setHashKeySerializer(keySerializer()); 30 | redisTemplate.setValueSerializer(valueSerializer()); 31 | redisTemplate.setHashValueSerializer(valueSerializer()); 32 | return redisTemplate; 33 | } 34 | 35 | @Primary 36 | @Bean 37 | public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { 38 | //缓存配置对象 39 | RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig(); 40 | 41 | redisCacheConfiguration = redisCacheConfiguration.entryTtl(Duration.ofMinutes(30L)) //设置缓存的默认超时时间:30分钟 42 | .disableCachingNullValues() //如果是空值,不缓存 43 | .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(keySerializer())) //设置key序列化器 44 | .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer((valueSerializer()))); //设置value序列化器 45 | 46 | return RedisCacheManager 47 | .builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory)) 48 | .cacheDefaults(redisCacheConfiguration).build(); 49 | } 50 | 51 | private RedisSerializer keySerializer() { 52 | return new StringRedisSerializer(); 53 | } 54 | 55 | private RedisSerializer valueSerializer() { 56 | return new GenericJackson2JsonRedisSerializer(); 57 | } 58 | } -------------------------------------------------------------------------------- /spring-cache-demo/src/main/java/com/example/demo/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.controller; 2 | 3 | import com.example.demo.entity.User; 4 | import com.example.demo.service.UserService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.cache.annotation.Cacheable; 7 | import org.springframework.web.bind.annotation.PathVariable; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | @RestController 12 | public class UserController { 13 | @Autowired 14 | UserService service; 15 | 16 | /** 17 | * 根据id 查询 18 | */ 19 | 20 | @RequestMapping("/getUser/{id}") 21 | public User getUser(@PathVariable("id") Integer id) { 22 | return service.getUser(id); 23 | } 24 | 25 | /** 26 | * 根据id 删除 27 | */ 28 | @RequestMapping("/deleteUser/{id}") 29 | public String deleteUser(@PathVariable("id") Integer id) { 30 | service.deleteUser(id); 31 | return "删除成功!"; 32 | } 33 | 34 | 35 | @RequestMapping("/updateUser/{id}") 36 | public User updateUser(@PathVariable("id") Integer id) { 37 | User user = service.getUser(id); 38 | user.setName(user.getName()+System.currentTimeMillis()); 39 | return service.updateUser(user); 40 | } 41 | } -------------------------------------------------------------------------------- /spring-cache-demo/src/main/java/com/example/demo/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.entity; 2 | 3 | import javax.persistence.*; 4 | 5 | @Entity 6 | @Table(name = "user") 7 | public class User { 8 | 9 | @Id 10 | @GeneratedValue(strategy = GenerationType.AUTO) 11 | private Integer id; 12 | private String name; 13 | private String password; 14 | 15 | public Integer getId() { 16 | return id; 17 | } 18 | 19 | public void setId(Integer id) { 20 | this.id = id; 21 | } 22 | 23 | public String getName() { 24 | return name; 25 | } 26 | 27 | public void setName(String name) { 28 | this.name = name; 29 | } 30 | 31 | public String getPassword() { 32 | return password; 33 | } 34 | 35 | public void setPassword(String password) { 36 | this.password = password; 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /spring-cache-demo/src/main/java/com/example/demo/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.repository; 2 | 3 | import com.example.demo.entity.User; 4 | import org.springframework.data.jpa.repository.Query; 5 | import org.springframework.data.repository.CrudRepository; 6 | import org.springframework.data.repository.query.Param; 7 | 8 | /** 9 | * 注意: 10 | * 1.这里这里是interface,不是class 11 | * 2.CrudRepository里面的泛型,第一个是实体类,第二个是主键的类型 12 | */ 13 | public interface UserRepository extends CrudRepository { 14 | 15 | @Query("from User where id =:id ") 16 | public User getUser(@Param("id") Integer id); 17 | } -------------------------------------------------------------------------------- /spring-cache-demo/src/main/java/com/example/demo/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.service; 2 | 3 | 4 | import com.example.demo.entity.User; 5 | 6 | public interface UserService { 7 | 8 | 9 | public User getUser(Integer id); 10 | 11 | public void deleteUser(Integer id); 12 | 13 | 14 | public User updateUser(User updateUser); 15 | } -------------------------------------------------------------------------------- /spring-cache-demo/src/main/java/com/example/demo/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.service.impl; 2 | 3 | import com.example.demo.entity.User; 4 | import com.example.demo.repository.UserRepository; 5 | import com.example.demo.service.UserService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.cache.annotation.CacheConfig; 8 | import org.springframework.cache.annotation.CacheEvict; 9 | import org.springframework.cache.annotation.CachePut; 10 | import org.springframework.cache.annotation.Cacheable; 11 | import org.springframework.stereotype.Service; 12 | 13 | @Service 14 | public class UserServiceImpl implements UserService { 15 | 16 | @Autowired 17 | UserRepository repository; 18 | 19 | //查询数据库后 数据添加到缓存 20 | @Override 21 | @Cacheable(cacheNames = "cacheManager", key = "'USER:'+#id", unless = "#result == null") 22 | public User getUser(Integer id) { 23 | return repository.getUser(id); 24 | } 25 | 26 | //清除一条缓存,key为要清空的数据 27 | @Override 28 | @CacheEvict(cacheNames = "cacheManager", key = "'USER:'+#id") 29 | public void deleteUser(Integer id) { 30 | repository.deleteById(id); 31 | } 32 | 33 | 34 | //修改数据后更新缓存 35 | @Override 36 | @CachePut(cacheNames = "cacheManager", key = "'USER:'+#updateUser.id", unless = "#result == null") 37 | public User updateUser(User updateUser) { 38 | return repository.save(updateUser); 39 | } 40 | 41 | 42 | } -------------------------------------------------------------------------------- /spring-cache-demo/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suisui2019/springboot-study/13083fe6fa2e748097a0b98c78bc76cb6f2ab98f/spring-cache-demo/src/main/resources/application.properties -------------------------------------------------------------------------------- /spring-cache-demo/src/test/java/com/example/demo/DemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.demo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class DemoApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-cloud-consumer-fegin-hystrix/.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 | -------------------------------------------------------------------------------- /spring-cloud-consumer-fegin-hystrix/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suisui2019/springboot-study/13083fe6fa2e748097a0b98c78bc76cb6f2ab98f/spring-cloud-consumer-fegin-hystrix/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /spring-cloud-consumer-fegin-hystrix/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /spring-cloud-consumer-fegin-hystrix/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.9.RELEASE 9 | 10 | 11 | com.example 12 | spring-cloud-consumer-fegin-hystrix 13 | 0.0.1-SNAPSHOT 14 | spring-cloud-consumer-fegin-hystrix 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.springframework.cloud 28 | spring-cloud-starter-openfeign 29 | 30 | 31 | 32 | org.springframework.cloud 33 | spring-cloud-starter-netflix-eureka-client 34 | 35 | 36 | 37 | org.projectlombok 38 | lombok 39 | true 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-starter-test 44 | test 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.springframework.cloud 53 | spring-cloud-dependencies 54 | Finchley.RELEASE 55 | pom 56 | import 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | org.springframework.boot 66 | spring-boot-maven-plugin 67 | 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /spring-cloud-consumer-fegin-hystrix/src/main/java/com/example/springcloudconsumerfeginhystrix/SpringCloudConsumerFeginHystrixApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springcloudconsumerfeginhystrix; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 6 | import org.springframework.cloud.openfeign.EnableFeignClients; 7 | 8 | @EnableEurekaClient 9 | @EnableFeignClients 10 | @SpringBootApplication 11 | public class SpringCloudConsumerFeginHystrixApplication { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(SpringCloudConsumerFeginHystrixApplication.class, args); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /spring-cloud-consumer-fegin-hystrix/src/main/java/com/example/springcloudconsumerfeginhystrix/controller/FeginHystrixController.java: -------------------------------------------------------------------------------- 1 | package com.example.springcloudconsumerfeginhystrix.controller; 2 | 3 | import com.example.springcloudconsumerfeginhystrix.service.UserFeginService; 4 | import org.springframework.beans.factory.annotation.Autowired; 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 | @RestController 10 | @RequestMapping("/hystrix/consumer") 11 | public class FeginHystrixController { 12 | 13 | @Autowired 14 | private UserFeginService userFeginService; 15 | 16 | @GetMapping("/getUser") 17 | public String getUser(Integer id) { 18 | return userFeginService.getUser(id); 19 | } 20 | } -------------------------------------------------------------------------------- /spring-cloud-consumer-fegin-hystrix/src/main/java/com/example/springcloudconsumerfeginhystrix/service/UserFeginService.java: -------------------------------------------------------------------------------- 1 | package com.example.springcloudconsumerfeginhystrix.service; 2 | 3 | import com.example.springcloudconsumerfeginhystrix.service.impl.UserFeginFailBackImpl; 4 | import org.springframework.cloud.openfeign.FeignClient; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RequestParam; 7 | 8 | //表示"user-service"的服务,指定fallback 9 | @FeignClient(value = "user-service", fallback = UserFeginFailBackImpl.class) 10 | public interface UserFeginService { 11 | 12 | @RequestMapping(value = "/provider/getUser") 13 | public String getUser(@RequestParam("id") Integer id); 14 | } -------------------------------------------------------------------------------- /spring-cloud-consumer-fegin-hystrix/src/main/java/com/example/springcloudconsumerfeginhystrix/service/impl/UserFeginFailBackImpl.java: -------------------------------------------------------------------------------- 1 | package com.example.springcloudconsumerfeginhystrix.service.impl; 2 | 3 | import com.example.springcloudconsumerfeginhystrix.service.UserFeginService; 4 | import lombok.extern.slf4j.Slf4j; 5 | import org.springframework.stereotype.Component; 6 | 7 | @Slf4j 8 | @Component 9 | public class UserFeginFailBackImpl implements UserFeginService { 10 | 11 | @Override 12 | public String getUser(Integer id) { 13 | log.info("熔断,默认回调函数"); 14 | return "{\"id\":-1,\"name\":\"熔断用户\",\"msg\":\"请求异常,返回熔断用户!\"}"; 15 | } 16 | } -------------------------------------------------------------------------------- /spring-cloud-consumer-fegin-hystrix/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8082 #服务端口 3 | eureka: 4 | client: 5 | serviceUrl: 6 | defaultZone: http://localhost:9001/eureka/ 7 | spring: 8 | application: 9 | name: fegin-hystrix-consumer 10 | 11 | feign: 12 | hystrix: 13 | # feign熔断器开关 14 | enabled: true -------------------------------------------------------------------------------- /spring-cloud-consumer-fegin-hystrix/src/test/java/com/example/springcloudconsumerfeginhystrix/SpringCloudConsumerFeginHystrixApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springcloudconsumerfeginhystrix; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringCloudConsumerFeginHystrixApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-cloud-consumer-fegin/.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 | -------------------------------------------------------------------------------- /spring-cloud-consumer-fegin/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suisui2019/springboot-study/13083fe6fa2e748097a0b98c78bc76cb6f2ab98f/spring-cloud-consumer-fegin/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /spring-cloud-consumer-fegin/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /spring-cloud-consumer-fegin/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.8.RELEASE 9 | 10 | 11 | com.example 12 | spring-cloud-consumer-fegin 13 | 0.0.1-SNAPSHOT 14 | spring-cloud-consumer-fegin 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.springframework.cloud 28 | spring-cloud-starter-openfeign 29 | 30 | 31 | 32 | org.springframework.cloud 33 | spring-cloud-starter-netflix-eureka-client 34 | 35 | 36 | 37 | org.projectlombok 38 | lombok 39 | true 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-starter-test 44 | test 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.springframework.cloud 53 | spring-cloud-dependencies 54 | Finchley.RELEASE 55 | pom 56 | import 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | org.springframework.boot 65 | spring-boot-maven-plugin 66 | 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /spring-cloud-consumer-fegin/src/main/java/com/example/springcloudconsumerfegin/SpringCloudConsumerFeginApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springcloudconsumerfegin; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 7 | import org.springframework.cloud.openfeign.EnableFeignClients; 8 | 9 | @EnableEurekaClient 10 | @EnableFeignClients 11 | @SpringBootApplication 12 | public class SpringCloudConsumerFeginApplication { 13 | 14 | public static void main(String[] args) { 15 | SpringApplication.run(SpringCloudConsumerFeginApplication.class, args); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /spring-cloud-consumer-fegin/src/main/java/com/example/springcloudconsumerfegin/controller/FeginController.java: -------------------------------------------------------------------------------- 1 | package com.example.springcloudconsumerfegin.controller; 2 | 3 | import com.example.springcloudconsumerfegin.service.IFeginService; 4 | import org.springframework.beans.factory.annotation.Autowired; 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 | @RestController 10 | @RequestMapping("/consumer") 11 | public class FeginController { 12 | 13 | @Autowired 14 | private IFeginService feginService; 15 | 16 | @GetMapping("/getUser") 17 | public String getUser(Integer id) { 18 | return feginService.getUser(id); 19 | } 20 | } -------------------------------------------------------------------------------- /spring-cloud-consumer-fegin/src/main/java/com/example/springcloudconsumerfegin/service/IFeginService.java: -------------------------------------------------------------------------------- 1 | package com.example.springcloudconsumerfegin.service; 2 | 3 | import org.springframework.cloud.openfeign.FeignClient; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RequestParam; 6 | 7 | //表示"user-service"的服务 提供 8 | @FeignClient(value = "user-service") 9 | public interface IFeginService { 10 | 11 | @RequestMapping(value = "/provider/getUser") 12 | public String getUser(@RequestParam("id") Integer id); 13 | } -------------------------------------------------------------------------------- /spring-cloud-consumer-fegin/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | 2 | server: 3 | port: 8083 #服务端口 4 | eureka: 5 | client: 6 | serviceUrl: 7 | defaultZone: http://localhost:9001/eureka/ 8 | spring: 9 | application: 10 | name: fegin-consumer -------------------------------------------------------------------------------- /spring-cloud-consumer-fegin/src/test/java/com/example/springcloudconsumerfegin/SpringCloudConsumerFeginApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springcloudconsumerfegin; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringCloudConsumerFeginApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-cloud-consumer-ribbon/.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 | -------------------------------------------------------------------------------- /spring-cloud-consumer-ribbon/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suisui2019/springboot-study/13083fe6fa2e748097a0b98c78bc76cb6f2ab98f/spring-cloud-consumer-ribbon/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /spring-cloud-consumer-ribbon/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /spring-cloud-consumer-ribbon/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.0.3.RELEASE 9 | 10 | 11 | com.example 12 | demo 13 | 0.0.1-SNAPSHOT 14 | RibbonConsumer 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.cloud 29 | spring-cloud-starter-netflix-eureka-client 30 | 31 | 32 | 33 | org.projectlombok 34 | lombok 35 | true 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-test 40 | test 41 | 42 | 43 | 44 | org.springframework.cloud 45 | spring-cloud-starter-netflix-hystrix 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | org.springframework.cloud 54 | spring-cloud-dependencies 55 | Finchley.RELEASE 56 | pom 57 | import 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | org.springframework.boot 67 | spring-boot-maven-plugin 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /spring-cloud-consumer-ribbon/src/main/java/com/example/demo/RibbonConsumerApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | import org.springframework.cloud.client.loadbalancer.LoadBalanced; 7 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 8 | import org.springframework.cloud.netflix.hystrix.EnableHystrix; 9 | import org.springframework.context.annotation.Bean; 10 | import org.springframework.web.client.RestTemplate; 11 | 12 | 13 | @EnableHystrix //在启动类上添加@EnableHystrix注解开启Hystrix的熔断器功能。 14 | @EnableEurekaClient 15 | @SpringBootApplication 16 | public class RibbonConsumerApplication { 17 | 18 | //当添加@LoadBalanced注解,就代表启动Ribbon,进行负载均衡 19 | @LoadBalanced 20 | @Bean 21 | public RestTemplate restTemplate() { 22 | return new RestTemplate(); 23 | } 24 | 25 | public static void main(String[] args) { 26 | SpringApplication.run(RibbonConsumerApplication.class, args); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /spring-cloud-consumer-ribbon/src/main/java/com/example/demo/controller/HystrixConsumerController.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.controller; 2 | 3 | import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; 4 | import lombok.extern.slf4j.Slf4j; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; 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 | import org.springframework.web.client.RestTemplate; 11 | 12 | /** 13 | * 消费者 14 | */ 15 | @Slf4j 16 | @RestController 17 | @RequestMapping("/hystrix/consumer") 18 | public class HystrixConsumerController { 19 | 20 | @Autowired 21 | private RestTemplate restTemplate; 22 | 23 | /** 24 | * 调用 user微服务 25 | */ 26 | @HystrixCommand(fallbackMethod = "getDefaultUser") 27 | @GetMapping("getUser") 28 | public String getUser(Integer id) { 29 | String url = "http://user-service/provider/getUser?id=" + id; 30 | return restTemplate.getForObject(url, String.class); 31 | } 32 | 33 | public String getDefaultUser(Integer id) { 34 | log.info("熔断,默认回调函数"); 35 | return "{\"id\":-1,\"name\":\"熔断用户\",\"msg\":\"请求异常,返回熔断用户!\"}"; 36 | } 37 | } -------------------------------------------------------------------------------- /spring-cloud-consumer-ribbon/src/main/java/com/example/demo/controller/RibbonConsumerController.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.controller; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.cloud.client.ServiceInstance; 6 | import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; 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 | import org.springframework.web.client.RestTemplate; 11 | 12 | /** 13 | * 消费者 14 | */ 15 | @Slf4j 16 | @RestController 17 | @RequestMapping("/consumer") 18 | public class RibbonConsumerController { 19 | 20 | @Autowired 21 | private RestTemplate restTemplate; 22 | 23 | /** 24 | * 调用 user微服务 25 | */ 26 | @GetMapping("getUser") 27 | public String getUser(Integer id) { 28 | String url = "http://user-service/provider/getUser?id=" + id; 29 | return restTemplate.getForObject(url, String.class); 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /spring-cloud-consumer-ribbon/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | 2 | server: 3 | port: 8082 #服务端口 4 | eureka: 5 | client: 6 | serviceUrl: 7 | defaultZone: http://localhost:9001/eureka/ 8 | spring: 9 | application: 10 | name: user-service-consumer-ribbon -------------------------------------------------------------------------------- /spring-cloud-consumer-ribbon/src/test/java/com/example/demo/RibbonConsumerApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.demo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class RibbonConsumerApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-cloud-eureka/.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 | -------------------------------------------------------------------------------- /spring-cloud-eureka/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suisui2019/springboot-study/13083fe6fa2e748097a0b98c78bc76cb6f2ab98f/spring-cloud-eureka/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /spring-cloud-eureka/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /spring-cloud-eureka/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.0.3.RELEASE 9 | 10 | 11 | com.example 12 | demo 13 | 0.0.1-SNAPSHOT 14 | spring-cloud-eureka 15 | spring-cloud-eureka demo 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | org.springframework.cloud 29 | spring-cloud-starter-netflix-eureka-server 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-test 35 | test 36 | 37 | 38 | 39 | 40 | 41 | 42 | org.springframework.cloud 43 | spring-cloud-dependencies 44 | Finchley.RELEASE 45 | pom 46 | import 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | org.springframework.boot 55 | spring-boot-maven-plugin 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /spring-cloud-eureka/src/main/java/com/example/demo/SpringCloudEurekaApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 | 7 | @EnableEurekaServer 8 | @SpringBootApplication 9 | public class SpringCloudEurekaApplication { 10 | public static void main(String[] args) { 11 | SpringApplication.run(SpringCloudEurekaApplication.class, args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /spring-cloud-eureka/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 9001 #服务端口 3 | eureka: 4 | instance: 5 | hostname: localhost 6 | client: 7 | register-with-eureka: false #是否将eureka自身作为应用注册到eureka注册中心 8 | fetch-registry: false #为true时,可以启动,但报异常:Cannot execute request on any known server 9 | serviceUrl: 10 | defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ -------------------------------------------------------------------------------- /spring-cloud-eureka/src/test/java/com/example/demo/SpringCloudEurekaApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.demo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringCloudEurekaApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-cloud-user-consumer/.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 | -------------------------------------------------------------------------------- /spring-cloud-user-consumer/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suisui2019/springboot-study/13083fe6fa2e748097a0b98c78bc76cb6f2ab98f/spring-cloud-user-consumer/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /spring-cloud-user-consumer/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /spring-cloud-user-consumer/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.8.RELEASE 9 | 10 | 11 | com.example 12 | spring-cloud-user-consumer 13 | 0.0.1-SNAPSHOT 14 | spring-cloud-user-consumer 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 | 34 | org.springframework.cloud 35 | spring-cloud-starter-netflix-eureka-client 36 | 37 | 38 | 39 | org.projectlombok 40 | lombok 41 | true 42 | 43 | 44 | 45 | 46 | 47 | 48 | org.springframework.cloud 49 | spring-cloud-dependencies 50 | Finchley.RELEASE 51 | pom 52 | import 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | org.springframework.boot 61 | spring-boot-maven-plugin 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /spring-cloud-user-consumer/src/main/java/com/example/demo/SpringCloudUserConsumerApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.web.client.RestTemplate; 8 | 9 | @EnableDiscoveryClient 10 | @SpringBootApplication 11 | public class SpringCloudUserConsumerApplication { 12 | 13 | @Bean 14 | public RestTemplate restTemplate() { 15 | return new RestTemplate(); 16 | } 17 | 18 | public static void main(String[] args) { 19 | SpringApplication.run(SpringCloudUserConsumerApplication.class, args); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring-cloud-user-consumer/src/main/java/com/example/demo/controller/ConsumerController.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.controller; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.cloud.client.ServiceInstance; 6 | import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; 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 | import org.springframework.web.client.RestTemplate; 11 | 12 | /** 13 | * 消费者 14 | */ 15 | @Slf4j 16 | @RestController 17 | @RequestMapping("/consumer") 18 | public class ConsumerController { 19 | 20 | @Autowired 21 | private RestTemplate restTemplate; 22 | 23 | @Autowired 24 | private LoadBalancerClient loadBalancerClient; 25 | 26 | /** 27 | * 调用 user微服务 28 | */ 29 | @GetMapping("getUser") 30 | public String getUser(Integer id) { 31 | ServiceInstance serviceInstance = loadBalancerClient.choose("user-service"); 32 | log.info("打印user服务信息={}", serviceInstance); 33 | String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/provider/getUser?id=" + id; 34 | return restTemplate.getForObject(url, String.class); 35 | } 36 | 37 | } -------------------------------------------------------------------------------- /spring-cloud-user-consumer/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | 2 | server: 3 | port: 8082 #服务端口 4 | eureka: 5 | client: 6 | serviceUrl: 7 | defaultZone: http://localhost:9001/eureka/ 8 | spring: 9 | application: 10 | name: user-service-consumer -------------------------------------------------------------------------------- /spring-cloud-user-consumer/src/test/java/com/example/demo/SpringCloudUserConsumerApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.demo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringCloudUserConsumerApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-cloud-user-service/.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 | -------------------------------------------------------------------------------- /spring-cloud-user-service/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suisui2019/springboot-study/13083fe6fa2e748097a0b98c78bc76cb6f2ab98f/spring-cloud-user-service/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /spring-cloud-user-service/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /spring-cloud-user-service/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.0.3.RELEASE 9 | 10 | 11 | com.example 12 | spring-cloud-user-service 13 | 0.0.1-SNAPSHOT 14 | spring-cloud-user-service 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 | 34 | com.alibaba 35 | fastjson 36 | 1.2.60 37 | 38 | 39 | 40 | org.springframework.cloud 41 | spring-cloud-starter-netflix-eureka-client 42 | 43 | 44 | 45 | org.projectlombok 46 | lombok 47 | true 48 | 49 | 50 | 51 | 52 | 53 | 54 | org.springframework.cloud 55 | spring-cloud-dependencies 56 | Finchley.RELEASE 57 | pom 58 | import 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | org.springframework.boot 67 | spring-boot-maven-plugin 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /spring-cloud-user-service/src/main/java/com/example/demo/UserServiceDemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 6 | 7 | @EnableEurekaClient 8 | @SpringBootApplication 9 | public class UserServiceDemoApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(UserServiceDemoApplication.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /spring-cloud-user-service/src/main/java/com/example/demo/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.controller; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.example.demo.entity.User; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.HashMap; 10 | import java.util.Map; 11 | 12 | /** 13 | * 用户服务 14 | */ 15 | @Slf4j 16 | @RestController 17 | @RequestMapping("/provider") 18 | public class UserController { 19 | 20 | static Map userMap = new HashMap<>(); 21 | 22 | // static { 23 | // //模拟数据库 24 | // User user1 = new User(1, "张三", "123456"); 25 | // userMap.put(1, user1); 26 | // User user2 = new User(2, "李四", "123123"); 27 | // userMap.put(2, user2); 28 | // } 29 | 30 | /** 31 | * 根据id 查询 32 | */ 33 | @RequestMapping("/getUser") 34 | public String getUser(Integer id) { 35 | log.info("调用getUser接口,id={}",id); 36 | User user = userMap.get(id); 37 | return JSON.toJSONString(user); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /spring-cloud-user-service/src/main/java/com/example/demo/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.entity; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | /** 7 | * @Auther: lifq 8 | * @Description: 9 | */ 10 | @Data 11 | public class User { 12 | 13 | private static final long serialVersionUID = 1L; 14 | 15 | private Integer id; 16 | private String name; 17 | private String password; 18 | } 19 | -------------------------------------------------------------------------------- /spring-cloud-user-service/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | 2 | server: 3 | port: 8081 #服务端口 4 | eureka: 5 | client: 6 | serviceUrl: 7 | defaultZone: http://localhost:9001/eureka/ 8 | spring: 9 | application: 10 | name: user-service -------------------------------------------------------------------------------- /spring-cloud-user-service/src/test/java/com/example/demo/DemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.demo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class DemoApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-cloud-user-service/src/test/java/com/example/demo/Test.java: -------------------------------------------------------------------------------- 1 | package com.example.demo; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.example.demo.entity.User; 5 | 6 | import java.util.ArrayList; 7 | import java.util.HashMap; 8 | import java.util.List; 9 | import java.util.Map; 10 | 11 | public class Test { 12 | 13 | 14 | @org.junit.Test 15 | public void testFastJson() { 16 | Map dataMap = new HashMap<>(); 17 | List userList1 = new ArrayList<>(); 18 | List userList2 = new ArrayList<>(); 19 | 20 | for (int i = 0; i < 2; i++) { 21 | User user = new User(); 22 | user.setId(i); 23 | userList1.add(user); 24 | userList2.add(user); 25 | } 26 | dataMap.put("userList1", userList1); 27 | dataMap.put("userList2", userList2); 28 | 29 | System.out.println(JSON.toJSONString(dataMap)); 30 | } 31 | 32 | 33 | } 34 | 35 | -------------------------------------------------------------------------------- /spring-mybatis-generator/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | #数据源 3 | datasource: 4 | url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull 5 | username: root 6 | password: 123456 7 | driver-class-name: com.mysql.jdbc.Driver 8 | #mybatis配置 9 | mybatis: 10 | mapperLocations: classpath:mapper/*.xml 11 | -------------------------------------------------------------------------------- /spring-mybatis-generator/src/main/resources/mybatis-generator.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 |
60 | 61 | 62 | 63 |
64 | 65 |
66 |
--------------------------------------------------------------------------------