├── springboot(十六) └── es-job1 │ └── es-job │ ├── job_debug.txt │ ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ ├── maven-wrapper.properties │ │ └── MavenWrapperDownloader.java │ ├── src │ ├── main │ │ ├── resources │ │ │ └── application.yml │ │ └── java │ │ │ └── com │ │ │ └── kevin │ │ │ └── task │ │ │ ├── task │ │ │ ├── MySimpleJob.java │ │ │ └── MyDataflowJob.java │ │ │ ├── EsJobApplication.java │ │ │ ├── entity │ │ │ └── Foo.java │ │ │ ├── listener │ │ │ └── SimpleJobListener.java │ │ │ └── config │ │ │ ├── JobEventConfig.java │ │ │ ├── RegistryCenterConfig.java │ │ │ ├── DataflowJobConfig.java │ │ │ └── MySimpleJobConfig.java │ └── test │ │ └── java │ │ └── com │ │ └── kevin │ │ └── task │ │ └── EsJobApplicationTests.java │ ├── .gitignore │ └── pom.xml ├── springboot(十一) ├── import │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ ├── application.yml │ │ │ │ ├── templates │ │ │ │ │ └── index.html │ │ │ │ └── banner.txt │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── zhb │ │ │ │ ├── ImportApplication.java │ │ │ │ ├── controller │ │ │ │ └── ImportController.java │ │ │ │ └── service │ │ │ │ └── ImportService.java │ │ └── test │ │ │ └── java │ │ │ └── com │ │ │ └── zhb │ │ │ └── ImportApplicationTests.java │ ├── .gitignore │ └── pom.xml └── SpringBoot(十一)_springboot上传excel并读取数据.md ├── springboot(二) ├── girl │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ ├── application.yml │ │ │ │ ├── application-dev.yml │ │ │ │ └── application-prod.yml │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── imooc │ │ │ │ ├── GirlApplication.java │ │ │ │ ├── properties │ │ │ │ └── GirlProperties.java │ │ │ │ └── controller │ │ │ │ └── HelloController.java │ │ └── test │ │ │ └── java │ │ │ └── com │ │ │ └── imooc │ │ │ └── GirlApplicationTests.java │ ├── .gitignore │ └── pom.xml └── SpringBoot(二)_项目属性配置.md ├── springboot(十三) ├── pdf │ └── pdf_demo │ │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ ├── application.yml │ │ │ │ └── simhei.ttf │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── kevin │ │ │ │ └── pdf_demo │ │ │ │ ├── PdfDemoApplication.java │ │ │ │ └── controller │ │ │ │ ├── B64ImgReplacedElementFactory.java │ │ │ │ └── PdfController.java │ │ └── test │ │ │ └── java │ │ │ └── com │ │ │ └── kevin │ │ │ └── pdf_demo │ │ │ └── PdfDemoApplicationTests.java │ │ ├── .gitignore │ │ └── pom.xml └── SpringBoot(十三)_springboot实现预览pdf.md ├── springboot(一) ├── girl │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ ├── application-dev.yml │ │ │ │ ├── application-prod.yml │ │ │ │ └── application.yml │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── imooc │ │ │ │ ├── GirlApplication.java │ │ │ │ └── controller │ │ │ │ └── HelloController.java │ │ └── test │ │ │ └── java │ │ │ └── com │ │ │ └── imooc │ │ │ └── GirlApplicationTests.java │ ├── .gitignore │ └── pom.xml └── SpringBoot(一)_快速实战搭建项目.md ├── springboot(七) ├── girl │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ ├── application-dev.yml │ │ │ │ ├── application-prod.yml │ │ │ │ ├── application.yml │ │ │ │ └── banner.txt │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── imooc │ │ │ │ ├── GirlApplication.java │ │ │ │ ├── repository │ │ │ │ └── GirlRepository.java │ │ │ │ ├── exception │ │ │ │ └── GirlException.java │ │ │ │ ├── enums │ │ │ │ └── ResultEnum.java │ │ │ │ ├── utils │ │ │ │ └── ResultUtil.java │ │ │ │ ├── controller │ │ │ │ ├── HelloController.java │ │ │ │ └── GirlController.java │ │ │ │ ├── properties │ │ │ │ └── GirlProperties.java │ │ │ │ ├── entity │ │ │ │ ├── Result.java │ │ │ │ └── Girl.java │ │ │ │ ├── service │ │ │ │ └── GirlService.java │ │ │ │ ├── handle │ │ │ │ └── ExceptionHandle.java │ │ │ │ └── aspect │ │ │ │ └── HttpAspect.java │ │ └── test │ │ │ └── java │ │ │ └── com │ │ │ └── imooc │ │ │ └── GirlApplicationTests.java │ ├── .gitignore │ └── pom.xml └── SpringBoot(七)_统一异常处理.md ├── springboot(四) ├── girl │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ ├── application-dev.yml │ │ │ │ ├── application-prod.yml │ │ │ │ ├── application.yml │ │ │ │ └── banner.txt │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── imooc │ │ │ │ ├── service │ │ │ │ └── GirlService.java │ │ │ │ ├── GirlApplication.java │ │ │ │ ├── repository │ │ │ │ └── GirlRepository.java │ │ │ │ ├── controller │ │ │ │ ├── HelloController.java │ │ │ │ └── GirlController.java │ │ │ │ ├── properties │ │ │ │ └── GirlProperties.java │ │ │ │ └── entity │ │ │ │ └── Girl.java │ │ └── test │ │ │ └── java │ │ │ └── com │ │ │ └── imooc │ │ │ └── GirlApplicationTests.java │ ├── .gitignore │ └── pom.xml └── SpringBoot(四)_SpringDataJPA的使用.md ├── springboot(十四) ├── report-srping-boot │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ ├── coffee.jpg │ │ │ │ ├── coffee_stain.png │ │ │ │ ├── jaspers │ │ │ │ │ └── demo.jasper │ │ │ │ ├── application.yml │ │ │ │ └── banner.txt │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── kevin │ │ │ │ ├── ReportSrpingBootApplication.java │ │ │ │ └── controller │ │ │ │ └── ReportController.java │ │ └── test │ │ │ └── java │ │ │ └── com │ │ │ └── kevin │ │ │ └── ReportSrpingBootApplicationTests.java │ ├── .gitignore │ ├── student.sql │ └── pom.xml └── springboot整合JasperReport6.6.0.md ├── springboot(九) ├── mybatis │ ├── src │ │ ├── main │ │ │ ├── java │ │ │ │ └── com │ │ │ │ │ └── zhb │ │ │ │ │ ├── enums │ │ │ │ │ └── UserSexEnum.java │ │ │ │ │ ├── mapper │ │ │ │ │ ├── AreaTreeMapper.java │ │ │ │ │ └── UserMapper.java │ │ │ │ │ ├── MybatisApplication.java │ │ │ │ │ ├── param │ │ │ │ │ └── UserParam.java │ │ │ │ │ ├── entity │ │ │ │ │ ├── AreaTreeEntity.java │ │ │ │ │ └── UserEntity.java │ │ │ │ │ ├── controller │ │ │ │ │ ├── UserController.java │ │ │ │ │ └── AreaTreeController.java │ │ │ │ │ └── Properties │ │ │ │ │ └── SwaggerProperties.java │ │ │ └── resources │ │ │ │ ├── application.yml │ │ │ │ └── mybatis │ │ │ │ ├── mybatis-config.xml │ │ │ │ └── mapper │ │ │ │ ├── AreaTreeMapper.xml │ │ │ │ └── UserMapper.xml │ │ └── test │ │ │ └── java │ │ │ └── com │ │ │ └── zhb │ │ │ └── MybatisApplicationTests.java │ ├── .gitignore │ └── pom.xml ├── sql语句 │ ├── area.sql │ └── users.sql └── SpringBoot(九)_springboot集成MyBatis.md ├── springboot(十二) ├── rabbit_mq │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ └── application.properties │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── zhb │ │ │ │ ├── direct │ │ │ │ ├── UserEntity.java │ │ │ │ ├── RabbitConfig.java │ │ │ │ ├── HelloReceiver.java │ │ │ │ └── HelloSender.java │ │ │ │ ├── RabbitMqApplication.java │ │ │ │ ├── fanout │ │ │ │ ├── FanoutReceiverA.java │ │ │ │ ├── FanoutReceiverB.java │ │ │ │ ├── FanoutReceiverC.java │ │ │ │ ├── FanoutSender.java │ │ │ │ └── FanoutRabbitConfig.java │ │ │ │ └── topic │ │ │ │ ├── Receiver1.java │ │ │ │ ├── Receiver2.java │ │ │ │ ├── Sender.java │ │ │ │ └── TopicRabbitConfig.java │ │ └── test │ │ │ └── java │ │ │ └── com │ │ │ └── zhb │ │ │ └── RabbitMqApplicationTests.java │ ├── .gitignore │ └── pom.xml └── SpringBoot(十二)_springboot整合RabbitMQ.md ├── .github └── workflows │ └── maven.yml ├── springboot(十) └── redis-demo-01 │ ├── .gitignore │ ├── src │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── zhb │ │ │ │ ├── RedisDemo01Application.java │ │ │ │ └── service │ │ │ │ └── RedisService.java │ │ └── resources │ │ │ └── application.yml │ └── test │ │ └── java │ │ └── com │ │ └── zhb │ │ ├── service │ │ └── RedisServiceTest.java │ │ └── RedisDemo01ApplicationTests.java │ └── pom.xml ├── springboot(五) └── SpringBoot(五)_表单验证.md ├── README.md ├── springboot(三) └── SpringBoot(三)_controller的使用.md ├── springboot(六) └── SpringBoot(六)_AOP统一处理请求.md └── springboot(八) └── SpringBoot(八)_springboot集成swagger2.md /springboot(十六)/es-job1/es-job/job_debug.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /springboot(十一)/import/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /springboot(二)/girl/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | profiles: 3 | active: dev -------------------------------------------------------------------------------- /springboot(十三)/pdf/pdf_demo/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | 2 | 3 | HTML: freemarker.ftl 4 | FONT: simhei.ttf 5 | -------------------------------------------------------------------------------- /springboot(一)/girl/src/main/resources/application-dev.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8080 3 | girl: 4 | cupSize: B 5 | age: 18 6 | -------------------------------------------------------------------------------- /springboot(一)/girl/src/main/resources/application-prod.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8081 3 | girl: 4 | cupSize: F 5 | age: 18 6 | -------------------------------------------------------------------------------- /springboot(七)/girl/src/main/resources/application-dev.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8099 3 | girl: 4 | name: maomao 5 | age: 18 6 | -------------------------------------------------------------------------------- /springboot(七)/girl/src/main/resources/application-prod.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8088 3 | girl: 4 | name: mengmeng 5 | age: 18 6 | -------------------------------------------------------------------------------- /springboot(二)/girl/src/main/resources/application-dev.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8099 3 | girl: 4 | name: maomao 5 | age: 18 6 | -------------------------------------------------------------------------------- /springboot(二)/girl/src/main/resources/application-prod.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8088 3 | girl: 4 | name: mengmeng 5 | age: 18 6 | -------------------------------------------------------------------------------- /springboot(四)/girl/src/main/resources/application-dev.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8099 3 | girl: 4 | name: maomao 5 | age: 18 6 | -------------------------------------------------------------------------------- /springboot(四)/girl/src/main/resources/application-prod.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8088 3 | girl: 4 | name: mengmeng 5 | age: 18 6 | -------------------------------------------------------------------------------- /springboot(十三)/pdf/pdf_demo/src/main/resources/simhei.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/runzhenghengbin/SpringBoot/HEAD/springboot(十三)/pdf/pdf_demo/src/main/resources/simhei.ttf -------------------------------------------------------------------------------- /springboot(十六)/es-job1/es-job/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/runzhenghengbin/SpringBoot/HEAD/springboot(十六)/es-job1/es-job/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /springboot(十四)/report-srping-boot/src/main/resources/coffee.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/runzhenghengbin/SpringBoot/HEAD/springboot(十四)/report-srping-boot/src/main/resources/coffee.jpg -------------------------------------------------------------------------------- /springboot(十六)/es-job1/es-job/src/main/resources/application.yml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/runzhenghengbin/SpringBoot/HEAD/springboot(十六)/es-job1/es-job/src/main/resources/application.yml -------------------------------------------------------------------------------- /springboot(十四)/report-srping-boot/src/main/resources/coffee_stain.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/runzhenghengbin/SpringBoot/HEAD/springboot(十四)/report-srping-boot/src/main/resources/coffee_stain.png -------------------------------------------------------------------------------- /springboot(十四)/report-srping-boot/src/main/resources/jaspers/demo.jasper: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/runzhenghengbin/SpringBoot/HEAD/springboot(十四)/report-srping-boot/src/main/resources/jaspers/demo.jasper -------------------------------------------------------------------------------- /springboot(四)/girl/src/main/java/com/imooc/service/GirlService.java: -------------------------------------------------------------------------------- 1 | package com.imooc.service; 2 | 3 | /** 4 | * @Auther: curry 5 | * @Date: 2018/5/28 23:03 6 | * @Description: 7 | */ 8 | public class GirlService { 9 | } 10 | -------------------------------------------------------------------------------- /springboot(九)/mybatis/src/main/java/com/zhb/enums/UserSexEnum.java: -------------------------------------------------------------------------------- 1 | package com.zhb.enums; 2 | 3 | /** 4 | * @Auther: curry 5 | * @Date: 2018/6/8 21:08 6 | * @Description: 7 | */ 8 | public enum UserSexEnum { 9 | MAN,WOMAN 10 | } 11 | -------------------------------------------------------------------------------- /springboot(十二)/rabbit_mq/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=spirng-boot-rabbitmq 2 | 3 | spring.rabbitmq.host= localhost 4 | spring.rabbitmq.port= 5672 5 | spring.rabbitmq.username=allen 6 | spring.rabbitmq.password=123456 7 | 8 | server.port=8080 9 | -------------------------------------------------------------------------------- /springboot(十六)/es-job1/es-job/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip 2 | wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar 3 | -------------------------------------------------------------------------------- /springboot(七)/girl/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | profiles: 3 | active: dev 4 | datasource: 5 | driver-class-name: com.mysql.jdbc.Driver 6 | url: jdbc:mysql://127.0.0.1:3306/dbgirl?characterEncoding=utf8&useSSL=false 7 | username: root 8 | password: root 9 | jpa: 10 | hibernate: 11 | ddl-auto: update 12 | show-sql: true -------------------------------------------------------------------------------- /springboot(九)/mybatis/src/main/java/com/zhb/mapper/AreaTreeMapper.java: -------------------------------------------------------------------------------- 1 | package com.zhb.mapper; 2 | 3 | import com.zhb.entity.AreaTreeEntity; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @Auther: curry 9 | * @Date: 2018/6/8 22:37 10 | * @Description: 11 | */ 12 | public interface AreaTreeMapper { 13 | 14 | List getAreaTree(); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /springboot(四)/girl/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | profiles: 3 | active: dev 4 | datasource: 5 | driver-class-name: com.mysql.jdbc.Driver 6 | url: jdbc:mysql://127.0.0.1:3306/dbgirl?characterEncoding=utf8&useSSL=false 7 | username: root 8 | password: root 9 | jpa: 10 | hibernate: 11 | ddl-auto: update 12 | show-sql: true -------------------------------------------------------------------------------- /.github/workflows/maven.yml: -------------------------------------------------------------------------------- 1 | name: Java CI 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: ubuntu-latest 9 | 10 | steps: 11 | - uses: actions/checkout@v1 12 | - name: Set up JDK 1.8 13 | uses: actions/setup-java@v1 14 | with: 15 | java-version: 1.8 16 | - name: Build with Maven 17 | run: mvn package --file pom.xml 18 | -------------------------------------------------------------------------------- /springboot(一)/girl/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | profiles: 3 | active: dev 4 | datasource: 5 | driver-class-name: com.mysql.jdbc.Driver 6 | url: jdbc:mysql://127.0.0.1:3306/dbgirl?characterEncoding=utf8&useSSL=false 7 | username: root 8 | password: root 9 | jpa: 10 | hibernate: 11 | ddl-auto: create 12 | show-sql: true 13 | -------------------------------------------------------------------------------- /springboot(十二)/rabbit_mq/src/main/java/com/zhb/direct/UserEntity.java: -------------------------------------------------------------------------------- 1 | package com.zhb.direct; 2 | 3 | import lombok.Data; 4 | 5 | import java.io.Serializable; 6 | 7 | /** 8 | * @author: curry 9 | * @Date: 2018/9/5 10 | */ 11 | @Data 12 | public class UserEntity implements Serializable { 13 | 14 | private String name; 15 | 16 | private String address; 17 | } 18 | -------------------------------------------------------------------------------- /springboot(十一)/import/src/main/resources/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 上传文件 6 | 7 | 8 |
9 | 10 | 11 |
12 | 13 | -------------------------------------------------------------------------------- /springboot(一)/girl/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ -------------------------------------------------------------------------------- /springboot(七)/girl/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ -------------------------------------------------------------------------------- /springboot(二)/girl/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ -------------------------------------------------------------------------------- /springboot(四)/girl/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ -------------------------------------------------------------------------------- /springboot(九)/mybatis/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ -------------------------------------------------------------------------------- /springboot(十一)/import/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ -------------------------------------------------------------------------------- /springboot(十)/redis-demo-01/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ -------------------------------------------------------------------------------- /springboot(十三)/pdf/pdf_demo/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ -------------------------------------------------------------------------------- /springboot(十二)/rabbit_mq/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ -------------------------------------------------------------------------------- /springboot(一)/girl/src/main/java/com/imooc/GirlApplication.java: -------------------------------------------------------------------------------- 1 | package com.imooc; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class GirlApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(GirlApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /springboot(七)/girl/src/main/java/com/imooc/GirlApplication.java: -------------------------------------------------------------------------------- 1 | package com.imooc; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class GirlApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(GirlApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /springboot(二)/girl/src/main/java/com/imooc/GirlApplication.java: -------------------------------------------------------------------------------- 1 | package com.imooc; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class GirlApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(GirlApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /springboot(十四)/report-srping-boot/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ -------------------------------------------------------------------------------- /springboot(四)/girl/src/main/java/com/imooc/GirlApplication.java: -------------------------------------------------------------------------------- 1 | package com.imooc; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class GirlApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(GirlApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /springboot(十一)/import/src/main/java/com/zhb/ImportApplication.java: -------------------------------------------------------------------------------- 1 | package com.zhb; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class ImportApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(ImportApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /springboot(十二)/rabbit_mq/src/main/java/com/zhb/RabbitMqApplication.java: -------------------------------------------------------------------------------- 1 | package com.zhb; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class RabbitMqApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(RabbitMqApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /springboot(九)/mybatis/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | mybatis: 2 | config-location: classpath:mybatis/mybatis-config.xml 3 | mapper-locations: classpath:mybatis/mapper/*.xml 4 | type-aliases-package: com.zhb.entity 5 | spring: 6 | datasource: 7 | driver-class-name: com.mysql.jdbc.Driver 8 | url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8 9 | password: root 10 | username: root 11 | 12 | -------------------------------------------------------------------------------- /springboot(十)/redis-demo-01/src/main/java/com/zhb/RedisDemo01Application.java: -------------------------------------------------------------------------------- 1 | package com.zhb; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class RedisDemo01Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(RedisDemo01Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /springboot(十三)/pdf/pdf_demo/src/main/java/com/kevin/pdf_demo/PdfDemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.kevin.pdf_demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class PdfDemoApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(PdfDemoApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /springboot(七)/girl/src/main/java/com/imooc/repository/GirlRepository.java: -------------------------------------------------------------------------------- 1 | package com.imooc.repository; 2 | 3 | import com.imooc.entity.Girl; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * @Auther: curry 10 | * @Date: 2018/5/28 21:59 11 | * @Description: 12 | */ 13 | public interface GirlRepository extends JpaRepository { 14 | public List findByAge(Integer age); 15 | } 16 | -------------------------------------------------------------------------------- /springboot(九)/mybatis/src/test/java/com/zhb/MybatisApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.zhb; 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 MybatisApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /springboot(十一)/import/src/test/java/com/zhb/ImportApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.zhb; 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 ImportApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /springboot(四)/girl/src/main/java/com/imooc/repository/GirlRepository.java: -------------------------------------------------------------------------------- 1 | package com.imooc.repository; 2 | 3 | import com.imooc.entity.Girl; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * @Auther: curry 10 | * @Date: 2018/5/28 21:59 11 | * @Description: 12 | */ 13 | public interface GirlRepository extends JpaRepository { 14 | public List findByAge(Integer age); 15 | } 16 | -------------------------------------------------------------------------------- /springboot(七)/girl/src/test/java/com/imooc/GirlApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.imooc; 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 GirlApplicationTests { 11 | 12 | 13 | @Test 14 | public void contextLoads(){ 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /springboot(二)/girl/src/test/java/com/imooc/GirlApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.imooc; 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 GirlApplicationTests { 11 | 12 | 13 | @Test 14 | public void contextLoads(){ 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /springboot(十二)/rabbit_mq/src/test/java/com/zhb/RabbitMqApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.zhb; 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 RabbitMqApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | 17 | } 18 | -------------------------------------------------------------------------------- /springboot(十四)/report-srping-boot/src/main/java/com/kevin/ReportSrpingBootApplication.java: -------------------------------------------------------------------------------- 1 | package com.kevin; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class ReportSrpingBootApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(ReportSrpingBootApplication.class, args); 11 | } 12 | 13 | } 14 | 15 | -------------------------------------------------------------------------------- /springboot(四)/girl/src/test/java/com/imooc/GirlApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.imooc; 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 GirlApplicationTests { 11 | 12 | 13 | @Test 14 | public void contextLoads(){ 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /springboot(十)/redis-demo-01/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | redis: 3 | # Redis 服务器地址 4 | host: 192.168.142.128 5 | # Redis 服务器连接端口 6 | port: 6379 7 | # Redis 服务器连接密码(默认为空) 8 | password: 9 | # Redis 数据库索引(默认为0) 10 | database: 1 11 | jedis: 12 | pool: 13 | # 连接池最大连接数(使用负值表示没有限制) 14 | max-active: 8 15 | # 连接池最大阻塞等待时间(使用负值表示没有限制) 16 | max-wait: -1 17 | # 连接池中的最大空闲连接 18 | max-idle: 8 19 | -------------------------------------------------------------------------------- /springboot(十四)/report-srping-boot/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | # Server settings 2 | server: 3 | port: 8080 4 | 5 | # SPRING PROFILES 6 | spring: 7 | http: 8 | encoding.charset: UTF-8 9 | encoding.enable: true 10 | encoding.force: true 11 | datasource: 12 | url: jdbc:mysql://127.0.0.1:3306/kevin?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8 13 | username: root 14 | password: 123456 15 | driver-class-name: com.mysql.jdbc.Driver 16 | 17 | -------------------------------------------------------------------------------- /springboot(十二)/rabbit_mq/src/main/java/com/zhb/direct/RabbitConfig.java: -------------------------------------------------------------------------------- 1 | package com.zhb.direct; 2 | 3 | import org.springframework.amqp.core.Queue; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | 7 | 8 | /** 9 | * @author: curry 10 | * @Date: 2018/8/27 11 | */ 12 | @Configuration 13 | public class RabbitConfig { 14 | 15 | @Bean 16 | public Queue Queue() { 17 | return new Queue("hello"); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /springboot(九)/mybatis/src/main/java/com/zhb/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.zhb.mapper; 2 | 3 | import com.zhb.entity.UserEntity; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @Auther: curry 9 | * @Date: 2018/6/8 21:10 10 | * @Description: 11 | */ 12 | public interface UserMapper { 13 | 14 | List getAll(); 15 | 16 | UserEntity getOne(Long id); 17 | 18 | void insert(UserEntity user); 19 | 20 | void update(UserEntity user); 21 | 22 | void delete(Long id); 23 | } 24 | -------------------------------------------------------------------------------- /springboot(十四)/report-srping-boot/src/test/java/com/kevin/ReportSrpingBootApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.kevin; 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 ReportSrpingBootApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | 18 | -------------------------------------------------------------------------------- /springboot(九)/mybatis/src/main/java/com/zhb/MybatisApplication.java: -------------------------------------------------------------------------------- 1 | package com.zhb; 2 | 3 | import org.mybatis.spring.annotation.MapperScan; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | 7 | @SpringBootApplication 8 | @MapperScan("com.zhb.mapper") 9 | public class MybatisApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(MybatisApplication.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /springboot(十六)/es-job1/es-job/.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 | -------------------------------------------------------------------------------- /springboot(十六)/es-job1/es-job/src/test/java/com/kevin/task/EsJobApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.kevin.task; 2 | 3 | 4 | import org.junit.Test; 5 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 6 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | 9 | @SpringBootTest 10 | @EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class) 11 | class EsJobApplicationTests { 12 | 13 | @Test 14 | void contextLoads() { 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /springboot(十六)/es-job1/es-job/src/main/java/com/kevin/task/task/MySimpleJob.java: -------------------------------------------------------------------------------- 1 | package com.kevin.task.task; 2 | 3 | import com.dangdang.ddframe.job.api.ShardingContext; 4 | import com.dangdang.ddframe.job.api.simple.SimpleJob; 5 | import org.springframework.stereotype.Component; 6 | 7 | /** 8 | * @author: kevin 9 | * @Date: 2020/1/22 10 | */ 11 | 12 | @Component 13 | public class MySimpleJob implements SimpleJob { 14 | @Override 15 | public void execute(ShardingContext shardingContext) { 16 | System.out.println(shardingContext.getShardingParameter()); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /springboot(十六)/es-job1/es-job/src/main/java/com/kevin/task/EsJobApplication.java: -------------------------------------------------------------------------------- 1 | package com.kevin.task; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 7 | 8 | @SpringBootApplication 9 | public class EsJobApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(EsJobApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /springboot(一)/girl/src/main/java/com/imooc/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.imooc.controller; 2 | 3 | import org.springframework.web.bind.annotation.RequestMapping; 4 | import org.springframework.web.bind.annotation.RequestMethod; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | 8 | 9 | /** 10 | * @Auther: curry 11 | * @Date: 2018/5/24 06:42 12 | * @Description: 13 | */ 14 | @RestController 15 | public class HelloController { 16 | @RequestMapping(value = {"/hello"},method = RequestMethod.GET) 17 | public String say(){ 18 | return "Hello Spring Boot!"; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /springboot(十二)/rabbit_mq/src/main/java/com/zhb/fanout/FanoutReceiverA.java: -------------------------------------------------------------------------------- 1 | package com.zhb.fanout; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.amqp.rabbit.annotation.RabbitHandler; 5 | import org.springframework.amqp.rabbit.annotation.RabbitListener; 6 | import org.springframework.stereotype.Component; 7 | 8 | /** 9 | * @author: curry 10 | * @Date: 2018/9/6 11 | */ 12 | @Slf4j 13 | @Component 14 | @RabbitListener(queues = "fanout.A") 15 | public class FanoutReceiverA { 16 | 17 | @RabbitHandler 18 | public void process(String message) { 19 | log.info("fanout A:{}", message); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /springboot(十二)/rabbit_mq/src/main/java/com/zhb/fanout/FanoutReceiverB.java: -------------------------------------------------------------------------------- 1 | package com.zhb.fanout; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.amqp.rabbit.annotation.RabbitHandler; 5 | import org.springframework.amqp.rabbit.annotation.RabbitListener; 6 | import org.springframework.stereotype.Component; 7 | 8 | /** 9 | * @author: curry 10 | * @Date: 2018/9/6 11 | */ 12 | @Slf4j 13 | @Component 14 | @RabbitListener(queues = "fanout.B") 15 | public class FanoutReceiverB { 16 | 17 | @RabbitHandler 18 | public void process(String message) { 19 | log.info("fanout B:{}", message); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /springboot(十二)/rabbit_mq/src/main/java/com/zhb/fanout/FanoutReceiverC.java: -------------------------------------------------------------------------------- 1 | package com.zhb.fanout; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.amqp.rabbit.annotation.RabbitHandler; 5 | import org.springframework.amqp.rabbit.annotation.RabbitListener; 6 | import org.springframework.stereotype.Component; 7 | 8 | /** 9 | * @author: curry 10 | * @Date: 2018/9/6 11 | */ 12 | @Slf4j 13 | @Component 14 | @RabbitListener(queues = "fanout.C") 15 | public class FanoutReceiverC { 16 | 17 | @RabbitHandler 18 | public void process(String message) { 19 | log.info("fanout C:{}", message); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /springboot(十二)/rabbit_mq/src/main/java/com/zhb/topic/Receiver1.java: -------------------------------------------------------------------------------- 1 | package com.zhb.topic; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.amqp.rabbit.annotation.RabbitHandler; 5 | import org.springframework.amqp.rabbit.annotation.RabbitListener; 6 | import org.springframework.stereotype.Component; 7 | 8 | /** 9 | * @author: curry 10 | * @Date: 2018/9/5 11 | */ 12 | @Slf4j 13 | @Component 14 | @RabbitListener(queues = "topic.message") 15 | public class Receiver1 { 16 | 17 | @RabbitHandler 18 | public void process(String message) { 19 | log.info("topic Receive1:{}", message); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /springboot(十二)/rabbit_mq/src/main/java/com/zhb/topic/Receiver2.java: -------------------------------------------------------------------------------- 1 | package com.zhb.topic; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.amqp.rabbit.annotation.RabbitHandler; 5 | import org.springframework.amqp.rabbit.annotation.RabbitListener; 6 | import org.springframework.stereotype.Component; 7 | 8 | /** 9 | * @author: curry 10 | * @Date: 2018/9/5 11 | */ 12 | @Slf4j 13 | @Component 14 | @RabbitListener(queues = "topic.messages") 15 | public class Receiver2 { 16 | 17 | @RabbitHandler 18 | public void process(String message) { 19 | log.info("topic Receive2:{}", message); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /springboot(七)/girl/src/main/java/com/imooc/exception/GirlException.java: -------------------------------------------------------------------------------- 1 | package com.imooc.exception; 2 | 3 | import com.imooc.enums.ResultEnum; 4 | 5 | /** 6 | * @Auther: curry 7 | * @Date: 2018/6/2 15:30 8 | * @Description: 9 | */ 10 | public class GirlException extends RuntimeException{ 11 | 12 | private Integer code; 13 | 14 | public GirlException(ResultEnum resultEnum) { 15 | super(resultEnum.getMsg()); 16 | this.code = resultEnum.getCode(); 17 | } 18 | 19 | public Integer getCode() { 20 | return code; 21 | } 22 | 23 | public void setCode(Integer code) { 24 | this.code = code; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /springboot(九)/mybatis/src/main/java/com/zhb/param/UserParam.java: -------------------------------------------------------------------------------- 1 | package com.zhb.param; 2 | 3 | /** 4 | * @Auther: curry 5 | * @Date: 2018/6/8 21:31 6 | * @Description: 7 | */ 8 | public class UserParam { 9 | 10 | 11 | private String userName; 12 | 13 | private String userSex; 14 | 15 | public String getUserName() { 16 | return userName; 17 | } 18 | 19 | public void setUserName(String userName) { 20 | this.userName = userName; 21 | } 22 | 23 | public String getUserSex() { 24 | return userSex; 25 | } 26 | 27 | public void setUserSex(String userSex) { 28 | this.userSex = userSex; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /springboot(七)/girl/src/main/java/com/imooc/enums/ResultEnum.java: -------------------------------------------------------------------------------- 1 | package com.imooc.enums; 2 | 3 | /** 4 | * @Auther: curry 5 | * @Date: 2018/6/2 15:46 6 | * @Description: 7 | */ 8 | public enum ResultEnum { 9 | UNKNOW_ERROR(-1,"未知错误"), 10 | SUCCESS(0,"成功"), 11 | PRIMARY_SCHOOL(100,"还在小学"), 12 | MIDDLE_SCHOOL(101,"还在初中"), 13 | 14 | ; 15 | 16 | 17 | private Integer code; 18 | 19 | private String msg; 20 | 21 | ResultEnum(Integer code, String msg) { 22 | this.code = code; 23 | this.msg = msg; 24 | } 25 | 26 | public Integer getCode() { 27 | return code; 28 | } 29 | 30 | 31 | 32 | public String getMsg() { 33 | return msg; 34 | } 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /springboot(十二)/rabbit_mq/src/main/java/com/zhb/fanout/FanoutSender.java: -------------------------------------------------------------------------------- 1 | package com.zhb.fanout; 2 | 3 | import org.springframework.amqp.core.AmqpTemplate; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | import javax.annotation.Resource; 8 | 9 | /** 10 | * @author: curry 11 | * @Date: 2018/9/6 12 | */ 13 | @RestController 14 | public class FanoutSender { 15 | 16 | @Resource 17 | private AmqpTemplate rabbitTemplate; 18 | 19 | @GetMapping(value = "fanoutSend") 20 | public void send(){ 21 | String context = "hi, fanout msg"; 22 | rabbitTemplate.convertAndSend("fanoutExchange","",context); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /springboot(十六)/es-job1/es-job/src/main/java/com/kevin/task/entity/Foo.java: -------------------------------------------------------------------------------- 1 | package com.kevin.task.entity; 2 | 3 | /** 4 | * @author: kevin 5 | * @Date: 2020/1/30 6 | */ 7 | public class Foo { 8 | 9 | private String id; 10 | private String name; 11 | 12 | public Foo() { 13 | } 14 | public Foo(String id, String name) { 15 | super(); 16 | this.id = id; 17 | this.name = name; 18 | } 19 | public String getId() { 20 | return id; 21 | } 22 | public void setId(String id) { 23 | this.id = id; 24 | } 25 | public String getName() { 26 | return name; 27 | } 28 | public void setName(String name) { 29 | this.name = name; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /springboot(十二)/rabbit_mq/src/main/java/com/zhb/direct/HelloReceiver.java: -------------------------------------------------------------------------------- 1 | package com.zhb.direct; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.amqp.rabbit.annotation.RabbitHandler; 5 | import org.springframework.amqp.rabbit.annotation.RabbitListener; 6 | import org.springframework.stereotype.Component; 7 | 8 | /** 9 | * @author: curry 10 | * @Date: 2018/8/27 11 | */ 12 | @Component 13 | @RabbitListener(queues = "hello") 14 | @Slf4j 15 | public class HelloReceiver { 16 | 17 | @RabbitHandler 18 | public void process(String hello) { 19 | log.info("消息接受为:{}",hello); 20 | } 21 | 22 | @RabbitHandler 23 | public void process(UserEntity userEntity) { 24 | log.info("消息接受为:{}",userEntity); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /springboot(十六)/es-job1/es-job/src/main/java/com/kevin/task/listener/SimpleJobListener.java: -------------------------------------------------------------------------------- 1 | package com.kevin.task.listener; 2 | 3 | import com.dangdang.ddframe.job.executor.ShardingContexts; 4 | import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener; 5 | 6 | 7 | /** 8 | * @author: kevin 9 | * @Date: 2020/1/22 10 | */ 11 | 12 | public class SimpleJobListener implements ElasticJobListener { 13 | @Override 14 | public void beforeJobExecuted(ShardingContexts shardingContexts) { 15 | // log.info("-----------------执行任务之前:{}", shardingContexts); 16 | } 17 | 18 | @Override 19 | public void afterJobExecuted(ShardingContexts shardingContexts) { 20 | //log.info("-----------------执行任务之前:{}", shardingContexts); 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /springboot(七)/girl/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | _ooOoo_ 2 | o8888888o 3 | 88" . "88 4 | (| -_- |) 5 | O\ = /O 6 | ____/`---'\____ 7 | .' \\| |// `. 8 | / \\||| : |||// \ 9 | / _||||| -:- |||||- \ 10 | | | \\\ - /// | | 11 | | \_| ''\---/'' | | 12 | \ .-\__ `-` ___/-. / 13 | ___`. .' /--.--\ `. . __ 14 | ."" '< `.___\_<|>_/___.' >'"". 15 | | | : `- \`.;`\ _ /`;.`/ - ` : | | 16 | \ \ `-. \_ __\ /__ _/ .-` / / 17 | ======`-.____`-.___\_____/___.-`____.-'====== 18 | `=---=' 19 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 20 | 佛祖保佑 永无BUG -------------------------------------------------------------------------------- /springboot(十三)/pdf/pdf_demo/src/test/java/com/kevin/pdf_demo/PdfDemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.kevin.pdf_demo; 2 | 3 | import com.itextpdf.text.Document; 4 | import com.itextpdf.text.DocumentException; 5 | import com.itextpdf.text.Font; 6 | import com.itextpdf.text.Paragraph; 7 | import com.itextpdf.text.pdf.BaseFont; 8 | import com.itextpdf.text.pdf.PdfWriter; 9 | import org.junit.Test; 10 | import org.junit.runner.RunWith; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.test.context.junit4.SpringRunner; 13 | 14 | import java.io.*; 15 | 16 | @RunWith(SpringRunner.class) 17 | @SpringBootTest 18 | public class PdfDemoApplicationTests { 19 | 20 | @Test 21 | public void contextLoads() { 22 | 23 | } 24 | 25 | 26 | 27 | } 28 | -------------------------------------------------------------------------------- /springboot(四)/girl/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | _ooOoo_ 2 | o8888888o 3 | 88" . "88 4 | (| -_- |) 5 | O\ = /O 6 | ____/`---'\____ 7 | .' \\| |// `. 8 | / \\||| : |||// \ 9 | / _||||| -:- |||||- \ 10 | | | \\\ - /// | | 11 | | \_| ''\---/'' | | 12 | \ .-\__ `-` ___/-. / 13 | ___`. .' /--.--\ `. . __ 14 | ."" '< `.___\_<|>_/___.' >'"". 15 | | | : `- \`.;`\ _ /`;.`/ - ` : | | 16 | \ \ `-. \_ __\ /__ _/ .-` / / 17 | ======`-.____`-.___\_____/___.-`____.-'====== 18 | `=---=' 19 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 20 | 佛祖保佑 永无BUG -------------------------------------------------------------------------------- /springboot(七)/girl/src/main/java/com/imooc/utils/ResultUtil.java: -------------------------------------------------------------------------------- 1 | package com.imooc.utils; 2 | 3 | import com.imooc.entity.Result; 4 | 5 | /** 6 | * @Auther: curry 7 | * @Date: 2018/6/2 14:39 8 | * @Description: 9 | */ 10 | public class ResultUtil { 11 | 12 | public static Result success(Object object){ 13 | Result result = new Result(); 14 | result.setCode(0); 15 | result.setMsg("成功"); 16 | result.setData(object); 17 | return result; 18 | } 19 | 20 | public static Result success(){ 21 | return success(null); 22 | } 23 | public static Result error(Integer code,String msg){ 24 | Result result = new Result(); 25 | result.setCode(code); 26 | result.setMsg(msg); 27 | return result; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /springboot(十一)/import/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | _ooOoo_ 2 | o8888888o 3 | 88" . "88 4 | (| -_- |) 5 | O\ = /O 6 | ____/`---'\____ 7 | .' \\| |// `. 8 | / \\||| : |||// \ 9 | / _||||| -:- |||||- \ 10 | | | \\\ - /// | | 11 | | \_| ''\---/'' | | 12 | \ .-\__ `-` ___/-. / 13 | ___`. .' /--.--\ `. . __ 14 | ."" '< `.___\_<|>_/___.' >'"". 15 | | | : `- \`.;`\ _ /`;.`/ - ` : | | 16 | \ \ `-. \_ __\ /__ _/ .-` / / 17 | ======`-.____`-.___\_____/___.-`____.-'====== 18 | `=---=' 19 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 20 | 佛祖保佑 永无BUG -------------------------------------------------------------------------------- /springboot(十四)/report-srping-boot/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | _ooOoo_ 2 | o8888888o 3 | 88" . "88 4 | (| -_- |) 5 | O\ = /O 6 | ____/`---'\____ 7 | .' \\| |// `. 8 | / \\||| : |||// \ 9 | / _||||| -:- |||||- \ 10 | | | \\\ - /// | | 11 | | \_| ''\---/'' | | 12 | \ .-\__ `-` ___/-. / 13 | ___`. .' /--.--\ `. . __ 14 | ."" '< `.___\_<|>_/___.' >'"". 15 | | | : `- \`.;`\ _ /`;.`/ - ` : | | 16 | \ \ `-. \_ __\ /__ _/ .-` / / 17 | ======`-.____`-.___\_____/___.-`____.-'====== 18 | `=---=' 19 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 20 | 佛祖保佑 永无BUG -------------------------------------------------------------------------------- /springboot(七)/girl/src/main/java/com/imooc/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.imooc.controller; 2 | 3 | import com.imooc.properties.GirlProperties; 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.web.bind.annotation.*; 6 | 7 | import javax.annotation.Resource; 8 | 9 | 10 | /** 11 | * @Auther: curry 12 | * @Date: 2018/5/24 06:42 13 | * @Description: 14 | */ 15 | @RestController 16 | @RequestMapping("/hello") 17 | public class HelloController { 18 | 19 | @Resource 20 | private GirlProperties girlProperties; 21 | // @RequestMapping(value = "/say",method = RequestMethod.GET) 22 | @GetMapping("/say") 23 | public String say(@RequestParam(value = "id",required = false,defaultValue = "0") Integer id){ 24 | return "id :"+id; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /springboot(四)/girl/src/main/java/com/imooc/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.imooc.controller; 2 | 3 | import com.imooc.properties.GirlProperties; 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.web.bind.annotation.*; 6 | 7 | import javax.annotation.Resource; 8 | 9 | 10 | /** 11 | * @Auther: curry 12 | * @Date: 2018/5/24 06:42 13 | * @Description: 14 | */ 15 | @RestController 16 | @RequestMapping("/hello") 17 | public class HelloController { 18 | 19 | @Resource 20 | private GirlProperties girlProperties; 21 | // @RequestMapping(value = "/say",method = RequestMethod.GET) 22 | @GetMapping("/say") 23 | public String say(@RequestParam(value = "id",required = false,defaultValue = "0") Integer id){ 24 | return "id :"+id; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /springboot(七)/girl/src/main/java/com/imooc/properties/GirlProperties.java: -------------------------------------------------------------------------------- 1 | package com.imooc.properties; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | import org.springframework.stereotype.Component; 5 | 6 | /** 7 | * @Auther: curry 8 | * @Date: 2018/5/27 15:29 9 | * @Description: 10 | */ 11 | 12 | @Component 13 | @ConfigurationProperties(prefix = "girl") 14 | public class GirlProperties { 15 | 16 | private String name; 17 | 18 | private int age; 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public void setName(String name) { 25 | this.name = name; 26 | } 27 | 28 | public int getAge() { 29 | return age; 30 | } 31 | 32 | public void setAge(int age) { 33 | this.age = age; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /springboot(二)/girl/src/main/java/com/imooc/properties/GirlProperties.java: -------------------------------------------------------------------------------- 1 | package com.imooc.properties; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | import org.springframework.stereotype.Component; 5 | 6 | /** 7 | * @Auther: curry 8 | * @Date: 2018/5/27 15:29 9 | * @Description: 10 | */ 11 | 12 | @Component 13 | @ConfigurationProperties(prefix = "girl") 14 | public class GirlProperties { 15 | 16 | private String name; 17 | 18 | private int age; 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public void setName(String name) { 25 | this.name = name; 26 | } 27 | 28 | public int getAge() { 29 | return age; 30 | } 31 | 32 | public void setAge(int age) { 33 | this.age = age; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /springboot(四)/girl/src/main/java/com/imooc/properties/GirlProperties.java: -------------------------------------------------------------------------------- 1 | package com.imooc.properties; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | import org.springframework.stereotype.Component; 5 | 6 | /** 7 | * @Auther: curry 8 | * @Date: 2018/5/27 15:29 9 | * @Description: 10 | */ 11 | 12 | @Component 13 | @ConfigurationProperties(prefix = "girl") 14 | public class GirlProperties { 15 | 16 | private String name; 17 | 18 | private int age; 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public void setName(String name) { 25 | this.name = name; 26 | } 27 | 28 | public int getAge() { 29 | return age; 30 | } 31 | 32 | public void setAge(int age) { 33 | this.age = age; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /springboot(十六)/es-job1/es-job/src/main/java/com/kevin/task/config/JobEventConfig.java: -------------------------------------------------------------------------------- 1 | package com.kevin.task.config; 2 | 3 | import com.dangdang.ddframe.job.event.JobEventConfiguration; 4 | import com.dangdang.ddframe.job.event.rdb.JobEventRdbConfiguration; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | 9 | import javax.sql.DataSource; 10 | 11 | /** 12 | * @author: kevin 13 | * @Date: 2020/1/22 14 | */ 15 | @Configuration 16 | public class JobEventConfig { 17 | 18 | @Autowired 19 | private DataSource dataSource; 20 | 21 | @Bean 22 | public JobEventConfiguration jobEventConfiguration() { 23 | return new JobEventRdbConfiguration(dataSource); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /springboot(九)/mybatis/src/main/resources/mybatis/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /springboot(二)/girl/src/main/java/com/imooc/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.imooc.controller; 2 | 3 | import com.imooc.properties.GirlProperties; 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RequestMethod; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import javax.annotation.Resource; 10 | 11 | 12 | /** 13 | * @Auther: curry 14 | * @Date: 2018/5/24 06:42 15 | * @Description: 16 | */ 17 | @RestController 18 | public class HelloController { 19 | 20 | @Resource 21 | private GirlProperties girlProperties; 22 | @RequestMapping(value = {"/hello"},method = RequestMethod.GET) 23 | public String say(){ 24 | return girlProperties.getName(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /springboot(十)/redis-demo-01/src/test/java/com/zhb/service/RedisServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.zhb.service; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | import org.springframework.test.context.junit4.SpringRunner; 9 | 10 | import javax.annotation.Resource; 11 | 12 | 13 | /** 14 | * @Auther: curry 15 | * @Date: 2018/6/20 11:02 16 | * @Description: 17 | */ 18 | @RunWith(SpringRunner.class) 19 | @SpringBootTest 20 | public class RedisServiceTest { 21 | @Resource 22 | private RedisService redisService; 23 | 24 | @Test 25 | public void testString() throws Exception { 26 | redisService.set("maomao", "hello"); 27 | System.out.println(redisService.get("maomao")); 28 | } 29 | 30 | 31 | 32 | } -------------------------------------------------------------------------------- /springboot(十二)/rabbit_mq/src/main/java/com/zhb/topic/Sender.java: -------------------------------------------------------------------------------- 1 | package com.zhb.topic; 2 | 3 | import org.springframework.amqp.core.AmqpTemplate; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | import javax.annotation.Resource; 8 | 9 | /** 10 | * @author: curry 11 | * @Date: 2018/9/5 12 | */ 13 | @RestController 14 | public class Sender { 15 | @Resource 16 | private AmqpTemplate rabbitTemplate; 17 | 18 | @GetMapping(value = "send1") 19 | public void send1(){ 20 | String context = "hi, i am message1"; 21 | rabbitTemplate.convertAndSend("exchange","topic.message",context); 22 | } 23 | 24 | @GetMapping(value = "send2") 25 | public void send2(){ 26 | String context = "hi, i am message2"; 27 | rabbitTemplate.convertAndSend("exchange","topic.messages",context); 28 | } 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /springboot(七)/girl/src/main/java/com/imooc/entity/Result.java: -------------------------------------------------------------------------------- 1 | package com.imooc.entity; 2 | 3 | /** 4 | * http请求做外层对象 5 | * @Auther: curry 6 | * @Date: 2018/6/2 14:35 7 | * @Description: 8 | */ 9 | public class Result { 10 | 11 | /** 12 | * 状态码 13 | */ 14 | private Integer code; 15 | 16 | /** 17 | * 提示信息 18 | */ 19 | private String msg; 20 | 21 | /** 22 | * 返回数据 23 | */ 24 | private T data; 25 | 26 | public Integer getCode() { 27 | return code; 28 | } 29 | 30 | public void setCode(Integer code) { 31 | this.code = code; 32 | } 33 | 34 | public String getMsg() { 35 | return msg; 36 | } 37 | 38 | public void setMsg(String msg) { 39 | this.msg = msg; 40 | } 41 | 42 | public T getData() { 43 | return data; 44 | } 45 | 46 | public void setData(T data) { 47 | this.data = data; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /springboot(七)/girl/src/main/java/com/imooc/service/GirlService.java: -------------------------------------------------------------------------------- 1 | package com.imooc.service; 2 | 3 | import com.imooc.entity.Girl; 4 | import com.imooc.enums.ResultEnum; 5 | import com.imooc.exception.GirlException; 6 | import com.imooc.repository.GirlRepository; 7 | import org.springframework.stereotype.Service; 8 | 9 | import javax.annotation.Resource; 10 | 11 | /** 12 | * @Auther: curry 13 | * @Date: 2018/5/28 23:03 14 | * @Description: 15 | */ 16 | @Service 17 | public class GirlService { 18 | @Resource 19 | private GirlRepository girlRepository; 20 | public void getAge(Integer id) throws Exception { 21 | Girl girl = girlRepository.getOne(id); 22 | Integer age = girl.getAge(); 23 | if(age<10){ 24 | throw new GirlException(ResultEnum.PRIMARY_SCHOOL); 25 | }else if(age >10 && age< 16){ 26 | throw new GirlException(ResultEnum.MIDDLE_SCHOOL); 27 | } 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /springboot(四)/girl/src/main/java/com/imooc/entity/Girl.java: -------------------------------------------------------------------------------- 1 | package com.imooc.entity; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.GeneratedValue; 5 | import javax.persistence.Id; 6 | 7 | /** 8 | * @Auther: curry 9 | * @Date: 2018/5/27 22:18 10 | * @Description: 11 | */ 12 | 13 | @Entity 14 | public class Girl { 15 | 16 | 17 | @Id 18 | @GeneratedValue 19 | private int id; 20 | 21 | private String name; 22 | 23 | private int age; 24 | 25 | public Girl() { 26 | } 27 | 28 | public int getId() { 29 | return id; 30 | } 31 | 32 | public void setId(int id) { 33 | this.id = id; 34 | } 35 | 36 | public String getName() { 37 | return name; 38 | } 39 | 40 | public void setName(String name) { 41 | this.name = name; 42 | } 43 | 44 | public int getAge() { 45 | return age; 46 | } 47 | 48 | public void setAge(int age) { 49 | this.age = age; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /springboot(九)/mybatis/src/main/resources/mybatis/mapper/AreaTreeMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 14 | 15 | 18 | 19 | -------------------------------------------------------------------------------- /springboot(五)/SpringBoot(五)_表单验证.md: -------------------------------------------------------------------------------- 1 | 2 | > 参数校验在我们日常开发中非常常见,最基本的校验有判断属性是否为空、长度是否符合要求等,在传统的开发模式中需要写一堆的 if else 来处理这些逻辑,很繁琐,效率也低。使用 @Valid + BindingResult 就可以优雅地解决这些问题 3 | 4 | (1)首先在实体类中增加注解 5 | 6 | ``` 7 | @Entity 8 | public class Girl { 9 | 10 | 11 | @Id 12 | @GeneratedValue 13 | private int id; 14 | 15 | @NotEmpty(message = "姓名不能为空") 16 | private String name; 17 | 18 | @Min(value = 18,message = "未成年禁止入内") 19 | private int age; 20 | 21 | public Girl() { 22 | } 23 | 24 | ``` 25 | 26 | (2) 在controller 中增加@Valid + BindingResult 27 | 28 | ``` 29 | @PostMapping("/girls") 30 | public Girl girlAdd(@Valid Girl girl, BindingResult bindingResult){ 31 | if(bindingResult.hasErrors()){ 32 | 33 | System.out.println(bindingResult.getFieldError().getDefaultMessage()); 34 | return null; 35 | } 36 | return girlRepository.save(girl); 37 | } 38 | ``` 39 | 40 | (3) 测试结果 41 | ``` 42 | 未成年禁止入内 43 | ``` 44 | 45 | > 结果显示均已经触发了校验规则,返回了错误信息,在实际使用过程中可以对错误信息进行包装,最后返回到前端进行展示。 -------------------------------------------------------------------------------- /springboot(九)/sql语句/area.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat MySQL Data Transfer 3 | 4 | Source Server : 本地 5 | Source Server Version : 50721 6 | Source Host : localhost:3306 7 | Source Database : test 8 | 9 | Target Server Type : MYSQL 10 | Target Server Version : 50721 11 | File Encoding : 65001 12 | 13 | Date: 2018-06-09 23:16:03 14 | */ 15 | 16 | SET FOREIGN_KEY_CHECKS=0; 17 | 18 | -- ---------------------------- 19 | -- Table structure for area 20 | -- ---------------------------- 21 | DROP TABLE IF EXISTS `area`; 22 | CREATE TABLE `area` ( 23 | `id` varchar(36) COLLATE utf8_bin NOT NULL, 24 | `name` varchar(255) COLLATE utf8_bin DEFAULT NULL, 25 | `parent_id` varchar(36) COLLATE utf8_bin DEFAULT NULL, 26 | PRIMARY KEY (`id`) 27 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; 28 | 29 | -- ---------------------------- 30 | -- Records of area 31 | -- ---------------------------- 32 | INSERT INTO `area` VALUES ('33700101', '高新区', '37001'); 33 | INSERT INTO `area` VALUES ('370', '山东', '0'); 34 | INSERT INTO `area` VALUES ('37001', '济南', '370'); 35 | INSERT INTO `area` VALUES ('37002', '泰安', '370'); 36 | -------------------------------------------------------------------------------- /springboot(九)/mybatis/src/main/java/com/zhb/entity/AreaTreeEntity.java: -------------------------------------------------------------------------------- 1 | package com.zhb.entity; 2 | 3 | import java.awt.geom.Area; 4 | import java.util.List; 5 | 6 | /** 7 | * @Auther: curry 8 | * @Date: 2018/6/8 22:31 9 | * @Description: 10 | */ 11 | public class AreaTreeEntity { 12 | 13 | private String id; 14 | 15 | private String name; 16 | 17 | private String pid; 18 | 19 | public String getPid() { 20 | return pid; 21 | } 22 | 23 | public void setPid(String pid) { 24 | this.pid = pid; 25 | } 26 | 27 | private List children; 28 | 29 | public String getId() { 30 | return id; 31 | } 32 | 33 | public void setId(String id) { 34 | this.id = id; 35 | } 36 | 37 | public String getName() { 38 | return name; 39 | } 40 | 41 | public void setName(String name) { 42 | this.name = name; 43 | } 44 | 45 | public List getChildren() { 46 | return children; 47 | } 48 | 49 | public void setChildren(List children) { 50 | this.children = children; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /springboot(九)/sql语句/users.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat MySQL Data Transfer 3 | 4 | Source Server : 本地 5 | Source Server Version : 50721 6 | Source Host : localhost:3306 7 | Source Database : test 8 | 9 | Target Server Type : MYSQL 10 | Target Server Version : 50721 11 | File Encoding : 65001 12 | 13 | Date: 2018-06-09 23:15:44 14 | */ 15 | 16 | SET FOREIGN_KEY_CHECKS=0; 17 | 18 | -- ---------------------------- 19 | -- Table structure for users 20 | -- ---------------------------- 21 | DROP TABLE IF EXISTS `users`; 22 | CREATE TABLE `users` ( 23 | `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id', 24 | `userName` varchar(32) DEFAULT NULL COMMENT '用户名', 25 | `passWord` varchar(32) DEFAULT NULL COMMENT '密码', 26 | `user_sex` varchar(32) DEFAULT NULL, 27 | `nick_name` varchar(32) DEFAULT NULL, 28 | PRIMARY KEY (`id`) 29 | ) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8; 30 | 31 | -- ---------------------------- 32 | -- Records of users 33 | -- ---------------------------- 34 | INSERT INTO `users` VALUES ('28', '毛毛', '1234', 'MAN', '324'); 35 | INSERT INTO `users` VALUES ('29', '12', '12', 'MAN', null); 36 | -------------------------------------------------------------------------------- /springboot(十二)/rabbit_mq/src/main/java/com/zhb/direct/HelloSender.java: -------------------------------------------------------------------------------- 1 | package com.zhb.direct; 2 | 3 | import org.springframework.amqp.core.AmqpTemplate; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | 9 | /** 10 | * @author: curry 11 | * @Date: 2018/8/27 12 | */ 13 | 14 | @RestController 15 | public class HelloSender { 16 | 17 | @Autowired 18 | private AmqpTemplate amqpTemplate; 19 | 20 | @GetMapping(value = "/sendString") 21 | public String sendString() { 22 | //发送消息 String routingKey, Object object 23 | amqpTemplate.convertAndSend("hello", "hello rabbitMQ"); 24 | return "消息已发送"; 25 | } 26 | 27 | @GetMapping(value = "/sendObject") 28 | public String sendObject() { 29 | //发送消息 String routingKey, Object object 30 | UserEntity userEntity = new UserEntity(); 31 | userEntity.setName("allen"); 32 | userEntity.setAddress("山东济南"); 33 | amqpTemplate.convertAndSend("hello", userEntity); 34 | return "消息已发送"; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /springboot(十六)/es-job1/es-job/src/main/java/com/kevin/task/task/MyDataflowJob.java: -------------------------------------------------------------------------------- 1 | package com.kevin.task.task; 2 | 3 | import com.dangdang.ddframe.job.api.ShardingContext; 4 | import com.dangdang.ddframe.job.api.dataflow.DataflowJob; 5 | import com.kevin.task.entity.Foo; 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | /** 13 | * @author: kevin 14 | * @Date: 2020/1/30 15 | */ 16 | public class MyDataflowJob implements DataflowJob { 17 | 18 | private static final Logger LOGGER = LoggerFactory.getLogger(MyDataflowJob.class); 19 | 20 | @Override 21 | public List fetchData(final ShardingContext shardingContext) { 22 | System.err.println("--------------@@@@@@@@@@ 抓取数据集合...--------------"); 23 | List list = new ArrayList(); 24 | list.add(new Foo("001", "张三")); 25 | list.add(new Foo("002", "李四")); 26 | return list; 27 | } 28 | 29 | @Override 30 | public void processData(final ShardingContext shardingContext, final List data) { 31 | System.err.println("--------------@@@@@@@@@ 处理数据集合...--------------"); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /springboot(一)/girl/src/test/java/com/imooc/GirlApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.imooc; 2 | 3 | import com.imooc.controller.HelloController; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | import org.springframework.http.MediaType; 9 | import org.springframework.mock.web.MockHttpServletRequest; 10 | import org.springframework.test.context.junit4.SpringRunner; 11 | import org.springframework.test.web.servlet.MockMvc; 12 | import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; 13 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; 14 | 15 | import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; 16 | 17 | @RunWith(SpringRunner.class) 18 | @SpringBootTest 19 | public class GirlApplicationTests { 20 | 21 | private MockMvc mockMvc; 22 | 23 | @Before 24 | public void setup(){ 25 | mockMvc = MockMvcBuilders.standaloneSetup( new HelloController()).build(); 26 | } 27 | @Test 28 | public void contextLoads() throws Exception { 29 | mockMvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON_UTF8)).andDo(print()); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /springboot(七)/girl/src/main/java/com/imooc/handle/ExceptionHandle.java: -------------------------------------------------------------------------------- 1 | package com.imooc.handle; 2 | 3 | import com.imooc.aspect.HttpAspect; 4 | import com.imooc.entity.Result; 5 | import com.imooc.exception.GirlException; 6 | import com.imooc.utils.ResultUtil; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.web.bind.annotation.ControllerAdvice; 10 | import org.springframework.web.bind.annotation.ExceptionHandler; 11 | import org.springframework.web.bind.annotation.ResponseBody; 12 | 13 | /** 14 | * @Auther: curry 15 | * @Date: 2018/6/2 15:05 16 | * @Description: 17 | */ 18 | @ControllerAdvice 19 | public class ExceptionHandle { 20 | private final static Logger logger = LoggerFactory.getLogger(HttpAspect.class); 21 | @ExceptionHandler(value = Exception.class) 22 | @ResponseBody 23 | public Result handle(Exception e){ 24 | // return ResultUtil.error(100,e.getMessage()); 25 | 26 | if (e instanceof GirlException){ 27 | GirlException girlException = (GirlException)e; 28 | return ResultUtil.error(girlException.getCode(),girlException.getMessage()); 29 | }else { 30 | logger.info("【系统异常】{}",e); 31 | return ResultUtil.error(-1,"未知错误"); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /springboot(十二)/rabbit_mq/src/main/java/com/zhb/topic/TopicRabbitConfig.java: -------------------------------------------------------------------------------- 1 | package com.zhb.topic; 2 | 3 | import org.springframework.amqp.core.*; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | 7 | /** 8 | * @author: curry 9 | * @Date: 2018/9/5 10 | */ 11 | @Configuration 12 | public class TopicRabbitConfig { 13 | 14 | final static String message = "topic.message"; 15 | final static String messages = "topic.messages"; 16 | 17 | //定义队列 18 | @Bean 19 | public Queue queueMessage() { 20 | return new Queue(TopicRabbitConfig.message); 21 | } 22 | 23 | @Bean 24 | public Queue queueMessages() { 25 | return new Queue(TopicRabbitConfig.messages); 26 | } 27 | 28 | //exchange 29 | @Bean 30 | TopicExchange exchange() { 31 | return new TopicExchange("exchange"); 32 | } 33 | 34 | //将队列和交换机进行绑定 35 | @Bean 36 | Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) { 37 | return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message"); 38 | } 39 | 40 | @Bean 41 | Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) { 42 | return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#"); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /springboot(九)/mybatis/src/main/java/com/zhb/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.zhb.controller; 2 | 3 | import com.zhb.entity.UserEntity; 4 | import com.zhb.mapper.UserMapper; 5 | import org.springframework.web.bind.annotation.*; 6 | 7 | import javax.annotation.Resource; 8 | import java.util.List; 9 | 10 | /** 11 | * @Auther: curry 12 | * @Date: 2018/6/8 21:13 13 | * @Description: 14 | */ 15 | @RestController 16 | public class UserController { 17 | 18 | @Resource 19 | private UserMapper userMapper; 20 | 21 | 22 | @GetMapping(value = "/users") 23 | public List getUsers() { 24 | List users=userMapper.getAll(); 25 | return users; 26 | } 27 | 28 | 29 | 30 | @GetMapping(value = "/users/{id}") 31 | public UserEntity getUser(@PathVariable(value = "id") Long id) { 32 | UserEntity user=userMapper.getOne(id); 33 | return user; 34 | } 35 | 36 | @PostMapping("/users") 37 | public void save(UserEntity user) { 38 | userMapper.insert(user); 39 | } 40 | 41 | @PutMapping("/users") 42 | public void update(UserEntity user) { 43 | userMapper.update(user); 44 | } 45 | 46 | @DeleteMapping(value="/users/{id}") 47 | public void delete(@PathVariable("id") Long id) { 48 | userMapper.delete(id); 49 | } 50 | 51 | 52 | } 53 | -------------------------------------------------------------------------------- /springboot(七)/girl/src/main/java/com/imooc/entity/Girl.java: -------------------------------------------------------------------------------- 1 | package com.imooc.entity; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.GeneratedValue; 5 | import javax.persistence.Id; 6 | import javax.validation.constraints.Min; 7 | import javax.validation.constraints.NotEmpty; 8 | 9 | /** 10 | * @Auther: curry 11 | * @Date: 2018/5/27 22:18 12 | * @Description: 13 | */ 14 | 15 | @Entity 16 | public class Girl { 17 | 18 | 19 | @Id 20 | @GeneratedValue 21 | private int id; 22 | 23 | @NotEmpty(message = "姓名不能为空") 24 | private String name; 25 | 26 | @Min(value = 18,message = "未成年禁止入内") 27 | private int age; 28 | 29 | public Girl() { 30 | } 31 | 32 | public int getId() { 33 | return id; 34 | } 35 | 36 | public void setId(int id) { 37 | this.id = id; 38 | } 39 | 40 | public String getName() { 41 | return name; 42 | } 43 | 44 | public void setName(String name) { 45 | this.name = name; 46 | } 47 | 48 | public int getAge() { 49 | return age; 50 | } 51 | 52 | public void setAge(int age) { 53 | this.age = age; 54 | } 55 | 56 | @Override 57 | public String toString() { 58 | return "Girl{" + 59 | "id=" + id + 60 | ", name='" + name + '\'' + 61 | ", age=" + age + 62 | '}'; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /springboot(九)/mybatis/src/main/java/com/zhb/Properties/SwaggerProperties.java: -------------------------------------------------------------------------------- 1 | package com.zhb.Properties; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import springfox.documentation.builders.ApiInfoBuilder; 6 | import springfox.documentation.builders.PathSelectors; 7 | import springfox.documentation.builders.RequestHandlerSelectors; 8 | import springfox.documentation.service.ApiInfo; 9 | import springfox.documentation.spi.DocumentationType; 10 | import springfox.documentation.spring.web.plugins.Docket; 11 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 12 | 13 | /** 14 | * @Auther: curry 15 | * @Date: 2018/6/8 21:33 16 | * @Description: 17 | */ 18 | @Configuration 19 | @EnableSwagger2 20 | public class SwaggerProperties { 21 | @Bean 22 | public Docket createRestApi() { 23 | return new Docket(DocumentationType.SWAGGER_2) 24 | .apiInfo(apiInfo()) 25 | .select() 26 | .apis(RequestHandlerSelectors.basePackage("com.zhb.controller")) 27 | .paths(PathSelectors.any()) 28 | .build(); 29 | } 30 | private ApiInfo apiInfo() { 31 | return new ApiInfoBuilder() 32 | .title("springboot利用swagger构建api文档") 33 | .description("") 34 | .termsOfServiceUrl("") 35 | .version("1.0") 36 | .build(); 37 | } 38 | } -------------------------------------------------------------------------------- /springboot(十四)/report-srping-boot/student.sql: -------------------------------------------------------------------------------- 1 | /* 2 | SQLyog 企业版 - MySQL GUI v8.14 3 | MySQL - 5.7.21 : Database - kevin 4 | ********************************************************************* 5 | */ 6 | 7 | /*!40101 SET NAMES utf8 */; 8 | 9 | /*!40101 SET SQL_MODE=''*/; 10 | 11 | /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; 12 | /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; 13 | /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; 14 | /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; 15 | CREATE DATABASE /*!32312 IF NOT EXISTS*/`kevin` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_bin */; 16 | 17 | USE `kevin`; 18 | 19 | /*Table structure for table `student` */ 20 | 21 | DROP TABLE IF EXISTS `student`; 22 | 23 | CREATE TABLE `student` ( 24 | `id` int(11) NOT NULL AUTO_INCREMENT, 25 | `name` varchar(200) COLLATE utf8_bin DEFAULT NULL, 26 | `address` varchar(200) COLLATE utf8_bin DEFAULT NULL, 27 | PRIMARY KEY (`id`) 28 | ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; 29 | 30 | /*Data for the table `student` */ 31 | 32 | insert into `student`(`id`,`name`,`address`) values (1,'周杰伦','山东省济南市高新区'),(2,'范特西','北京市朝阳区人们群众'); 33 | 34 | /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; 35 | /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; 36 | /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; 37 | /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; 38 | -------------------------------------------------------------------------------- /springboot(十二)/rabbit_mq/src/main/java/com/zhb/fanout/FanoutRabbitConfig.java: -------------------------------------------------------------------------------- 1 | package com.zhb.fanout; 2 | 3 | import org.springframework.amqp.core.Binding; 4 | import org.springframework.amqp.core.BindingBuilder; 5 | import org.springframework.amqp.core.FanoutExchange; 6 | import org.springframework.amqp.core.Queue; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.Configuration; 9 | 10 | /** 11 | * @author: curry 12 | * @Date: 2018/9/6 13 | */ 14 | @Configuration 15 | public class FanoutRabbitConfig { 16 | 17 | @Bean 18 | public Queue AMessage() { 19 | return new Queue("fanout.A"); 20 | } 21 | 22 | @Bean 23 | public Queue BMessage() { 24 | return new Queue("fanout.B"); 25 | } 26 | 27 | @Bean 28 | public Queue CMessage() { 29 | return new Queue("fanout.C"); 30 | } 31 | 32 | //定义交换机 33 | @Bean 34 | FanoutExchange fanoutExchange() { 35 | return new FanoutExchange("fanoutExchange"); 36 | } 37 | 38 | //分部进行绑定 39 | @Bean 40 | Binding bindingExchangeA(Queue AMessage, FanoutExchange fanoutExchange) { 41 | return BindingBuilder.bind(AMessage).to(fanoutExchange); 42 | } 43 | 44 | @Bean 45 | Binding bindingExchangeB(Queue BMessage, FanoutExchange fanoutExchange) { 46 | return BindingBuilder.bind(BMessage).to(fanoutExchange); 47 | } 48 | 49 | @Bean 50 | Binding bindingExchangeC(Queue CMessage, FanoutExchange fanoutExchange) { 51 | return BindingBuilder.bind(CMessage).to(fanoutExchange); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /springboot(十一)/import/src/main/java/com/zhb/controller/ImportController.java: -------------------------------------------------------------------------------- 1 | package com.zhb.controller; 2 | 3 | import com.zhb.service.ImportService; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.web.bind.annotation.*; 7 | import org.springframework.web.multipart.MultipartFile; 8 | import org.springframework.web.multipart.MultipartHttpServletRequest; 9 | 10 | import javax.servlet.http.HttpServletRequest; 11 | import java.io.InputStream; 12 | import java.util.List; 13 | 14 | /** 15 | * @author: curry 16 | * @Date: 2018/8/16 17 | */ 18 | @Controller 19 | public class ImportController { 20 | 21 | @Autowired 22 | private ImportService importService; 23 | 24 | 25 | @PostMapping(value = "/upload") 26 | @ResponseBody 27 | public String uploadExcel(HttpServletRequest request) throws Exception { 28 | MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; 29 | 30 | MultipartFile file = multipartRequest.getFile("filename"); 31 | if (file.isEmpty()) { 32 | return "文件不能为空"; 33 | } 34 | InputStream inputStream = file.getInputStream(); 35 | List> list = importService.getBankListByExcel(inputStream, file.getOriginalFilename()); 36 | inputStream.close(); 37 | 38 | for (int i = 0; i < list.size(); i++) { 39 | List lo = list.get(i); 40 | //TODO 随意发挥 41 | System.out.println(lo); 42 | 43 | } 44 | return "上传成功"; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /springboot(十六)/es-job1/es-job/src/main/java/com/kevin/task/config/RegistryCenterConfig.java: -------------------------------------------------------------------------------- 1 | package com.kevin.task.config; 2 | import org.springframework.beans.factory.annotation.Value; 3 | import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration; 4 | import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter; 5 | import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | 9 | /** 10 | * @author: kevin 11 | * @Date: 2020/1/22 12 | */ 13 | @Configuration 14 | @ConditionalOnExpression("'${zookeeper.address}'.length() > 0") 15 | public class RegistryCenterConfig { 16 | 17 | /** 18 | * 把注册中心加载到spring 容器中 19 | * @return 20 | */ 21 | @Bean(initMethod = "init") 22 | public ZookeeperRegistryCenter registryCenter(@Value("${zookeeper.address}") final String serverLists, 23 | @Value("${zookeeper.namespace}") final String namespace, 24 | @Value("${zookeeper.connectionTimeout}") final int connectionTimeout, 25 | @Value("${zookeeper.sessionTimeout}") final int sessionTimeout, 26 | @Value("${zookeeper.maxRetries}") final int maxRetries) { 27 | ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(serverLists, namespace); 28 | zookeeperConfiguration.setConnectionTimeoutMilliseconds(connectionTimeout); 29 | zookeeperConfiguration.setSessionTimeoutMilliseconds(sessionTimeout); 30 | zookeeperConfiguration.setMaxRetries(maxRetries); 31 | 32 | return new ZookeeperRegistryCenter(zookeeperConfiguration); 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /springboot(十)/redis-demo-01/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.zhb 7 | redis-demo-01 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | redis-demo-01 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 2.0.3.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-data-redis 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-test 36 | test 37 | 38 | 39 | 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-maven-plugin 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /springboot(七)/girl/src/main/java/com/imooc/aspect/HttpAspect.java: -------------------------------------------------------------------------------- 1 | package com.imooc.aspect; 2 | 3 | import org.aspectj.lang.JoinPoint; 4 | import org.aspectj.lang.annotation.*; 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.springframework.stereotype.Component; 8 | import org.springframework.web.context.request.RequestContextHolder; 9 | import org.springframework.web.context.request.ServletRequestAttributes; 10 | import org.springframework.web.servlet.support.RequestContext; 11 | 12 | import javax.servlet.http.HttpServletRequest; 13 | 14 | /** 15 | * @Auther: curry 16 | * @Date: 2018/6/2 13:45 17 | * @Description: 18 | */ 19 | @Aspect 20 | @Component 21 | public class HttpAspect { 22 | 23 | private final static Logger logger = LoggerFactory.getLogger(HttpAspect.class); 24 | 25 | @Pointcut("execution( public * com.imooc.controller.GirlController.*(..))") 26 | public void log(){ 27 | 28 | } 29 | 30 | @Before("log()") 31 | public void doBefore(JoinPoint joinPoint){ 32 | ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); 33 | HttpServletRequest request = attributes.getRequest(); 34 | // url 35 | logger.info("url={}",request.getRequestURL()); 36 | //method 37 | logger.info("method = {}",request.getMethod()); 38 | //ip 39 | logger.info("ip = {}",request.getRemoteAddr()); 40 | //类方法 41 | logger.info("class_method={}",joinPoint.getSignature().getDeclaringTypeName()+"."+ joinPoint.getSignature().getName()); 42 | //参数 43 | logger.info("args = {}",joinPoint.getArgs()); 44 | 45 | } 46 | 47 | 48 | @After("log()") 49 | public void doAfter(){ 50 | 51 | } 52 | 53 | @AfterReturning(pointcut = "log()",returning = "object") 54 | public void doAfterReturning(Object object){ 55 | logger.info("response = {}",object); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /springboot(九)/mybatis/src/main/java/com/zhb/entity/UserEntity.java: -------------------------------------------------------------------------------- 1 | package com.zhb.entity; 2 | 3 | import com.zhb.enums.UserSexEnum; 4 | import org.apache.commons.lang3.builder.ToStringBuilder; 5 | 6 | import java.io.Serializable; 7 | 8 | /** 9 | * @Auther: curry 10 | * @Date: 2018/6/8 21:07 11 | * @Description: 12 | */ 13 | public class UserEntity implements Serializable { 14 | 15 | private static final long serialVersionUID = 1L; 16 | private Long id; 17 | private String userName; 18 | private String passWord; 19 | private UserSexEnum userSex; 20 | private String nickName; 21 | 22 | public UserEntity() { 23 | super(); 24 | } 25 | 26 | public UserEntity(String userName, String passWord, UserSexEnum userSex) { 27 | super(); 28 | this.passWord = passWord; 29 | this.userName = userName; 30 | this.userSex = userSex; 31 | } 32 | 33 | public Long getId() { 34 | return id; 35 | } 36 | 37 | public void setId(Long id) { 38 | this.id = id; 39 | } 40 | 41 | public String getUserName() { 42 | return userName; 43 | } 44 | 45 | public void setUserName(String userName) { 46 | this.userName = userName; 47 | } 48 | 49 | public String getPassWord() { 50 | return passWord; 51 | } 52 | 53 | public void setPassWord(String passWord) { 54 | this.passWord = passWord; 55 | } 56 | 57 | public UserSexEnum getUserSex() { 58 | return userSex; 59 | } 60 | 61 | public void setUserSex(UserSexEnum userSex) { 62 | this.userSex = userSex; 63 | } 64 | 65 | public String getNickName() { 66 | return nickName; 67 | } 68 | 69 | public void setNickName(String nickName) { 70 | this.nickName = nickName; 71 | } 72 | 73 | @Override 74 | public String toString() { 75 | return ToStringBuilder.reflectionToString(this); 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /springboot(四)/girl/src/main/java/com/imooc/controller/GirlController.java: -------------------------------------------------------------------------------- 1 | package com.imooc.controller; 2 | 3 | import com.imooc.entity.Girl; 4 | import com.imooc.repository.GirlRepository; 5 | import org.springframework.web.bind.annotation.*; 6 | 7 | import javax.annotation.Resource; 8 | import java.util.List; 9 | 10 | /** 11 | * @Auther: curry 12 | * @Date: 2018/5/28 21:57 13 | * @Description: 14 | */ 15 | @RestController 16 | public class GirlController { 17 | 18 | @Resource 19 | private GirlRepository girlRepository; 20 | 21 | @GetMapping("/girls") 22 | public List getList(){ 23 | return girlRepository.findAll(); 24 | } 25 | 26 | @PostMapping("/girls") 27 | public Girl girlAdd(@RequestParam("name") String name, 28 | @RequestParam("age") int age){ 29 | 30 | Girl girl = new Girl(); 31 | girl.setAge(age); 32 | girl.setName(name); 33 | return girlRepository.save(girl); 34 | 35 | } 36 | 37 | @GetMapping(value = "/girls/{id}") 38 | public Girl find(@PathVariable(value = "id") Integer id){ 39 | return girlRepository.findById(id).get(); 40 | } 41 | 42 | @PostMapping(value = "/girls/{id}") 43 | public Girl update(@PathVariable(value = "id") Integer id, 44 | @RequestParam("name") String name, 45 | @RequestParam("age") int age){ 46 | Girl girl = new Girl(); 47 | girl.setId(id); 48 | girl.setAge(age); 49 | girl.setName(name); 50 | return girlRepository.save(girl); 51 | 52 | } 53 | 54 | @DeleteMapping(value = "/girls/{id}") 55 | public void delete(@PathVariable(value = "id") Integer id){ 56 | girlRepository.deleteById(id); 57 | } 58 | 59 | @GetMapping(value = "/girls/age/{age}") 60 | public List findByAge(@PathVariable(value = "age") Integer age){ 61 | 62 | return girlRepository.findByAge(age); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /springboot(二)/girl/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.imooc 7 | girl 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | girl 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 2.0.2.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-web 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-test 36 | test 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-configuration-processor 41 | true 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | org.springframework.boot 50 | spring-boot-maven-plugin 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # spring boot 学习记录 2 | 3 | ## 简介(Introduction) 4 | 5 | 学习 spring boot 的个人笔记。 6 | 7 | ## 入门篇(Basics) 8 | * [SpringBoot(一)_快速实战搭建项目](https://github.com/runzhenghengbin/SpringBoot/wiki/1-springboot-kuai-su-da-jian) 9 | * [SpringBoot(二)_项目属性配置](https://github.com/runzhenghengbin/SpringBoot/wiki/2-springboot-shu-xing-pei-zhi) 10 | * [SpringBoot(三)_controller的使用](https://github.com/runzhenghengbin/SpringBoot/wiki/3-springboot-controller-shi-yong) 11 | * [SpringBoot(四)_SpringDataJPA的使用](https://github.com/runzhenghengbin/SpringBoot/wiki/4-springboot-spring-data-jpa) 12 | * [SpringBoot(五)_表单验证](https://github.com/runzhenghengbin/SpringBoot/wiki/5-springboot-biao-dan-yan-zheng) 13 | * [SpringBoot(六)_AOP统一处理请求](https://github.com/runzhenghengbin/SpringBoot/wiki/6-springboot-aop-chu-li-qing-qiu) 14 | * [SpringBoot(七)_统一异常处理](https://github.com/runzhenghengbin/SpringBoot/wiki/7-springboot-yi-chang-chu-li) 15 | * [SpringBoot(八)_springboot集成swagger2](https://github.com/runzhenghengbin/SpringBoot/wiki/8-springboot-swagger2) 16 | * [SpringBoot(九)_springboot集成MyBatis](https://github.com/runzhenghengbin/SpringBoot/wiki/9-spring-boot-mybatis) 17 | * [SpringBoot(十)_springboot集成Redis](https://github.com/runzhenghengbin/SpringBoot/wiki/10-springboot-redis) 18 | * [SpringBoot(十一)_springboot导入excel读取excel中的数据](https://github.com/runzhenghengbin/SpringBoot/wiki/11-springboot-excel) 19 | * [SpringBoot(十二)_springboot整合RabbitMQ](https://github.com/runzhenghengbin/SpringBoot/wiki/12-springboot-rabbitmq) 20 | * [SpringBoot(十三)_springboot实现预览pdf](https://github.com/runzhenghengbin/SpringBoot/wiki/13-springboot-pdf-view) 21 | * [SpringBoot(十四)_springboot整合JasperReport6.6.0](https://github.com/runzhenghengbin/SpringBoot/wiki/14-springboot-jasperreport-6.6.0) 22 | * [SpringBoot(十五)_springboot跨域处理](https://github.com/runzhenghengbin/SpringBoot/wiki/15-springboot-kua-yu) 23 | * [SpringBoot(十六)_springboot整合elastic-job](https://github.com/runzhenghengbin/SpringBoot) 24 | 25 | ## 附录(Appendix) 26 | 27 | ### Feedback 28 | - email : 923604825@qq.com 29 | 30 | - blog : [kevin-养码青年](https://www.cnblogs.com/zhenghengbin/) 31 | 32 | 33 | 欢迎加入qq群:`475061997`-后端攻城狮成长之路 34 | -------------------------------------------------------------------------------- /springboot(十二)/rabbit_mq/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.zhb 7 | demo 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | rabbit_mq 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 2.0.4.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-amqp 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-web 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-test 39 | test 40 | 41 | 42 | org.projectlombok 43 | lombok 44 | 1.16.10 45 | 46 | 47 | 48 | 49 | 50 | 51 | org.springframework.boot 52 | spring-boot-maven-plugin 53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /springboot(十一)/import/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.zhb 7 | import 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | import 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 2.0.4.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-web 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-thymeleaf 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-test 39 | test 40 | 41 | 42 | org.apache.poi 43 | poi 44 | 4.1.1 45 | 46 | 47 | org.apache.poi 48 | poi-ooxml 49 | 3.13 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | org.springframework.boot 58 | spring-boot-maven-plugin 59 | 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /springboot(一)/girl/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.imooc 7 | girl 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | girl 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 2.0.2.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-web 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-test 36 | test 37 | 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-configuration-processor 42 | true 43 | 44 | 45 | org.springframework.boot 46 | spring-boot-starter-data-jpa 47 | 48 | 49 | mysql 50 | mysql-connector-java 51 | 52 | 53 | 54 | 55 | 56 | 57 | org.springframework.boot 58 | spring-boot-maven-plugin 59 | 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /springboot(四)/girl/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.imooc 7 | girl 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | girl 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 2.0.2.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-web 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-test 36 | test 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-configuration-processor 41 | true 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-starter-data-jpa 46 | 47 | 48 | mysql 49 | mysql-connector-java 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | org.springframework.boot 58 | spring-boot-maven-plugin 59 | 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /springboot(十四)/report-srping-boot/src/main/java/com/kevin/controller/ReportController.java: -------------------------------------------------------------------------------- 1 | package com.kevin.controller; 2 | 3 | 4 | import net.sf.jasperreports.engine.*; 5 | import net.sf.jasperreports.engine.util.JRLoader; 6 | import org.springframework.core.io.ClassPathResource; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.PathVariable; 9 | import org.springframework.web.bind.annotation.RequestParam; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | import javax.annotation.Resource; 13 | import javax.servlet.http.HttpServletResponse; 14 | import javax.sql.DataSource; 15 | import java.io.File; 16 | import java.io.IOException; 17 | import java.io.InputStream; 18 | import java.io.OutputStream; 19 | import java.sql.SQLException; 20 | import java.util.HashMap; 21 | import java.util.Map; 22 | 23 | @RestController 24 | public class ReportController { 25 | 26 | @Resource 27 | private DataSource dataSource; 28 | 29 | 30 | /** 31 | * 转换为pdf展示 32 | * 33 | * @param reportName 34 | * @param parameters 35 | * @param response 36 | * @throws SQLException 37 | * @throws ClassNotFoundException 38 | * @throws JRException 39 | * @throws IOException 40 | */ 41 | @GetMapping("/{reportName}") 42 | public void getReportByParam( 43 | @PathVariable("reportName") final String reportName, 44 | @RequestParam(required = false) Map parameters, 45 | HttpServletResponse response) throws SQLException, ClassNotFoundException, JRException, IOException { 46 | 47 | parameters = parameters == null ? new HashMap<>() : parameters; 48 | //获取文件流 49 | ClassPathResource resource = new ClassPathResource("jaspers" + File.separator + reportName + ".jasper"); 50 | InputStream jasperStream = resource.getInputStream(); 51 | 52 | JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream); 53 | JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource.getConnection()); 54 | // JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, new JREmptyDataSource()); 55 | response.setContentType("application/pdf"); 56 | response.setHeader("Content-Disposition", "inline;"); 57 | final OutputStream outputStream = response.getOutputStream(); 58 | JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /springboot(三)/SpringBoot(三)_controller的使用.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | > 针对controller 中 如何使用注解进行解析 4 | 5 | #### @RestController 6 | 7 | - 返回数据类型为 Json 字符串,特别适合我们给其他系统提供接口时使用。 8 | 9 | #### @RequestMapping 10 | 11 | (1) 不同前缀访问同一个方法,此时访问hello和hi 都可以访问到say()这个方法 12 | 13 | ``` 14 | @RequestMapping(value = {"/hello","/hi"},method = RequestMethod.GET) 15 | public String say(){ 16 | return girlProperties.getName(); 17 | } 18 | ``` 19 | (2)给类一个RequestMapping, 访问时就是:http://localhost:8099/hello/say 20 | 21 | ``` 22 | @RestController 23 | @RequestMapping("/hello") 24 | public class HelloController { 25 | 26 | @Resource 27 | private GirlProperties girlProperties; 28 | @RequestMapping(value = "/say",method = RequestMethod.GET) 29 | public String say(){ 30 | return girlProperties.getName(); 31 | } 32 | } 33 | ``` 34 | 35 | #### @PathVariable:获取url中的数据 36 | 37 | ``` 38 | @RestController 39 | @RequestMapping("/hello") 40 | public class HelloController { 41 | 42 | @Resource 43 | private GirlProperties girlProperties; 44 | @RequestMapping(value = "/say/{id}",method = RequestMethod.GET) 45 | public String say(@PathVariable("id") Integer id){ 46 | return "id :"+id; 47 | } 48 | } 49 | ``` 50 | 访问http://localhost:8099/hello/say/100, 结果如下 51 | 52 | ``` 53 | id :100 54 | ``` 55 | 56 | #### @RequestParam :获取请求参数的值 57 | (1) 正常请求 58 | ``` 59 | @RestController 60 | @RequestMapping("/hello") 61 | public class HelloController { 62 | 63 | @Resource 64 | private GirlProperties girlProperties; 65 | @RequestMapping(value = "/say",method = RequestMethod.GET) 66 | public String say(@RequestParam("id") Integer id){ 67 | return "id :"+id; 68 | } 69 | } 70 | ``` 71 | 访问 http://localhost:8099/hello/say?id=111 结果如下 72 | ``` 73 | id :111 74 | ``` 75 | (2)设置参数非必须的,并且设置上默认值 76 | ``` 77 | @RestController 78 | @RequestMapping("/hello") 79 | public class HelloController { 80 | 81 | @Resource 82 | private GirlProperties girlProperties; 83 | @RequestMapping(value = "/say",method = RequestMethod.GET) 84 | public String say(@RequestParam(value = "id",required = false,defaultValue = "0") Integer id){ 85 | return "id :"+id; 86 | } 87 | } 88 | 89 | ``` 90 | 访问http://localhost:8099/hello/say 结果如下 91 | ``` 92 | id :0 93 | ``` 94 | #### @GetMapping ,当然也有对应的Post等请求的简化写法 95 | 96 | - 这里对应的就是下面这句代码 97 | ``` 98 | @GetMapping("/say") 99 | //等同于下面代码 100 | @RequestMapping(value = "/say",method = RequestMethod.GET) 101 | ``` -------------------------------------------------------------------------------- /springboot(七)/girl/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.imooc 7 | girl 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | girl 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 2.0.2.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-web 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-test 36 | test 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-aop 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-configuration-processor 45 | true 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-starter-data-jpa 50 | 51 | 52 | mysql 53 | mysql-connector-java 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | org.springframework.boot 62 | spring-boot-maven-plugin 63 | 64 | 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /springboot(十四)/report-srping-boot/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.1.RELEASE 9 | 10 | 11 | com.kevin 12 | report-srping-boot 13 | 0.0.1-SNAPSHOT 14 | report-srping-boot 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-jdbc 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-freemarker 33 | 34 | 35 | mysql 36 | mysql-connector-java 37 | runtime 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-starter-test 42 | test 43 | 44 | 45 | 46 | net.sf.jasperreports 47 | jasperreports 48 | 6.6.0 49 | 50 | 51 | com.kevin 52 | myfont 53 | 1.0.0 54 | 55 | 56 | 57 | 58 | 59 | 60 | org.springframework.boot 61 | spring-boot-maven-plugin 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /springboot(七)/girl/src/main/java/com/imooc/controller/GirlController.java: -------------------------------------------------------------------------------- 1 | package com.imooc.controller; 2 | 3 | import com.imooc.entity.Girl; 4 | import com.imooc.entity.Result; 5 | import com.imooc.repository.GirlRepository; 6 | import com.imooc.service.GirlService; 7 | import com.imooc.utils.ResultUtil; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | import org.springframework.validation.BindingResult; 11 | import org.springframework.validation.ObjectError; 12 | import org.springframework.web.bind.annotation.*; 13 | 14 | import javax.annotation.Resource; 15 | import javax.validation.Valid; 16 | import java.util.List; 17 | 18 | /** 19 | * @Auther: curry 20 | * @Date: 2018/5/28 21:57 21 | * @Description: 22 | */ 23 | @RestController 24 | public class GirlController { 25 | private final static Logger logger = LoggerFactory.getLogger(GirlController.class); 26 | @Resource 27 | private GirlRepository girlRepository; 28 | 29 | @Resource 30 | private GirlService girlService; 31 | 32 | @GetMapping("/girls") 33 | public List getList(){ 34 | logger.info("getList"); 35 | return girlRepository.findAll(); 36 | } 37 | 38 | @PostMapping("/girls") 39 | public Result girlAdd(@Valid Girl girl, BindingResult bindingResult){ 40 | if(bindingResult.hasErrors()){ 41 | return ResultUtil.error(1,bindingResult.getFieldError().getDefaultMessage()); 42 | } 43 | return ResultUtil.success(girlRepository.save(girl)); 44 | 45 | } 46 | 47 | @GetMapping(value = "/girls/{id}") 48 | public Girl find(@PathVariable(value = "id") Integer id){ 49 | return girlRepository.findById(id).get(); 50 | } 51 | 52 | @PostMapping(value = "/girls/{id}") 53 | public Girl update(@PathVariable(value = "id") Integer id, 54 | @RequestParam("name") String name, 55 | @RequestParam("age") int age){ 56 | Girl girl = new Girl(); 57 | girl.setId(id); 58 | girl.setAge(age); 59 | girl.setName(name); 60 | return girlRepository.save(girl); 61 | 62 | } 63 | 64 | @DeleteMapping(value = "/girls/{id}") 65 | public void delete(@PathVariable(value = "id") Integer id){ 66 | girlRepository.deleteById(id); 67 | } 68 | 69 | @GetMapping(value = "/girls/age/{age}") 70 | public List findByAge(@PathVariable(value = "age") Integer age){ 71 | return girlRepository.findByAge(age); 72 | } 73 | @GetMapping(value = "/girls/getAge/{id}") 74 | public void getAge(@PathVariable("id") Integer id) throws Exception { 75 | girlService.getAge(id); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /springboot(十六)/es-job1/es-job/src/main/java/com/kevin/task/config/DataflowJobConfig.java: -------------------------------------------------------------------------------- 1 | package com.kevin.task.config; 2 | 3 | import com.dangdang.ddframe.job.api.dataflow.DataflowJob; 4 | import com.dangdang.ddframe.job.config.JobCoreConfiguration; 5 | import com.dangdang.ddframe.job.config.dataflow.DataflowJobConfiguration; 6 | import com.dangdang.ddframe.job.event.JobEventConfiguration; 7 | import com.dangdang.ddframe.job.lite.api.JobScheduler; 8 | import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration; 9 | import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler; 10 | import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter; 11 | import com.kevin.task.task.MyDataflowJob; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.beans.factory.annotation.Value; 14 | import org.springframework.context.annotation.Bean; 15 | import org.springframework.context.annotation.Configuration; 16 | 17 | /** 18 | * @author: kevin 19 | * @Date: 2020/1/30 20 | */ 21 | @Configuration 22 | public class DataflowJobConfig { 23 | 24 | @Autowired 25 | private ZookeeperRegistryCenter regCenter; 26 | 27 | @Autowired 28 | private JobEventConfiguration jobEventConfiguration; 29 | 30 | @Bean 31 | public DataflowJob dataflowJob() { 32 | return new MyDataflowJob(); 33 | } 34 | 35 | @Bean(initMethod = "init") 36 | public JobScheduler dataflowJobScheduler(final DataflowJob dataflowJob, @Value("${dataflowJob.cron}") final String cron, 37 | @Value("${dataflowJob.shardingTotalCount}") final int shardingTotalCount, 38 | @Value("${dataflowJob.shardingItemParameters}") final String shardingItemParameters) { 39 | 40 | SpringJobScheduler springJobScheduler = new SpringJobScheduler(dataflowJob, regCenter, getLiteJobConfiguration(dataflowJob.getClass(), cron, 41 | shardingTotalCount, shardingItemParameters), jobEventConfiguration); 42 | return springJobScheduler; 43 | } 44 | 45 | private LiteJobConfiguration getLiteJobConfiguration(final Class jobClass, final String cron, final int shardingTotalCount, final String shardingItemParameters) { 46 | return LiteJobConfiguration.newBuilder( 47 | new DataflowJobConfiguration(JobCoreConfiguration.newBuilder(jobClass.getName(), cron, shardingTotalCount) 48 | .shardingItemParameters(shardingItemParameters).build(), 49 | jobClass.getCanonicalName(), 50 | false)) //streamingProcess 51 | .overwrite(false) 52 | .build(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /springboot(十一)/import/src/main/java/com/zhb/service/ImportService.java: -------------------------------------------------------------------------------- 1 | package com.zhb.service; 2 | 3 | 4 | import org.apache.poi.hssf.usermodel.HSSFWorkbook; 5 | import org.apache.poi.ss.usermodel.Cell; 6 | import org.apache.poi.ss.usermodel.Row; 7 | import org.apache.poi.ss.usermodel.Sheet; 8 | import org.apache.poi.ss.usermodel.Workbook; 9 | import org.apache.poi.xssf.usermodel.XSSFWorkbook; 10 | import org.springframework.stereotype.Service; 11 | 12 | import java.io.InputStream; 13 | import java.util.ArrayList; 14 | import java.util.List; 15 | 16 | /** 17 | * @author: curry 18 | * @Date: 2018/8/16 19 | */ 20 | @Service 21 | public class ImportService { 22 | 23 | 24 | /** 25 | * 处理上传的文件 26 | * 27 | * @param in 28 | * @param fileName 29 | * @return 30 | * @throws Exception 31 | */ 32 | public List getBankListByExcel(InputStream in, String fileName) throws Exception { 33 | List list = new ArrayList<>(); 34 | //创建Excel工作薄 35 | Workbook work = this.getWorkbook(in, fileName); 36 | if (null == work) { 37 | throw new Exception("创建Excel工作薄为空!"); 38 | } 39 | Sheet sheet = null; 40 | Row row = null; 41 | Cell cell = null; 42 | 43 | for (int i = 0; i < work.getNumberOfSheets(); i++) { 44 | sheet = work.getSheetAt(i); 45 | if (sheet == null) { 46 | continue; 47 | } 48 | 49 | for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) { 50 | row = sheet.getRow(j); 51 | if (row == null || row.getFirstCellNum() == j) { 52 | continue; 53 | } 54 | 55 | List li = new ArrayList<>(); 56 | for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) { 57 | cell = row.getCell(y); 58 | li.add(cell); 59 | } 60 | list.add(li); 61 | } 62 | } 63 | work.close(); 64 | return list; 65 | } 66 | 67 | /** 68 | * 判断文件格式 69 | * 70 | * @param inStr 71 | * @param fileName 72 | * @return 73 | * @throws Exception 74 | */ 75 | public Workbook getWorkbook(InputStream inStr, String fileName) throws Exception { 76 | Workbook workbook = null; 77 | String fileType = fileName.substring(fileName.lastIndexOf(".")); 78 | if (".xls".equals(fileType)) { 79 | workbook = new HSSFWorkbook(inStr); 80 | } else if (".xlsx".equals(fileType)) { 81 | workbook = new XSSFWorkbook(inStr); 82 | } else { 83 | throw new Exception("请上传excel文件!"); 84 | } 85 | return workbook; 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /springboot(九)/mybatis/src/main/java/com/zhb/controller/AreaTreeController.java: -------------------------------------------------------------------------------- 1 | package com.zhb.controller; 2 | 3 | import com.zhb.entity.AreaTreeEntity; 4 | import com.zhb.mapper.AreaTreeMapper; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | import javax.annotation.Resource; 9 | import java.util.ArrayList; 10 | import java.util.HashMap; 11 | import java.util.List; 12 | import java.util.Map; 13 | 14 | /** 15 | * @Auther: curry 16 | * @Date: 2018/6/8 22:50 17 | * @Description: 18 | */ 19 | @RestController 20 | public class AreaTreeController { 21 | 22 | @Resource 23 | private AreaTreeMapper areaTreeMapper; 24 | 25 | 26 | @GetMapping(value = "/areaTree") 27 | public List getTree(){ 28 | return areaTreeMapper.getAreaTree(); 29 | 30 | } 31 | @GetMapping(value = "/area") 32 | public List get(){ 33 | List s = new ArrayList<>(); 34 | 35 | List res= new ArrayList<>(); 36 | Map> childMap = new HashMap<>(16); 37 | 38 | AreaTreeEntity a = new AreaTreeEntity(); 39 | a.setId("1"); 40 | a.setName("山东"); 41 | a.setPid("0"); 42 | s.add(a); 43 | 44 | AreaTreeEntity a1 = new AreaTreeEntity(); 45 | a1.setId("2"); 46 | a1.setName("济南"); 47 | a1.setPid("1"); 48 | s.add(a1); 49 | 50 | AreaTreeEntity a2 = new AreaTreeEntity(); 51 | a2.setId("3"); 52 | a2.setName("高新区"); 53 | a2.setPid("2"); 54 | s.add(a2); 55 | 56 | for(AreaTreeEntity entity : s){ 57 | 58 | if ("0".equals(entity.getPid())) { 59 | 60 | res.add(entity); 61 | } 62 | else { 63 | 64 | List childList = (childMap.containsKey(entity.getPid())) ? childMap.get(entity.getPid()) : new ArrayList<>(); 65 | childList.add(entity); 66 | 67 | if (!childMap.containsKey(entity.getPid())){ 68 | childMap.put(entity.getPid(),childList); 69 | } 70 | } 71 | } 72 | 73 | for(AreaTreeEntity entity : res){ 74 | findChild(entity,childMap); 75 | } 76 | 77 | return res; 78 | } 79 | 80 | public void findChild(AreaTreeEntity entity,Map> childMap){ 81 | 82 | if (childMap.containsKey(entity.getId())){ 83 | List chidList = childMap.get(entity.getId()); 84 | for (AreaTreeEntity childEntity : chidList){ 85 | findChild(childEntity,childMap); 86 | } 87 | entity.setChildren(chidList); 88 | } 89 | } 90 | } 91 | 92 | -------------------------------------------------------------------------------- /springboot(九)/mybatis/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.zhb 7 | mybatis 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | mybatis 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 2.0.2.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-web 31 | 32 | 33 | 34 | mysql 35 | mysql-connector-java 36 | runtime 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-test 41 | test 42 | 43 | 44 | org.mybatis.spring.boot 45 | mybatis-spring-boot-starter 46 | 1.3.1 47 | 48 | 49 | org.apache.commons 50 | commons-lang3 51 | 3.6 52 | 53 | 54 | io.springfox 55 | springfox-swagger2 56 | 2.8.0 57 | 58 | 59 | 60 | io.springfox 61 | springfox-swagger-ui 62 | 2.8.0 63 | 64 | 65 | 66 | 67 | 68 | 69 | org.springframework.boot 70 | spring-boot-maven-plugin 71 | 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /springboot(九)/mybatis/src/main/resources/mybatis/mapper/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | id, userName, passWord, user_sex, nick_name 14 | 15 | 16 | 17 | 18 | and userName = #{userName} 19 | 20 | 21 | and user_sex = #{userSex} 22 | 23 | 24 | 25 | 30 | 31 | 39 | 40 | 47 | 48 | 54 | 55 | 56 | INSERT INTO 57 | users 58 | (userName,passWord,user_sex) 59 | VALUES 60 | (#{userName}, #{passWord}, #{userSex}) 61 | 62 | 63 | 64 | UPDATE 65 | users 66 | SET 67 | userName = #{userName}, 68 | passWord = #{passWord}, 69 | nick_name = #{nickName} 70 | WHERE 71 | id = #{id} 72 | 73 | 74 | 75 | DELETE FROM 76 | users 77 | WHERE 78 | id =#{id} 79 | 80 | 81 | -------------------------------------------------------------------------------- /springboot(十三)/pdf/pdf_demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.kevin 7 | pdf_demo 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | pdf_demo 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 2.1.0.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-freemarker 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-test 36 | test 37 | 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-starter-web 42 | 2.5.12 43 | compile 44 | 45 | 46 | 47 | 48 | com.itextpdf 49 | itextpdf 50 | 5.5.13 51 | 52 | 53 | 54 | 55 | com.itextpdf.tool 56 | xmlworker 57 | 5.5.13 58 | 59 | 60 | 61 | org.xhtmlrenderer 62 | core-renderer 63 | R8 64 | 65 | 66 | 67 | 68 | commons-io 69 | commons-io 70 | 2.7 71 | 72 | 73 | 74 | 75 | 76 | 77 | org.springframework.boot 78 | spring-boot-maven-plugin 79 | 80 | 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /springboot(二)/SpringBoot(二)_项目属性配置.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | #### 修改端口 4 | 5 | 在main/resources/application.properties修改端口 6 | ``` 7 | server.port=8088 8 | ``` 9 | 此时启动访问localhost:8088/hello 就会看到 10 | ``` 11 | Hello Spring Boot! 12 | ``` 13 | 14 | #### 使用yml文件替换properties 文件 15 | 16 | (1)在main/resources 文件下新建一个application.yml 文件 17 | 18 | (2)在yml文件中修改端口 19 | ``` 20 | server: 21 | port: 8099 22 | ``` 23 | (3) 删除掉application.properties文件,只保留yml文件 24 | 25 | (4) 运行程序,此时访问8099端口即可 26 | 27 | #### 获取配置文件的值 28 | (1) 在application.yml 文件中,编写上其他内容 29 | ``` 30 | server: 31 | port: 8099 32 | name: maomao 33 | age: 18 34 | ``` 35 | (2) 利用@Value 注解 36 | ``` 37 | @RestController 38 | public class HelloController { 39 | 40 | @Value("${name}") 41 | private String name; 42 | 43 | @Value("${age}") 44 | private int age; 45 | @RequestMapping(value = {"/hello"},method = RequestMethod.GET) 46 | public String say(){ 47 | return name+age; 48 | } 49 | } 50 | ``` 51 | (3)访问8099端口,就获取到值 52 | ``` 53 | maomao 18 54 | ``` 55 | #### 使用自定义配置类 56 | 57 | > 如果属性很多,我们每个属性都需要写,显得有些费事,我们可以利用自定义配置类进行获取 58 | 59 | (1) 修改yml 文件 60 | ``` 61 | server: 62 | port: 8099 63 | girl: 64 | name: maomao 65 | age: 18 66 | 67 | ``` 68 | (2) 创建properties/GirlProperties.java 69 | ``` 70 | @Component 71 | @ConfigurationProperties(prefix = "girl") 72 | public class GirlProperties { 73 | 74 | private String name; 75 | 76 | private int age; 77 | 78 | //get和set方法省略 79 | } 80 | 81 | ``` 82 | (3) 我在使用@ConfigurationProperties 这个注解时,提示找不到class,需在pom文件中引入 83 | 84 | ``` 85 | 86 | org.springframework.boot 87 | spring-boot-configuration-processor 88 | true 89 | 90 | ``` 91 | 92 | (4) 修改controller文件 93 | 94 | ``` 95 | @RestController 96 | public class HelloController { 97 | 98 | @Resource 99 | private GirlProperties girlProperties; 100 | @RequestMapping(value = {"/hello"},method = RequestMethod.GET) 101 | public String say(){ 102 | return girlProperties.getName(); 103 | } 104 | } 105 | ``` 106 | (5)验证结果 107 | ``` 108 | maomao 109 | ``` 110 | #### 开发环境和生成环境配置不同的问题 111 | 112 | > 这个问题经常见,比如我们开发环境 name 是maomao ,生成环境是 毛毛,我们大部分都是修改配置文件,但是这样还是很麻烦。 113 | 114 | (1) 复制2个yml文件,分别是application-dev.yml (开发环境) application-prod.yml(生产环境) 115 | 116 | (2) 修改application-prod.yml(生产环境)文件 117 | 118 | ``` 119 | server: 120 | port: 8088 121 | girl: 122 | name: 毛毛 123 | age: 18 124 | 125 | ``` 126 | (3) application-dev.yml (开发环境)文件内容 127 | ``` 128 | server: 129 | port: 8099 130 | girl: 131 | name: maomao 132 | age: 18 133 | 134 | ``` 135 | (4)application.yml文件内容,这个就代表使用dev的配置文件 136 | 137 | ``` 138 | spring: 139 | profiles: 140 | active: dev 141 | ``` 142 | (5)上篇文章讲过java -jar 的启动方式 143 | 144 | - 先执行 mvn install 145 | - 在执行启动 java -jar girl-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod 146 | 147 | (6)此时就是访问的prod 的配置8088,(注意我们配置的application.yml 中用的是dev 这个配置文件,但是我们启动的时候加上后面的参数就自动切换到 prod 文件上) 148 | 149 | 150 | #### 总结 151 | 152 | 在使用yml进行配置更加简单方便,使用java -jar 启动 加上参数,就可以避免我们来回修改配置文件,有漏掉的情况。 153 | 154 | 源码下载:[github](https://github.com/runzhenghengbin/SpringBoot.git) -------------------------------------------------------------------------------- /springboot(十一)/SpringBoot(十一)_springboot上传excel并读取数据.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | #### 修改端口 4 | 5 | 在main/resources/application.properties修改端口 6 | ``` 7 | server.port=8088 8 | ``` 9 | 此时启动访问localhost:8088/hello 就会看到 10 | ``` 11 | Hello Spring Boot! 12 | ``` 13 | 14 | #### 使用yml文件替换properties 文件 15 | 16 | (1)在main/resources 文件下新建一个application.yml 文件 17 | 18 | (2)在yml文件中修改端口 19 | ``` 20 | server: 21 | port: 8099 22 | ``` 23 | (3) 删除掉application.properties文件,只保留yml文件 24 | 25 | (4) 运行程序,此时访问8099端口即可 26 | 27 | #### 获取配置文件的值 28 | (1) 在application.yml 文件中,编写上其他内容 29 | ``` 30 | server: 31 | port: 8099 32 | name: maomao 33 | age: 18 34 | ``` 35 | (2) 利用@Value 注解 36 | ``` 37 | @RestController 38 | public class HelloController { 39 | 40 | @Value("${name}") 41 | private String name; 42 | 43 | @Value("${age}") 44 | private int age; 45 | @RequestMapping(value = {"/hello"},method = RequestMethod.GET) 46 | public String say(){ 47 | return name+age; 48 | } 49 | } 50 | ``` 51 | (3)访问8099端口,就获取到值 52 | ``` 53 | maomao 18 54 | ``` 55 | #### 使用自定义配置类 56 | 57 | > 如果属性很多,我们每个属性都需要写,显得有些费事,我们可以利用自定义配置类进行获取 58 | 59 | (1) 修改yml 文件 60 | ``` 61 | server: 62 | port: 8099 63 | girl: 64 | name: maomao 65 | age: 18 66 | 67 | ``` 68 | (2) 创建properties/GirlProperties.java 69 | ``` 70 | @Component 71 | @ConfigurationProperties(prefix = "girl") 72 | public class GirlProperties { 73 | 74 | private String name; 75 | 76 | private int age; 77 | 78 | //get和set方法省略 79 | } 80 | 81 | ``` 82 | (3) 我在使用@ConfigurationProperties 这个注解时,提示找不到class,需在pom文件中引入 83 | 84 | ``` 85 | 86 | org.springframework.boot 87 | spring-boot-configuration-processor 88 | true 89 | 90 | ``` 91 | 92 | (4) 修改controller文件 93 | 94 | ``` 95 | @RestController 96 | public class HelloController { 97 | 98 | @Resource 99 | private GirlProperties girlProperties; 100 | @RequestMapping(value = {"/hello"},method = RequestMethod.GET) 101 | public String say(){ 102 | return girlProperties.getName(); 103 | } 104 | } 105 | ``` 106 | (5)验证结果 107 | ``` 108 | maomao 109 | ``` 110 | #### 开发环境和生成环境配置不同的问题 111 | 112 | > 这个问题经常见,比如我们开发环境 name 是maomao ,生成环境是 毛毛,我们大部分都是修改配置文件,但是这样还是很麻烦。 113 | 114 | (1) 复制2个yml文件,分别是application-dev.yml (开发环境) application-prod.yml(生产环境) 115 | 116 | (2) 修改application-prod.yml(生产环境)文件 117 | 118 | ``` 119 | server: 120 | port: 8088 121 | girl: 122 | name: 毛毛 123 | age: 18 124 | 125 | ``` 126 | (3) application-dev.yml (开发环境)文件内容 127 | ``` 128 | server: 129 | port: 8099 130 | girl: 131 | name: maomao 132 | age: 18 133 | 134 | ``` 135 | (4)application.yml文件内容,这个就代表使用dev的配置文件 136 | 137 | ``` 138 | spring: 139 | profiles: 140 | active: dev 141 | ``` 142 | (5)上篇文章讲过java -jar 的启动方式 143 | 144 | - 先执行 mvn install 145 | - 在执行启动 java -jar girl-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod 146 | 147 | (6)此时就是访问的prod 的配置8088,(注意我们配置的application.yml 中用的是dev 这个配置文件,但是我们启动的时候加上后面的参数就自动切换到 prod 文件上) 148 | 149 | 150 | #### 总结 151 | 152 | 在使用yml进行配置更加简单方便,使用java -jar 启动 加上参数,就可以避免我们来回修改配置文件,有漏掉的情况。 153 | 154 | 源码下载:[github](https://github.com/runzhenghengbin/SpringBoot.git) -------------------------------------------------------------------------------- /springboot(一)/SpringBoot(一)_快速实战搭建项目.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | > 现在在学习springboot 相关的知识,感觉真的很好用,用idea 进行开发,根据慕课网和纯洁的微笑的课程。进行总结下。 4 | 5 | ##### 使用idea创建springboot项目 6 | 7 | (1)单击 File | New | Project… 命令,弹出新建项目框。 8 | 9 | (2)选择 Spring Initializr 选项,单击 Next 按钮,也会出现上述类似的配置界面,Idea 帮我们做了集成。 10 | 11 | ![enter image description here](http://p9d3cdwaf.bkt.clouddn.com/TIM%E6%88%AA%E5%9B%BE20180524062212.png) 12 | 13 | (3)选择web,这里我选择的版本是2.0.2,单击 Next 按钮,最后确定信息无误单击 Finish 按钮。 14 | 15 | ![enter image description here](http://p9d3cdwaf.bkt.clouddn.com/TIM%E6%88%AA%E5%9B%BE20180524062558.png) 16 | 17 | (4)删除无用的文件 18 | 19 | ![enter image description here](http://p9d3cdwaf.bkt.clouddn.com/TIM%E6%88%AA%E5%9B%BE20180524062727.png) 20 | 21 | ##### 项目结构 22 | - src/main/java:程序开发以及主程序入口 23 | - src/main/resources:配置文件 24 | - src/test/java:测试程序 25 | 26 | ##### 简单web实战 27 | 28 | (1) 创建controller 类 29 | 30 | ``` 31 | @RestController 32 | public class HelloController { 33 | @RequestMapping(value = {"/hello"},method = RequestMethod.GET) 34 | public String say(){ 35 | return "Hello Spring Boot!"; 36 | } 37 | } 38 | ``` 39 | (2)启动主程序,打开浏览器访问 http://localhost:8080/hello,就可以看到以下内容 40 | ``` 41 | Hello Spring Boot! 42 | ``` 43 | ##### 三种启动方式 44 | 45 | (1)启动主程序进行启动 46 | 47 | (2)进入工程目录, mvn spring-boot:run 48 | 49 | ![enter image description here](http://p9d3cdwaf.bkt.clouddn.com/TIM%E6%88%AA%E5%9B%BE20180524064821.png) 50 | 51 | (3) jar包启动 52 | 53 | - 执行mvn install 54 | ![enter image description here](http://p9d3cdwaf.bkt.clouddn.com/TIM%E6%88%AA%E5%9B%BE20180524064906.png) 55 | - 执行 java -jar 56 | ![enter image description here](http://p9d3cdwaf.bkt.clouddn.com/TIM%E6%88%AA%E5%9B%BE20180524065026.png) 57 | 58 | ##### 单元测试 59 | 在 Spring Boot 中,Spring 给出了一个简单的解决方案;使用 mockmvc 进行 web 测试,mockmvc 内置了很多工具类和方法,可以模拟 post、get 请求,并且判断返回的结果是否正确等,也可以利用print()打印执行结果。 60 | 61 | ``` 62 | @SpringBootTest 63 | public class GirlApplicationTests { 64 | 65 | private MockMvc mockMvc; 66 | 67 | @Before 68 | public void setup(){ 69 | mockMvc = MockMvcBuilders.standaloneSetup( new HelloController()).build(); 70 | } 71 | @Test 72 | public void contextLoads() throws Exception { 73 | mockMvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON_UTF8)).andDo(print()); 74 | } 75 | 76 | } 77 | ``` 78 | 运行结果 79 | 80 | ``` 81 | MockHttpServletRequest: 82 | HTTP Method = GET 83 | Request URI = /hello 84 | Parameters = {} 85 | Headers = {Accept=[application/json;charset=UTF-8]} 86 | Body = 87 | Session Attrs = {} 88 | 89 | Handler: 90 | Type = com.imooc.controller.HelloController 91 | Method = public java.lang.String com.imooc.controller.HelloController.say() 92 | 93 | ... 94 | MockHttpServletResponse: 95 | Status = 200 96 | Error message = null 97 | Headers = {Content-Type=[application/json;charset=UTF-8], Content-Length=[18]} 98 | Content type = application/json;charset=UTF-8 99 | Body = Hello Spring Boot! 100 | Forwarded URL = null 101 | Redirected URL = null 102 | Cookies = [] 103 | ``` 104 | 105 | ##### 总结 106 | 使用 Spring Boot 可以非常方便、快速搭建项目,而不用关心框架之间的兼容性、适用版本等各种问题,我们想使用任何东西,仅仅添加一个配置就可以,所以使用 Sping Boot 非常适合构建微服务。 -------------------------------------------------------------------------------- /springboot(十六)/es-job1/es-job/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.0.1.RELEASE 9 | 10 | 11 | com.kevin 12 | es-job 13 | 0.0.1-SNAPSHOT 14 | es-job 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 2.1.4 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-test 31 | test 32 | 33 | 34 | 35 | 36 | com.dangdang 37 | elastic-job-lite-core 38 | ${elastic-job.version} 39 | 40 | 41 | com.dangdang 42 | elastic-job-lite-spring 43 | ${elastic-job.version} 44 | 45 | 46 | 47 | 48 | 49 | org.springframework.boot 50 | spring-boot-starter-web 51 | 52 | 53 | org.springframework.boot 54 | spring-boot-starter-actuator 55 | 56 | 57 | org.springframework.boot 58 | spring-boot-configuration-processor 59 | true 60 | 61 | 62 | 63 | org.springframework.boot 64 | spring-boot-starter-jdbc 65 | 66 | 67 | 68 | mysql 69 | mysql-connector-java 70 | 71 | 72 | org.springframework.boot 73 | spring-boot-starter-data-jpa 74 | 75 | 76 | 77 | 78 | 79 | 80 | org.springframework.boot 81 | spring-boot-maven-plugin 82 | 83 | 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /springboot(十三)/pdf/pdf_demo/src/main/java/com/kevin/pdf_demo/controller/B64ImgReplacedElementFactory.java: -------------------------------------------------------------------------------- 1 | package com.kevin.pdf_demo.controller; 2 | 3 | 4 | 5 | import java.io.IOException ; 6 | import org.w3c.dom.Element ; 7 | import org.xhtmlrenderer.extend.FSImage ; 8 | import org.xhtmlrenderer.extend.ReplacedElement ; 9 | import org.xhtmlrenderer.extend.ReplacedElementFactory ; 10 | import org.xhtmlrenderer.extend.UserAgentCallback ; 11 | import org.xhtmlrenderer.layout.LayoutContext ; 12 | import org.xhtmlrenderer.pdf.ITextFSImage ; 13 | import org.xhtmlrenderer.pdf.ITextImageElement ; 14 | import org.xhtmlrenderer.render.BlockBox ; 15 | import org.xhtmlrenderer.simple.extend.FormSubmissionListener ; 16 | import com.lowagie.text.BadElementException ; 17 | import com.lowagie.text.Image ; 18 | import com.lowagie.text.pdf.codec.Base64 ; 19 | 20 | 21 | 22 | public class B64ImgReplacedElementFactory implements ReplacedElementFactory { 23 | 24 | /** 25 | * 实现createReplacedElement 替换html中的Img标签 26 | * 27 | * @param c 上下文 28 | * @param box 盒子 29 | * @param uac 回调 30 | * @param cssWidth css宽 31 | * @param cssHeight css高 32 | * @return ReplacedElement 33 | */ 34 | public ReplacedElement createReplacedElement(LayoutContext c, BlockBox box, UserAgentCallback uac, 35 | int cssWidth, int cssHeight) { 36 | Element e = box.getElement(); 37 | if (e == null) { 38 | return null; 39 | } 40 | String nodeName = e.getNodeName(); 41 | // 找到img标签 42 | if (nodeName.equals("img")) { 43 | String attribute = e.getAttribute("src"); 44 | FSImage fsImage; 45 | try { 46 | // 生成itext图像 47 | fsImage = buildImage(attribute, uac); 48 | } catch (BadElementException e1) { 49 | fsImage = null; 50 | } catch (IOException e1) { 51 | fsImage = null; 52 | } 53 | if (fsImage != null) { 54 | // 对图像进行缩放 55 | if (cssWidth != -1 || cssHeight != -1) { 56 | fsImage.scale(cssWidth, cssHeight); 57 | } 58 | return new ITextImageElement(fsImage); 59 | } 60 | } 61 | 62 | return null; 63 | } 64 | 65 | /** 66 | * 将base64编码解码并生成itext图像 67 | * 68 | * @param srcAttr 属性 69 | * @param uac 回调 70 | * @return FSImage 71 | * @throws IOException io异常 72 | * @throws BadElementException BadElementException 73 | */ 74 | protected FSImage buildImage(String srcAttr, UserAgentCallback uac) throws IOException, 75 | BadElementException { 76 | FSImage fsImage; 77 | if (srcAttr.startsWith("data:image/")) { 78 | String b64encoded = srcAttr.substring(srcAttr.indexOf("base64,") + "base64,".length(), 79 | srcAttr.length()); 80 | // 解码 81 | byte[] decodedBytes = Base64.decode(b64encoded); 82 | 83 | fsImage = new ITextFSImage(Image.getInstance(decodedBytes)); 84 | } else { 85 | fsImage = uac.getImageResource(srcAttr).getImage(); 86 | } 87 | return fsImage; 88 | } 89 | 90 | 91 | /** 92 | * 实现reset 93 | */ 94 | public void reset() { 95 | } 96 | 97 | 98 | @Override 99 | public void remove(Element arg0) {} 100 | 101 | @Override 102 | public void setFormSubmissionListener(FormSubmissionListener arg0) {} 103 | } -------------------------------------------------------------------------------- /springboot(六)/SpringBoot(六)_AOP统一处理请求.md: -------------------------------------------------------------------------------- 1 | 2 | ##### 什么是AOP 3 | 4 | - AOP 是一种编程范式,与编程语言无关; 5 | - 将通用逻辑从业务逻辑中分离出来(假如你的业务是一条线,我们不在业务线上写一行代码就能完成附加任务!我们会把代码写在其他的地方); 6 | 7 | ##### 具体实现 8 | 9 | (1) 引入依赖 10 | 11 | ``` 12 | 13 | org.springframework.boot 14 | spring-boot-starter-aop 15 | 16 | ``` 17 | (2) 创建HttpAspect.java 文件 18 | - 类上加入@Aspect @Component 注解 19 | - 使用@Pointcut 定义一个公共的方法,定义切哪个点 20 | - @Before @After @AfterReturning 这三个注解是切的时间点 21 | - 使用org.slf4j.Logger 进行日志记录 22 | ``` 23 | package com.imooc.aspect; 24 | 25 | import org.aspectj.lang.JoinPoint; 26 | import org.aspectj.lang.annotation.*; 27 | import org.slf4j.Logger; 28 | import org.slf4j.LoggerFactory; 29 | import org.springframework.stereotype.Component; 30 | import org.springframework.web.context.request.RequestContextHolder; 31 | import org.springframework.web.context.request.ServletRequestAttributes; 32 | import org.springframework.web.servlet.support.RequestContext; 33 | 34 | import javax.servlet.http.HttpServletRequest; 35 | 36 | /** 37 | * @Auther: curry 38 | * @Date: 2018/6/2 13:45 39 | * @Description: 40 | */ 41 | @Aspect 42 | @Component 43 | public class HttpAspect { 44 | 45 | private final static Logger logger = LoggerFactory.getLogger(HttpAspect.class); 46 | 47 | @Pointcut("execution( public * com.imooc.controller.GirlController.*(..))") 48 | public void log(){ 49 | 50 | } 51 | 52 | @Before("log()") 53 | public void doBefore(JoinPoint joinPoint){ 54 | ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); 55 | HttpServletRequest request = attributes.getRequest(); 56 | // url 57 | logger.info("url={}",request.getRequestURL()); 58 | //method 59 | logger.info("method = {}",request.getMethod()); 60 | //ip 61 | logger.info("ip = {}",request.getRemoteAddr()); 62 | //类方法 63 | logger.info("class_method={}",joinPoint.getSignature().getDeclaringTypeName()+"."+ joinPoint.getSignature().getName()); 64 | //参数 65 | logger.info("args = {}",joinPoint.getArgs()); 66 | 67 | } 68 | 69 | 70 | @After("log()") 71 | public void doAfter(){ 72 | 73 | } 74 | 75 | @AfterReturning(pointcut = "log()",returning = "object") 76 | public void doAfterReturning(Object object){ 77 | logger.info("response = {}",object); 78 | } 79 | } 80 | 81 | ``` 82 | 83 | (3)进行测试 84 | 85 | 86 | ``` 87 | //前面省略 以下代码 88 | 2018-06-02 19:53:17.874 INFO 10088 --- [nio-8099-exec-1] com.imooc.aspect.HttpAspect 89 | 90 | : url=http://localhost:8099/girls 91 | : method = POST 92 | : ip = 0:0:0:0:0:0:0:1 93 | : class_method=com.imooc.controller.GirlController.girlAdd 94 | : args = Girl{id=0, name='maomao', age=7} 95 | : response = com.imooc.entity.Result@6a84c72f 96 | ``` 97 | 98 | ##### 小彩蛋 99 | > springboot 支持打印自定义banner,只要在resources 下面新建一个banner.txt 文件 100 | 101 | 文件内容,每次启动,就会显示下面的图,是不是很酷 102 | ``` 103 | _ooOoo_ 104 | o8888888o 105 | 88" . "88 106 | (| -_- |) 107 | O\ = /O 108 | ____/`---'\____ 109 | .' \\| |// `. 110 | / \\||| : |||// \ 111 | / _||||| -:- |||||- \ 112 | | | \\\ - /// | | 113 | | \_| ''\---/'' | | 114 | \ .-\__ `-` ___/-. / 115 | ___`. .' /--.--\ `. . __ 116 | ."" '< `.___\_<|>_/___.' >'"". 117 | | | : `- \`.;`\ _ /`;.`/ - ` : | | 118 | \ \ `-. \_ __\ /__ _/ .-` / / 119 | ======`-.____`-.___\_____/___.-`____.-'====== 120 | `=---=' 121 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 122 | 佛祖保佑 永无BUG 123 | ``` -------------------------------------------------------------------------------- /springboot(四)/SpringBoot(四)_SpringDataJPA的使用.md: -------------------------------------------------------------------------------- 1 | 2 | > JPA 绝对是简化数据库操作的一大利器。 3 | 4 | #### 概念 5 | ##### 首先了解 JPA 是什么? 6 | 7 | JPA(Java Persistence API)是 Sun 官方提出的 Java 持久化规范。它为 Java 开发人员提供了一种对象/关联映射工具来管理 Java 应用中的关系数据。 8 | 9 | > 注意:JPA 是一套规范,不是一套产品,那么像 Hibernate、TopLink、JDO 它们是一套产品,如果说这些产品实现了这个 JPA 规范,那么就可以叫它们为 JPA 的实现产品。 10 | 11 | #### 实例操作 12 | 13 | ##### 添加依赖 14 | ``` 15 | 16 | org.springframework.boot 17 | spring-boot-starter-data-jpa 18 | 19 | 20 | mysql 21 | mysql-connector-java 22 | 23 | ``` 24 | 25 | ##### 配置文件 26 | application.yml 27 | ``` 28 | spring: 29 | profiles: 30 | active: dev 31 | datasource: 32 | driver-class-name: com.mysql.jdbc.Driver 33 | url: jdbc:mysql://127.0.0.1:3306/dbgirl?characterEncoding=utf8&useSSL=false 34 | username: root 35 | password: root 36 | jpa: 37 | hibernate: 38 | ddl-auto: update 39 | show-sql: true 40 | ``` 41 | 其实这个 hibernate.hbm2ddl.auto 参数的作用主要用于:自动创建 | 更新 | 验证数据库表结构,有四个值: 42 | 43 | > create:每次加载 hibernate 时都会删除上一次的生成的表,然后根据 model 类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这就是导致数据库表数据丢失的一个重要原因。 44 | 45 | > create-drop:每次加载 hibernate 时根据 model 类生成表,但是 sessionFactory 一关闭,表就自动删除。 46 | 47 | > update:最常用的属性,第一次加载 hibernate 时根据 model 类会自动建立起表的结构(前提是先建立好数据库),以后加载 hibernate 时根据 model 类自动更新表结构,即使表结构改变了,但表中的行仍然存在,不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等应用第一次运行起来后才会。 48 | 49 | > validate:每次加载 hibernate 时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。 50 | 51 | 52 | 53 | ##### 实体类和 Dao 54 | 55 | ``` 56 | @Entity 57 | public class Girl { 58 | 59 | 60 | @Id 61 | @GeneratedValue 62 | private int id; 63 | 64 | private String name; 65 | 66 | private int age; 67 | 68 | //添加无参构造 69 | public Girl() { 70 | } 71 | // get和set方法省略 72 | } 73 | 74 | ``` 75 | Dao 只要继承 JpaRepository 类,不需要写方法 76 | 77 | ``` 78 | public interface GirlRepository extends JpaRepository { 79 | 80 | } 81 | ``` 82 | ##### GirlController类 83 | 84 | ``` 85 | @RestController 86 | public class GirlController { 87 | 88 | @Resource 89 | private GirlRepository girlRepository; 90 | 91 | @GetMapping("/girls") 92 | public List getList(){ 93 | return girlRepository.findAll(); 94 | } 95 | 96 | @PostMapping("/girls") 97 | public Girl girlAdd(@RequestParam("name") String name, 98 | @RequestParam("age") int age){ 99 | 100 | Girl girl = new Girl(); 101 | girl.setAge(age); 102 | girl.setName(name); 103 | return girlRepository.save(girl); 104 | 105 | } 106 | 107 | @GetMapping(value = "/girls/{id}") 108 | public Girl find(@PathVariable(value = "id") Integer id){ 109 | return girlRepository.findById(id).get(); 110 | } 111 | 112 | @PostMapping(value = "/girls/{id}") 113 | public Girl update(@PathVariable(value = "id") Integer id, 114 | @RequestParam("name") String name, 115 | @RequestParam("age") int age){ 116 | Girl girl = new Girl(); 117 | girl.setId(id); 118 | girl.setAge(age); 119 | girl.setName(name); 120 | return girlRepository.save(girl); 121 | 122 | } 123 | 124 | @DeleteMapping(value = "/girls/{id}") 125 | public void delete(@PathVariable(value = "id") Integer id){ 126 | girlRepository.deleteById(id); 127 | } 128 | 129 | @GetMapping(value = "/girls/age/{age}") 130 | public List findByAge(@PathVariable(value = "age") Integer age){ 131 | 132 | return girlRepository.findByAge(age); 133 | } 134 | } 135 | 136 | ``` 137 | 138 | 注意: 139 | - 数据库建库dbgirl 140 | - 我使用的是springboot2.0.2版本,已经没有findOne 方法,使用findById(id).get() 来获得 141 | - 测试大家可以使用谷歌浏览器插件Restlet Client - REST API Testing 142 | - 源码下载:[github](https://github.com/runzhenghengbin/SpringBoot) 143 | -------------------------------------------------------------------------------- /springboot(十三)/pdf/pdf_demo/src/main/java/com/kevin/pdf_demo/controller/PdfController.java: -------------------------------------------------------------------------------- 1 | package com.kevin.pdf_demo.controller; 2 | 3 | import com.itextpdf.text.Document; 4 | import com.itextpdf.text.DocumentException; 5 | 6 | import com.itextpdf.text.pdf.BaseFont; 7 | import com.itextpdf.tool.xml.XMLWorkerFontProvider; 8 | import com.itextpdf.tool.xml.XMLWorkerHelper; 9 | import com.lowagie.text.Image; 10 | import com.lowagie.text.pdf.PdfContentByte; 11 | import com.lowagie.text.pdf.PdfWriter; 12 | import freemarker.template.Configuration; 13 | import freemarker.template.Template; 14 | import freemarker.template.TemplateException; 15 | import org.springframework.beans.factory.annotation.Value; 16 | import org.springframework.web.bind.annotation.RequestMapping; 17 | import org.springframework.web.bind.annotation.RestController; 18 | import org.xhtmlrenderer.layout.SharedContext; 19 | import org.xhtmlrenderer.pdf.ITextFontResolver; 20 | import org.xhtmlrenderer.pdf.ITextRenderer; 21 | 22 | import javax.annotation.Resource; 23 | import javax.servlet.ServletOutputStream; 24 | import javax.servlet.http.HttpServletResponse; 25 | import java.io.*; 26 | import java.nio.charset.Charset; 27 | import java.util.HashMap; 28 | import java.util.Map; 29 | 30 | /** 31 | * @author: kevin 32 | * @Date: 2018/11/16 33 | */ 34 | @RestController 35 | public class PdfController { 36 | @Value("${HTML}") 37 | private String html; 38 | @Value("${FONT}") 39 | private String font; 40 | @Resource 41 | private Configuration freemarkerConfig; 42 | 43 | 44 | @RequestMapping(value = "helloPdf") 45 | public void showPdf(HttpServletResponse response) throws IOException, DocumentException, TemplateException, com.lowagie.text.DocumentException { 46 | //需要填充的数据 47 | Map data = new HashMap<>(16); 48 | data.put("name", "kevin"); 49 | 50 | String content = freeMarkerRender(data, html); 51 | response.reset(); 52 | response.setContentType("application/pdf"); 53 | response.addHeader("Content-Disposition", "inline;filename=" + new String("filename.pdf".getBytes("gbk"), "ISO8859-1")); 54 | //创建pdf 55 | ServletOutputStream out = response.getOutputStream(); 56 | createPdf(content, out); 57 | out.flush(); 58 | } 59 | 60 | public void createPdf(String content, OutputStream dest) throws IOException, com.lowagie.text.DocumentException { 61 | // // step 1 62 | // Document document = new Document(); 63 | // // step 2 64 | // PdfWriter writer = PdfWriter.getInstance(document, dest); 65 | // // step 3 66 | // document.open(); 67 | // // step 4 68 | // XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS); 69 | // fontImp.register(font); 70 | // XMLWorkerHelper.getInstance().parseXHtml(writer, document, 71 | // new ByteArrayInputStream(content.getBytes("UTF-8")), null, Charset.forName("UTF-8"), fontImp); 72 | // step 5 73 | ITextRenderer renderer = new ITextRenderer(); 74 | ITextFontResolver fontResolver = renderer.getFontResolver(); 75 | fontResolver.addFont(font, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 76 | 77 | SharedContext sharedContext = renderer.getSharedContext(); 78 | // 解决base64图片支持问题 79 | sharedContext.setReplacedElementFactory(new B64ImgReplacedElementFactory()); 80 | sharedContext.getTextRenderer().setSmoothingThreshold(0); 81 | renderer.setDocumentFromString(content); 82 | renderer.layout(); 83 | renderer.createPDF(dest); 84 | 85 | 86 | 87 | 88 | 89 | 90 | // document.close(); 91 | 92 | } 93 | 94 | /** 95 | * freemarker渲染html 96 | */ 97 | public String freeMarkerRender(Map data, String htmlTmp) throws IOException, TemplateException { 98 | Writer out = new StringWriter(); 99 | try { 100 | Template template = freemarkerConfig.getTemplate(htmlTmp); 101 | //将合并后的数据和模板写入到流中,这里使用的字符流 102 | template.process(data, out); 103 | out.flush(); 104 | System.out.println(out.toString()); 105 | return out.toString(); 106 | } finally { 107 | out.close(); 108 | } 109 | } 110 | 111 | 112 | } 113 | -------------------------------------------------------------------------------- /springboot(八)/SpringBoot(八)_springboot集成swagger2.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | > swagger是一个功能强大的api框架,它的集成非常简单,不仅提供了在线文档的查阅,而且还提供了在线文档的测试。 4 | 5 | (1) 引入依赖,我们选择现在最新的版本 6 | 7 | ``` 8 | 9 | io.springfox 10 | springfox-swagger2 11 | 2.8.0 12 | 13 | 14 | 15 | io.springfox 16 | springfox-swagger-ui 17 | 2.8.0 18 | 19 | ``` 20 | 21 | (2) 写配置类 22 | 23 | ``` 24 | /** 25 | * @Auther: curry 26 | * @Date: 2018/6/3 12:46 27 | * @Description: 28 | */ 29 | @Configuration 30 | @EnableSwagger2 31 | public class SwaggerProperties { 32 | @Bean 33 | public Docket createRestApi() { 34 | return new Docket(DocumentationType.SWAGGER_2) 35 | .apiInfo(apiInfo()) 36 | .select() 37 | .apis(RequestHandlerSelectors.basePackage("com.imooc.controller")) 38 | .paths(PathSelectors.any()) 39 | .build(); 40 | } 41 | private ApiInfo apiInfo() { 42 | return new ApiInfoBuilder() 43 | .title("springboot利用swagger构建api文档") 44 | .description("") 45 | .termsOfServiceUrl("") 46 | .version("1.0") 47 | .build(); 48 | } 49 | } 50 | 51 | ``` 52 | (3) 在controller 中进行引入注解(当然也可以不写) 53 | 54 | 55 | ``` 56 | 57 | /** 58 | * @Auther: curry 59 | * @Date: 2018/5/28 21:57 60 | * @Description: 61 | */ 62 | @RestController 63 | public class GirlController { 64 | private final static Logger logger = LoggerFactory.getLogger(GirlController.class); 65 | @Resource 66 | private GirlRepository girlRepository; 67 | 68 | @Resource 69 | private GirlService girlService; 70 | 71 | @ApiOperation(value="获取女孩列表", notes="获取女孩列表") 72 | @GetMapping("/girls") 73 | public List getList(){ 74 | logger.info("getList"); 75 | return girlRepository.findAll(); 76 | } 77 | 78 | @ApiOperation(value = "添加女孩" ,notes="添加女孩") 79 | @PostMapping("/girls") 80 | public Result girlAdd(@Valid Girl girl, BindingResult bindingResult){ 81 | if(bindingResult.hasErrors()){ 82 | return ResultUtil.error(1,bindingResult.getFieldError().getDefaultMessage()); 83 | } 84 | return ResultUtil.success(girlRepository.save(girl)); 85 | 86 | } 87 | 88 | @ApiOperation(value = "查找女孩",notes = "查找女孩") 89 | @ApiImplicitParam(name = "id" ,value ="查找女孩" ,required = true,dataType = "int",paramType = "path") 90 | @GetMapping(value = "/girls/{id}") 91 | public Girl find(@PathVariable(value = "id") Integer id){ 92 | return girlRepository.findById(id).get(); 93 | } 94 | 95 | 96 | @ApiOperation(value = "修改女孩",notes = "根据id查找女孩并修改") 97 | @PostMapping(value = "/girls/{id}") 98 | public Girl update(@PathVariable(value = "id") Integer id, 99 | @RequestParam("name") String name, 100 | @RequestParam("age") int age){ 101 | Girl girl = new Girl(); 102 | girl.setId(id); 103 | girl.setAge(age); 104 | girl.setName(name); 105 | return girlRepository.save(girl); 106 | 107 | } 108 | 109 | @ApiOperation(value = "删除女孩",notes = "根据id删除女孩") 110 | @DeleteMapping(value = "/girls/{id}") 111 | public void delete(@PathVariable(value = "id") Integer id){ 112 | girlRepository.deleteById(id); 113 | } 114 | 115 | @ApiOperation(value = "根据年龄查询女孩") 116 | @GetMapping(value = "/girls/age/{age}") 117 | public List findByAge(@PathVariable(value = "age") Integer age){ 118 | return girlRepository.findByAge(age); 119 | } 120 | 121 | 122 | 123 | @GetMapping(value = "/girls/getAge/{id}") 124 | public void getAge(@PathVariable("id") Integer id) throws Exception { 125 | girlService.getAge(id); 126 | } 127 | } 128 | 129 | ``` 130 | 131 | (4) 访问http://localhost:8099/swagger-ui.html 132 | ![enter image description here](http://p7zk4x9pv.bkt.clouddn.com/TIM%E6%88%AA%E5%9B%BE20180603172418.png) 133 | ![enter image description here](http://p7zk4x9pv.bkt.clouddn.com/TIM%E6%88%AA%E5%9B%BE20180603172621.png) 134 | 135 | (5)当然,你也可以不在controller 中增加这个注解,是不是感觉很方便,很能测试,just do it! -------------------------------------------------------------------------------- /springboot(十六)/es-job1/es-job/src/main/java/com/kevin/task/config/MySimpleJobConfig.java: -------------------------------------------------------------------------------- 1 | package com.kevin.task.config; 2 | 3 | import com.dangdang.ddframe.job.api.simple.SimpleJob; 4 | import com.dangdang.ddframe.job.config.JobCoreConfiguration; 5 | import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration; 6 | import com.dangdang.ddframe.job.event.JobEventConfiguration; 7 | import com.dangdang.ddframe.job.lite.api.JobScheduler; 8 | import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration; 9 | import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler; 10 | import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter; 11 | import com.kevin.task.listener.SimpleJobListener; 12 | import com.kevin.task.task.MySimpleJob; 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.beans.factory.annotation.Value; 15 | import org.springframework.context.annotation.Bean; 16 | import org.springframework.context.annotation.Configuration; 17 | 18 | /** 19 | * @author: kevin 20 | * @Date: 2020/1/22 21 | */ 22 | @Configuration 23 | public class MySimpleJobConfig { 24 | @Autowired 25 | private ZookeeperRegistryCenter registryCenter; 26 | 27 | @Autowired 28 | private JobEventConfiguration jobEventConfiguration; 29 | 30 | /** 31 | * 具体真正的定时任务执行逻辑 32 | * @return 33 | */ 34 | @Bean 35 | public SimpleJob simpleJob() { 36 | return new MySimpleJob(); 37 | } 38 | 39 | /** 40 | * @param simpleJob 41 | * @return 42 | */ 43 | @Bean(initMethod = "init") 44 | public JobScheduler simpleJobScheduler(final SimpleJob simpleJob, 45 | @Value("${simpleJob.cron}") final String cron, 46 | @Value("${simpleJob.shardingTotalCount}") final int shardingTotalCount, 47 | @Value("${simpleJob.shardingItemParameters}") final String shardingItemParameters, 48 | @Value("${simpleJob.jobParameter}") final String jobParameter, 49 | @Value("${simpleJob.failover}") final boolean failover, 50 | @Value("${simpleJob.monitorExecution}") final boolean monitorExecution, 51 | @Value("${simpleJob.monitorPort}") final int monitorPort, 52 | @Value("${simpleJob.maxTimeDiffSeconds}") final int maxTimeDiffSeconds, 53 | @Value("${simpleJob.jobShardingStrategyClass}") final String jobShardingStrategyClass) { 54 | 55 | return new SpringJobScheduler(simpleJob, 56 | registryCenter, 57 | getLiteJobConfiguration(simpleJob.getClass(), 58 | cron, 59 | shardingTotalCount, 60 | shardingItemParameters, 61 | jobParameter, 62 | failover, 63 | monitorExecution, 64 | monitorPort, 65 | maxTimeDiffSeconds, 66 | jobShardingStrategyClass), 67 | jobEventConfiguration, 68 | new SimpleJobListener()); 69 | 70 | } 71 | 72 | 73 | private LiteJobConfiguration getLiteJobConfiguration(Class jobClass, String cron, 74 | int shardingTotalCount, String shardingItemParameters, String jobParameter, boolean failover, 75 | boolean monitorExecution, int monitorPort, int maxTimeDiffSeconds, String jobShardingStrategyClass) { 76 | 77 | JobCoreConfiguration jobCoreConfiguration = JobCoreConfiguration 78 | .newBuilder(jobClass.getName(), cron, shardingTotalCount) 79 | .misfire(true) 80 | .failover(failover) 81 | .jobParameter(jobParameter) 82 | .shardingItemParameters(shardingItemParameters) 83 | .build(); 84 | 85 | SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(jobCoreConfiguration, jobClass.getCanonicalName()); 86 | 87 | LiteJobConfiguration liteJobConfiguration = LiteJobConfiguration.newBuilder(simpleJobConfiguration) 88 | .jobShardingStrategyClass(jobShardingStrategyClass) 89 | .monitorExecution(monitorExecution) 90 | .monitorPort(monitorPort) 91 | .maxTimeDiffSeconds(maxTimeDiffSeconds) 92 | .overwrite(true) 93 | .build(); 94 | 95 | return liteJobConfiguration; 96 | } 97 | 98 | 99 | 100 | 101 | 102 | 103 | } 104 | 105 | -------------------------------------------------------------------------------- /springboot(十六)/es-job1/es-job/.mvn/wrapper/MavenWrapperDownloader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2007-present the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import java.net.*; 18 | import java.io.*; 19 | import java.nio.channels.*; 20 | import java.util.Properties; 21 | 22 | public class MavenWrapperDownloader { 23 | 24 | private static final String WRAPPER_VERSION = "0.5.6"; 25 | /** 26 | * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. 27 | */ 28 | private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" 29 | + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; 30 | 31 | /** 32 | * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to 33 | * use instead of the default one. 34 | */ 35 | private static final String MAVEN_WRAPPER_PROPERTIES_PATH = 36 | ".mvn/wrapper/maven-wrapper.properties"; 37 | 38 | /** 39 | * Path where the maven-wrapper.jar will be saved to. 40 | */ 41 | private static final String MAVEN_WRAPPER_JAR_PATH = 42 | ".mvn/wrapper/maven-wrapper.jar"; 43 | 44 | /** 45 | * Name of the property which should be used to override the default download url for the wrapper. 46 | */ 47 | private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; 48 | 49 | public static void main(String args[]) { 50 | System.out.println("- Downloader started"); 51 | File baseDirectory = new File(args[0]); 52 | System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); 53 | 54 | // If the maven-wrapper.properties exists, read it and check if it contains a custom 55 | // wrapperUrl parameter. 56 | File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); 57 | String url = DEFAULT_DOWNLOAD_URL; 58 | if (mavenWrapperPropertyFile.exists()) { 59 | FileInputStream mavenWrapperPropertyFileInputStream = null; 60 | try { 61 | mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); 62 | Properties mavenWrapperProperties = new Properties(); 63 | mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); 64 | url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); 65 | } catch (IOException e) { 66 | System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); 67 | } finally { 68 | try { 69 | if (mavenWrapperPropertyFileInputStream != null) { 70 | mavenWrapperPropertyFileInputStream.close(); 71 | } 72 | } catch (IOException e) { 73 | // Ignore ... 74 | } 75 | } 76 | } 77 | System.out.println("- Downloading from: " + url); 78 | 79 | File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); 80 | if (!outputFile.getParentFile().exists()) { 81 | if (!outputFile.getParentFile().mkdirs()) { 82 | System.out.println( 83 | "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); 84 | } 85 | } 86 | System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); 87 | try { 88 | downloadFileFromURL(url, outputFile); 89 | System.out.println("Done"); 90 | System.exit(0); 91 | } catch (Throwable e) { 92 | System.out.println("- Error downloading"); 93 | e.printStackTrace(); 94 | System.exit(1); 95 | } 96 | } 97 | 98 | private static void downloadFileFromURL(String urlString, File destination) throws Exception { 99 | if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { 100 | String username = System.getenv("MVNW_USERNAME"); 101 | char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); 102 | Authenticator.setDefault(new Authenticator() { 103 | @Override 104 | protected PasswordAuthentication getPasswordAuthentication() { 105 | return new PasswordAuthentication(username, password); 106 | } 107 | }); 108 | } 109 | URL website = new URL(urlString); 110 | ReadableByteChannel rbc; 111 | rbc = Channels.newChannel(website.openStream()); 112 | FileOutputStream fos = new FileOutputStream(destination); 113 | fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); 114 | fos.close(); 115 | rbc.close(); 116 | } 117 | 118 | } 119 | -------------------------------------------------------------------------------- /springboot(十四)/springboot整合JasperReport6.6.0.md: -------------------------------------------------------------------------------- 1 | > 现在项目上要求实现套打,结果公司里有个人建议用JaperReport进行实现,就进入这个东西的坑中。好歹经过挣扎现在已经脱离此坑中。现在我也是仅能实现读取数据库数据转成pdf进行展示,包括中文的展示。于是记录下整个过程。 2 | 3 | ### 1.下载 安装 Jaspersoft Studio 4 | 5 | 下载地址:https://community.jaspersoft.com/community-download 6 | 7 | ![](https://img2018.cnblogs.com/blog/891580/201901/891580-20190114182413464-839905257.png) 8 | 9 | 10 | 我下载的就是6.6.0这个版本,Jasper Report 分为专业版(收费)和社区版(免费),这里下载的社区版本。 11 | 12 | ### 2.设计模板 13 | 14 | 对这个模板设计我也不是很熟悉,这里我就不展开说明了。大家自行设计吧 15 | 16 | #### 2.1 导入并设置字体 17 | 18 | 这里需要注意一点就是,这个设计出的不显示中文,需要导入字体。 19 | 20 | 点击window->Preferences->jaspersoft Studio->font->add 21 | 22 | ![](https://img2018.cnblogs.com/blog/891580/201901/891580-20190114191800124-2112897974.png) 23 | 24 | 25 | 26 | 27 | 设置完成后,点击Finish. 28 | 29 | #### 2.2 设计模板选择设置的myfont字体 30 | 31 | 查看jrxml文件后,设置后会多出`font`标签 32 | 33 | ``` 34 | 35 | 36 | 37 | ``` 38 | #### 2.3 导出myfont字体jar包 39 | 40 | 点击window->Preferences->jaspersoft Studio->font 41 | 42 | 选择myfont 点击export 导出。这里我保存为kevin.jar 43 | 44 | #### 2.4 将kevin.jar 安装到本地maven库 45 | 46 | ``` 47 | mvn install:install-file -Dfile=kevin.jar -DgroupId=com.kevin -DartifactId=myfont -Dversion=1.0.0 -Dpackaging=jar 48 | ``` 49 | 50 | 至此,前期的准备工作都已经完成。 51 | 52 | ### 3.构建springboot项目 53 | 这里我使用的版本是 `2.1.1.RELEASE` 54 | 55 | #### 3.1 pom文件 56 | 57 | ``` 58 | 59 | 60 | org.springframework.boot 61 | spring-boot-starter-jdbc 62 | 63 | 64 | org.springframework.boot 65 | spring-boot-starter-web 66 | 67 | 68 | org.springframework.boot 69 | spring-boot-starter-freemarker 70 | 71 | 72 | mysql 73 | mysql-connector-java 74 | runtime 75 | 76 | 77 | org.springframework.boot 78 | spring-boot-starter-test 79 | test 80 | 81 | 82 | 83 | net.sf.jasperreports 84 | jasperreports 85 | 6.6.0 86 | 87 | 88 | com.kevin 89 | myfont 90 | 1.0.0 91 | 92 | 93 | ``` 94 | 95 | **注意 ,我这里引用的是我设置的`com.kevin.myfont`版本,个人根据自己步骤2.4设置的情况进行修改** 96 | 97 | #### 3.2 application.yml 98 | 99 | ``` 100 | # Server settings 101 | server: 102 | port: 8080 103 | 104 | # SPRING PROFILES 105 | spring: 106 | http: 107 | encoding.charset: UTF-8 108 | encoding.enable: true 109 | encoding.force: true 110 | datasource: 111 | url: jdbc:mysql://127.0.0.1:3306/kevin?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8 112 | username: root 113 | password: 123456 114 | driver-class-name: com.mysql.jdbc.Driver 115 | 116 | 117 | ``` 118 | 119 | #### 3.3 ReportController 代码 120 | 121 | ``` 122 | @RestController 123 | public class ReportController { 124 | 125 | @Resource 126 | private DataSource dataSource; 127 | 128 | 129 | /** 130 | * 转换为pdf展示 131 | * 132 | * @param reportName 133 | * @param parameters 134 | * @param response 135 | * @throws SQLException 136 | * @throws ClassNotFoundException 137 | * @throws JRException 138 | * @throws IOException 139 | */ 140 | @GetMapping("/{reportName}") 141 | public void getReportByParam( 142 | @PathVariable("reportName") final String reportName, 143 | @RequestParam(required = false) Map parameters, 144 | HttpServletResponse response) throws SQLException, ClassNotFoundException, JRException, IOException { 145 | 146 | parameters = parameters == null ? new HashMap<>() : parameters; 147 | //获取文件流 148 | ClassPathResource resource = new ClassPathResource("jaspers" + File.separator + reportName + ".jasper"); 149 | InputStream jasperStream = resource.getInputStream(); 150 | 151 | JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream); 152 | JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource.getConnection()); 153 | // JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, new JREmptyDataSource()); 154 | response.setContentType("application/pdf"); 155 | response.setHeader("Content-Disposition", "inline;"); 156 | final OutputStream outputStream = response.getOutputStream(); 157 | JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream); 158 | } 159 | } 160 | ``` 161 | 162 | #### 3.4 完整目录结构 163 | 164 | ![](https://img2018.cnblogs.com/blog/891580/201901/891580-20190114185908162-419343926.png) 165 | 166 | 167 | 168 | ### 4.运行效果 169 | 170 | 启动项目,运行`http://localhost:8080/demo` 171 | 172 | ![](https://img2018.cnblogs.com/blog/891580/201901/891580-20190114190035597-1779703786.png) 173 | 174 | ### 完整代码 175 | 176 | github:https://github.com/runzhenghengbin/SpringBoot 177 | 178 | 玩的开心! -------------------------------------------------------------------------------- /springboot(十)/redis-demo-01/src/test/java/com/zhb/RedisDemo01ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.zhb; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.data.redis.core.*; 7 | import org.springframework.test.context.junit4.SpringRunner; 8 | 9 | import javax.annotation.Resource; 10 | import java.util.List; 11 | import java.util.Set; 12 | import java.util.concurrent.TimeUnit; 13 | 14 | @RunWith(SpringRunner.class) 15 | @SpringBootTest 16 | public class RedisDemo01ApplicationTests { 17 | 18 | 19 | @Resource 20 | private RedisTemplate redisTemplate; 21 | 22 | 23 | @Test 24 | public void contextLoads() { 25 | } 26 | 27 | /** 28 | * 测试存储String 29 | */ 30 | @Test 31 | public void testString(){ 32 | redisTemplate.opsForValue().set("maomao", "hello"); 33 | System.out.println(redisTemplate.opsForValue().get("maomao").toString()); 34 | } 35 | 36 | /** 37 | * 测试超时失效 38 | * @throws InterruptedException 39 | */ 40 | @Test 41 | public void testExpire() throws InterruptedException { 42 | ValueOperations operations=redisTemplate.opsForValue(); 43 | operations.set("expire", "java",100,TimeUnit.MILLISECONDS); 44 | Thread.sleep(1000); 45 | boolean exists=redisTemplate.hasKey("expire"); 46 | if(exists){ 47 | System.out.println("exists is true"); 48 | }else{ 49 | System.out.println("exists is false"); 50 | } 51 | } 52 | 53 | /** 54 | * 删除数据 55 | */ 56 | @Test 57 | public void testDelete() { 58 | ValueOperations operations=redisTemplate.opsForValue(); 59 | operations.set("deletekey", "springboot"); 60 | redisTemplate.delete("deletekey"); 61 | //判断key是否还在 62 | boolean exists=redisTemplate.hasKey("deletekey"); 63 | if(exists){ 64 | System.out.println("exists is true"); 65 | }else{ 66 | System.out.println("exists is false"); 67 | } 68 | } 69 | 70 | /** 71 | * hash 72 | */ 73 | @Test 74 | public void testHash() { 75 | HashOperations hash = redisTemplate.opsForHash(); 76 | hash.put("hash","hello","java"); 77 | String value=(String) hash.get("hash","hello"); 78 | System.out.println("hash value :"+value); 79 | } 80 | 81 | @Test 82 | public void testListPop() { 83 | String key="list"; 84 | redisTemplate.delete(key); 85 | ListOperations list = redisTemplate.opsForList(); 86 | list.leftPush(key,"just"); 87 | list.leftPush(key,"do"); 88 | list.leftPush(key,"it"); 89 | String value=list.leftPop(key); 90 | System.out.println("list value :"+value); 91 | } 92 | 93 | @Test 94 | public void testListRange() { 95 | String key="list"; 96 | redisTemplate.delete(key); 97 | ListOperations list = redisTemplate.opsForList(); 98 | list.rightPush(key,"just"); 99 | list.rightPush(key,"do"); 100 | list.rightPush(key,"it"); 101 | 102 | List values=list.range(key,0,3); 103 | for (String v:values){ 104 | System.out.println("list range :"+v); 105 | } 106 | } 107 | 108 | @Test 109 | public void testSet(){ 110 | String key = "set"; 111 | redisTemplate.delete(key); 112 | SetOperations set = redisTemplate.opsForSet(); 113 | set.add(key,"just"); 114 | set.add(key,"do"); 115 | set.add(key,"do"); 116 | set.add(key,"it"); 117 | Set values = set.members(key); 118 | for (String value:values){ 119 | System.out.println("set value :"+value); 120 | } 121 | } 122 | 123 | @Test 124 | public void testSetMore() { 125 | SetOperations set = redisTemplate.opsForSet(); 126 | String key1="setMore1"; 127 | String key2="setMore2"; 128 | 129 | set.add(key1,"just"); 130 | set.add(key1,"do"); 131 | set.add(key1,"do"); 132 | set.add(key1,"it"); 133 | 134 | set.add(key2,"java"); 135 | set.add(key2,"do"); 136 | 137 | Set diffs=set.difference(key1,key2); 138 | for (String v:diffs){ 139 | System.out.println("diffs set value :"+v); 140 | } 141 | 142 | String key3="setMore3"; 143 | String key4="setMore4"; 144 | set.add(key3,"just"); 145 | set.add(key3,"do"); 146 | set.add(key3,"java"); 147 | set.add(key4,"it"); 148 | set.add(key4,"do"); 149 | set.add(key4,"hello"); 150 | Set unions=set.union(key3,key4); 151 | for (String v:unions){ 152 | System.out.println("unions value :"+v); 153 | } 154 | } 155 | 156 | @Test 157 | public void testZset(){ 158 | String key="zset"; 159 | redisTemplate.delete(key); 160 | ZSetOperations zset = redisTemplate.opsForZSet(); 161 | zset.add(key,"just",1); 162 | zset.add(key,"now",5); 163 | zset.add(key,"it",4); 164 | zset.add(key,"do",3); 165 | 166 | Set zsets=zset.range(key,0,2); 167 | for (String v:zsets){ 168 | System.out.println("zset value :"+v); 169 | } 170 | 171 | Set zsetB=zset.rangeByScore(key,0,3); 172 | for (String v:zsetB){ 173 | System.out.println("zsetB value :"+v); 174 | } 175 | } 176 | 177 | } 178 | -------------------------------------------------------------------------------- /springboot(十)/redis-demo-01/src/main/java/com/zhb/service/RedisService.java: -------------------------------------------------------------------------------- 1 | package com.zhb.service; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.data.redis.core.*; 7 | import org.springframework.stereotype.Service; 8 | 9 | import java.io.Serializable; 10 | import java.util.List; 11 | import java.util.Set; 12 | import java.util.concurrent.TimeUnit; 13 | 14 | /** 15 | * @Auther: curry 16 | * @Date: 2018/6/20 11:01 17 | * @Description: 18 | */ 19 | @Service 20 | public class RedisService { 21 | private Logger logger = LoggerFactory.getLogger(RedisService.class); 22 | @Autowired 23 | private RedisTemplate redisTemplate; 24 | 25 | /** 26 | * set value 27 | * @param key 28 | * @param value 29 | * @return 30 | */ 31 | public boolean set(final String key, Object value) { 32 | boolean result = false; 33 | try { 34 | ValueOperations operations = redisTemplate.opsForValue(); 35 | operations.set(key, value); 36 | result = true; 37 | } catch (Exception e) { 38 | logger.error("set error: key {}, value {}",key,value,e); 39 | } 40 | return result; 41 | } 42 | 43 | /** 44 | * set value with expireTime 45 | * @param key 46 | * @param value 47 | * @param expireTime 48 | * @return 49 | */ 50 | public boolean set(final String key, Object value, Long expireTime) { 51 | boolean result = false; 52 | try { 53 | ValueOperations operations = redisTemplate.opsForValue(); 54 | operations.set(key, value); 55 | redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); 56 | result = true; 57 | } catch (Exception e) { 58 | logger.error("set error: key {}, value {},expireTime {}",key,value,expireTime,e); 59 | } 60 | return result; 61 | } 62 | 63 | /** 64 | * @param key 65 | * @return 66 | */ 67 | public boolean exists(final String key) { 68 | return redisTemplate.hasKey(key); 69 | } 70 | 71 | /** 72 | * @param key 73 | * @return 74 | */ 75 | public Object get(final String key) { 76 | Object result = null; 77 | ValueOperations operations = redisTemplate.opsForValue(); 78 | result = operations.get(key); 79 | return result; 80 | } 81 | 82 | /** 83 | * remove single key 84 | * @param key 85 | */ 86 | public void remove(final String key) { 87 | if (exists(key)) { 88 | redisTemplate.delete(key); 89 | } 90 | } 91 | 92 | /** 93 | * batch delete 94 | * @param keys 95 | */ 96 | public void remove(final String... keys) { 97 | for (String key : keys) { 98 | remove(key); 99 | } 100 | } 101 | 102 | /** 103 | * batch delete with pattern 104 | * @param pattern 105 | */ 106 | public void removePattern(final String pattern) { 107 | Set keys = redisTemplate.keys(pattern); 108 | if (keys.size() > 0) 109 | redisTemplate.delete(keys); 110 | } 111 | 112 | /** 113 | * hash set 114 | * @param key 115 | * @param hashKey 116 | * @param value 117 | */ 118 | public void hashSet(String key, Object hashKey, Object value){ 119 | HashOperations hash = redisTemplate.opsForHash(); 120 | hash.put(key,hashKey,value); 121 | } 122 | 123 | /** 124 | * hash get 125 | * @param key 126 | * @param hashKey 127 | * @return 128 | */ 129 | public Object hashGet(String key, Object hashKey){ 130 | HashOperations hash = redisTemplate.opsForHash(); 131 | return hash.get(key,hashKey); 132 | } 133 | 134 | /** 135 | * list push 136 | * @param k 137 | * @param v 138 | */ 139 | public void push(String k,Object v){ 140 | ListOperations list = redisTemplate.opsForList(); 141 | list.rightPush(k,v); 142 | } 143 | 144 | /** 145 | * list range 146 | * @param k 147 | * @param l 148 | * @param l1 149 | * @return 150 | */ 151 | public List range(String k, long l, long l1){ 152 | ListOperations list = redisTemplate.opsForList(); 153 | return list.range(k,l,l1); 154 | } 155 | 156 | /** 157 | * set add 158 | * @param key 159 | * @param value 160 | */ 161 | public void setAdd(String key,Object value){ 162 | SetOperations set = redisTemplate.opsForSet(); 163 | set.add(key,value); 164 | } 165 | 166 | /** 167 | * set get 168 | * @param key 169 | * @return 170 | */ 171 | public Set setMembers(String key){ 172 | SetOperations set = redisTemplate.opsForSet(); 173 | return set.members(key); 174 | } 175 | 176 | /** 177 | * ordered set add 178 | * @param key 179 | * @param value 180 | * @param scoure 181 | */ 182 | public void zAdd(String key,Object value,double scoure){ 183 | ZSetOperations zset = redisTemplate.opsForZSet(); 184 | zset.add(key,value,scoure); 185 | } 186 | 187 | /** 188 | * rangeByScore 189 | * @param key 190 | * @param scoure 191 | * @param scoure1 192 | * @return 193 | */ 194 | public Set rangeByScore(String key, double scoure, double scoure1){ 195 | ZSetOperations zset = redisTemplate.opsForZSet(); 196 | return zset.rangeByScore(key, scoure, scoure1); 197 | } 198 | } 199 | -------------------------------------------------------------------------------- /springboot(九)/SpringBoot(九)_springboot集成MyBatis.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | > MyBatis 是一款标准的 ORM 框架,被广泛的应用于各企业开发中。具体细节这里就不在叙述,大家自行查找资料进行学习下。 6 | 7 | #### 加载依赖 8 | 9 | ``` 10 | 11 | org.mybatis.spring.boot 12 | mybatis-spring-boot-starter 13 | 1.3.1 14 | 15 | ``` 16 | #### application 配置(application.yml) 17 | 18 | ``` 19 | mybatis: 20 | config-location: classpath:mybatis/mybatis-config.xml 21 | mapper-locations: classpath:mybatis/mapper/*.xml 22 | type-aliases-package: com.zhb.entity 23 | spring: 24 | datasource: 25 | driver-class-name: com.mysql.jdbc.Driver 26 | url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8 27 | password: root 28 | username: root 29 | ``` 30 | 31 | #### 启动类 32 | 在启动类中添加对 Mapper 包扫描@MapperScan,Spring Boot 启动的时候会自动加载包路径下的 Mapper。 33 | 34 | ``` 35 | @Spring BootApplication 36 | @MapperScan("com.zhb.mapper") 37 | public class Application { 38 | 39 | public static void main(String[] args) { 40 | SpringApplication.run(Application.class, args); 41 | } 42 | } 43 | ``` 44 | 或者直接在 Mapper 类上面添加注解@Mapper,建议使用上面那种,不然每个 Mapper 加个注解会很麻烦。 45 | 46 | #### 代码展示 47 | 这里只说下,我们在controller 中 尽量使用 Restful 风格 48 | 49 | ``` 50 | @RestController 51 | public class UserController { 52 | 53 | @Resource 54 | private UserMapper userMapper; 55 | 56 | 57 | @GetMapping(value = "/users") 58 | public List getUsers() { 59 | List users=userMapper.getAll(); 60 | return users; 61 | } 62 | 63 | 64 | 65 | @GetMapping(value = "/users/{id}") 66 | public UserEntity getUser(@PathVariable(value = "id") Long id) { 67 | UserEntity user=userMapper.getOne(id); 68 | return user; 69 | } 70 | 71 | @PostMapping("/users") 72 | public void save(UserEntity user) { 73 | userMapper.insert(user); 74 | } 75 | 76 | @PutMapping("/users") 77 | public void update(UserEntity user) { 78 | userMapper.update(user); 79 | } 80 | 81 | @DeleteMapping(value="/users/{id}") 82 | public void delete(@PathVariable("id") Long id) { 83 | userMapper.delete(id); 84 | } 85 | 86 | 87 | } 88 | ``` 89 | 90 | #### 实际开发常遇问题 91 | 92 | 在平常开发中,我们经常会遇到返回树结构,如下展示 93 | 94 | ``` 95 | [ 96 | { 97 | "id": "1", 98 | "name": "山东", 99 | "pid": "0", 100 | "children": [ 101 | { 102 | "id": "2", 103 | "name": "济南", 104 | "pid": "1", 105 | "children": [ 106 | { 107 | "id": "3", 108 | "name": "高新区", 109 | "pid": "2", 110 | "children": null 111 | } 112 | ] 113 | } 114 | ] 115 | } 116 | ] 117 | ``` 118 | 119 | ##### (1) java中 通过对list数据进行拼接成树 120 | 121 | 如下面的方法(这里直接声明了list,往里面添加数据,来演示查询出的list集合) 122 | 123 | ``` 124 | @GetMapping(value = "/area") 125 | public List get(){ 126 | 127 | List res= new ArrayList<>(); 128 | Map> childMap = new HashMap<>(16); 129 | 130 | //为了方便测试,构建list数据 131 | List s = new ArrayList<>(); 132 | AreaTreeEntity a = new AreaTreeEntity(); 133 | a.setId("1"); 134 | a.setName("山东"); 135 | a.setPid("0"); 136 | s.add(a); 137 | 138 | AreaTreeEntity a1 = new AreaTreeEntity(); 139 | a1.setId("2"); 140 | a1.setName("济南"); 141 | a1.setPid("1"); 142 | s.add(a1); 143 | 144 | AreaTreeEntity a2 = new AreaTreeEntity(); 145 | a2.setId("3"); 146 | a2.setName("高新区"); 147 | a2.setPid("2"); 148 | s.add(a2); 149 | 150 | for(AreaTreeEntity entity : s){ 151 | 152 | if ("0".equals(entity.getPid())) { 153 | 154 | res.add(entity); 155 | } 156 | else { 157 | 158 | List childList = (childMap.containsKey(entity.getPid())) ? childMap.get(entity.getPid()) : new ArrayList<>(); 159 | childList.add(entity); 160 | 161 | if (!childMap.containsKey(entity.getPid())){ 162 | childMap.put(entity.getPid(),childList); 163 | } 164 | } 165 | } 166 | 167 | for(AreaTreeEntity entity : res){ 168 | findChild(entity,childMap); 169 | } 170 | 171 | return res; 172 | } 173 | 174 | public void findChild(AreaTreeEntity entity,Map> childMap){ 175 | 176 | if (childMap.containsKey(entity.getId())){ 177 | List chidList = childMap.get(entity.getId()); 178 | for (AreaTreeEntity childEntity : chidList){ 179 | findChild(childEntity,childMap); 180 | } 181 | entity.setChildren(chidList); 182 | } 183 | } 184 | ``` 185 | 186 | 经过验证,返回数据如下 187 | ``` 188 | [ 189 | { 190 | "id": "1", 191 | "name": "山东", 192 | "pid": "0", 193 | "children": [ 194 | { 195 | "id": "2", 196 | "name": "济南", 197 | "pid": "1", 198 | "children": [ 199 | { 200 | "id": "3", 201 | "name": "高新区", 202 | "pid": "2", 203 | "children": null 204 | } 205 | ] 206 | } 207 | ] 208 | } 209 | ] 210 | ``` 211 | 212 | ##### (2) 使用mabatis的collection 直接查询出树结构 213 | 214 | ``` 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 227 | 228 | 231 | 232 | 233 | ``` 234 | 235 | 完整代码下载:[github](https://github.com/runzhenghengbin/SpringBoot.git) 236 | 237 | 注:项目中加入swagger ,方便大家测试,直接访问 http://localhost:8080/swagger-ui.html -------------------------------------------------------------------------------- /springboot(七)/SpringBoot(七)_统一异常处理.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | > 我感觉看了这节课,给我的思考还是很多的,感觉受益良多。废话不多说,一起学习。 4 | 5 | ##### 统一的 外层结构返回 6 | > 这样利于代码看着也规范,前端处理也统一 7 | 8 | ``` 9 | # 错误返回 10 | { 11 | "code": 1, 12 | "msg": "未成年禁止入内", 13 | "data": null 14 | } 15 | 16 | # 正确返回 17 | { 18 | "code": 0, 19 | "msg": "成功", 20 | "data":{ 21 | "id": 8, 22 | "name": "maomao", 23 | "age": 19 24 | } 25 | } 26 | ``` 27 | 28 | (1) 实现这个我们要定义一个返回结果的实体类 29 | 30 | ``` 31 | package com.imooc.entity; 32 | 33 | /** 34 | * http请求做外层对象 35 | * @Auther: curry 36 | * @Date: 2018/6/2 14:35 37 | * @Description: 38 | */ 39 | public class Result { 40 | 41 | /** 42 | * 状态码 43 | */ 44 | private Integer code; 45 | 46 | /** 47 | * 提示信息 48 | */ 49 | private String msg; 50 | 51 | /** 52 | * 返回数据 53 | */ 54 | private T data; 55 | 56 | //get和set省略 57 | } 58 | 59 | ``` 60 | (2)定义返回结果的工具类 61 | 62 | ``` 63 | package com.imooc.utils; 64 | 65 | import com.imooc.entity.Result; 66 | 67 | /** 68 | * @Auther: curry 69 | * @Date: 2018/6/2 14:39 70 | * @Description: 71 | */ 72 | public class ResultUtil { 73 | 74 | public static Result success(Object object){ 75 | Result result = new Result(); 76 | result.setCode(0); 77 | result.setMsg("成功"); 78 | result.setData(object); 79 | return result; 80 | } 81 | 82 | public static Result success(){ 83 | return success(null); 84 | } 85 | public static Result error(Integer code,String msg){ 86 | Result result = new Result(); 87 | result.setCode(code); 88 | result.setMsg(msg); 89 | return result; 90 | } 91 | } 92 | 93 | ``` 94 | (3) 使用规定的返回结果 95 | > 注意:controller 只是用于请求和参数的传递,业务处理应该在service进行处理。这只是方便演示 96 | 97 | ``` 98 | @PostMapping("/girls") 99 | public Result girlAdd(@Valid Girl girl, BindingResult bindingResult){ 100 | if(bindingResult.hasErrors()){ 101 | return ResultUtil.error(1,bindingResult.getFieldError().getDefaultMessage()); 102 | } 103 | return ResultUtil.success(girlRepository.save(girl)); 104 | 105 | } 106 | ``` 107 | (4) 这样就使我们返回结果如上面所示的一样了 108 | 109 | ##### 定义统一的异常处理 110 | 111 | > 实现业务:获取女生的年龄, 112 | > 如果小于10岁:返回:还在小学 113 | > 如果大于10岁小于16岁:返回:还在初中 114 | 115 | controller层 116 | 117 | ``` 118 | @GetMapping(value = "/girls/getAge/{id}") 119 | public void getAge(@PathVariable("id") Integer id) throws Exception { 120 | girlService.getAge(id); 121 | } 122 | ``` 123 | service层 124 | ``` 125 | @Service 126 | public class GirlService { 127 | @Resource 128 | private GirlRepository girlRepository; 129 | public void getAge(Integer id) throws Exception { 130 | Girl girl = girlRepository.getOne(id); 131 | Integer age = girl.getAge(); 132 | if(age<10){ 133 | throw new Exception("还在小学"); 134 | }else if(age >10 && age< 16){ 135 | throw new Exception("还在初中"); 136 | } 137 | 138 | } 139 | } 140 | 141 | ``` 142 | 监听异常 143 | 144 | ``` 145 | package com.imooc.handle; 146 | 147 | import com.imooc.aspect.HttpAspect; 148 | import com.imooc.entity.Result; 149 | import com.imooc.exception.GirlException; 150 | import com.imooc.utils.ResultUtil; 151 | import org.slf4j.Logger; 152 | import org.slf4j.LoggerFactory; 153 | import org.springframework.web.bind.annotation.ControllerAdvice; 154 | import org.springframework.web.bind.annotation.ExceptionHandler; 155 | import org.springframework.web.bind.annotation.ResponseBody; 156 | 157 | /** 158 | * @Auther: curry 159 | * @Date: 2018/6/2 15:05 160 | * @Description: 161 | */ 162 | @ControllerAdvice 163 | public class ExceptionHandle { 164 | private final static Logger logger = LoggerFactory.getLogger(HttpAspect.class); 165 | @ExceptionHandler(value = Exception.class) 166 | @ResponseBody 167 | public Result handle(Exception e){ 168 | return ResultUtil.error(100,e.getMessage()); 169 | } 170 | } 171 | 172 | ``` 173 | 测试返回结果,控制台不会报错 174 | 175 | ``` 176 | { 177 | "code": 100, 178 | "msg": "还在初中", 179 | "data": null 180 | } 181 | ``` 182 | 183 | ##### 实现自己的Exception 184 | 185 | 创建自己的Exception 186 | > 继承自RuntimeException 是因为 spring 这个框架对运行时异常会进行数据回滚,如果是Exception .则不会 187 | ``` 188 | package com.imooc.exception; 189 | 190 | import com.imooc.enums.ResultEnum; 191 | 192 | /** 193 | * @Auther: curry 194 | * @Date: 2018/6/2 15:30 195 | * @Description: 196 | */ 197 | public class GirlException extends RuntimeException{ 198 | 199 | private Integer code; 200 | 201 | // 这里使用了枚举 202 | public GirlException(ResultEnum resultEnum) { 203 | super(resultEnum.getMsg()); 204 | this.code = resultEnum.getCode(); 205 | } 206 | 207 | public Integer getCode() { 208 | return code; 209 | } 210 | 211 | public void setCode(Integer code) { 212 | this.code = code; 213 | } 214 | } 215 | 216 | ``` 217 | 218 | 创建枚举类 219 | 220 | ``` 221 | package com.imooc.enums; 222 | 223 | /** 224 | * @Auther: curry 225 | * @Date: 2018/6/2 15:46 226 | * @Description: 227 | */ 228 | public enum ResultEnum { 229 | UNKNOW_ERROR(-1,"未知错误"), 230 | SUCCESS(0,"成功"), 231 | PRIMARY_SCHOOL(100,"还在小学"), 232 | MIDDLE_SCHOOL(101,"还在初中"), 233 | 234 | ; 235 | 236 | 237 | private Integer code; 238 | 239 | private String msg; 240 | 241 | ResultEnum(Integer code, String msg) { 242 | this.code = code; 243 | this.msg = msg; 244 | } 245 | 246 | public Integer getCode() { 247 | return code; 248 | } 249 | 250 | 251 | 252 | public String getMsg() { 253 | return msg; 254 | } 255 | 256 | 257 | } 258 | 259 | ``` 260 | 修改异常捕获类 261 | 262 | ``` 263 | public Result handle(Exception e){ 264 | // return ResultUtil.error(100,e.getMessage()); 265 | 266 | if (e instanceof GirlException){ 267 | GirlException girlException = (GirlException)e; 268 | return ResultUtil.error(girlException.getCode(),girlException.getMessage()); 269 | }else { 270 | logger.info("【系统异常】{}",e); 271 | return ResultUtil.error(-1,"未知错误"); 272 | } 273 | } 274 | ``` 275 | 修改service类 276 | 277 | ``` 278 | public void getAge(Integer id) throws Exception { 279 | Girl girl = girlRepository.getOne(id); 280 | Integer age = girl.getAge(); 281 | if(age<10){ 282 | throw new GirlException(ResultEnum.PRIMARY_SCHOOL); 283 | }else if(age >10 && age< 16){ 284 | throw new GirlException(ResultEnum.MIDDLE_SCHOOL); 285 | } 286 | 287 | } 288 | ``` 289 | 测试 290 | 291 | ``` 292 | { 293 | "code": 101, 294 | "msg": "还在初中", 295 | "data": null 296 | } 297 | ``` 298 | 299 | 玩的开心! -------------------------------------------------------------------------------- /springboot(十三)/SpringBoot(十三)_springboot实现预览pdf.md: -------------------------------------------------------------------------------- 1 | > 最近,项目上要做个打印的东西,还要预览。我想就直接生成pdf预览,然后用户选择打印 2 | 3 | 于是,昨天找了找资料。一般用itext 进行转pdf。于是我就用springboot试了试,代码比较简单,现在只是简单的一个实现。 4 | 5 | 整体流程如下 6 | 7 | - 1.获取数据(比如从数据库获取数据) 8 | - 2.通过freemarker 渲染页面 9 | - 3.将渲染的页面进行转换为pdf,放在本地 10 | - 4.读取pdf,实现预览 11 | 12 | ### 主要代码 13 | 14 | #### pom文件 15 | ``` 16 | 17 | 18 | org.springframework.boot 19 | spring-boot-starter-freemarker 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-test 25 | test 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-web 31 | RELEASE 32 | compile 33 | 34 | 35 | 36 | 37 | com.itextpdf 38 | itextpdf 39 | 5.5.13 40 | 41 | 42 | 43 | 44 | com.itextpdf.tool 45 | xmlworker 46 | 5.5.13 47 | 48 | 49 | 50 | 51 | commons-io 52 | commons-io 53 | 2.5 54 | 55 | 56 | ``` 57 | 58 | #### PdfController代码 59 | 60 | ``` 61 | package com.kevin.pdf_demo.controller; 62 | 63 | import com.itextpdf.text.Document; 64 | import com.itextpdf.text.DocumentException; 65 | import com.itextpdf.text.pdf.PdfWriter; 66 | import com.itextpdf.tool.xml.XMLWorkerFontProvider; 67 | import com.itextpdf.tool.xml.XMLWorkerHelper; 68 | import freemarker.template.Configuration; 69 | import freemarker.template.Template; 70 | import org.apache.commons.io.IOUtils; 71 | import org.springframework.beans.factory.annotation.Value; 72 | import org.springframework.core.io.ClassPathResource; 73 | import org.springframework.web.bind.annotation.RequestMapping; 74 | import org.springframework.web.bind.annotation.RestController; 75 | 76 | import javax.servlet.http.HttpServletResponse; 77 | import java.io.*; 78 | import java.nio.charset.Charset; 79 | import java.util.HashMap; 80 | import java.util.Map; 81 | 82 | /** 83 | * @author: kevin 84 | * @Date: 2018/11/16 85 | */ 86 | @RestController 87 | public class PdfController { 88 | @Value("${DEST}") 89 | private String dest; 90 | @Value("${HTML}") 91 | private String html; 92 | @Value("${FONT}") 93 | private String font; 94 | private static Configuration freemarkerCfg = null; 95 | 96 | 97 | @RequestMapping(value = "helloPdf") 98 | public void showPdf(HttpServletResponse response) throws IOException, DocumentException { 99 | //需要填充的数据 100 | Map data = new HashMap<>(16); 101 | data.put("name", "kevin"); 102 | 103 | String content = freeMarkerRender(data,html); 104 | //创建pdf 105 | createPdf(content, dest); 106 | 107 | // 读取pdf并预览 108 | readPdf(response); 109 | 110 | } 111 | 112 | public void createPdf(String content,String dest) throws IOException, DocumentException { 113 | // step 1 114 | Document document = new Document(); 115 | // step 2 116 | PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest)); 117 | // step 3 118 | document.open(); 119 | // step 4 120 | XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS); 121 | fontImp.register(font); 122 | XMLWorkerHelper.getInstance().parseXHtml(writer, document, 123 | new ByteArrayInputStream(content.getBytes("UTF-8")), null, Charset.forName("UTF-8"), fontImp); 124 | // step 5 125 | document.close(); 126 | 127 | } 128 | 129 | /** 130 | * freemarker渲染html 131 | */ 132 | public String freeMarkerRender(Map data, String htmlTmp) { 133 | Writer out = new StringWriter(); 134 | 135 | try { 136 | // 获取模板,并设置编码方式 137 | setFreemarkerCfg(); 138 | Template template = freemarkerCfg.getTemplate(htmlTmp); 139 | template.setEncoding("UTF-8"); 140 | //将合并后的数据和模板写入到流中,这里使用的字符流 141 | template.process(data, out); 142 | out.flush(); 143 | return out.toString(); 144 | } catch (Exception e) { 145 | e.printStackTrace(); 146 | } finally { 147 | try { 148 | out.close(); 149 | } catch (IOException ex) { 150 | ex.printStackTrace(); 151 | } 152 | } 153 | return null; 154 | } 155 | /** 156 | * 设置freemarkerCfg 157 | */ 158 | private void setFreemarkerCfg() { 159 | freemarkerCfg = new Configuration(); 160 | //freemarker的模板目录 161 | try { 162 | freemarkerCfg.setDirectoryForTemplateLoading(new ClassPathResource("template").getFile()); 163 | } catch (IOException e) { 164 | e.printStackTrace(); 165 | } 166 | } 167 | 168 | /** 169 | * 读取本地pdf,这里设置的是预览 170 | */ 171 | private void readPdf(HttpServletResponse response) { 172 | response.reset(); 173 | response.setContentType("application/pdf"); 174 | try { 175 | File file = new File(dest); 176 | FileInputStream fileInputStream = new FileInputStream(file); 177 | OutputStream outputStream = response.getOutputStream(); 178 | IOUtils.write(IOUtils.toByteArray(fileInputStream), outputStream); 179 | response.setHeader("Content-Disposition", 180 | "inline; filename= file"); 181 | outputStream.flush(); 182 | } catch (FileNotFoundException e) { 183 | e.printStackTrace(); 184 | } catch (IOException e) { 185 | e.printStackTrace(); 186 | } 187 | } 188 | 189 | 190 | 191 | } 192 | 193 | ``` 194 | 195 | ### 运行效果 196 | ![](https://img2018.cnblogs.com/blog/891580/201811/891580-20181117163612091-1579251056.png) 197 | 198 | ### 设置头信息使浏览器下载文件或预览 199 | #### 强制浏览器下载 200 | ``` 201 | response.setHeader("content-disposition", "attachment;filename=" + realName); 202 | ``` 203 | #### 浏览器尝试打开,支持office online或浏览器预览pdf功能 204 | ``` 205 | response.setHeader("content-disposition", "inline;filename=" + realName); 206 | ``` 207 | 208 | 完整代码 : [github](https://github.com/runzhenghengbin/SpringBoot) 209 | 210 | 玩的开心! -------------------------------------------------------------------------------- /springboot(十二)/SpringBoot(十二)_springboot整合RabbitMQ.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ### RabbitMQ简介 6 | 7 | AMQP:Advanced Message Queue,高级消息队列协议。它是应用层协议的一个开放标准,为面向消息的中间件设计,基于此协议的客户端与消息中间件可传递消息,并不受产品、开发语言等条件的限制。 8 | 9 | RabbitMQ 是一个由Erlang语言开发的AMQP的开源实现。(PS:前几天有篇文章介绍了阿里P10的淘宝褚霸,就是erlang大神)。支持多种客户端。用于在分布式系统中存储转发消息,在易用性、扩展性、高可用性等方面表现不俗。具体特点如下: 10 | 11 | 1、可靠性 12 | 13 | RabbitMQ 使用一些机制来保证可靠性,如持久化,传输确认、发布确认。 14 | 15 | 2、灵活的路由 16 | 17 | 在消息进入队列之前,通过Exchange来路由消息。对于典型的路由功能,RabbitMQ 提供了内置的Exchange来实现。针对更复杂的路由功能。可以将多个Exchange 绑定在一起,也通过插件机制实现自己的 Exchange。 18 | 19 | 3、消息集群 20 | 21 | 多个RabbitMQ 服务器可以组成一个集群,形成一个逻辑Broker. 22 | 23 | 4、高可用 24 | 25 | 队列可以在集群中的机器上进行镜像,使得在部分节点出问题的情况下队列任然可用。 26 | 27 | 5、多种协议 28 | 29 | RabbitMQ支持多种消息队列协议。比如STOMP、MQTT等等 30 | 31 | 6、多语言客户端 32 | 33 | RabbitMQ支持多种语言,比如java/Ruby等 34 | 35 | 7、管理界面 36 | 37 | RabbitMQ提供了一个易用的用户界面。使得用户可有监控和管理Broker的许多方面 38 | 39 | 8、跟踪机制 40 | 41 | 如果消息异常,RabbitMQ 提供了消息跟踪机制,使用者可以找出 42 | 43 | 9、插件机制 44 | 45 | RabbitMQ提供了许多插件,从多方面进行扩展,也可以编写自己的插件 46 | 47 | ### RabitMQ 概念模型 48 | 49 | ![](http://p7zk4x9pv.bkt.clouddn.com/111.png) 50 | 51 | 1、Message 52 | 53 | 消息,消息是不具名的,它由消息头和消息体组成,消息体是不透明的。而消息头则由一系列的可选属性组成。包括routing-key(路由键)、priority(相对于其他消息的优先权)、delivery-mode(指出该消息可能需要持久性存储)等 54 | 55 | 2、Publisher 56 | 57 | 消息的生产者,也是一个向交换器发布消息的客户端应用程序 58 | 59 | 3、Exchange 60 | 61 | 交换器,*用来接收生产者发送的消息并将这些消息路由给服务器中的队列。* 62 | 63 | 4、Binding 64 | 65 | 绑定,用于消息队列和交换器之间的关联。一个绑定就是基于路由键将交换器和消息队列连接起来的路由规则。所以可以将交换器理解成一个由绑定构成的路由表。 66 | 67 | 5、Queue 68 | 69 | 消息队列,用来保存消息直到发送给消费者。它是消息的容器,也是消息的终点。一个消息可投入一个或多个队列。消息一直在队列里面。等待消息消费者连接到这个队列将其取走。 70 | 71 | 6、Connection 72 | 73 | 网络连接 74 | 75 | 7、Channel 76 | 77 | 信道,多路复用连接中的一条独立的双向数据流通道。 78 | 79 | 8、Consumer 80 | 81 | 消息的消费者,表示一个从消息队列中取得消息的客户端应用程序 82 | 83 | 9、Virtual Host 84 | 85 | 虚拟主机、表示一批交换器、消息队列和相关对象。虚拟主机是共享相同的身份认证和加密环境的独立服务器域。每个vhost 本质上就是一个mini版的RabbitMQ服务器。拥有自己的队列、交换器、绑定、和权限机制。vhost是AMQP概念的基础。必须在连接时指定,RabbitMQ默认的Vhost是/. 86 | 87 | 10、Broker 88 | 89 | 表示消息队列服务器实体。 90 | 91 | ### Exchange 类型 92 | 93 | 94 | 95 | Exchange 有四种类型:Direct、Topic、Headers 和 Fanout 。 96 | 97 | - Direct:该类型的行为是“先匹配,再投送”,即在绑定时设定一个 **routing_key**,消息的**routing_key** 匹配时,才会被交换器投送到绑定的队列中去。 98 | - Topic:按规则转发消息(最灵活)。 99 | - Headers:设置 header attribute 参数类型的交换机。 100 | - Fanout:转发消息到所有绑定队列。 101 | 102 | > headers 交换器和 direct 交换器完全一致,但性能差很多,目前几乎用不到了,这里不再详细介绍 103 | 104 | #### Driect 105 | 106 | ![](http://p7zk4x9pv.bkt.clouddn.com/direct.png) 107 | 108 | 109 | 110 | Direct Exchange 是 RabbitMQ 默认的交换机模式,也是最简单的模式,根据 key 全文匹配去寻找队列。 111 | 112 | 113 | 114 | 消息中的路由键(routing key)如果和 Binding 中的 binding key 一致, 交换器就将消息发到对应的队列中。路由键与队列名完全匹配.它是完全匹配、单播的模式。 115 | 116 | #### Topic 117 | 118 | ![](http://p7zk4x9pv.bkt.clouddn.com/topic.png) 119 | 120 | Topic Exchange 转发消息主要是根据通配符。在这种交换机下,队列和交换机的绑定会定义一种路由模式,那么,通配符就要在这种路由模式和路由键之间匹配后交换机才能转发消息。 121 | 122 | 在这种交换机模式下: 123 | 124 | - 路由键必须是一串字符,用句号(.)隔开,如 agreements.us,或者 agreements.eu.stockholm 等。 125 | 126 | topic 和 direct 类似,只是匹配上支持了“模式”,在“点分”的 `routing_key` 形式中,可以使用两个通配符: 127 | 128 | - `*`表示一个词。 129 | - `#`表示零个或多个词。 130 | 131 | 132 | 133 | 134 | 135 | #### Fanout 136 | 137 | ![](http://p7zk4x9pv.bkt.clouddn.com/fanout.png) 138 | 139 | Fanout Exchange 消息广播的模式,不管路由键或者是路由模式,**会把消息发给绑定给它的全部队列**,如果配置了`routing_key`会被忽略。 140 | 141 | 142 | 143 | ### 代码实战 144 | 145 | 146 | 147 | > 此部分代码基于springboot进行,全部代码放到github上 148 | 149 | Spring Boot 集成 RabbitMQ 非常简单,仅需非常少的配置就可使用,Spring Boot 提供了 spring-boot-starter-amqp 组件对MQ消息支持。 150 | 151 | #### 简单使用 152 | 153 | (1)配置 pom 包,主要是添加 spring-boot-starter-amqp 的支持。 154 | 155 | ``` 156 | 157 | org.springframework.boot 158 | spring-boot-starter-amqp 159 | 160 | ``` 161 | 162 | (2)配置文件,配置 rabbitmq 的安装地址、端口及账户信息。 163 | 164 | ``` 165 | spring.application.name=spirng-boot-rabbitmq 166 | 167 | spring.rabbitmq.host= localhost 168 | spring.rabbitmq.port= 5672 169 | spring.rabbitmq.username=allen 170 | spring.rabbitmq.password=123456 171 | 172 | server.port=8080 173 | ``` 174 | 175 | (3)定义队列 176 | 177 | ``` 178 | @Configuration 179 | public class RabbitConfig { 180 | 181 | @Bean 182 | public Queue Queue() { 183 | return new Queue("hello"); 184 | } 185 | } 186 | ``` 187 | 188 | (4)发送者 189 | 190 | AmqpTemplate 是 Spring Boot 提供的默认实现。 191 | 192 | ``` 193 | @RestController 194 | public class HelloSender { 195 | 196 | @Autowired 197 | private AmqpTemplate amqpTemplate; 198 | 199 | @GetMapping(value = "/sendString") 200 | public String sendString() { 201 | //发送消息 String routingKey, Object object 202 | amqpTemplate.convertAndSend("hello", "hello rabbitMQ"); 203 | return "消息已发送"; 204 | } 205 | 206 | @GetMapping(value = "/sendObject") 207 | public String sendObject() { 208 | //发送消息 String routingKey, Object object 209 | UserEntity userEntity = new UserEntity(); 210 | userEntity.setName("allen"); 211 | userEntity.setAddress("山东济南"); 212 | amqpTemplate.convertAndSend("hello", userEntity); 213 | return "消息已发送"; 214 | } 215 | ``` 216 | 217 | (5)接收者 218 | 219 | 注意使用注解`@RabbitListener`,使用 queues 指明队列名称,`@RabbitHandler`为具体接收的方法。 220 | 221 | ``` 222 | @Component 223 | @RabbitListener(queues = "hello") 224 | @Slf4j 225 | public class HelloReceiver { 226 | 227 | @RabbitHandler 228 | public void process(String hello) { 229 | log.info("消息接受为:{}",hello); 230 | } 231 | 232 | @RabbitHandler 233 | public void process(UserEntity userEntity) { 234 | log.info("消息接受为:{}",userEntity); 235 | } 236 | } 237 | 238 | ``` 239 | 240 | (5)访问结果 241 | 242 | ``` 243 | com.zhb.direct.HelloReceiver : 消息接受为:hello rabbitMQ 244 | com.zhb.direct.HelloReceiver : 消息接受为:UserEntity(name=allen, address=山东济南) 245 | 246 | ``` 247 | 248 | #### Topic Exchange 249 | (1)定义队列、交换机并绑定 250 | 251 | ``` 252 | /** 253 | * @author: curry 254 | * @Date: 2018/9/5 255 | */ 256 | @Configuration 257 | public class TopicRabbitConfig { 258 | 259 | final static String message = "topic.message"; 260 | final static String messages = "topic.messages"; 261 | 262 | //定义队列 263 | @Bean 264 | public Queue queueMessage() { 265 | return new Queue(TopicRabbitConfig.message); 266 | } 267 | 268 | @Bean 269 | public Queue queueMessages() { 270 | return new Queue(TopicRabbitConfig.messages); 271 | } 272 | 273 | //exchange 274 | @Bean 275 | TopicExchange exchange() { 276 | return new TopicExchange("exchange"); 277 | } 278 | 279 | //将队列和交换机进行绑定 280 | @Bean 281 | Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) { 282 | return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message"); 283 | } 284 | 285 | @Bean 286 | Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) { 287 | return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#"); 288 | } 289 | } 290 | ``` 291 | (2)发送者 292 | 293 | ``` 294 | /** 295 | * @author: curry 296 | * @Date: 2018/9/5 297 | */ 298 | @RestController 299 | public class Sender { 300 | @Resource 301 | private AmqpTemplate rabbitTemplate; 302 | 303 | @GetMapping(value = "send1") 304 | public void send1(){ 305 | String context = "hi, i am message1"; 306 | rabbitTemplate.convertAndSend("exchange","topic.message",context); 307 | } 308 | 309 | @GetMapping(value = "send2") 310 | public void send2(){ 311 | String context = "hi, i am message2"; 312 | rabbitTemplate.convertAndSend("exchange","topic.messages",context); 313 | } 314 | 315 | ``` 316 | (3) 接受者 317 | 318 | ``` 319 | /** 320 | * @author: curry 321 | * @Date: 2018/9/5 322 | */ 323 | @Slf4j 324 | @Component 325 | @RabbitListener(queues = "topic.message") 326 | public class Receiver1 { 327 | 328 | @RabbitHandler 329 | public void process(String message) { 330 | log.info("topic Receive1:{}", message); 331 | } 332 | } 333 | 334 | /** 335 | * @author: curry 336 | * @Date: 2018/9/5 337 | */ 338 | @Slf4j 339 | @Component 340 | @RabbitListener(queues = "topic.messages") 341 | public class Receiver2 { 342 | 343 | @RabbitHandler 344 | public void process(String message) { 345 | log.info("topic Receive2:{}", message); 346 | } 347 | } 348 | 349 | ``` 350 | 351 | > 其余代码不再演示,完整代码见github 352 | 353 | 完整代码:[github](https://github.com/runzhenghengbin/SpringBoot) 354 | 355 | 好了,玩的开心! 356 | 357 | --------------------------------------------------------------------------------