├── .gitignore ├── LICENSE ├── README.md ├── pom.xml ├── springboot-aop ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springbootaop │ │ │ ├── ServletInitializer.java │ │ │ ├── SpringbootAopApplication.java │ │ │ ├── controller │ │ │ └── IndexController.java │ │ │ ├── section │ │ │ ├── CommonAspect.java │ │ │ ├── UserAround.java │ │ │ └── UserAspect.java │ │ │ └── service │ │ │ ├── IUserService.java │ │ │ └── impl │ │ │ └── UserService.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── example │ └── springbootaop │ └── SpringbootAopApplicationTests.java ├── springboot-config ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springbootconfig │ │ │ ├── ServletInitializer.java │ │ │ ├── SpringbootConfigApplication.java │ │ │ └── controller │ │ │ ├── Entity.java │ │ │ └── IndexController.java │ └── resources │ │ ├── application-dev.properties │ │ ├── application-prod.properties │ │ └── application.properties │ └── test │ └── java │ └── com │ └── example │ └── springbootconfig │ └── SpringbootConfigApplicationTests.java ├── springboot-dubbo-core ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── example │ └── springbootdubbocore │ ├── dto │ ├── BaseDTO.java │ └── UserVo.java │ └── interfaces │ └── IUserService.java ├── springboot-dubbo-main ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springbootdubbomain │ │ │ ├── ServletInitializer.java │ │ │ ├── SpringbootDubboMainApplication.java │ │ │ ├── config │ │ │ └── DubboCfg.java │ │ │ ├── controller │ │ │ └── IndexController.java │ │ │ └── handler │ │ │ └── IndexHandler.java │ └── resources │ │ ├── application.properties │ │ ├── dubbo │ │ ├── dubbo-service.xml │ │ └── dubbo.properties │ │ └── zookeeper-3.4.13.zip │ └── test │ └── java │ └── com │ └── example │ └── springbootdubbomain │ └── SpringbootDubboMainApplicationTests.java ├── springboot-dubbo-service ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springbootdubboservice │ │ │ ├── SpringbootDubboServiceApplication.java │ │ │ ├── config │ │ │ └── DubboCfg.java │ │ │ └── impl │ │ │ └── UserServiceImpl.java │ └── resources │ │ ├── application.properties │ │ └── dubbo │ │ ├── dubbo-service.xml │ │ └── dubbo.properties │ └── test │ └── java │ └── com │ └── example │ └── springbootdubboservice │ └── SpringbootDubboServiceApplicationTests.java ├── springboot-example.iml ├── springboot-exception ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springbootexception │ │ │ ├── ServletInitializer.java │ │ │ ├── SpringbootExceptionApplication.java │ │ │ ├── controller │ │ │ └── IndexController.java │ │ │ └── error │ │ │ ├── BaseException.java │ │ │ ├── BaseRs.java │ │ │ ├── ErrorController.java │ │ │ └── StatusCodeEnum.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── example │ └── springbootexception │ └── SpringbootExceptionApplicationTests.java ├── springboot-interceptor ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springbootinterceptor │ │ │ ├── ServletInitializer.java │ │ │ ├── SpringbootInterceptorApplication.java │ │ │ ├── controller │ │ │ └── IndexController.java │ │ │ └── interceptor │ │ │ ├── Interceptor.java │ │ │ └── MvcConfigurer.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── example │ └── springbootinterceptor │ └── SpringbootInterceptorApplicationTests.java ├── springboot-ioc ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springbootioc │ │ │ ├── ServletInitializer.java │ │ │ ├── SpringbootIoCApplication.java │ │ │ ├── config │ │ │ ├── Config.java │ │ │ ├── User.java │ │ │ ├── UserComponent.java │ │ │ ├── UserCondition.java │ │ │ ├── UserExclude.java │ │ │ └── UserParam.java │ │ │ ├── controller │ │ │ ├── IndexController.java │ │ │ └── StudentController.java │ │ │ ├── student │ │ │ ├── IStudent.java │ │ │ ├── XiaoGuang.java │ │ │ └── XiaoMing.java │ │ │ ├── student2 │ │ │ ├── IStudent2.java │ │ │ ├── XiaoGuang2.java │ │ │ └── XiaoMing2.java │ │ │ └── student3 │ │ │ ├── IStudent3.java │ │ │ ├── XiaoGuang3.java │ │ │ └── XiaoMing3.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── example │ └── springbootioc │ └── SpringbootBeanApplicationTests.java ├── springboot-jdbc ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springbootjdbc │ │ │ ├── ServletInitializer.java │ │ │ ├── SpringbootJdbcApplication.java │ │ │ ├── controller │ │ │ └── IndexController.java │ │ │ ├── dao │ │ │ └── UserDao.java │ │ │ ├── entity │ │ │ └── User.java │ │ │ ├── enums │ │ │ └── SexEnum.java │ │ │ └── service │ │ │ ├── IUserService.java │ │ │ └── impl │ │ │ └── UserService.java │ └── resources │ │ ├── application.properties │ │ └── jdbc演示数据.sql │ └── test │ └── java │ └── com │ └── example │ └── springbootjdbc │ └── SpringbootJdbcApplicationTests.java ├── springboot-jpa ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springbootjpa │ │ │ ├── ServletInitializer.java │ │ │ ├── SpringbootJpaApplication.java │ │ │ ├── controller │ │ │ └── IndexController.java │ │ │ ├── dao │ │ │ └── UserRepository.java │ │ │ ├── entity │ │ │ └── User.java │ │ │ └── service │ │ │ ├── IUserService.java │ │ │ └── impl │ │ │ ├── UserService.java │ │ │ └── jpa命名关键字.png │ └── resources │ │ ├── application.properties │ │ └── jpa演示数据.sql │ └── test │ └── java │ └── com │ └── example │ └── springbootjpa │ └── SpringbootJpaApplicationTests.java ├── springboot-jwt ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springbootjwt │ │ │ ├── ServletInitializer.java │ │ │ ├── SpringbootJwtApplication.java │ │ │ ├── controller │ │ │ └── IndexController.java │ │ │ ├── entity │ │ │ └── User.java │ │ │ └── system │ │ │ └── JWT.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── example │ └── springbootjwt │ └── SpringbootJwtApplicationTests.java ├── springboot-language ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springbootlanguage │ │ │ ├── ServletInitializer.java │ │ │ ├── SpringbootLanguageApplication.java │ │ │ ├── controller │ │ │ └── IndexController.java │ │ │ └── interceptor │ │ │ └── MvcConfigurer.java │ └── resources │ │ ├── application.properties │ │ ├── messages.properties │ │ ├── messages_en_US.properties │ │ ├── messages_zh_CN.properties │ │ └── templates │ │ └── a.html │ └── test │ └── java │ └── com │ └── example │ └── springbootlanguage │ └── SpringbootLanguageApplicationTests.java ├── springboot-log ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springbootlog │ │ │ ├── ServletInitializer.java │ │ │ ├── SpringbootLogApplication.java │ │ │ ├── common │ │ │ └── LogUtil.java │ │ │ └── controller │ │ │ └── IndexController.java │ └── resources │ │ ├── application.properties │ │ └── logback.xml │ └── test │ └── java │ └── com │ └── example │ └── springbootlog │ └── SpringbootLogApplicationTests.java ├── springboot-mongodb ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springbootmongodb │ │ │ ├── ServletInitializer.java │ │ │ ├── SpringbootMongodbApplication.java │ │ │ ├── controller │ │ │ └── IndexController.java │ │ │ ├── entity │ │ │ └── User.java │ │ │ └── service │ │ │ ├── IUserService.java │ │ │ └── impl │ │ │ └── UserService.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── example │ └── springbootmongodb │ └── SpringbootMongodbApplicationTests.java ├── springboot-mybatis ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springbootmybatis │ │ │ ├── ServletInitializer.java │ │ │ ├── SpringbootMybatisApplication.java │ │ │ ├── controller │ │ │ └── IndexController.java │ │ │ ├── dao │ │ │ └── UserDao.java │ │ │ └── entity │ │ │ └── User.java │ └── resources │ │ ├── application.properties │ │ └── mybatis │ │ ├── mapper │ │ └── UserMapper.xml │ │ └── mybatis-config.xml │ └── test │ └── java │ └── com │ └── example │ └── springbootmybatis │ └── SpringbootMybatisApplicationTests.java ├── springboot-rabbitmq ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springbootrabbitmq │ │ │ ├── ServletInitializer.java │ │ │ ├── SpringbootRabbitmqApplication.java │ │ │ ├── config │ │ │ └── RabbitConf.java │ │ │ ├── controller │ │ │ └── IndexController.java │ │ │ ├── entity │ │ │ └── User.java │ │ │ └── rabbit │ │ │ ├── RabbitMQReceiver.java │ │ │ └── RabbitMqProduce.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── example │ └── springbootrabbitmq │ └── SpringbootRabbitmqApplicationTests.java ├── springboot-redis ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springbootredis │ │ │ ├── ServletInitializer.java │ │ │ ├── SpringbootRedisApplication.java │ │ │ ├── controller │ │ │ └── IndexController.java │ │ │ ├── entity │ │ │ └── User.java │ │ │ └── redis │ │ │ ├── RedisListener.java │ │ │ ├── RedisListenerConf.java │ │ │ └── RedisUtil.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── example │ └── springbootredis │ └── SpringbootRedisApplicationTests.java ├── springboot-schedule ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springbootschedule │ │ │ ├── ServletInitializer.java │ │ │ ├── SpringbootScheduleApplication.java │ │ │ └── config │ │ │ └── Schedule.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── example │ └── springbootschedule │ └── SpringbootScheduleApplicationTests.java ├── springboot-thymeleaf ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springbootthymeleaf │ │ │ ├── ServletInitializer.java │ │ │ ├── SpringbootThymeleafApplication.java │ │ │ └── controller │ │ │ ├── IndexController.java │ │ │ └── User.java │ └── resources │ │ ├── application.properties │ │ ├── static │ │ └── e.js │ │ └── templates │ │ ├── a.html │ │ ├── b.html │ │ ├── c.html │ │ ├── d.html │ │ └── e.html │ └── test │ └── java │ └── com │ └── example │ └── springbootthymeleaf │ └── SpringbootThymeleafApplicationTests.java ├── springboot-upload ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springbootupload │ │ │ ├── ServletInitializer.java │ │ │ ├── SpringbootUploadApplication.java │ │ │ └── controller │ │ │ └── IndexController.java │ └── resources │ │ ├── application.properties │ │ └── templates │ │ └── index.html │ └── test │ └── java │ └── com │ └── example │ └── springbootupload │ └── SpringbootUploadApplicationTests.java ├── springboot-web ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── README.md ├── mvnw ├── mvnw.cmd ├── pom.xml ├── spring boot web访问 └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springbootweb │ │ │ ├── ServletInitializer.java │ │ │ ├── SpringbootWebApplication.java │ │ │ └── controller │ │ │ ├── Index2Controller.java │ │ │ ├── Index3Controller.java │ │ │ ├── IndexController.java │ │ │ ├── User.java │ │ │ ├── UserValid.java │ │ │ └── ValidController.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── example │ └── springbootweb │ └── SpringbootWebApplicationTests.java └── springboot-websocket ├── .gitignore ├── .mvn └── wrapper │ ├── MavenWrapperDownloader.java │ ├── maven-wrapper.jar │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── example │ │ └── springbootwebsocket │ │ ├── ServletInitializer.java │ │ ├── SpringbootWebsocketApplication.java │ │ ├── config │ │ └── WebSocketConfig.java │ │ ├── controller │ │ ├── IndexController.java │ │ └── WsController.java │ │ └── entity │ │ ├── RequestMessage.java │ │ └── ResponseMessage.java └── resources │ ├── application.properties │ ├── static │ ├── jquery-3.3.1.min.js │ ├── sockjs.min.js │ └── stomp.js │ └── templates │ ├── indexa.html │ ├── indexb.html │ └── indexc.html └── test └── java └── com └── example └── springbootwebsocket └── SpringbootWebsocketApplicationTests.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | logs/ 7 | dubbo_cache/ 8 | # BlueJ files 9 | *.ctxt 10 | 11 | # Mobile Tools for Java (J2ME) 12 | .mtj.tmp/ 13 | 14 | # Package Files # 15 | *.jar 16 | *.war 17 | *.nar 18 | *.ear 19 | *.zip 20 | *.tar.gz 21 | *.rar 22 | 23 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 24 | hs_err_pid* 25 | .idea -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Spring Boot 2 示例教程(求star 求fork) 2 | 3 | 章节目录: 4 | 5 | **springboot-web** :路由、参数、数据验证。 6 | 7 | **springboot-config** :配置文件、读取配置、映射实体、分环境配置。 8 | 9 | **springboot-ioc** :装配bean、自定义bean、条件装配、排除装配、依赖注入、接口注入、优先注入。 10 | 11 | **springboot-thymeleaf** :thymeleaf模板引擎。 12 | 13 | **springboot-interceptor** :拦截器、跨域。 14 | 15 | **springboot-language** :国际化。 16 | 17 | **springboot-jdbc** :jdbc。 18 | 19 | **springboot-jpa** :jpa。 20 | 21 | **springboot-mybatis** :mybatis、事务。 22 | 23 | **springboot-mongodb** :MongoDB。 24 | 25 | **springboot-redis** :Redis操作与封装、发布订阅。 26 | 27 | **springboot-log** :Logback极简使用、按天按级别生成文件。 28 | 29 | **springboot-exception** :统一异常处理。 30 | 31 | **springboot-aop** :开发切面、环绕通知、公共切面。 32 | 33 | **springboot-upload** :文件上传。 34 | 35 | **springboot-schedule** :计划任务。 36 | 37 | **springboot-jwt** :jwt令牌。 38 | 39 | **springboot-websocket** :Websocket实时推送。 40 | 41 | **springboot-rabbitmq** :RabbitMQ消息队列。 42 | 43 | **springboot-dubbo-main** :zookeeper注册中心调用层。 44 | 45 | **springboot-dubbo-service** :zookeeper注册中心服务层。 46 | (zookeeper压缩包在里面,解压后打开bin目录双击zkServer.cmd即可,springboot-dubbo-main和springboot-dubbo-service两个系统均要启动,然后浏览器访问localhost:8080/index) 47 | 48 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | springboot 8 | springboot-example 9 | 1.0-SNAPSHOT 10 | 11 | 12 | -------------------------------------------------------------------------------- /springboot-aop/.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-aop/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-aop/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /springboot-aop/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /springboot-aop/src/main/java/com/example/springbootaop/ServletInitializer.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootaop; 2 | 3 | import org.springframework.boot.builder.SpringApplicationBuilder; 4 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 5 | 6 | public class ServletInitializer extends SpringBootServletInitializer { 7 | 8 | @Override 9 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 10 | return application.sources(SpringbootAopApplication.class); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-aop/src/main/java/com/example/springbootaop/SpringbootAopApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootaop; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringbootAopApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringbootAopApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-aop/src/main/java/com/example/springbootaop/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootaop.controller; 2 | 3 | import com.example.springbootaop.service.IUserService; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | /** 9 | * @Description 10 | * @Author zhaobaolin 11 | * @Date 2019/6/27 12 | */ 13 | @RestController 14 | @RequestMapping("/Index") 15 | public class IndexController { 16 | 17 | @Autowired 18 | private IUserService userService; 19 | 20 | @RequestMapping("/a") 21 | public String a(){ 22 | return userService.getName(); 23 | } 24 | 25 | @RequestMapping("/b") 26 | public String b(){ 27 | return userService.getId(); 28 | } 29 | 30 | @RequestMapping("/c") 31 | public String c(){ 32 | return userService.getSex(); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /springboot-aop/src/main/java/com/example/springbootaop/section/CommonAspect.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootaop.section; 2 | 3 | 4 | import org.aspectj.lang.annotation.*; 5 | import org.springframework.stereotype.Component; 6 | 7 | @Component 8 | @Aspect 9 | public class CommonAspect { 10 | 11 | //公共切面 匹配指定包下的所有方法 12 | @Pointcut("within(com.example.springbootaop.service.impl..*)") 13 | public void manyAspects() { 14 | } 15 | 16 | @Before("manyAspects()") 17 | public void before() { 18 | System.out.println("公共切面开始了哦 before ......"); 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /springboot-aop/src/main/java/com/example/springbootaop/section/UserAround.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootaop.section; 2 | 3 | 4 | import org.aspectj.lang.ProceedingJoinPoint; 5 | import org.aspectj.lang.annotation.*; 6 | import org.springframework.stereotype.Component; 7 | 8 | @Component 9 | @Aspect 10 | public class UserAround { 11 | 12 | // * 表示任意返回类型 13 | // com.example.springbootaop.service.impl.UserService 指定类地址 14 | // getName 指定方法 15 | // .. 表示任意参数 16 | @Pointcut("execution(* com.example.springbootaop.service.impl.UserService.getId(..))") 17 | public void manyAspects() { 18 | } 19 | 20 | @Around("manyAspects()") 21 | public Object around(ProceedingJoinPoint joinPoint) throws Throwable { 22 | System.out.println("准备了环绕开始"); 23 | Object obj = joinPoint.proceed();//调用原方法 24 | //joinPoint中还可以获取:getArgs()(返回方法参数)、getThis()(返回代理对象)、getTarget()(返回目标) 25 | System.out.println("环绕过去了"); 26 | return obj; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /springboot-aop/src/main/java/com/example/springbootaop/section/UserAspect.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootaop.section; 2 | 3 | 4 | import org.aspectj.lang.annotation.*; 5 | import org.springframework.stereotype.Component; 6 | 7 | @Component 8 | @Aspect 9 | public class UserAspect { 10 | 11 | // * 表示任意返回类型 12 | // com.example.springbootaop.service.impl.UserService 指定类地址 13 | // getName 指定方法 可以用通配符*号代替,表明植入所有方法。 14 | // .. 表示任意参数 15 | @Pointcut("execution(* com.example.springbootaop.service.impl.UserService.getName(..))") 16 | public void manyAspects() { 17 | } 18 | 19 | @Before("manyAspects()") 20 | public void before() { 21 | System.out.println("切面开始了哦 before ......"); 22 | } 23 | 24 | @After("manyAspects()") 25 | public void after() { 26 | System.out.println("方法切面完成了哦 after ......"); 27 | } 28 | 29 | @AfterReturning("manyAspects()") 30 | public void afterReturning() { 31 | System.out.println("已经return了 afterReturning ......"); 32 | } 33 | 34 | @AfterThrowing("manyAspects()") 35 | public void afterThrowing() { 36 | System.out.println("抛出异常我才会出现的 afterThrowing ......"); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /springboot-aop/src/main/java/com/example/springbootaop/service/IUserService.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootaop.service; 2 | 3 | /** 4 | * @Description 5 | * @Author zhaobaolin 6 | * @Date 2019/6/27 7 | */ 8 | public interface IUserService { 9 | 10 | String getName(); 11 | String getId(); 12 | String getSex(); 13 | } 14 | -------------------------------------------------------------------------------- /springboot-aop/src/main/java/com/example/springbootaop/service/impl/UserService.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootaop.service.impl; 2 | 3 | import com.example.springbootaop.service.IUserService; 4 | import org.springframework.stereotype.Service; 5 | 6 | /** 7 | * @Description 8 | * @Author zhaobaolin 9 | * @Date 2019/6/27 10 | */ 11 | @Service 12 | public class UserService implements IUserService { 13 | 14 | @Override 15 | public String getName() { 16 | String name = "小明"; 17 | System.out.println(name); 18 | return name; 19 | } 20 | 21 | @Override 22 | public String getId() { 23 | String name = "小明的ID"; 24 | System.out.println(name); 25 | return name; 26 | } 27 | 28 | @Override 29 | public String getSex() { 30 | String sex = "小明的性别不告诉你"; 31 | System.out.println(sex); 32 | return sex; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /springboot-aop/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /springboot-aop/src/test/java/com/example/springbootaop/SpringbootAopApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootaop; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringbootAopApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-config/.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-config/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-config/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /springboot-config/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /springboot-config/src/main/java/com/example/springbootconfig/ServletInitializer.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootconfig; 2 | 3 | import org.springframework.boot.builder.SpringApplicationBuilder; 4 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 5 | 6 | public class ServletInitializer extends SpringBootServletInitializer { 7 | 8 | @Override 9 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 10 | return application.sources(SpringbootConfigApplication.class); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-config/src/main/java/com/example/springbootconfig/SpringbootConfigApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootconfig; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringbootConfigApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringbootConfigApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-config/src/main/java/com/example/springbootconfig/controller/Entity.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootconfig.controller; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 5 | 6 | /** 7 | * @Description 8 | * @Author zhaobaolin 9 | * @Date 2019/6/19 10 | */ 11 | 12 | //指定某个前缀将子配置加载到实体中 这个类成了bean 使用的时候直接注入 另外 使用这个东西需要加引入一个包 请查看pom.xml 13 | @EnableConfigurationProperties 14 | @ConfigurationProperties("boot") 15 | public class Entity { 16 | private String a; 17 | private String b; 18 | private String c; 19 | private String d; 20 | 21 | public String getA() { 22 | return a; 23 | } 24 | 25 | public void setA(String a) { 26 | this.a = a; 27 | } 28 | 29 | public String getB() { 30 | return b; 31 | } 32 | 33 | public void setB(String b) { 34 | this.b = b; 35 | } 36 | 37 | public String getC() { 38 | return c; 39 | } 40 | 41 | public void setC(String c) { 42 | this.c = c; 43 | } 44 | 45 | public String getD() { 46 | return d; 47 | } 48 | 49 | public void setD(String d) { 50 | this.d = d; 51 | } 52 | 53 | @Override 54 | public String toString() { 55 | return "Entity{" + 56 | "a='" + a + '\'' + 57 | ", b='" + b + '\'' + 58 | ", c='" + c + '\'' + 59 | ", d='" + d + '\'' + 60 | '}'; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /springboot-config/src/main/java/com/example/springbootconfig/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootconfig.controller; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | /** 9 | * @Description 10 | * @Author zhaobaolin 11 | * @Date 2019/6/17 12 | */ 13 | @RestController 14 | @RequestMapping("/Index") 15 | public class IndexController { 16 | 17 | //读取配置 18 | @Value("${boot.a}") 19 | private String valueA; 20 | 21 | //如果配置没有就给个默认值 22 | @Value("${boot.h:替代者}") 23 | private String valueH; 24 | 25 | @Autowired 26 | private Entity entity; 27 | 28 | //访问:http://localhost:8888/Index/a 29 | @RequestMapping("/a") 30 | public String a(){ 31 | System.out.println(valueA); 32 | return valueA; 33 | } 34 | 35 | //访问:http://localhost:8888/Index/h 36 | @RequestMapping("/h") 37 | public String h(){ 38 | System.out.println(valueH); 39 | return valueH; 40 | } 41 | 42 | 43 | //读取配置到方法参数上 44 | //访问:http://localhost:8888/Index/b 45 | @RequestMapping("/b") 46 | public String b(@Value("${boot.b}") String valueB){ 47 | System.out.println(valueB); 48 | return valueB; 49 | } 50 | 51 | @RequestMapping("/c") 52 | public String c(){ 53 | System.out.println(entity.toString()); 54 | return entity.toString(); 55 | } 56 | 57 | 58 | //这种玩法就别用了,启动时就会自动跑一遍这个方法,此时b会是配置中的值,但是当你url请求时,b就会是null,它不会把配置映射到参数上了。 59 | @Value("${boot.b}") 60 | @RequestMapping("/r") 61 | public String r(String str){ 62 | System.out.println(str); 63 | return str; 64 | } 65 | 66 | 67 | } 68 | -------------------------------------------------------------------------------- /springboot-config/src/main/resources/application-dev.properties: -------------------------------------------------------------------------------- 1 | 2 | server.port=9999 3 | -------------------------------------------------------------------------------- /springboot-config/src/main/resources/application-prod.properties: -------------------------------------------------------------------------------- 1 | 2 | server.port=8888 3 | boot.a=config_a 4 | boot.b=config_b 5 | boot.c=config_c 6 | boot.d=config_d -------------------------------------------------------------------------------- /springboot-config/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-config/src/main/resources/application.properties -------------------------------------------------------------------------------- /springboot-config/src/test/java/com/example/springbootconfig/SpringbootConfigApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootconfig; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringbootConfigApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-dubbo-core/.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-dubbo-core/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-dubbo-core/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /springboot-dubbo-core/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /springboot-dubbo-core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.example 7 | springboot-dubbo-core 8 | 0.0.1-SNAPSHOT 9 | jar 10 | springboot-dubbo-core 11 | Demo project for Spring Boot 12 | 13 | 14 | 1.8 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /springboot-dubbo-core/src/main/java/com/example/springbootdubbocore/dto/BaseDTO.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootdubbocore.dto; 2 | 3 | import java.io.Serializable; 4 | 5 | 6 | public class BaseDTO implements Serializable { 7 | 8 | private static final long serialVersionUID = 3240218212503015930L; 9 | } 10 | -------------------------------------------------------------------------------- /springboot-dubbo-core/src/main/java/com/example/springbootdubbocore/dto/UserVo.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootdubbocore.dto; 2 | 3 | public class UserVo extends BaseDTO { 4 | 5 | private long id; 6 | private String name; 7 | 8 | public long getId() { 9 | return id; 10 | } 11 | 12 | public void setId(long id) { 13 | this.id = id; 14 | } 15 | 16 | public String getName() { 17 | return name; 18 | } 19 | 20 | public void setName(String name) { 21 | this.name = name; 22 | } 23 | 24 | @Override 25 | public String toString() { 26 | return "UserVo{" + 27 | "id=" + id + 28 | ", name='" + name + '\'' + 29 | "} " + super.toString(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /springboot-dubbo-core/src/main/java/com/example/springbootdubbocore/interfaces/IUserService.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootdubbocore.interfaces; 2 | 3 | import com.example.springbootdubbocore.dto.UserVo; 4 | 5 | 6 | public interface IUserService { 7 | 8 | /** 9 | * 获取用户详情 10 | * @return 11 | */ 12 | UserVo userDetail(); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /springboot-dubbo-main/.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-dubbo-main/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-dubbo-main/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /springboot-dubbo-main/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /springboot-dubbo-main/src/main/java/com/example/springbootdubbomain/ServletInitializer.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootdubbomain; 2 | 3 | import org.springframework.boot.builder.SpringApplicationBuilder; 4 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 5 | 6 | public class ServletInitializer extends SpringBootServletInitializer { 7 | 8 | @Override 9 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 10 | return application.sources(SpringbootDubboMainApplication.class); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-dubbo-main/src/main/java/com/example/springbootdubbomain/SpringbootDubboMainApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootdubbomain; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringbootDubboMainApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringbootDubboMainApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-dubbo-main/src/main/java/com/example/springbootdubbomain/config/DubboCfg.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootdubbomain.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.context.annotation.ImportResource; 5 | import org.springframework.context.annotation.PropertySource; 6 | 7 | @Configuration 8 | @PropertySource("classpath:dubbo/dubbo.properties") 9 | @ImportResource({ "classpath:dubbo/dubbo-*.xml" }) 10 | public class DubboCfg {} 11 | -------------------------------------------------------------------------------- /springboot-dubbo-main/src/main/java/com/example/springbootdubbomain/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootdubbomain.controller; 2 | 3 | import com.example.springbootdubbocore.dto.UserVo; 4 | import com.example.springbootdubbomain.handler.IndexHandler; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.http.ResponseEntity; 8 | import org.springframework.stereotype.Controller; 9 | import org.springframework.web.bind.annotation.RequestMapping; 10 | import org.springframework.web.bind.annotation.ResponseBody; 11 | 12 | 13 | 14 | @Controller 15 | public class IndexController { 16 | 17 | @Autowired 18 | private IndexHandler indexHandler; 19 | 20 | @RequestMapping("/index") 21 | @ResponseBody 22 | public ResponseEntity index() 23 | { 24 | UserVo userVo = indexHandler.index(); 25 | return new ResponseEntity<>(userVo, HttpStatus.OK); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /springboot-dubbo-main/src/main/java/com/example/springbootdubbomain/handler/IndexHandler.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootdubbomain.handler; 2 | 3 | import com.example.springbootdubbocore.dto.UserVo; 4 | import com.example.springbootdubbocore.interfaces.IUserService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Component; 7 | 8 | 9 | @Component 10 | public class IndexHandler { 11 | 12 | @Autowired 13 | private IUserService userService; 14 | 15 | public UserVo index() 16 | { 17 | UserVo userVo = userService.userDetail(); 18 | return userVo; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /springboot-dubbo-main/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /springboot-dubbo-main/src/main/resources/dubbo/dubbo-service.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /springboot-dubbo-main/src/main/resources/dubbo/dubbo.properties: -------------------------------------------------------------------------------- 1 | dubbo.protocol.host=127.0.0.1 2 | dubbo.application.name=springboot-dubbo-main 3 | dubbo.registry.protocol=zookeeper 4 | dubbo.registry.address=127.0.0.1:2181 5 | dubbo.registry.cache=dubbo_cache/localhost/springboot-dubbo-main 6 | dubbo.protocol.name=dubbo 7 | dubbo.protocol.port=30101 -------------------------------------------------------------------------------- /springboot-dubbo-main/src/main/resources/zookeeper-3.4.13.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-dubbo-main/src/main/resources/zookeeper-3.4.13.zip -------------------------------------------------------------------------------- /springboot-dubbo-main/src/test/java/com/example/springbootdubbomain/SpringbootDubboMainApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootdubbomain; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringbootDubboMainApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-dubbo-service/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/** 5 | !**/src/test/** 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | 30 | ### VS Code ### 31 | .vscode/ 32 | -------------------------------------------------------------------------------- /springboot-dubbo-service/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-dubbo-service/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /springboot-dubbo-service/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /springboot-dubbo-service/src/main/java/com/example/springbootdubboservice/SpringbootDubboServiceApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootdubboservice; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.context.annotation.ComponentScan; 6 | 7 | @ComponentScan(basePackages = {"com.example.springbootdubboservice.*"}) 8 | @SpringBootApplication 9 | public class SpringbootDubboServiceApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(SpringbootDubboServiceApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /springboot-dubbo-service/src/main/java/com/example/springbootdubboservice/config/DubboCfg.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootdubboservice.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.context.annotation.ImportResource; 5 | import org.springframework.context.annotation.PropertySource; 6 | 7 | @Configuration 8 | @PropertySource("classpath:dubbo/dubbo.properties") 9 | @ImportResource({ "classpath:dubbo/dubbo-*.xml" }) 10 | public class DubboCfg {} 11 | -------------------------------------------------------------------------------- /springboot-dubbo-service/src/main/java/com/example/springbootdubboservice/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootdubboservice.impl; 2 | 3 | import com.example.springbootdubbocore.dto.UserVo; 4 | import com.example.springbootdubbocore.interfaces.IUserService; 5 | import org.springframework.stereotype.Service; 6 | 7 | 8 | @Service("userService") 9 | public class UserServiceImpl implements IUserService { 10 | 11 | @Override 12 | public UserVo userDetail() { 13 | System.out.println("这是服务层"); 14 | UserVo userVo = new UserVo(); 15 | userVo.setId(3); 16 | userVo.setName("张三"); 17 | return userVo; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /springboot-dubbo-service/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=7073 -------------------------------------------------------------------------------- /springboot-dubbo-service/src/main/resources/dubbo/dubbo-service.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /springboot-dubbo-service/src/main/resources/dubbo/dubbo.properties: -------------------------------------------------------------------------------- 1 | dubbo.protocol.host=127.0.0.1 2 | dubbo.application.name=springboot-dubbo-service 3 | dubbo.registry.protocol=zookeeper 4 | dubbo.registry.address=127.0.0.1:2181 5 | dubbo.registry.cache=dubbo_cache/localhost/springboot-dubbo-service 6 | dubbo.protocol.name=dubbo 7 | dubbo.protocol.port=30041 8 | -------------------------------------------------------------------------------- /springboot-dubbo-service/src/test/java/com/example/springbootdubboservice/SpringbootDubboServiceApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootdubboservice; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringbootDubboServiceApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-example.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /springboot-exception/.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-exception/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-exception/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /springboot-exception/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /springboot-exception/src/main/java/com/example/springbootexception/ServletInitializer.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootexception; 2 | 3 | import org.springframework.boot.builder.SpringApplicationBuilder; 4 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 5 | 6 | public class ServletInitializer extends SpringBootServletInitializer { 7 | 8 | @Override 9 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 10 | return application.sources(SpringbootExceptionApplication.class); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-exception/src/main/java/com/example/springbootexception/SpringbootExceptionApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootexception; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringbootExceptionApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringbootExceptionApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-exception/src/main/java/com/example/springbootexception/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootexception.controller; 2 | 3 | import com.example.springbootexception.error.BaseException; 4 | import com.example.springbootexception.error.StatusCodeEnum; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | /** 9 | * @Description 10 | * @Author zhaobaolin 11 | * @Date 2019/6/27 12 | */ 13 | @RestController 14 | @RequestMapping("/Index") 15 | public class IndexController { 16 | 17 | @RequestMapping("/a") 18 | public String a() throws Exception{ 19 | BaseException.error(StatusCodeEnum.USER_INVALID); 20 | return "a"; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /springboot-exception/src/main/java/com/example/springbootexception/error/BaseException.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootexception.error; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | 5 | /** 6 | * @Description 7 | * @Author zhaobaolin 8 | * @Date 2019/6/27 9 | */ 10 | public class BaseException { 11 | private int code; 12 | private String message; 13 | 14 | public static void error(StatusCodeEnum statusCodeEnum) throws Exception 15 | { 16 | error(statusCodeEnum.getCode(),statusCodeEnum.getMsg()); 17 | } 18 | 19 | public static void error(String message) throws Exception 20 | { 21 | error(-1,message); 22 | } 23 | 24 | public static void error(int code,String message) throws Exception 25 | { 26 | BaseException baseException = new BaseException(); 27 | baseException.setCode(code); 28 | baseException.setMessage(message); 29 | throw new Exception(JSON.toJSONString(baseException)) ; 30 | } 31 | 32 | 33 | public int getCode() { 34 | return code; 35 | } 36 | 37 | public void setCode(int code) { 38 | this.code = code; 39 | } 40 | 41 | public String getMessage() { 42 | return message; 43 | } 44 | 45 | public void setMessage(String message) { 46 | this.message = message; 47 | } 48 | 49 | @Override 50 | public String toString() { 51 | return "BaseException{" + 52 | "code=" + code + 53 | ", message='" + message + '\'' + 54 | '}'; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /springboot-exception/src/main/java/com/example/springbootexception/error/BaseRs.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootexception.error; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * @Description 7 | * @Author zhaobaolin 8 | * @Date 2019/6/27 9 | */ 10 | public class BaseRs implements Serializable { 11 | 12 | private int code; 13 | 14 | private String message; 15 | 16 | public BaseRs() { 17 | } 18 | 19 | /** 20 | * 返回内容 21 | */ 22 | private T content; 23 | 24 | public int getCode() { 25 | return code; 26 | } 27 | 28 | public String getMessage() { 29 | return message; 30 | } 31 | 32 | public T getContent() { 33 | return content; 34 | } 35 | 36 | public BaseRs(int code, String message) { 37 | this.code = code; 38 | this.message = message; 39 | } 40 | 41 | public BaseRs(int code, String message, T content) { 42 | this.code = code; 43 | this.message = message; 44 | this.content = content; 45 | } 46 | 47 | public BaseRs(StatusCodeEnum status) { 48 | this.code = status.getCode(); 49 | this.message = status.getMsg(); 50 | } 51 | 52 | public BaseRs(StatusCodeEnum status, T content) { 53 | this.code = status.getCode(); 54 | this.message = status.getMsg(); 55 | this.content = content; 56 | } 57 | 58 | public static BaseRs ok(V content) { 59 | return new BaseRs(StatusCodeEnum.SUCCESS, content); 60 | } 61 | 62 | public static BaseRs ok() { 63 | return new BaseRs(StatusCodeEnum.SUCCESS); 64 | } 65 | 66 | public static BaseRs error(StatusCodeEnum error) { 67 | return new BaseRs(error); 68 | } 69 | 70 | public void setCode(StatusCodeEnum status) { 71 | this.code = status.getCode(); 72 | } 73 | 74 | public void setMessage(String message) { 75 | this.message = message; 76 | } 77 | 78 | public void setContent(T content) { 79 | this.content = content; 80 | } 81 | 82 | @Override 83 | public String toString() { 84 | return "BaseRs{" + 85 | "code=" + code + 86 | ", message='" + message + '\'' + 87 | ", content=" + content + 88 | '}'; 89 | } 90 | 91 | public void setCode(int code) { 92 | this.code = code; 93 | } 94 | } -------------------------------------------------------------------------------- /springboot-exception/src/main/java/com/example/springbootexception/error/ErrorController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootexception.error; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | import com.fasterxml.jackson.databind.ObjectMapper; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | 7 | import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController; 8 | 9 | import org.springframework.boot.web.servlet.error.DefaultErrorAttributes; 10 | import org.springframework.stereotype.Controller; 11 | import org.springframework.web.bind.annotation.RequestMapping; 12 | import org.springframework.web.bind.annotation.ResponseBody; 13 | 14 | import javax.servlet.ServletException; 15 | import javax.servlet.http.HttpServletRequest; 16 | import javax.servlet.http.HttpServletResponse; 17 | import java.util.Collections; 18 | import java.util.Map; 19 | 20 | /** 21 | * @Description 22 | * @Author zhaobaolin 23 | * @Date 2019/6/27 24 | */ 25 | @Controller 26 | public class ErrorController extends AbstractErrorController { 27 | 28 | @Autowired 29 | ObjectMapper objectMapper; 30 | 31 | public ErrorController() { 32 | super(new DefaultErrorAttributes()); 33 | } 34 | 35 | @Override 36 | public String getErrorPath() { 37 | return null; 38 | } 39 | 40 | @RequestMapping("/error") 41 | @ResponseBody 42 | public BaseRs getErrorPath(HttpServletRequest request, HttpServletResponse response) { 43 | 44 | Map model = Collections.unmodifiableMap(getErrorAttributes(request,false)); 45 | 46 | //获取异常 可将异常打印到日志 47 | Throwable cause = getCause(request); 48 | int status = (Integer)model.get("status"); 49 | 50 | //自定义友好错误信息 51 | String msg = (String)model.get("message"); 52 | 53 | JSONObject object = JSONObject.parseObject(msg); 54 | int code = object.getInteger("code"); 55 | String message = object.getString("message"); 56 | 57 | return new BaseRs(code,message); 58 | } 59 | 60 | 61 | protected Throwable getCause(HttpServletRequest request) 62 | { 63 | Throwable error = (Throwable)request.getAttribute("javax.servlet.error.exception"); 64 | if(null == error){ 65 | //MVC有可能会封装异常成ServletException ,需要调用getCause获取真正的异常 66 | while (error instanceof ServletException && error.getCause() != null){ 67 | error = ((ServletException) error).getCause(); 68 | } 69 | } 70 | return error; 71 | } 72 | } -------------------------------------------------------------------------------- /springboot-exception/src/main/java/com/example/springbootexception/error/StatusCodeEnum.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootexception.error; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * @Description 7 | * @Author zhaobaolin 8 | * @Date 2019/6/27 9 | */ 10 | public enum StatusCodeEnum implements Serializable { 11 | SUCCESS(0, "成功"), 12 | ERROR(-1, "失败"), 13 | USER_INVALID(60000, "无效用户"), 14 | SYS_ARG_INVALID(11000, "无效参数"), 15 | 16 | ; 17 | private static final long serialVersionUID = 1L; 18 | private int code; 19 | private String msg; 20 | 21 | StatusCodeEnum(int code, String msg) { 22 | this.code = code; 23 | this.msg = msg; 24 | } 25 | public int getCode() { 26 | return code; 27 | } 28 | 29 | public void setCode(int code) { 30 | this.code = code; 31 | } 32 | 33 | public String getMsg() { 34 | return msg; 35 | } 36 | 37 | public void setMsg(String msg) { 38 | this.msg = msg; 39 | } 40 | 41 | //根据code获取对应枚举 42 | public static StatusCodeEnum getByCode(int code) { 43 | StatusCodeEnum[] values = StatusCodeEnum.values(); 44 | 45 | for (StatusCodeEnum bizStatusCodeEnum : values) { 46 | if (bizStatusCodeEnum.code == code) { 47 | return bizStatusCodeEnum; 48 | } 49 | } 50 | return null; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /springboot-exception/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /springboot-exception/src/test/java/com/example/springbootexception/SpringbootExceptionApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootexception; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringbootExceptionApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-interceptor/.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-interceptor/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-interceptor/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /springboot-interceptor/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /springboot-interceptor/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.0.BUILD-SNAPSHOT 9 | 10 | 11 | com.example 12 | springboot-interceptor 13 | 0.0.1-SNAPSHOT 14 | war 15 | springboot-interceptor 16 | Demo project for Spring Boot 17 | 18 | 19 | 1.8 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-tomcat 31 | provided 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-test 36 | test 37 | 38 | 39 | org.junit.vintage 40 | junit-vintage-engine 41 | 42 | 43 | junit 44 | junit 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | org.springframework.boot 54 | spring-boot-maven-plugin 55 | 56 | 57 | 58 | 59 | 60 | 61 | spring-snapshots 62 | Spring Snapshots 63 | https://repo.spring.io/snapshot 64 | 65 | true 66 | 67 | 68 | 69 | spring-milestones 70 | Spring Milestones 71 | https://repo.spring.io/milestone 72 | 73 | 74 | 75 | 76 | spring-snapshots 77 | Spring Snapshots 78 | https://repo.spring.io/snapshot 79 | 80 | true 81 | 82 | 83 | 84 | spring-milestones 85 | Spring Milestones 86 | https://repo.spring.io/milestone 87 | 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /springboot-interceptor/src/main/java/com/example/springbootinterceptor/ServletInitializer.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootinterceptor; 2 | 3 | import org.springframework.boot.builder.SpringApplicationBuilder; 4 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 5 | 6 | public class ServletInitializer extends SpringBootServletInitializer { 7 | 8 | @Override 9 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 10 | return application.sources(SpringbootInterceptorApplication.class); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-interceptor/src/main/java/com/example/springbootinterceptor/SpringbootInterceptorApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootinterceptor; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringbootInterceptorApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringbootInterceptorApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-interceptor/src/main/java/com/example/springbootinterceptor/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootinterceptor.controller; 2 | 3 | import org.springframework.web.bind.annotation.RequestMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | /** 7 | * @Description 8 | * @Author zhaobaolin 9 | * @Date 2019/6/18 10 | */ 11 | @RestController 12 | @RequestMapping("/Index") 13 | public class IndexController { 14 | 15 | @RequestMapping("/a") 16 | public String a(){ 17 | return "a"; 18 | } 19 | 20 | @RequestMapping("/b") 21 | public String b(){ 22 | return "b"; 23 | } 24 | 25 | @RequestMapping("/c") 26 | public String c(){ 27 | return "c"; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /springboot-interceptor/src/main/java/com/example/springbootinterceptor/interceptor/Interceptor.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootinterceptor.interceptor; 2 | 3 | import org.springframework.web.servlet.HandlerInterceptor; 4 | import org.springframework.web.servlet.ModelAndView; 5 | 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | 9 | /** 10 | * @Description 11 | * @Author zhaobaolin 12 | * @Date 2019/6/18 13 | */ 14 | public class Interceptor implements HandlerInterceptor { 15 | @Override 16 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 17 | //在调用controller方法之前会调用此方法 18 | 19 | 20 | //System.out.println(request.getServletPath()); 21 | //stem.out.println(request.getRequestURI()); 22 | //System.out.println(request.getHeader("token")); 23 | //System.out.println(request.getSession()); 24 | 25 | 26 | //重定向到c 27 | response.sendRedirect("/Index/c"); 28 | 29 | //return false为拦截 return true为放行 30 | return false; 31 | } 32 | 33 | @Override 34 | public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { 35 | //controller处理完毕后 调用这里 36 | } 37 | 38 | @Override 39 | public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { 40 | //页面渲染完毕后调用这里 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /springboot-interceptor/src/main/java/com/example/springbootinterceptor/interceptor/MvcConfigurer.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootinterceptor.interceptor; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.web.servlet.config.annotation.CorsRegistry; 5 | import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 6 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 7 | 8 | /** 9 | * @Description //注册拦截器 10 | * @Author zhaobaolin 11 | * @Date 2019/6/18 12 | */ 13 | 14 | @Configuration 15 | public class MvcConfigurer implements WebMvcConfigurer { 16 | 17 | //注册拦截器 18 | @Override 19 | public void addInterceptors(InterceptorRegistry registry) { 20 | 21 | //给指定url增加拦截器 可将要拦截的Url搭配通配符写在配置文件中 /Index/** excludePathPatterns方法排除url 22 | registry.addInterceptor(new Interceptor()) 23 | .addPathPatterns("/Index/**") 24 | .excludePathPatterns("/Index/c") 25 | .excludePathPatterns("/Index/d") 26 | ; 27 | 28 | //如果你愿意 可以在这注册多个拦截器 拦截不同的路由 29 | // registry.addInterceptor(new Interceptor1()).addPathPatterns("/Index1/a"); 30 | // registry.addInterceptor(new Interceptor2()).addPathPatterns("/Index2/a"); 31 | // registry.addInterceptor(new Interceptor3()).addPathPatterns("/Index3/a"); 32 | 33 | } 34 | 35 | //跨域配置 36 | @Override 37 | public void addCorsMappings(CorsRegistry registry) { 38 | registry.addMapping("/**") 39 | .allowedOrigins("*") 40 | .allowCredentials(true) 41 | .allowedMethods("GET", "POST", "DELETE", "PUT","PATCH") 42 | .allowedHeaders("*") 43 | .maxAge(3600); 44 | } 45 | 46 | 47 | } 48 | -------------------------------------------------------------------------------- /springboot-interceptor/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /springboot-interceptor/src/test/java/com/example/springbootinterceptor/SpringbootInterceptorApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootinterceptor; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringbootInterceptorApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-ioc/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | /target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | 5 | ### STS ### 6 | .apt_generated 7 | .classpath 8 | .factorypath 9 | .project 10 | .settings 11 | .springBeans 12 | .sts4-cache 13 | 14 | ### IntelliJ IDEA ### 15 | .idea 16 | *.iws 17 | *.iml 18 | *.ipr 19 | 20 | ### NetBeans ### 21 | /nbproject/private/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ 26 | /build/ 27 | 28 | ### VS Code ### 29 | .vscode/ 30 | -------------------------------------------------------------------------------- /springboot-ioc/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-ioc/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /springboot-ioc/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /springboot-ioc/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.0.BUILD-SNAPSHOT 9 | 10 | 11 | com.example 12 | springboot-ioc 13 | 0.0.1-SNAPSHOT 14 | war 15 | springboot-ioc 16 | Demo project for Spring Boot 17 | 18 | 19 | 1.8 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-tomcat 31 | provided 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-test 36 | test 37 | 38 | 39 | org.junit.vintage 40 | junit-vintage-engine 41 | 42 | 43 | junit 44 | junit 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | org.springframework.boot 54 | spring-boot-maven-plugin 55 | 56 | 57 | 58 | 59 | 60 | 61 | spring-snapshots 62 | Spring Snapshots 63 | https://repo.spring.io/snapshot 64 | 65 | true 66 | 67 | 68 | 69 | spring-milestones 70 | Spring Milestones 71 | https://repo.spring.io/milestone 72 | 73 | 74 | 75 | 76 | spring-snapshots 77 | Spring Snapshots 78 | https://repo.spring.io/snapshot 79 | 80 | true 81 | 82 | 83 | 84 | spring-milestones 85 | Spring Milestones 86 | https://repo.spring.io/milestone 87 | 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /springboot-ioc/src/main/java/com/example/springbootioc/ServletInitializer.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootioc; 2 | 3 | import org.springframework.boot.builder.SpringApplicationBuilder; 4 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 5 | 6 | public class ServletInitializer extends SpringBootServletInitializer { 7 | 8 | @Override 9 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 10 | return application.sources(SpringbootIoCApplication.class); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-ioc/src/main/java/com/example/springbootioc/SpringbootIoCApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootioc; 2 | 3 | import com.example.springbootioc.config.UserExclude; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.context.annotation.ComponentScan; 7 | import org.springframework.context.annotation.FilterType; 8 | 9 | @SpringBootApplication 10 | //@SpringBootApplication注解之中包含了@ComponentScan注解 默认扫描范围为当前包以及当前包的所有子包。 11 | 12 | //@ComponentScan(basePackages = {"com.example.springbootioc"},excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = UserExclude.class)}) //basePackages:重新指定当前项目扫描范围,excludeFilters:指定UserExclude类不装配。 13 | 14 | @ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = UserExclude.class)})//不带basePackages属性就是使用默认范围 excludeFilters:指定UserExclude类不装配。 excludeFilters属性的值是一个数组,可以设置多个@ComponentScan.Filter排除多个类 15 | public class SpringbootIoCApplication { 16 | 17 | public static void main(String[] args) { 18 | SpringApplication.run(SpringbootIoCApplication.class, args); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /springboot-ioc/src/main/java/com/example/springbootioc/config/Config.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootioc.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | 6 | /** 7 | * @Description 8 | * @Author zhaobaolin 9 | * @Date 2019/6/14 10 | */ 11 | @Configuration 12 | public class Config { 13 | 14 | //将User类装配成bean 名称是aaa 15 | @Bean 16 | public User aaa(){ 17 | return new User(); 18 | } 19 | 20 | //你完全可以使用@Bean这个注解将第三方包中的对象装配到IoC容器中 常见的使用场景为装配数据库连接库 21 | @Bean("cba") 22 | public String abc(){ 23 | return new String("你大爷"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /springboot-ioc/src/main/java/com/example/springbootioc/config/User.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootioc.config; 2 | 3 | //请看Config类 4 | public class User { 5 | 6 | private int id = 2; 7 | private String name = "我是User类的小光"; 8 | private String sex = "我是难的"; 9 | 10 | public int getId() { 11 | return id; 12 | } 13 | 14 | public void setId(int id) { 15 | this.id = id; 16 | } 17 | 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | public void setName(String name) { 23 | this.name = name; 24 | } 25 | 26 | public String getSex() { 27 | return sex; 28 | } 29 | 30 | public void setSex(String sex) { 31 | this.sex = sex; 32 | } 33 | 34 | @Override 35 | public String toString() { 36 | return "User{" + 37 | "id=" + id + 38 | ", name='" + name + '\'' + 39 | ", sex='" + sex + '\'' + 40 | '}'; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /springboot-ioc/src/main/java/com/example/springbootioc/config/UserComponent.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootioc.config; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | /** 6 | * @Description 7 | * @Author zhaobaolin 8 | * @Date 2019/6/14 9 | */ 10 | // @Service,@Repository,@Component 等都是一样的装配bean的注解 11 | // 也可以@Component("user")自定义你的bean的名称 如果不指定名称 像下面这样只用@Component注解,那么IoC容器会把类的第一个字母作为小写,其他不变作为Bean的名称放入IoC容器中,比如下面这个名称就会默认为 user2 12 | @Component 13 | public class UserComponent { 14 | 15 | private int id = 20; 16 | private String name = "我是UserComponent类的小明"; 17 | private String sex = "男"; 18 | 19 | public int getId() { 20 | return id; 21 | } 22 | 23 | public void setId(int id) { 24 | this.id = id; 25 | } 26 | 27 | public String getName() { 28 | return name; 29 | } 30 | 31 | public void setName(String name) { 32 | this.name = name; 33 | } 34 | 35 | public String getSex() { 36 | return sex; 37 | } 38 | 39 | public void setSex(String sex) { 40 | this.sex = sex; 41 | } 42 | 43 | @Override 44 | public String toString() { 45 | return "User{" + 46 | "id=" + id + 47 | ", name='" + name + '\'' + 48 | ", sex='" + sex + '\'' + 49 | '}'; 50 | } 51 | 52 | 53 | } 54 | -------------------------------------------------------------------------------- /springboot-ioc/src/main/java/com/example/springbootioc/config/UserCondition.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootioc.config; 2 | 3 | import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; 4 | import org.springframework.stereotype.Component; 5 | 6 | import javax.sql.DataSource; 7 | 8 | /** 9 | * @Description 10 | * @Author zhaobaolin 11 | * @Date 2019/6/14 12 | */ 13 | 14 | @Component 15 | @ConditionalOnBean(DataSource.class) //bean条件装配 指定在DataSource对象装配的情况下再装配自己 16 | //同样 还有一个注解 @ConditionalOnClass 指的是class条件装配 如果classpath中存在指定的类 则装配自己 17 | public class UserCondition { 18 | 19 | private int id = 30; 20 | private String name = "我是条件装配类的小哈哈哈哈哈"; 21 | private String sex = "女"; 22 | 23 | public int getId() { 24 | return id; 25 | } 26 | 27 | public void setId(int id) { 28 | this.id = id; 29 | } 30 | 31 | public String getName() { 32 | return name; 33 | } 34 | 35 | public void setName(String name) { 36 | this.name = name; 37 | } 38 | 39 | public String getSex() { 40 | return sex; 41 | } 42 | 43 | public void setSex(String sex) { 44 | this.sex = sex; 45 | } 46 | 47 | @Override 48 | public String toString() { 49 | return "User{" + 50 | "id=" + id + 51 | ", name='" + name + '\'' + 52 | ", sex='" + sex + '\'' + 53 | '}'; 54 | } 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /springboot-ioc/src/main/java/com/example/springbootioc/config/UserExclude.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootioc.config; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | /** 6 | * @Description 7 | * @Author zhaobaolin 8 | * @Date 2019/6/14 9 | */ 10 | 11 | //正常情况下@Component注解会被扫描到并且自动进行装配,但是这个类不会被装配,详情请查看启动类 SpringbootIoCApplication 12 | @Component 13 | public class UserExclude { 14 | 15 | private int id = 30; 16 | private String name = "我是要排除的人"; 17 | private String sex = "女"; 18 | 19 | public int getId() { 20 | return id; 21 | } 22 | 23 | public void setId(int id) { 24 | this.id = id; 25 | } 26 | 27 | public String getName() { 28 | return name; 29 | } 30 | 31 | public void setName(String name) { 32 | this.name = name; 33 | } 34 | 35 | public String getSex() { 36 | return sex; 37 | } 38 | 39 | public void setSex(String sex) { 40 | this.sex = sex; 41 | } 42 | 43 | @Override 44 | public String toString() { 45 | return "User{" + 46 | "id=" + id + 47 | ", name='" + name + '\'' + 48 | ", sex='" + sex + '\'' + 49 | '}'; 50 | } 51 | 52 | 53 | } 54 | -------------------------------------------------------------------------------- /springboot-ioc/src/main/java/com/example/springbootioc/config/UserParam.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootioc.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Component; 5 | 6 | /** 7 | * @Description 8 | * @Author zhaobaolin 9 | * @Date 2019/6/14 10 | */ 11 | @Component 12 | public class UserParam { 13 | 14 | private UserComponent userComponent = null; 15 | 16 | //从构造函数注入 17 | public UserParam(@Autowired UserComponent userComponent) { 18 | this.userComponent = userComponent; 19 | } 20 | 21 | public UserComponent getUserComponent() { 22 | return userComponent; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /springboot-ioc/src/main/java/com/example/springbootioc/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootioc.controller; 2 | 3 | import com.example.springbootioc.config.*; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | /** 10 | * @Description 11 | * @Author zhaobaolin 12 | * @Date 2019/6/14 13 | */ 14 | @RestController 15 | public class IndexController { 16 | 17 | @Autowired 18 | private User user;//虽然User类本身没有使用注解进行装配 但是在Config类里面已经将它装配成bean了 所以这里可以进行注入 19 | 20 | @Autowired 21 | private UserComponent userComponent; 22 | 23 | //@Autowired 24 | //private UserCondition userCondition;//由于UserCondition类指定在装配DataSource的情况下再装配自己 所以DataSource没有装配 UserCondition也不会被装配 那么这里无法注入 启动会报错 25 | 26 | 27 | //@Autowired 28 | //private UserExclude userExclude; //由于启动类SpringbootIoCApplication类中做了手脚,所以UserExclude不会被装配 那么这里无法注入 启动会报错 29 | 30 | 31 | @Autowired 32 | private UserParam userParam;//直接注入 尽管UserParam的构造函数需要参数 但是参数也自动注入了 33 | 34 | 35 | @RequestMapping("/a") 36 | public String a(){ 37 | 38 | //获取装配的bean 39 | AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class); 40 | Object aaa = context.getBean("aaa"); 41 | System.out.println(aaa.toString()); 42 | return aaa.toString(); 43 | } 44 | 45 | @RequestMapping("/b") 46 | public String b(){ 47 | System.out.println(user.toString()); 48 | return user.toString(); 49 | } 50 | 51 | @RequestMapping("/c") 52 | public String c(){ 53 | System.out.println(userComponent.toString()); 54 | return userComponent.toString(); 55 | } 56 | 57 | @RequestMapping("/d") 58 | public String d(){ 59 | System.out.println(userParam.getUserComponent().toString()); 60 | return userParam.getUserComponent().toString(); 61 | } 62 | 63 | 64 | } 65 | -------------------------------------------------------------------------------- /springboot-ioc/src/main/java/com/example/springbootioc/controller/StudentController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootioc.controller; 2 | 3 | import com.example.springbootioc.student.IStudent; 4 | import com.example.springbootioc.student.XiaoMing; 5 | import com.example.springbootioc.student2.IStudent2; 6 | import com.example.springbootioc.student3.IStudent3; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.beans.factory.annotation.Qualifier; 9 | import org.springframework.web.bind.annotation.RequestMapping; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | /** 13 | * @Description 14 | * @Author zhaobaolin 15 | * @Date 2019/6/14 16 | */ 17 | @RestController 18 | @RequestMapping("/Student") 19 | public class StudentController { 20 | 21 | //@Autowired 22 | //private IStudent student;//如果IStudent只有一个实现bean,那么这么注入是可以的。 23 | 24 | @Autowired 25 | private IStudent xiaoGuang;//如果有多个实现bean,那么必须将属性名称改为bean名称,以指定注入哪一个bean,如果装配的时候没有手动定义bean的名称的话,默认是类名首字母转小写其他不变。比如XiaoGuang.java的bean名称默认是xiaoGuang 26 | 27 | @Autowired 28 | private IStudent2 student2;//虽然IStudents有多个实现bean,但是其中XiaoGuang2类使用了优先注解@Primary,所以这里会优先将XiaoGuang2注入 29 | 30 | 31 | @Autowired 32 | @Qualifier("xiaoMing3") 33 | private IStudent3 student3;//由于实现IStudent3的bean都使用了优先注解,这时spring又懵逼了,因此这里需要@Qualifier注解来指定注入哪一个bean, 34 | 35 | 36 | @RequestMapping("/a") 37 | public String a(){ 38 | xiaoGuang.say(); 39 | return "a"; 40 | } 41 | 42 | 43 | @Autowired 44 | @RequestMapping("/b") 45 | public String b(XiaoMing xiaoMing){ 46 | //如果这么玩的话,程序启动就会自动到这里运行一遍,个人觉得不太实用。 47 | xiaoMing.say(); 48 | return "也可以这么注入哦"; 49 | } 50 | 51 | @RequestMapping("/c") 52 | public String c(){ 53 | student2.say(); 54 | return "c"; 55 | } 56 | 57 | @RequestMapping("/d") 58 | public String d(){ 59 | student3.say(); 60 | return "d"; 61 | } 62 | 63 | 64 | } 65 | -------------------------------------------------------------------------------- /springboot-ioc/src/main/java/com/example/springbootioc/student/IStudent.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootioc.student; 2 | 3 | /** 4 | * @Description 5 | * @Author zhaobaolin 6 | * @Date 2019/6/14 7 | */ 8 | public interface IStudent { 9 | public void say(); 10 | } 11 | -------------------------------------------------------------------------------- /springboot-ioc/src/main/java/com/example/springbootioc/student/XiaoGuang.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootioc.student; 2 | 3 | import org.springframework.stereotype.Service; 4 | 5 | /** 6 | * @Description 7 | * @Author zhaobaolin 8 | * @Date 2019/6/14 9 | */ 10 | @Service 11 | public class XiaoGuang implements IStudent { 12 | @Override 13 | public void say() { 14 | System.out.println("我是小光"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /springboot-ioc/src/main/java/com/example/springbootioc/student/XiaoMing.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootioc.student; 2 | 3 | import org.springframework.stereotype.Service; 4 | 5 | /** 6 | * @Description 7 | * @Author zhaobaolin 8 | * @Date 2019/6/14 9 | */ 10 | @Service 11 | public class XiaoMing implements IStudent { 12 | @Override 13 | public void say() { 14 | System.out.println("我是小明"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /springboot-ioc/src/main/java/com/example/springbootioc/student2/IStudent2.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootioc.student2; 2 | 3 | /** 4 | * @Description 5 | * @Author zhaobaolin 6 | * @Date 2019/6/14 7 | */ 8 | public interface IStudent2 { 9 | public void say(); 10 | } 11 | -------------------------------------------------------------------------------- /springboot-ioc/src/main/java/com/example/springbootioc/student2/XiaoGuang2.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootioc.student2; 2 | 3 | import org.springframework.context.annotation.Primary; 4 | import org.springframework.stereotype.Service; 5 | 6 | /** 7 | * @Description 8 | * @Author zhaobaolin 9 | * @Date 2019/6/14 10 | */ 11 | @Service 12 | @Primary 13 | public class XiaoGuang2 implements IStudent2 { 14 | @Override 15 | public void say() { 16 | System.out.println("我是学生接口实现的第二个 小光"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /springboot-ioc/src/main/java/com/example/springbootioc/student2/XiaoMing2.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootioc.student2; 2 | 3 | import org.springframework.stereotype.Service; 4 | 5 | /** 6 | * @Description 7 | * @Author zhaobaolin 8 | * @Date 2019/6/14 9 | */ 10 | @Service 11 | public class XiaoMing2 implements IStudent2 { 12 | @Override 13 | public void say() { 14 | System.out.println("我是学生接口实现的第二个 小明"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /springboot-ioc/src/main/java/com/example/springbootioc/student3/IStudent3.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootioc.student3; 2 | 3 | /** 4 | * @Description 5 | * @Author zhaobaolin 6 | * @Date 2019/6/14 7 | */ 8 | public interface IStudent3 { 9 | public void say(); 10 | } 11 | -------------------------------------------------------------------------------- /springboot-ioc/src/main/java/com/example/springbootioc/student3/XiaoGuang3.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootioc.student3; 2 | 3 | import org.springframework.context.annotation.Primary; 4 | import org.springframework.stereotype.Service; 5 | 6 | /** 7 | * @Description 8 | * @Author zhaobaolin 9 | * @Date 2019/6/14 10 | */ 11 | @Service 12 | @Primary 13 | public class XiaoGuang3 implements IStudent3 { 14 | @Override 15 | public void say() { 16 | System.out.println("我是学生接口实现的第三 个 小光"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /springboot-ioc/src/main/java/com/example/springbootioc/student3/XiaoMing3.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootioc.student3; 2 | 3 | import org.springframework.context.annotation.Primary; 4 | import org.springframework.stereotype.Service; 5 | 6 | /** 7 | * @Description 8 | * @Author zhaobaolin 9 | * @Date 2019/6/14 10 | */ 11 | @Service 12 | @Primary 13 | public class XiaoMing3 implements IStudent3 { 14 | @Override 15 | public void say() { 16 | System.out.println("我是学生接口实现的第三个 小明"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /springboot-ioc/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /springboot-ioc/src/test/java/com/example/springbootioc/SpringbootBeanApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootioc; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringbootBeanApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-jdbc/.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-jdbc/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-jdbc/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /springboot-jdbc/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /springboot-jdbc/src/main/java/com/example/springbootjdbc/ServletInitializer.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjdbc; 2 | 3 | import org.springframework.boot.builder.SpringApplicationBuilder; 4 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 5 | 6 | public class ServletInitializer extends SpringBootServletInitializer { 7 | 8 | @Override 9 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 10 | return application.sources(SpringbootJdbcApplication.class); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-jdbc/src/main/java/com/example/springbootjdbc/SpringbootJdbcApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjdbc; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringbootJdbcApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringbootJdbcApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-jdbc/src/main/java/com/example/springbootjdbc/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjdbc.controller; 2 | 3 | import com.example.springbootjdbc.entity.User; 4 | import com.example.springbootjdbc.enums.SexEnum; 5 | import com.example.springbootjdbc.service.IUserService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.web.bind.annotation.PathVariable; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RequestParam; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | import java.util.List; 13 | 14 | /** 15 | * @Description 16 | * @Author zhaobaolin 17 | * @Date 2019/6/19 18 | */ 19 | @RestController 20 | @RequestMapping("/Index") 21 | public class IndexController { 22 | 23 | @Autowired 24 | IUserService userService; 25 | 26 | //查询列表 27 | //访问:http://localhost:8080/Index/list?limit=1 28 | @RequestMapping("/list") 29 | public String list(@RequestParam(value = "begin",defaultValue = "0") int begin,@RequestParam(value = "limit",defaultValue = "10") int limit){ 30 | List list = userService.getUserList(begin,limit); 31 | return list.toString(); 32 | } 33 | 34 | 35 | //查询单条数据 36 | //访问:http://localhost:8080/Index/user/2 37 | @RequestMapping("/user/{id}") 38 | public String getUser(@PathVariable("id") Long id){ 39 | User user = userService.getUser(id); 40 | System.out.println(user.getUserName()+"的性别是:"+ SexEnum.getValueByKey(user.getSex()).getValue()); 41 | return user.toString(); 42 | } 43 | 44 | //插入数据 45 | //访问:http://localhost:8080/Index/add?userName=小王八&sex=0&age=200 46 | @RequestMapping("/add") 47 | public String add(User user){ 48 | Integer integer = userService.add(user); 49 | System.out.println(integer); 50 | return integer.toString(); 51 | } 52 | 53 | //插入数据返回自增id 54 | //访问:http://localhost:8080/Index/insertGetId?userName=小王八2&sex=1&age=3000 55 | @RequestMapping("/insertGetId") 56 | public String insertGetId(User user){ 57 | Long id = userService.insertGetId(user); 58 | System.out.println(id); 59 | return id.toString(); 60 | } 61 | 62 | //更新数据 63 | //访问:http://localhost:8080/Index/edit?age=2000&id=3 64 | @RequestMapping("/edit") 65 | public String edit(User user){ 66 | Integer integer = userService.edit(user); 67 | System.out.println(integer); 68 | return integer.toString(); 69 | } 70 | 71 | 72 | //删除数据 73 | //访问:http://localhost:8080/Index/del?id=3 74 | @RequestMapping("/del") 75 | public String del(Long id){ 76 | Integer integer = userService.del(id); 77 | System.out.println(integer); 78 | return integer.toString(); 79 | } 80 | 81 | 82 | } 83 | -------------------------------------------------------------------------------- /springboot-jdbc/src/main/java/com/example/springbootjdbc/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjdbc.entity; 2 | 3 | import com.example.springbootjdbc.enums.SexEnum; 4 | 5 | /** 6 | * @Description 7 | * @Author zhaobaolin 8 | * @Date 2019/6/19 9 | */ 10 | public class User { 11 | private Long id; 12 | private String userName; 13 | private Integer sex; 14 | private Integer age; 15 | 16 | public Long getId() { 17 | return id; 18 | } 19 | 20 | public void setId(Long id) { 21 | this.id = id; 22 | } 23 | 24 | public String getUserName() { 25 | return userName; 26 | } 27 | 28 | public void setUserName(String userName) { 29 | this.userName = userName; 30 | } 31 | 32 | public Integer getSex() { 33 | return sex; 34 | } 35 | 36 | public void setSex(Integer sex) { 37 | this.sex = sex; 38 | } 39 | 40 | public Integer getAge() { 41 | return age; 42 | } 43 | 44 | public void setAge(Integer age) { 45 | this.age = age; 46 | } 47 | 48 | @Override 49 | public String toString() { 50 | return "User{" + 51 | "id=" + id + 52 | ", userName='" + userName + '\'' + 53 | ", sex=" + sex + 54 | ", age=" + age + 55 | '}'; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /springboot-jdbc/src/main/java/com/example/springbootjdbc/enums/SexEnum.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjdbc.enums; 2 | 3 | /** 4 | * @Description 5 | * @Author zhaobaolin 6 | * @Date 2019/6/19 7 | */ 8 | public enum SexEnum { 9 | MAN(1,"男"), 10 | WOMAN(2,"女"), 11 | UNKNOW(3,"未知") 12 | ; 13 | 14 | private int key; 15 | private String value; 16 | 17 | SexEnum(int key, String value) { 18 | this.key = key; 19 | this.value = value; 20 | } 21 | 22 | 23 | public static SexEnum getValueByKey(int key){ 24 | for(SexEnum e : SexEnum.values()){ 25 | if(e.key == key){ 26 | return e; 27 | } 28 | } 29 | return null; 30 | } 31 | 32 | public int getKey() { 33 | return key; 34 | } 35 | 36 | public void setKey(int key) { 37 | this.key = key; 38 | } 39 | 40 | public String getValue() { 41 | return value; 42 | } 43 | 44 | public void setValue(String value) { 45 | this.value = value; 46 | }} 47 | -------------------------------------------------------------------------------- /springboot-jdbc/src/main/java/com/example/springbootjdbc/service/IUserService.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjdbc.service; 2 | 3 | import com.example.springbootjdbc.entity.User; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @Description 9 | * @Author zhaobaolin 10 | * @Date 2019/6/19 11 | */ 12 | public interface IUserService { 13 | 14 | List getUserList(int begin,int limit); 15 | 16 | User getUser(Long id); 17 | 18 | int add(User user); 19 | 20 | Long insertGetId(User user); 21 | 22 | int edit(User user); 23 | 24 | int del(Long id); 25 | } 26 | -------------------------------------------------------------------------------- /springboot-jdbc/src/main/java/com/example/springbootjdbc/service/impl/UserService.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjdbc.service.impl; 2 | 3 | import com.example.springbootjdbc.dao.UserDao; 4 | import com.example.springbootjdbc.entity.User; 5 | import com.example.springbootjdbc.service.IUserService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * @Description 用户服务层 13 | * @Author zhaobaolin 14 | * @Date 2019/6/19 15 | */ 16 | @Service 17 | public class UserService implements IUserService { 18 | 19 | @Autowired 20 | private UserDao userDao; 21 | 22 | @Override 23 | public List getUserList(int begin,int limit) { 24 | List list = userDao.selectAll(begin,limit); 25 | System.out.println(list.toString()); 26 | 27 | System.out.println("我查完了,还可以继续做一些业务逻辑呢"); 28 | 29 | return list; 30 | } 31 | 32 | @Override 33 | public User getUser(Long id) { 34 | return userDao.findUser(id); 35 | } 36 | 37 | @Override 38 | public int add(User user) { 39 | return userDao.insertUser(user); 40 | } 41 | 42 | @Override 43 | public Long insertGetId(User user) { 44 | return userDao.insertGetId(user); 45 | } 46 | 47 | @Override 48 | public int edit(User user) { 49 | return userDao.updateUser(user); 50 | } 51 | 52 | @Override 53 | public int del(Long id) { 54 | return userDao.delete(id); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /springboot-jdbc/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | # spring.datasource.driver-class-name=com.mysql.jdbc.Driver 2 | spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC 3 | spring.datasource.username=root 4 | spring.datasource.password=root 5 | 6 | -------------------------------------------------------------------------------- /springboot-jdbc/src/main/resources/jdbc演示数据.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS `tb_user`; 2 | 3 | CREATE TABLE `tb_user` ( 4 | `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增id', 5 | `user_name` varchar(64) NOT NULL COMMENT '用户名', 6 | `sex` int(1) NOT NULL DEFAULT '0' COMMENT '性别 1男2女0未知', 7 | `age` int(11) NOT NULL COMMENT '年龄', 8 | PRIMARY KEY (`id`) 9 | ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; 10 | 11 | 12 | insert into `tb_user`(`id`,`user_name`,`sex`,`age`) values 13 | 14 | (1,'小明',1,20), 15 | 16 | (2,'小花',2,17); 17 | -------------------------------------------------------------------------------- /springboot-jdbc/src/test/java/com/example/springbootjdbc/SpringbootJdbcApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjdbc; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringbootJdbcApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-jpa/.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-jpa/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-jpa/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /springboot-jpa/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /springboot-jpa/src/main/java/com/example/springbootjpa/ServletInitializer.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjpa; 2 | 3 | import org.springframework.boot.builder.SpringApplicationBuilder; 4 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 5 | 6 | public class ServletInitializer extends SpringBootServletInitializer { 7 | 8 | @Override 9 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 10 | return application.sources(SpringbootJpaApplication.class); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-jpa/src/main/java/com/example/springbootjpa/SpringbootJpaApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjpa; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringbootJpaApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringbootJpaApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-jpa/src/main/java/com/example/springbootjpa/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjpa.controller; 2 | 3 | import com.example.springbootjpa.entity.User; 4 | import com.example.springbootjpa.service.IUserService; 5 | import com.example.springbootjpa.service.impl.UserService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.web.bind.annotation.PathVariable; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | import java.util.List; 12 | 13 | /** 14 | * @Description 15 | * @Author zhaobaolin 16 | * @Date 2019/6/19 17 | */ 18 | @RestController 19 | @RequestMapping("/Index") 20 | public class IndexController { 21 | 22 | @Autowired 23 | private IUserService userService; 24 | 25 | 26 | @RequestMapping("/list") 27 | public String list(){ 28 | List list = userService.selectAll(); 29 | System.out.println(list.toString()); 30 | return list.toString(); 31 | } 32 | 33 | @RequestMapping("/find") 34 | public String user(){ 35 | User user = userService.find(); 36 | System.out.println(user); 37 | return user.toString(); 38 | } 39 | 40 | @RequestMapping("/add") 41 | public String add(){ 42 | User user = userService.add(); 43 | System.out.println(user); 44 | return user.toString(); 45 | } 46 | 47 | @RequestMapping("/addAll") 48 | public String addAll(){ 49 | List user = userService.addAll(); 50 | System.out.println(user); 51 | return user.toString(); 52 | } 53 | 54 | @RequestMapping("/update") 55 | public String update(){ 56 | User user = userService.update(); 57 | System.out.println(user); 58 | return user.toString(); 59 | } 60 | 61 | @RequestMapping("/delete") 62 | public String delete(){ 63 | Integer res = userService.delete(); 64 | System.out.println(res); 65 | return res.toString(); 66 | } 67 | 68 | 69 | } 70 | -------------------------------------------------------------------------------- /springboot-jpa/src/main/java/com/example/springbootjpa/dao/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjpa.dao; 2 | 3 | import com.example.springbootjpa.entity.User; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | /** 10 | * @Description 11 | * @Author zhaobaolin 12 | * @Date 2019/6/19 13 | */ 14 | public interface UserRepository extends JpaRepository { 15 | 16 | 17 | List findBySex(Integer sex); 18 | 19 | List findBySexOrderByAgeDesc(Integer sex); 20 | 21 | List findFirst1BySexOrderByAgeDesc(Integer sex); 22 | 23 | List findByUserNameLike(String name); 24 | 25 | 26 | User getByUserName(String userName); 27 | 28 | } 29 | -------------------------------------------------------------------------------- /springboot-jpa/src/main/java/com/example/springbootjpa/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjpa.entity; 2 | 3 | import javax.persistence.*; 4 | 5 | /** 6 | * @Description 7 | * @Author zhaobaolin 8 | * @Date 2019/6/19 9 | */ 10 | @Entity 11 | @Table(name = "tb_user") 12 | public class User { 13 | 14 | @Id 15 | @GeneratedValue(strategy = GenerationType.IDENTITY) //自增主键 16 | private Long id; 17 | 18 | //和数据库中的字段不相同时需要手动绑定 19 | @Column(name = "user_name") 20 | private String userName; 21 | 22 | @Column 23 | private Integer sex; 24 | 25 | @Column 26 | private Integer age; 27 | 28 | public Long getId() { 29 | return id; 30 | } 31 | 32 | public void setId(Long id) { 33 | this.id = id; 34 | } 35 | 36 | public String getUserName() { 37 | return userName; 38 | } 39 | 40 | public void setUserName(String userName) { 41 | this.userName = userName; 42 | } 43 | 44 | public Integer getSex() { 45 | return sex; 46 | } 47 | 48 | public void setSex(Integer sex) { 49 | this.sex = sex; 50 | } 51 | 52 | public Integer getAge() { 53 | return age; 54 | } 55 | 56 | public void setAge(Integer age) { 57 | this.age = age; 58 | } 59 | 60 | @Override 61 | public String toString() { 62 | return "User{" + 63 | "id=" + id + 64 | ", userName='" + userName + '\'' + 65 | ", sex=" + sex + 66 | ", age=" + age + 67 | '}'; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /springboot-jpa/src/main/java/com/example/springbootjpa/service/IUserService.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjpa.service; 2 | 3 | import com.example.springbootjpa.entity.User; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @Description 9 | * @Author zhaobaolin 10 | * @Date 2019/6/19 11 | */ 12 | public interface IUserService { 13 | 14 | List selectAll(); 15 | 16 | User find(); 17 | 18 | User add(); 19 | 20 | List addAll(); 21 | 22 | User update(); 23 | 24 | int delete(); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /springboot-jpa/src/main/java/com/example/springbootjpa/service/impl/jpa命名关键字.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-jpa/src/main/java/com/example/springbootjpa/service/impl/jpa命名关键字.png -------------------------------------------------------------------------------- /springboot-jpa/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC 2 | spring.datasource.username=root 3 | spring.datasource.password=root 4 | 5 | spring.jpa.show-sql=true -------------------------------------------------------------------------------- /springboot-jpa/src/main/resources/jpa演示数据.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS `tb_user`; 2 | 3 | CREATE TABLE `tb_user` ( 4 | `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增id', 5 | `user_name` varchar(64) NOT NULL COMMENT '用户名', 6 | `sex` int(1) NOT NULL DEFAULT '0' COMMENT '性别 1男2女0未知', 7 | `age` int(11) NOT NULL COMMENT '年龄', 8 | PRIMARY KEY (`id`), 9 | UNIQUE KEY `user_name` (`user_name`) 10 | ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 11 | 12 | 13 | 14 | insert into `tb_user`(`id`,`user_name`,`sex`,`age`) values 15 | 16 | (1,'小明',1,20), 17 | 18 | (2,'小花',2,17); 19 | -------------------------------------------------------------------------------- /springboot-jpa/src/test/java/com/example/springbootjpa/SpringbootJpaApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjpa; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringbootJpaApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-jwt/.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-jwt/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-jwt/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /springboot-jwt/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /springboot-jwt/src/main/java/com/example/springbootjwt/ServletInitializer.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjwt; 2 | 3 | import org.springframework.boot.builder.SpringApplicationBuilder; 4 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 5 | 6 | public class ServletInitializer extends SpringBootServletInitializer { 7 | 8 | @Override 9 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 10 | return application.sources(SpringbootJwtApplication.class); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-jwt/src/main/java/com/example/springbootjwt/SpringbootJwtApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjwt; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringbootJwtApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringbootJwtApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-jwt/src/main/java/com/example/springbootjwt/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjwt.controller; 2 | 3 | import com.example.springbootjwt.entity.User; 4 | import com.example.springbootjwt.system.JWT; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | /** 9 | * @Description 10 | * @Author zhaobaolin 11 | * @Date 2019/7/1 12 | */ 13 | @RestController 14 | @RequestMapping("/Index") 15 | public class IndexController { 16 | 17 | @RequestMapping("/encode") 18 | public String encode(){ 19 | User user = new User(); 20 | user.setId(1L); 21 | user.setName("小明"); 22 | user.setSex("男"); 23 | 24 | String token = JWT.encode(user,60 * 1000); 25 | System.out.println("生成的token是:"+token); 26 | return "生成的token是:"+token; 27 | } 28 | 29 | @RequestMapping("/decode") 30 | public String decode(String token){ 31 | if(null == token){ 32 | return "请输入token"; 33 | } 34 | User user = JWT.decode(token,User.class); 35 | if(null == user){ 36 | return "token无效"; 37 | } 38 | System.out.println(user.toString()); 39 | return user.toString(); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /springboot-jwt/src/main/java/com/example/springbootjwt/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjwt.entity; 2 | 3 | /** 4 | * @Description 5 | * @Author zhaobaolin 6 | * @Date 2019/7/1 7 | */ 8 | public class User { 9 | private Long id; 10 | private String name; 11 | private String sex; 12 | 13 | public Long getId() { 14 | return id; 15 | } 16 | 17 | public void setId(Long id) { 18 | this.id = id; 19 | } 20 | 21 | public String getName() { 22 | return name; 23 | } 24 | 25 | public void setName(String name) { 26 | this.name = name; 27 | } 28 | 29 | public String getSex() { 30 | return sex; 31 | } 32 | 33 | public void setSex(String sex) { 34 | this.sex = sex; 35 | } 36 | 37 | @Override 38 | public String toString() { 39 | return "User{" + 40 | "id=" + id + 41 | ", name='" + name + '\'' + 42 | ", sex='" + sex + '\'' + 43 | '}'; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /springboot-jwt/src/main/java/com/example/springbootjwt/system/JWT.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjwt.system; 2 | 3 | import com.auth0.jwt.JWTSigner; 4 | import com.auth0.jwt.JWTVerifier; 5 | import com.auth0.jwt.internal.com.fasterxml.jackson.databind.ObjectMapper; 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | 9 | /** 10 | * @Description 11 | * @Author zhaobaolin 12 | * @Date 2019/7/1 13 | */ 14 | public class JWT { 15 | 16 | private static final String SECRET = "XX#$%()(#*!()!KL<>?N<:{LWPW"; 17 | 18 | private static final String EXP = "exp"; 19 | 20 | private static final String PAYLOAD = "payload"; 21 | 22 | 23 | /** 24 | * 生成token 25 | * @param object 26 | * @param time 有效时间 27 | * @param 28 | * @return 29 | */ 30 | public static String encode(T object, long time) { 31 | try { 32 | final JWTSigner signer = new JWTSigner(SECRET); 33 | final Map claims = new HashMap(); 34 | ObjectMapper mapper = new ObjectMapper(); 35 | String jsonString = mapper.writeValueAsString(object); 36 | claims.put(PAYLOAD, jsonString); 37 | claims.put(EXP, System.currentTimeMillis() + time); 38 | return signer.sign(claims); 39 | } catch (Exception e) { 40 | return null; 41 | } 42 | } 43 | 44 | 45 | /** 46 | * token解析 47 | * @param token 48 | * @param classT 49 | * @param 50 | * @return 51 | */ 52 | public static T decode(String token, Class classT) { 53 | final JWTVerifier verifier = new JWTVerifier(SECRET); 54 | try { 55 | final Map claims = verifier.verify(token); 56 | if (claims.containsKey(EXP) && claims.containsKey(PAYLOAD)) { 57 | long exp = (Long) claims.get(EXP); 58 | long currentTimeMillis = System.currentTimeMillis(); 59 | if (exp > currentTimeMillis) { 60 | String json = (String) claims.get(PAYLOAD); 61 | ObjectMapper objectMapper = new ObjectMapper(); 62 | return objectMapper.readValue(json, classT); 63 | } 64 | } 65 | return null; 66 | } catch (Exception e) { 67 | return null; 68 | } 69 | } 70 | 71 | 72 | } 73 | -------------------------------------------------------------------------------- /springboot-jwt/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /springboot-jwt/src/test/java/com/example/springbootjwt/SpringbootJwtApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjwt; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringbootJwtApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-language/.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-language/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-language/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /springboot-language/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /springboot-language/src/main/java/com/example/springbootlanguage/ServletInitializer.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootlanguage; 2 | 3 | import org.springframework.boot.builder.SpringApplicationBuilder; 4 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 5 | 6 | public class ServletInitializer extends SpringBootServletInitializer { 7 | 8 | @Override 9 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 10 | return application.sources(SpringbootLanguageApplication.class); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-language/src/main/java/com/example/springbootlanguage/SpringbootLanguageApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootlanguage; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringbootLanguageApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringbootLanguageApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-language/src/main/java/com/example/springbootlanguage/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootlanguage.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | 6 | /** 7 | * @Description 8 | * @Author zhaobaolin 9 | * @Date 2019/6/18 10 | */ 11 | @Controller 12 | @RequestMapping("/Index") 13 | public class IndexController { 14 | 15 | @RequestMapping("/a") 16 | public String a(){ 17 | return "/a"; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /springboot-language/src/main/java/com/example/springbootlanguage/interceptor/MvcConfigurer.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootlanguage.interceptor; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.web.servlet.LocaleResolver; 6 | import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 7 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 8 | import org.springframework.web.servlet.i18n.CookieLocaleResolver; 9 | import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; 10 | 11 | import java.util.Locale; 12 | 13 | /** 14 | * @Description //注册拦截器 15 | * @Author zhaobaolin 16 | * @Date 2019/6/18 17 | */ 18 | @Configuration 19 | public class MvcConfigurer implements WebMvcConfigurer { 20 | 21 | // 国际化解析器 22 | @Bean 23 | public LocaleResolver localeResolver() { 24 | 25 | CookieLocaleResolver localeResolver = new CookieLocaleResolver(); 26 | localeResolver.setCookieName("localeCookie");//设置cookie 27 | localeResolver.setDefaultLocale(Locale.US);//设置默认语言 28 | localeResolver.setCookieMaxAge(3600);//设置cookie有效期. 29 | return localeResolver; 30 | } 31 | 32 | // 创建国际化拦截器 33 | @Bean 34 | public LocaleChangeInterceptor localeChangeInterceptor() { 35 | LocaleChangeInterceptor lci = new LocaleChangeInterceptor(); 36 | lci.setParamName("lang");// 设置参数名 37 | return lci; 38 | } 39 | 40 | 41 | @Override 42 | public void addInterceptors(InterceptorRegistry registry) { 43 | //注册国际化处理器 44 | registry.addInterceptor(localeChangeInterceptor()); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /springboot-language/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | # spring.messages.basename=messages 3 | spring.messages.encoding=utf-8 4 | -------------------------------------------------------------------------------- /springboot-language/src/main/resources/messages.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-language/src/main/resources/messages.properties -------------------------------------------------------------------------------- /springboot-language/src/main/resources/messages_en_US.properties: -------------------------------------------------------------------------------- 1 | welcome = hello, welcome ! -------------------------------------------------------------------------------- /springboot-language/src/main/resources/messages_zh_CN.properties: -------------------------------------------------------------------------------- 1 | welcome = Spring MVC\u56FD\u9645\u5316 -------------------------------------------------------------------------------- /springboot-language/src/main/resources/templates/a.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Title 6 | 7 | 8 | 点击切换语言: 9 | 简体中文    10 | English(US)
11 |
12 | 13 |
14 | 15 | -------------------------------------------------------------------------------- /springboot-language/src/test/java/com/example/springbootlanguage/SpringbootLanguageApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootlanguage; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringbootLanguageApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-log/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/** 5 | !**/src/test/** 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | 30 | ### VS Code ### 31 | .vscode/ 32 | -------------------------------------------------------------------------------- /springboot-log/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-log/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /springboot-log/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /springboot-log/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.0.BUILD-SNAPSHOT 9 | 10 | 11 | com.example 12 | springboot-log 13 | 0.0.1-SNAPSHOT 14 | war 15 | springboot-log 16 | Demo project for Spring Boot 17 | 18 | 19 | 1.8 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-tomcat 31 | provided 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-test 36 | test 37 | 38 | 39 | org.junit.vintage 40 | junit-vintage-engine 41 | 42 | 43 | junit 44 | junit 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | org.springframework.boot 54 | spring-boot-maven-plugin 55 | 56 | 57 | 58 | 59 | 60 | 61 | spring-snapshots 62 | Spring Snapshots 63 | https://repo.spring.io/snapshot 64 | 65 | true 66 | 67 | 68 | 69 | spring-milestones 70 | Spring Milestones 71 | https://repo.spring.io/milestone 72 | 73 | 74 | 75 | 76 | spring-snapshots 77 | Spring Snapshots 78 | https://repo.spring.io/snapshot 79 | 80 | true 81 | 82 | 83 | 84 | spring-milestones 85 | Spring Milestones 86 | https://repo.spring.io/milestone 87 | 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /springboot-log/src/main/java/com/example/springbootlog/ServletInitializer.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootlog; 2 | 3 | import org.springframework.boot.builder.SpringApplicationBuilder; 4 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 5 | 6 | public class ServletInitializer extends SpringBootServletInitializer { 7 | 8 | @Override 9 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 10 | return application.sources(SpringbootLogApplication.class); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-log/src/main/java/com/example/springbootlog/SpringbootLogApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootlog; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringbootLogApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringbootLogApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-log/src/main/java/com/example/springbootlog/common/LogUtil.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootlog.common; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | import java.lang.reflect.Method; 7 | 8 | /** 9 | * @Description 10 | * @Author zhaobaolin 11 | * @Date 2019/6/25 12 | */ 13 | public class LogUtil { 14 | 15 | /** 16 | * 日志级别 17 | */ 18 | public enum LevelEnum{ 19 | trace, debug, info, warn, error; 20 | } 21 | 22 | /** 23 | * 默认打印级别 24 | */ 25 | private String defaulLevel = LevelEnum.info.name(); 26 | 27 | 28 | /** 29 | * 日志入口 30 | * @param smg 31 | */ 32 | public static void log(String smg){ 33 | StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); 34 | StackTraceElement stackTraceElement = stackTrace[2]; 35 | 36 | LogUtil logUtil = new LogUtil(); 37 | Logger logger = logUtil.getLogger(stackTraceElement); 38 | 39 | String title = stackTraceElement.getMethodName()+" "+stackTraceElement.getLineNumber()+"行 "; 40 | logUtil.print(logger,logUtil.defaulLevel,title+smg); 41 | } 42 | 43 | /** 44 | * 日志入口 45 | * @param level 46 | * @param smg 47 | */ 48 | public static void log(LevelEnum level,String smg){ 49 | StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); 50 | StackTraceElement stackTraceElement = stackTrace[2]; 51 | 52 | LogUtil logUtil = new LogUtil(); 53 | Logger logger = logUtil.getLogger(stackTraceElement); 54 | 55 | String title = stackTraceElement.getMethodName()+" "+stackTraceElement.getLineNumber()+"行 "; 56 | 57 | logUtil.print(logger,level.name(),title+smg); 58 | } 59 | 60 | /** 61 | * 打印 62 | * @param logger 63 | * @param level 64 | * @param content 65 | */ 66 | private void print(Logger logger, String level, String content){ 67 | try{ 68 | Method m = logger.getClass().getDeclaredMethod(level,String.class); 69 | m.invoke(logger,content); 70 | }catch (Exception e){ 71 | e.printStackTrace(); 72 | } 73 | } 74 | 75 | /** 76 | * 获取实例 77 | * @param stackTraceElement 78 | * @return 79 | */ 80 | private Logger getLogger(StackTraceElement stackTraceElement){ 81 | Class cla = null; 82 | try{ 83 | cla = Class.forName(stackTraceElement.getClassName()); 84 | }catch (Exception e){ 85 | e.printStackTrace(); 86 | } 87 | return LoggerFactory.getLogger(cla); 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /springboot-log/src/main/java/com/example/springbootlog/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootlog.controller; 2 | 3 | import com.example.springbootlog.common.LogUtil; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | /** 8 | * @Description 9 | * @Author zhaobaolin 10 | * @Date 2019/6/25 11 | */ 12 | @RestController 13 | @RequestMapping("/Index") 14 | public class IndexController { 15 | 16 | @RequestMapping("/a") 17 | public String a(){ 18 | 19 | LogUtil.log("打印一个日志变得超级简单哦"); 20 | 21 | LogUtil.log(LogUtil.LevelEnum.warn,"这个突然出现的warning"); 22 | 23 | LogUtil.log(LogUtil.LevelEnum.error,"你说我这个日志工具写的牛不牛呀?"); 24 | 25 | return "牛!"; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /springboot-log/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-log/src/main/resources/application.properties -------------------------------------------------------------------------------- /springboot-log/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | ${LOG_PATTERN} 18 | 19 | 20 | 21 | 22 | 23 | 24 | ${LOG_HOME}/file.log 25 | 26 | ${LOG_HOME}/file-%d{yyyy-MM-dd}.%i.log 27 | 20MB 28 | 60 29 | 10GB 30 | 31 | 32 | ${LOG_PATTERN} 33 | utf-8 34 | 35 | 36 | 37 | 38 | 39 | ${LOG_HOME}/error.log 40 | 41 | ${LOG_HOME}/error-%d{yyyy-MM-dd}.%i.log 42 | 20MB 43 | 60 44 | 10GB 45 | 46 | 47 | ${LOG_PATTERN} 48 | utf-8 49 | 50 | 51 | ERROR 52 | ACCEPT 53 | DENY 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /springboot-log/src/test/java/com/example/springbootlog/SpringbootLogApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootlog; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringbootLogApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-mongodb/.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-mongodb/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-mongodb/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /springboot-mongodb/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /springboot-mongodb/src/main/java/com/example/springbootmongodb/ServletInitializer.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootmongodb; 2 | 3 | import org.springframework.boot.builder.SpringApplicationBuilder; 4 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 5 | 6 | public class ServletInitializer extends SpringBootServletInitializer { 7 | 8 | @Override 9 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 10 | return application.sources(SpringbootMongodbApplication.class); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-mongodb/src/main/java/com/example/springbootmongodb/SpringbootMongodbApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootmongodb; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringbootMongodbApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringbootMongodbApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-mongodb/src/main/java/com/example/springbootmongodb/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootmongodb.controller; 2 | 3 | import com.example.springbootmongodb.entity.User; 4 | import com.example.springbootmongodb.service.IUserService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | /** 10 | * @Description 11 | * @Author zhaobaolin 12 | * @Date 2019/7/4 13 | */ 14 | @RestController 15 | @RequestMapping("/Index") 16 | public class IndexController { 17 | 18 | @Autowired 19 | private IUserService userService; 20 | 21 | @RequestMapping("/insert") 22 | public String insert(){ 23 | User user = new User(); 24 | user.setId(2L); 25 | user.setUserName("张飞aaa"); 26 | user.setSex("男a"); 27 | String result = userService.insert(user); 28 | System.out.println(result); 29 | return result; 30 | } 31 | 32 | @RequestMapping("/findById") 33 | public String findById(){ 34 | Long id = 1L; 35 | Object result = userService.findById(id); 36 | System.out.println(result.toString()); 37 | return result.toString(); 38 | } 39 | 40 | @RequestMapping("/find") 41 | public String find(){ 42 | Object result = userService.find(); 43 | System.out.println(result.toString()); 44 | return result.toString(); 45 | } 46 | 47 | @RequestMapping("/select") 48 | public String select(){ 49 | Object result = userService.select(); 50 | System.out.println(result.toString()); 51 | return result.toString(); 52 | } 53 | 54 | @RequestMapping("/count") 55 | public String count(){ 56 | Object result = userService.count(); 57 | System.out.println(result.toString()); 58 | return result.toString(); 59 | } 60 | 61 | @RequestMapping("/update") 62 | public String update(){ 63 | Long id = 1L; 64 | String userName = "张飞"; 65 | String newName = "小张飞"; 66 | Integer newAge = 111; 67 | Long result = userService.update(id,newName,newAge); 68 | System.out.println(result.toString()); 69 | return result.toString(); 70 | } 71 | 72 | @RequestMapping("/delete") 73 | public String delete(){ 74 | Object result = userService.delete(); 75 | System.out.println(result.toString()); 76 | return result.toString(); 77 | } 78 | 79 | 80 | } 81 | -------------------------------------------------------------------------------- /springboot-mongodb/src/main/java/com/example/springbootmongodb/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootmongodb.entity; 2 | 3 | import org.springframework.data.annotation.Id; 4 | import org.springframework.data.mongodb.core.mapping.Document; 5 | import org.springframework.data.mongodb.core.mapping.Field; 6 | 7 | /** 8 | * @Description 9 | * @Author zhaobaolin 10 | * @Date 2019/7/4 11 | */ 12 | @Document 13 | public class User { 14 | 15 | //主键id 对应MongoDB的_id字段,如果没有自定义主键id 则MongoDB把_id的值生成一个唯一字符串 16 | @Id 17 | private Long id; 18 | 19 | //设置该属性对应存储的字段名 20 | @Field("user_name") 21 | private String userName; 22 | 23 | //没有设置@Field则代表属性名就是字段名 24 | private String sex; 25 | 26 | public Long getId() { 27 | return id; 28 | } 29 | 30 | public void setId(Long id) { 31 | this.id = id; 32 | } 33 | 34 | public String getUserName() { 35 | return userName; 36 | } 37 | 38 | public void setUserName(String userName) { 39 | this.userName = userName; 40 | } 41 | 42 | public String getSex() { 43 | return sex; 44 | } 45 | 46 | public void setSex(String sex) { 47 | this.sex = sex; 48 | } 49 | 50 | @Override 51 | public String toString() { 52 | return "User{" + 53 | "id=" + id + 54 | ", userName='" + userName + '\'' + 55 | ", sex='" + sex + '\'' + 56 | '}'; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /springboot-mongodb/src/main/java/com/example/springbootmongodb/service/IUserService.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootmongodb.service; 2 | 3 | import com.example.springbootmongodb.entity.User; 4 | 5 | import java.util.List; 6 | 7 | 8 | /** 9 | * @Description 10 | * @Author zhaobaolin 11 | * @Date 2019/7/4 12 | */ 13 | public interface IUserService { 14 | 15 | String insert(User user); 16 | 17 | Object findById(Long id ); 18 | 19 | Object find(); 20 | 21 | String select(); 22 | 23 | Long count(); 24 | 25 | Long update(Long id,String userName,Integer age); 26 | 27 | Long delete(); 28 | 29 | 30 | } 31 | -------------------------------------------------------------------------------- /springboot-mongodb/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | spring.data.mongodb.uri=mongodb://192.168.0.101/test -------------------------------------------------------------------------------- /springboot-mongodb/src/test/java/com/example/springbootmongodb/SpringbootMongodbApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootmongodb; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringbootMongodbApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-mybatis/.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-mybatis/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-mybatis/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /springboot-mybatis/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /springboot-mybatis/src/main/java/com/example/springbootmybatis/ServletInitializer.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootmybatis; 2 | 3 | import org.springframework.boot.builder.SpringApplicationBuilder; 4 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 5 | 6 | public class ServletInitializer extends SpringBootServletInitializer { 7 | 8 | @Override 9 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 10 | return application.sources(SpringbootMybatisApplication.class); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-mybatis/src/main/java/com/example/springbootmybatis/SpringbootMybatisApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootmybatis; 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.example.springbootmybatis.dao") 9 | public class SpringbootMybatisApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(SpringbootMybatisApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /springboot-mybatis/src/main/java/com/example/springbootmybatis/dao/UserDao.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootmybatis.dao; 2 | 3 | import com.example.springbootmybatis.entity.User; 4 | import org.apache.ibatis.annotations.Param; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * @Description 10 | * @Author zhaobaolin 11 | * @Date 2019/6/25 12 | */ 13 | public interface UserDao { 14 | 15 | User findById(@Param("id") Integer id); 16 | 17 | String findNameById(@Param("id") Integer id); 18 | 19 | List selectAll(); 20 | 21 | List selectLikeName(@Param("userName") String name); 22 | 23 | List selectLimit(@Param("begin") int begin,@Param("limit") int limit); 24 | 25 | int insert(User user); 26 | 27 | int insertGetId(User user); 28 | 29 | int update(User user); 30 | 31 | int delete(Long id); 32 | 33 | 34 | //开启事务 35 | void startTrans(); 36 | 37 | //提交事务 38 | void commit(); 39 | 40 | //回滚事务 41 | void rollback(); 42 | 43 | } 44 | -------------------------------------------------------------------------------- /springboot-mybatis/src/main/java/com/example/springbootmybatis/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootmybatis.entity; 2 | 3 | /** 4 | * @Description 5 | * @Author zhaobaolin 6 | * @Date 2019/6/25 7 | */ 8 | 9 | public class User { 10 | 11 | private Long id; 12 | private String userName; 13 | private Integer sex; 14 | private Integer age; 15 | 16 | public Long getId() { 17 | return id; 18 | } 19 | 20 | public void setId(Long id) { 21 | this.id = id; 22 | } 23 | 24 | public String getUserName() { 25 | return userName; 26 | } 27 | 28 | public void setUserName(String userName) { 29 | this.userName = userName; 30 | } 31 | 32 | public Integer getSex() { 33 | return sex; 34 | } 35 | 36 | public void setSex(Integer sex) { 37 | this.sex = sex; 38 | } 39 | 40 | public Integer getAge() { 41 | return age; 42 | } 43 | 44 | public void setAge(Integer age) { 45 | this.age = age; 46 | } 47 | 48 | @Override 49 | public String toString() { 50 | return "User{" + 51 | "id=" + id + 52 | ", userName='" + userName + '\'' + 53 | ", sex=" + sex + 54 | ", age=" + age + 55 | '}'; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /springboot-mybatis/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 3 | spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC 4 | spring.datasource.username=root 5 | spring.datasource.password=root 6 | 7 | mybatis.config-locations=classpath:mybatis/mybatis-config.xml 8 | mybatis.mapper-locations=classpath:mybatis/mapper/*.xml 9 | # mybatis.type-aliases-package=com.zf 10 | -------------------------------------------------------------------------------- /springboot-mybatis/src/main/resources/mybatis/mapper/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | id, user_name, sex, age 13 | 14 | 15 | 16 | 22 | 23 | 24 | 30 | 31 | 32 | 37 | 38 | 39 | 47 | 48 | 49 | 57 | 58 | 59 | 60 | insert into tb_user (user_name,sex,age) values(#{userName},#{sex},#{age}); 61 | 62 | 63 | 64 | 65 | insert into tb_user (user_name,sex,age) values(#{userName},#{sex},#{age}); 66 | 67 | 68 | 69 | 70 | 71 | update tb_user set age=#{age} where id=#{id} 72 | 73 | 74 | 75 | 76 | 77 | delete from tb_user where id=#{id} 78 | 79 | 80 | 81 | 84 | 85 | 88 | 89 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /springboot-mybatis/src/main/resources/mybatis/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /springboot-mybatis/src/test/java/com/example/springbootmybatis/SpringbootMybatisApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootmybatis; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringbootMybatisApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-rabbitmq/.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-rabbitmq/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-rabbitmq/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /springboot-rabbitmq/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /springboot-rabbitmq/src/main/java/com/example/springbootrabbitmq/ServletInitializer.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootrabbitmq; 2 | 3 | import org.springframework.boot.builder.SpringApplicationBuilder; 4 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 5 | 6 | public class ServletInitializer extends SpringBootServletInitializer { 7 | 8 | @Override 9 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 10 | return application.sources(SpringbootRabbitmqApplication.class); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-rabbitmq/src/main/java/com/example/springbootrabbitmq/SpringbootRabbitmqApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootrabbitmq; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringbootRabbitmqApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringbootRabbitmqApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-rabbitmq/src/main/java/com/example/springbootrabbitmq/config/RabbitConf.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootrabbitmq.config; 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 | * @Description 9 | * @Author zhaobaolin 10 | * @Date 2019/7/5 11 | */ 12 | @Configuration 13 | public class RabbitConf { 14 | 15 | //创建两个队列名 16 | @Bean 17 | public Queue strQueue(){ 18 | return new Queue("str",true); 19 | } 20 | 21 | @Bean 22 | public Queue userQueue(){ 23 | return new Queue("user",true); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /springboot-rabbitmq/src/main/java/com/example/springbootrabbitmq/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootrabbitmq.controller; 2 | 3 | import com.example.springbootrabbitmq.entity.User; 4 | import com.example.springbootrabbitmq.rabbit.RabbitMqProduce; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | /** 10 | * @Description 11 | * @Author zhaobaolin 12 | * @Date 2019/7/5 13 | */ 14 | @RestController 15 | @RequestMapping("/Index") 16 | public class IndexController { 17 | 18 | @Autowired 19 | private RabbitMqProduce rabbitMqProduce; 20 | 21 | @RequestMapping("/sendStr") 22 | public String sendStr(){ 23 | rabbitMqProduce.sendStr("刘亦菲,我喜欢你。"); 24 | return "发送成功"; 25 | } 26 | 27 | @RequestMapping("/sendUser") 28 | public String sendUser(){ 29 | User user = new User(); 30 | user.setId(1L); 31 | user.setUserName("张三丰"); 32 | user.setSex("男"); 33 | rabbitMqProduce.sendUser(user); 34 | return "发送成功"; 35 | } 36 | 37 | 38 | } 39 | -------------------------------------------------------------------------------- /springboot-rabbitmq/src/main/java/com/example/springbootrabbitmq/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootrabbitmq.entity; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * @Description 7 | * @Author zhaobaolin 8 | * @Date 2019/7/5 9 | */ 10 | public class User implements Serializable { 11 | 12 | private static final long serialVersionUID = 4359709211352400087L; 13 | 14 | private Long id; 15 | private String userName; 16 | private String sex; 17 | 18 | public Long getId() { 19 | return id; 20 | } 21 | 22 | public void setId(Long id) { 23 | this.id = id; 24 | } 25 | 26 | public String getUserName() { 27 | return userName; 28 | } 29 | 30 | public void setUserName(String userName) { 31 | this.userName = userName; 32 | } 33 | 34 | public String getSex() { 35 | return sex; 36 | } 37 | 38 | public void setSex(String sex) { 39 | this.sex = sex; 40 | } 41 | 42 | @Override 43 | public String toString() { 44 | return "User{" + 45 | "id=" + id + 46 | ", userName='" + userName + '\'' + 47 | ", sex='" + sex + '\'' + 48 | '}'; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /springboot-rabbitmq/src/main/java/com/example/springbootrabbitmq/rabbit/RabbitMQReceiver.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootrabbitmq.rabbit; 2 | 3 | import com.example.springbootrabbitmq.entity.User; 4 | import org.springframework.amqp.rabbit.annotation.RabbitListener; 5 | import org.springframework.stereotype.Component; 6 | 7 | /** 8 | * @Description 9 | * @Author zhaobaolin 10 | * @Date 2019/7/5 11 | */ 12 | @Component 13 | public class RabbitMQReceiver { 14 | 15 | //消息接收 16 | 17 | @RabbitListener(queues = "str") 18 | public void receiverStr(String msg){ 19 | System.out.println("收到str队列的消息:"+msg); 20 | } 21 | 22 | @RabbitListener(queues = "user") 23 | public void receiverUser(User user){ 24 | System.out.println("收到user队列的消息"+user.toString()); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /springboot-rabbitmq/src/main/java/com/example/springbootrabbitmq/rabbit/RabbitMqProduce.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootrabbitmq.rabbit; 2 | 3 | import com.example.springbootrabbitmq.entity.User; 4 | import org.springframework.amqp.rabbit.connection.CorrelationData; 5 | import org.springframework.amqp.rabbit.core.RabbitTemplate; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | /** 10 | * @Description 11 | * @Author zhaobaolin 12 | * @Date 2019/7/5 13 | */ 14 | @Service 15 | public class RabbitMqProduce { 16 | 17 | @Autowired 18 | private RabbitTemplate rabbitTemplate; 19 | 20 | // rabbit的安装教程请参考:https://www.cnblogs.com/fengyumeng/p/11133924.html 21 | 22 | 23 | //消息发送 24 | 25 | public void sendStr(String msg) { 26 | rabbitTemplate.convertAndSend("str",msg);//发送到str队列 27 | } 28 | 29 | public void sendUser(User user) { 30 | rabbitTemplate.convertAndSend("user",user);//发送到user队列 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /springboot-rabbitmq/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | spring.rabbitmq.host=192.168.0.101 3 | spring.rabbitmq.port=5672 4 | spring.rabbitmq.username=zhaobl 5 | spring.rabbitmq.password=123456 6 | spring.rabbitmq.publisher-confirms=true 7 | 8 | -------------------------------------------------------------------------------- /springboot-rabbitmq/src/test/java/com/example/springbootrabbitmq/SpringbootRabbitmqApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootrabbitmq; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringbootRabbitmqApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-redis/.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-redis/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-redis/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /springboot-redis/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /springboot-redis/src/main/java/com/example/springbootredis/ServletInitializer.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootredis; 2 | 3 | import org.springframework.boot.builder.SpringApplicationBuilder; 4 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 5 | 6 | public class ServletInitializer extends SpringBootServletInitializer { 7 | 8 | @Override 9 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 10 | return application.sources(SpringbootRedisApplication.class); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-redis/src/main/java/com/example/springbootredis/SpringbootRedisApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootredis; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringbootRedisApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringbootRedisApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-redis/src/main/java/com/example/springbootredis/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootredis.controller; 2 | 3 | import com.example.springbootredis.redis.RedisUtil; 4 | import com.example.springbootredis.entity.User; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | /** 10 | * @Description 11 | * @Author zhaobaolin 12 | * @Date 2019/7/1 13 | */ 14 | @RestController 15 | @RequestMapping("/Index") 16 | public class IndexController { 17 | 18 | @Autowired 19 | private RedisUtil redisUtil; 20 | 21 | //访问:http://localhost:8080/Index/a?name=毛线 22 | @RequestMapping("/a") 23 | public String a(String name){ 24 | redisUtil.set("name",name); 25 | return "保存成功"; 26 | } 27 | 28 | //访问:http://localhost:8080/Index/b 29 | @RequestMapping("/b") 30 | public String b(){ 31 | String name = redisUtil.get("name"); 32 | return name; 33 | } 34 | 35 | 36 | //访问:http://localhost:8080/Index/c 37 | @RequestMapping("/c") 38 | public String c(){ 39 | User user = new User(); 40 | user.setId(1L); 41 | user.setName("张三"); 42 | user.setSex("男"); 43 | redisUtil.setObj("user",user); 44 | return "保存成功"; 45 | } 46 | 47 | 48 | //访问:http://localhost:8080/Index/d 49 | @RequestMapping("/d") 50 | public String d(){ 51 | User user = redisUtil.getObj("user",User.class); 52 | return user == null ? "无" : user.toString(); 53 | } 54 | 55 | 56 | 57 | //访问:http://localhost:8080/Index/send 58 | @RequestMapping("/send") 59 | public String send(){ 60 | redisUtil.sendMsg("a133","小姐姐你在哪"); 61 | return "发布成功"; 62 | } 63 | 64 | 65 | } 66 | -------------------------------------------------------------------------------- /springboot-redis/src/main/java/com/example/springbootredis/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootredis.entity; 2 | 3 | /** 4 | * @Description 5 | * @Author zhaobaolin 6 | * @Date 2019/7/1 7 | */ 8 | public class User { 9 | private Long id; 10 | private String name; 11 | private String sex; 12 | 13 | public Long getId() { 14 | return id; 15 | } 16 | 17 | public void setId(Long id) { 18 | this.id = id; 19 | } 20 | 21 | public String getName() { 22 | return name; 23 | } 24 | 25 | public void setName(String name) { 26 | this.name = name; 27 | } 28 | 29 | public String getSex() { 30 | return sex; 31 | } 32 | 33 | public void setSex(String sex) { 34 | this.sex = sex; 35 | } 36 | 37 | @Override 38 | public String toString() { 39 | return "User{" + 40 | "id=" + id + 41 | ", name='" + name + '\'' + 42 | ", sex='" + sex + '\'' + 43 | '}'; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /springboot-redis/src/main/java/com/example/springbootredis/redis/RedisListener.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootredis.redis; 2 | 3 | import org.springframework.data.redis.connection.Message; 4 | import org.springframework.data.redis.connection.MessageListener; 5 | import org.springframework.stereotype.Component; 6 | 7 | /** 8 | * @Description 9 | * @Author zhaobaolin 10 | * @Date 2019/7/1 11 | */ 12 | @Component 13 | public class RedisListener implements MessageListener { 14 | @Override 15 | public void onMessage(Message message, byte[] bytes) { 16 | System.out.println("收到消息"); 17 | String channel = new String(message.getChannel()); 18 | String body = new String(message.getBody()); 19 | System.out.println("渠道是:"+channel); 20 | System.out.println("内容是:"+body); 21 | 22 | 23 | } 24 | 25 | 26 | } 27 | -------------------------------------------------------------------------------- /springboot-redis/src/main/java/com/example/springbootredis/redis/RedisListenerConf.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootredis.redis; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.data.redis.connection.RedisConnectionFactory; 6 | import org.springframework.data.redis.listener.PatternTopic; 7 | import org.springframework.data.redis.listener.RedisMessageListenerContainer; 8 | import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; 9 | 10 | /** 11 | * @Description 12 | * @Author zhaobaolin 13 | * @Date 2019/7/1 14 | */ 15 | @Configuration 16 | public class RedisListenerConf { 17 | @Bean 18 | MessageListenerAdapter listenerAdapter( ) { 19 | MessageListenerAdapter adapter = new MessageListenerAdapter(new RedisListener()); 20 | return adapter; 21 | } 22 | 23 | @Bean 24 | RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) { 25 | RedisMessageListenerContainer container = new RedisMessageListenerContainer(); 26 | container.setConnectionFactory(connectionFactory); 27 | 28 | container.addMessageListener(listenerAdapter, new PatternTopic("*"));//订阅所有渠道 你也可以这样 new PatternTopic("A.*")订阅A下面的所有渠道 29 | return container; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /springboot-redis/src/main/java/com/example/springbootredis/redis/RedisUtil.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootredis.redis; 2 | 3 | 4 | import com.alibaba.fastjson.JSON; 5 | import com.alibaba.fastjson.JSONObject; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.data.redis.core.StringRedisTemplate; 8 | import org.springframework.stereotype.Component; 9 | 10 | import java.util.concurrent.TimeUnit; 11 | 12 | /** 13 | * @Description 14 | * @Author zhaobaolin 15 | * @Date 2019/7/1 16 | */ 17 | @Component 18 | public class RedisUtil { 19 | 20 | @Autowired 21 | private StringRedisTemplate redisTemplate; 22 | 23 | public void set(String name,String value) 24 | { 25 | redisTemplate.opsForValue().set(name,value); 26 | } 27 | 28 | public void setObj(String name,Object obj) 29 | { 30 | redisTemplate.opsForValue().set(name, JSON.toJSONString(obj)); 31 | } 32 | 33 | 34 | //设置有效期时间 默认秒 35 | public void set(String name,String value,int expire) 36 | { 37 | redisTemplate.opsForValue().set(name,value,expire,TimeUnit.SECONDS); 38 | } 39 | 40 | //设置有效期时间 41 | public void set(String name,String value,int expire,TimeUnit timeUnit) 42 | { 43 | redisTemplate.opsForValue().set(name,value,expire,timeUnit); 44 | } 45 | 46 | public String get(String name) 47 | { 48 | return redisTemplate.opsForValue().get(name); 49 | } 50 | 51 | 52 | //取对象 53 | public JSONObject getObj(String key){ 54 | String str = redisTemplate.opsForValue().get(key); 55 | if(null == str || "".equals(str)){ 56 | return null; 57 | } 58 | return JSON.parseObject(str); 59 | } 60 | 61 | //取对象 62 | public T getObj(String key,Class tClass){ 63 | String str = redisTemplate.opsForValue().get(key); 64 | if(null == str || "".equals(str)){ 65 | return null; 66 | } 67 | T obj = JSON.parseObject(str,tClass); 68 | return obj; 69 | } 70 | 71 | 72 | //增加步长 73 | public String setInc(String name,Long value) 74 | { 75 | if(null != value && value > 0){ 76 | redisTemplate.boundValueOps(name).increment(value);//步长增加 77 | } 78 | return get(name); 79 | } 80 | 81 | //减少步长 82 | public String setDec(String name,Long value) 83 | { 84 | if(null != value && value < 0){ 85 | redisTemplate.boundValueOps(name).increment(-value);//步长减少 86 | } 87 | return get(name); 88 | } 89 | 90 | 91 | public void delete(String name) 92 | { 93 | redisTemplate.delete(name); 94 | } 95 | 96 | 97 | //查询key是否存在 98 | public boolean hasKey(String name) 99 | { 100 | return redisTemplate.hasKey(name); 101 | } 102 | 103 | //发布消息 104 | public void sendMsg(String channel,Object message) 105 | { 106 | redisTemplate.convertAndSend(channel,message); 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /springboot-redis/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | spring.redis.host=127.0.0.1 3 | spring.redis.password= 4 | spring.redis.port=6379 5 | -------------------------------------------------------------------------------- /springboot-redis/src/test/java/com/example/springbootredis/SpringbootRedisApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootredis; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringbootRedisApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-schedule/.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-schedule/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-schedule/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /springboot-schedule/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /springboot-schedule/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.0.BUILD-SNAPSHOT 9 | 10 | 11 | com.example 12 | springboot-schedule 13 | 0.0.1-SNAPSHOT 14 | war 15 | springboot-schedule 16 | Demo project for Spring Boot 17 | 18 | 19 | 1.8 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-tomcat 31 | provided 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-test 36 | test 37 | 38 | 39 | org.junit.vintage 40 | junit-vintage-engine 41 | 42 | 43 | junit 44 | junit 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | org.springframework.boot 54 | spring-boot-maven-plugin 55 | 56 | 57 | 58 | 59 | 60 | 61 | spring-snapshots 62 | Spring Snapshots 63 | https://repo.spring.io/snapshot 64 | 65 | true 66 | 67 | 68 | 69 | spring-milestones 70 | Spring Milestones 71 | https://repo.spring.io/milestone 72 | 73 | 74 | 75 | 76 | spring-snapshots 77 | Spring Snapshots 78 | https://repo.spring.io/snapshot 79 | 80 | true 81 | 82 | 83 | 84 | spring-milestones 85 | Spring Milestones 86 | https://repo.spring.io/milestone 87 | 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /springboot-schedule/src/main/java/com/example/springbootschedule/ServletInitializer.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootschedule; 2 | 3 | import org.springframework.boot.builder.SpringApplicationBuilder; 4 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 5 | 6 | public class ServletInitializer extends SpringBootServletInitializer { 7 | 8 | @Override 9 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 10 | return application.sources(SpringbootScheduleApplication.class); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-schedule/src/main/java/com/example/springbootschedule/SpringbootScheduleApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootschedule; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.scheduling.annotation.EnableScheduling; 6 | 7 | @SpringBootApplication 8 | @EnableScheduling 9 | public class SpringbootScheduleApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(SpringbootScheduleApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /springboot-schedule/src/main/java/com/example/springbootschedule/config/Schedule.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootschedule.config; 2 | 3 | import org.springframework.scheduling.annotation.Scheduled; 4 | import org.springframework.stereotype.Service; 5 | 6 | import java.text.SimpleDateFormat; 7 | import java.util.Date; 8 | 9 | /** 10 | * @Description 11 | * @Author zhaobaolin 12 | * @Date 2019/6/30 13 | */ 14 | @Service 15 | public class Schedule { 16 | 17 | private static final SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss"); 18 | 19 | //fixedRate设置间隔时间 单位秒 20 | @Scheduled(fixedRate = 10000) 21 | public void a(){ 22 | System.out.println("每隔10秒钟执行一次:"+format.format(new Date())); 23 | } 24 | 25 | 26 | @Scheduled(cron = "0 20-25 14 * * *") 27 | public void b(){ 28 | System.out.println("今天下午14点20-25分钟之间每分钟跑一次:"+format.format(new Date())); 29 | } 30 | 31 | 32 | @Scheduled(cron = "0/10 * * * * *") 33 | public void c(){ 34 | System.out.println("我也是每十秒跑一次:"+format.format(new Date())); 35 | } 36 | 37 | //cron表达式 :秒、分、时、日、月、年 38 | /* 39 | 40 | * 表示所有值。在分钟里表示每一分钟触发。在小时,日期,月份等里面表示每一小时,每一日,每一月。 41 | ?表示不指定值。表示不关心当前位置设置的值。 比如不关心是周几,则周的位置填写? 42 | - 表示区间。小时设置为10-12表示10,11,12点均会触发。 43 | ,表示多个值。 小时设置成10,12表示10点和12点会触发。 44 | / 表示递增触发。 5/15表示从第5秒开始,每隔15秒触发。 45 | 46 | 47 | 示例  48 | "0 0 12 * * ?" 每天中午12点触发  49 | "0 15 10 ? * *" 每天上午10:15触发  50 | "0 15 10 * * ?" 每天上午10:15触发  51 | "0 15 10 * * ? 2005" 2005年的每天上午10:15触发  52 | "0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发  53 | "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发  54 | "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发  55 | "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发  56 | "0 15 10 15 * ?" 每月15日上午10:15触发  57 | "0 15 10 L * ?" 每月最后一日的上午10:15触发  58 | 59 | */ 60 | } 61 | -------------------------------------------------------------------------------- /springboot-schedule/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /springboot-schedule/src/test/java/com/example/springbootschedule/SpringbootScheduleApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootschedule; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringbootScheduleApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-thymeleaf/.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-thymeleaf/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-thymeleaf/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /springboot-thymeleaf/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /springboot-thymeleaf/src/main/java/com/example/springbootthymeleaf/ServletInitializer.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootthymeleaf; 2 | 3 | import org.springframework.boot.builder.SpringApplicationBuilder; 4 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 5 | 6 | public class ServletInitializer extends SpringBootServletInitializer { 7 | 8 | @Override 9 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 10 | return application.sources(SpringbootThymeleafApplication.class); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-thymeleaf/src/main/java/com/example/springbootthymeleaf/SpringbootThymeleafApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootthymeleaf; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringbootThymeleafApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringbootThymeleafApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-thymeleaf/src/main/java/com/example/springbootthymeleaf/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootthymeleaf.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.ui.Model; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | /** 11 | * @Description 12 | * @Author zhaobaolin 13 | * @Date 2019/6/18 14 | */ 15 | @Controller 16 | @RequestMapping("/Index") 17 | public class IndexController { 18 | 19 | //展示页面 20 | @RequestMapping("/a") 21 | public String a(){ 22 | return "a";//对应到a.html 23 | } 24 | 25 | //赋值 26 | @RequestMapping("/b") 27 | public String b(Model model){ 28 | model.addAttribute("name","小明"); 29 | return "b"; 30 | } 31 | 32 | //赋值对象 条件判断 33 | @RequestMapping("/c") 34 | public String c(Model model){ 35 | model.addAttribute("user",new User()); 36 | return "c"; 37 | } 38 | 39 | //数据遍历 40 | @RequestMapping("/d") 41 | public String d(Model model){ 42 | User user1 = new User("小花",10); 43 | User user2 = new User("小墙",20); 44 | 45 | List list= new ArrayList<>(); 46 | list.add(user1); 47 | list.add(user2); 48 | 49 | model.addAttribute("list",list); 50 | return "d"; 51 | } 52 | 53 | 54 | //更多用法请查看官方文档:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#introducing-thymeleaf 55 | 56 | 57 | 58 | 59 | //静态资源 60 | @RequestMapping("/e") 61 | public String e(){ 62 | return "e"; 63 | } 64 | 65 | 66 | } 67 | -------------------------------------------------------------------------------- /springboot-thymeleaf/src/main/java/com/example/springbootthymeleaf/controller/User.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootthymeleaf.controller; 2 | 3 | /** 4 | * @Description 5 | * @Author zhaobaolin 6 | * @Date 2019/6/18 7 | */ 8 | public class User { 9 | private String name = "小花"; 10 | private int age = 10; 11 | 12 | public User() { 13 | } 14 | 15 | public User(String name, int age) { 16 | this.name = name; 17 | this.age = age; 18 | } 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public void setName(String name) { 25 | this.name = name; 26 | } 27 | 28 | public int getAge() { 29 | return age; 30 | } 31 | 32 | public void setAge(int age) { 33 | this.age = age; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /springboot-thymeleaf/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-thymeleaf/src/main/resources/application.properties -------------------------------------------------------------------------------- /springboot-thymeleaf/src/main/resources/static/e.js: -------------------------------------------------------------------------------- 1 | console.log("哈喽,这是js在跑哦"); -------------------------------------------------------------------------------- /springboot-thymeleaf/src/main/resources/templates/a.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Title 6 | 7 | 8 | 9 |
10 | 这是a页面哦 11 |
12 | 13 | -------------------------------------------------------------------------------- /springboot-thymeleaf/src/main/resources/templates/b.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Title 6 | 7 | 8 | 9 |
10 | 我的名字是 11 |
12 | 13 | -------------------------------------------------------------------------------- /springboot-thymeleaf/src/main/resources/templates/c.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Title 6 | 7 | 8 | 9 |
10 | 我的名字是 11 | 我今年 岁了 12 | 13 | 我已经长大了 14 | 我还是个孩子呢 15 |
16 | 17 | -------------------------------------------------------------------------------- /springboot-thymeleaf/src/main/resources/templates/d.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Title 6 | 7 | 8 | 9 |
10 | 11 |
12 | 我的名字是 13 | 我今年 岁了 14 |
15 | 16 |
17 | 18 | -------------------------------------------------------------------------------- /springboot-thymeleaf/src/main/resources/templates/e.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Title 6 | 7 | 8 | 9 |
10 | 请打开F12控制台后刷新本页 11 |
12 | 13 | 14 | -------------------------------------------------------------------------------- /springboot-thymeleaf/src/test/java/com/example/springbootthymeleaf/SpringbootThymeleafApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootthymeleaf; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringbootThymeleafApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-upload/.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-upload/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-upload/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /springboot-upload/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /springboot-upload/src/main/java/com/example/springbootupload/ServletInitializer.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootupload; 2 | 3 | import org.springframework.boot.builder.SpringApplicationBuilder; 4 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 5 | 6 | public class ServletInitializer extends SpringBootServletInitializer { 7 | 8 | @Override 9 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 10 | return application.sources(SpringbootUploadApplication.class); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-upload/src/main/java/com/example/springbootupload/SpringbootUploadApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootupload; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringbootUploadApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringbootUploadApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-upload/src/main/java/com/example/springbootupload/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootupload.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.*; 5 | import org.springframework.web.multipart.MultipartFile; 6 | 7 | import java.io.File; 8 | 9 | /** 10 | * @Description 11 | * @Author zhaobaolin 12 | * @Date 2019/6/29 13 | */ 14 | @Controller 15 | @RequestMapping("/Index") 16 | public class IndexController { 17 | 18 | @RequestMapping("index") 19 | public String index() 20 | { 21 | return "/index"; 22 | } 23 | 24 | @ResponseBody 25 | @PostMapping("upload") 26 | public String upload(@RequestParam("fileUpload") MultipartFile multipartFile) 27 | { 28 | if(multipartFile.isEmpty()){ 29 | return "没有文件上传"; 30 | } 31 | 32 | String fileName = multipartFile.getOriginalFilename(); 33 | long size = multipartFile.getSize(); 34 | System.out.println("上传的文件名是:"+fileName+",文件大小是:"+size); 35 | 36 | //保存目录 绝对路径 37 | String path = "E:\\tmp\\log\\"; 38 | 39 | File file = new File(path,fileName); 40 | 41 | //创建目录 42 | if (!file.getParentFile().exists()) { 43 | file.getParentFile().mkdirs(); 44 | } 45 | 46 | try{ 47 | //保存文件 48 | multipartFile.transferTo(file); 49 | }catch (Exception e){ 50 | e.printStackTrace(); 51 | return "上传失败"; 52 | } 53 | return "上传成功,上传的路径是:"+file.getAbsolutePath(); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /springboot-upload/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-upload/src/main/resources/application.properties -------------------------------------------------------------------------------- /springboot-upload/src/main/resources/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 准备上传 6 | 7 | 8 | 9 |
10 | 11 | 12 |
13 | 14 | 15 | -------------------------------------------------------------------------------- /springboot-upload/src/test/java/com/example/springbootupload/SpringbootUploadApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootupload; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringbootUploadApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-web/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | /target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | 5 | ### STS ### 6 | .apt_generated 7 | .classpath 8 | .factorypath 9 | .project 10 | .settings 11 | .springBeans 12 | .sts4-cache 13 | 14 | ### IntelliJ IDEA ### 15 | .idea 16 | *.iws 17 | *.iml 18 | *.ipr 19 | 20 | ### NetBeans ### 21 | /nbproject/private/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ 26 | /build/ 27 | 28 | ### VS Code ### 29 | .vscode/ 30 | -------------------------------------------------------------------------------- /springboot-web/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-web/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /springboot-web/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /springboot-web/README.md: -------------------------------------------------------------------------------- 1 | # springboot web 访问 -------------------------------------------------------------------------------- /springboot-web/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | org.springframework.boot 8 | spring-boot-starter-parent 9 | 2.2.0.BUILD-SNAPSHOT 10 | 11 | 12 | 13 | 14 | 1.8 15 | 16 | 17 | 18 | com.example 19 | springboot-web 20 | 0.0.1-SNAPSHOT 21 | war 22 | springboot-web 23 | Demo project for Spring Boot 24 | 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-web 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-tomcat 35 | provided 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-test 40 | test 41 | 42 | 43 | org.junit.vintage 44 | junit-vintage-engine 45 | 46 | 47 | junit 48 | junit 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | org.springframework.boot 58 | spring-boot-maven-plugin 59 | 60 | 61 | 62 | 63 | 64 | 65 | spring-snapshots 66 | Spring Snapshots 67 | https://repo.spring.io/snapshot 68 | 69 | true 70 | 71 | 72 | 73 | spring-milestones 74 | Spring Milestones 75 | https://repo.spring.io/milestone 76 | 77 | 78 | 79 | 80 | spring-snapshots 81 | Spring Snapshots 82 | https://repo.spring.io/snapshot 83 | 84 | true 85 | 86 | 87 | 88 | spring-milestones 89 | Spring Milestones 90 | https://repo.spring.io/milestone 91 | 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /springboot-web/spring boot web访问: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-web/spring boot web访问 -------------------------------------------------------------------------------- /springboot-web/src/main/java/com/example/springbootweb/ServletInitializer.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootweb; 2 | 3 | import org.springframework.boot.builder.SpringApplicationBuilder; 4 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 5 | 6 | public class ServletInitializer extends SpringBootServletInitializer { 7 | 8 | @Override 9 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 10 | return application.sources(SpringbootWebApplication.class); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-web/src/main/java/com/example/springbootweb/SpringbootWebApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootweb; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringbootWebApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringbootWebApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-web/src/main/java/com/example/springbootweb/controller/Index2Controller.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootweb.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.*; 5 | 6 | /** 7 | * @Description 8 | * @Author zhaobaolin 9 | * @Date 2019/6/12 10 | */ 11 | @Controller //当前类申明成控制器bean 没有@ResponseBody的话那么当前控制器的方法返回默认渲染页面 12 | public class Index2Controller { 13 | 14 | //报错 因为没有返回数据对应的这个页面 15 | @GetMapping("/Index2/a") 16 | public int[] a(){ 17 | int[] arr = new int[3]; 18 | arr[0] = 10; 19 | arr[1] = 11; 20 | arr[2] = 12; 21 | return arr; 22 | } 23 | 24 | //报错 因为没有返回数据对应的这个页面 25 | @PostMapping("/Index2/b") 26 | public String b(){ 27 | return "你好,这是 Index2的 b方法"; 28 | } 29 | 30 | //返回 "你好,这是 Index2的 c方法" 因为加了@ResponseBody代表直接返回数据 而不是渲染页面 31 | @ResponseBody 32 | @RequestMapping("/Index2/c") 33 | public String c(){ 34 | return "你好,这是 Index2的 c方法"; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /springboot-web/src/main/java/com/example/springbootweb/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootweb.controller; 2 | 3 | import org.springframework.web.bind.annotation.*; 4 | 5 | /** 6 | * @Description 7 | * @Author zhaobaolin 8 | * @Date 2019/6/12 9 | */ 10 | @RestController //@Controller 和 @ResponseBody的结合 代表当前类申明成控制器bean,并且当前类中的所有方法都是直接返回数据,而不是渲染页面。 11 | @RequestMapping("/Index") //当前控制器可以接受任意请求 get、put、post、delete等 12 | public class IndexController { 13 | 14 | 15 | //当前方法只能get请求 返回数组数据 16 | @GetMapping("/a") 17 | public int[] a(){ 18 | int[] arr = new int[3]; 19 | arr[0] = 10; 20 | arr[1] = 11; 21 | arr[2] = 12; 22 | return arr; 23 | } 24 | 25 | //当前方法只能post请求 返回字符串 26 | @PostMapping("/b") 27 | public String b(){ 28 | return "你好,这是b方法"; 29 | } 30 | 31 | //当前方法允许任意请求 返回字符串 32 | @RequestMapping("/c") 33 | public String c(){ 34 | return "你好,这是c方法"; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /springboot-web/src/main/java/com/example/springbootweb/controller/User.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootweb.controller; 2 | 3 | /** 4 | * @Description 5 | * @Author zhaobaolin 6 | * @Date 2019/6/17 7 | */ 8 | public class User{ 9 | private String name; 10 | private Long sex; 11 | 12 | public String getName() { 13 | return name; 14 | } 15 | 16 | public void setName(String name) { 17 | this.name = name; 18 | } 19 | 20 | public Long getSex() { 21 | return sex; 22 | } 23 | 24 | public void setSex(Long sex) { 25 | this.sex = sex; 26 | } 27 | 28 | @Override 29 | public String toString() { 30 | return "User{" + 31 | "name='" + name + '\'' + 32 | ", sex=" + sex + 33 | '}'; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /springboot-web/src/main/java/com/example/springbootweb/controller/UserValid.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootweb.controller; 2 | 3 | import javax.validation.constraints.Max; 4 | import javax.validation.constraints.NotNull; 5 | 6 | /** 7 | * @Description 8 | * @Author zhaobaolin 9 | * @Date 2019/6/17 10 | */ 11 | public class UserValid{ 12 | 13 | @NotNull 14 | private String name; 15 | 16 | @Max(value = 10,message = "最大值是10") 17 | private Long sex; 18 | 19 | /** 20 | * @Null 可以为空 21 | * @NotNll 不能为空 22 | * @NotBlank 验证字符串不能为空 也包括空字符串 23 | * @NotEmpty 不能为null或者集合不能为空 其实和@NotBlank没什么区别 24 | * 25 | * @Size(min=,max=,message=) 长度限制 26 | * 27 | * @Min 最小值 28 | * @Max 最大值 29 | * @Range(min=,max=,message=) 限定范围 30 | * 31 | * @Email(message=) 邮箱验证 32 | * 33 | * @Pattern 自定义正则验证 34 | */ 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 Long getSex() { 46 | return sex; 47 | } 48 | 49 | public void setSex(Long sex) { 50 | this.sex = sex; 51 | } 52 | 53 | @Override 54 | public String toString() { 55 | return "User{" + 56 | "name='" + name + '\'' + 57 | ", sex=" + sex + 58 | '}'; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /springboot-web/src/main/java/com/example/springbootweb/controller/ValidController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootweb.controller; 2 | 3 | import org.springframework.validation.Errors; 4 | import org.springframework.validation.FieldError; 5 | import org.springframework.validation.ObjectError; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import javax.validation.Valid; 10 | import java.util.List; 11 | 12 | /** 13 | * @Description 14 | * @Author zhaobaolin 15 | * @Date 2019/6/18 16 | */ 17 | @RestController 18 | @RequestMapping("/Valid") 19 | public class ValidController { 20 | 21 | //数据验证 注意验证不通过不会终止程序 必须要手动遍历验证结果对象Errors查看是否验证不通过 22 | //访问 http://localhost:8080/Valid/a?sex=33 23 | @RequestMapping("/a") 24 | public String a(@Valid UserValid userValid, Errors errors){ 25 | 26 | List list = errors.getAllErrors(); 27 | for (ObjectError objectError : list){ 28 | FieldError fieldError = (FieldError)objectError; 29 | //输出 userValid --> name ---> 不能为null userValid --> sex ---> 最大值是10 30 | System.out.println(fieldError.getObjectName()+" --> "+fieldError.getField()+" ---> "+fieldError.getDefaultMessage()); 31 | } 32 | 33 | //输出:User{name='null', sex=33} 34 | System.out.println(userValid.toString()); 35 | return userValid.toString(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /springboot-web/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /springboot-web/src/test/java/com/example/springbootweb/SpringbootWebApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootweb; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringbootWebApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-websocket/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/** 5 | !**/src/test/** 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | 30 | ### VS Code ### 31 | .vscode/ 32 | -------------------------------------------------------------------------------- /springboot-websocket/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhao-baolin/springboot-example/ec708ba2087f97d029d8fbc6ab4b17b9b99bdedd/springboot-websocket/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /springboot-websocket/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /springboot-websocket/src/main/java/com/example/springbootwebsocket/ServletInitializer.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootwebsocket; 2 | 3 | import org.springframework.boot.builder.SpringApplicationBuilder; 4 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 5 | 6 | public class ServletInitializer extends SpringBootServletInitializer { 7 | 8 | @Override 9 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 10 | return application.sources(SpringbootWebsocketApplication.class); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-websocket/src/main/java/com/example/springbootwebsocket/SpringbootWebsocketApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootwebsocket; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringbootWebsocketApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringbootWebsocketApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-websocket/src/main/java/com/example/springbootwebsocket/config/WebSocketConfig.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootwebsocket.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.messaging.simp.config.MessageBrokerRegistry; 5 | import org.springframework.web.socket.config.annotation.*; 6 | 7 | /** 8 | * @Description 9 | * @Author zhaobaolin 10 | * @Date 2019/6/30 11 | */ 12 | @Configuration 13 | @EnableWebSocketMessageBroker 14 | public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { 15 | 16 | @Override 17 | public void registerStompEndpoints(StompEndpointRegistry registry) { 18 | registry.addEndpoint("/common").withSockJS();//对外暴露地址 19 | } 20 | 21 | @Override 22 | public void configureMessageBroker(MessageBrokerRegistry registry) { 23 | registry.enableSimpleBroker("/topic/hello");//配置一个topic 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /springboot-websocket/src/main/java/com/example/springbootwebsocket/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootwebsocket.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | 6 | /** 7 | * @Description 8 | * @Author zhaobaolin 9 | * @Date 2019/6/30 10 | */ 11 | @Controller 12 | @RequestMapping("/Index") 13 | public class IndexController { 14 | 15 | // 同时打开三个页面 广播式推送 16 | // http://localhost:8080/Index/index_a 17 | // http://localhost:8080/Index/index_b 18 | // http://localhost:8080/Index/index_c 19 | @RequestMapping("/index_a") 20 | public String indexa(){ 21 | return "indexa"; 22 | } 23 | 24 | @RequestMapping("/index_b") 25 | public String indexb(){ 26 | return "indexb"; 27 | } 28 | 29 | @RequestMapping("/index_c") 30 | public String indexc(){ 31 | return "indexc"; 32 | } 33 | 34 | 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /springboot-websocket/src/main/java/com/example/springbootwebsocket/controller/WsController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootwebsocket.controller; 2 | 3 | import com.example.springbootwebsocket.entity.RequestMessage; 4 | import com.example.springbootwebsocket.entity.ResponseMessage; 5 | import org.springframework.messaging.handler.annotation.MessageMapping; 6 | import org.springframework.messaging.handler.annotation.SendTo; 7 | import org.springframework.stereotype.Controller; 8 | 9 | /** 10 | * @Description 11 | * @Author zhaobaolin 12 | * @Date 2019/6/30 13 | */ 14 | @Controller 15 | public class WsController { 16 | 17 | //广播 18 | @MessageMapping("/hello") 19 | @SendTo("/topic/hello") 20 | public ResponseMessage hello(RequestMessage message){ 21 | return new ResponseMessage(message.getName()+":大家好,我是"+message.getName()); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /springboot-websocket/src/main/java/com/example/springbootwebsocket/entity/RequestMessage.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootwebsocket.entity; 2 | 3 | /** 4 | * @Description 5 | * @Author zhaobaolin 6 | * @Date 2019/6/30 7 | */ 8 | public class RequestMessage { 9 | private String name; 10 | 11 | public String getName() { 12 | return name; 13 | } 14 | 15 | public void setName(String name) { 16 | this.name = name; 17 | } 18 | 19 | @Override 20 | public String toString() { 21 | return "RequestMessage{" + 22 | "name='" + name + '\'' + 23 | '}'; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /springboot-websocket/src/main/java/com/example/springbootwebsocket/entity/ResponseMessage.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootwebsocket.entity; 2 | 3 | /** 4 | * @Description 5 | * @Author zhaobaolin 6 | * @Date 2019/6/30 7 | */ 8 | public class ResponseMessage { 9 | private String content; 10 | 11 | public ResponseMessage() { 12 | } 13 | 14 | public ResponseMessage(String content) { 15 | this.content = content; 16 | } 17 | 18 | public String getContent() { 19 | return content; 20 | } 21 | 22 | public void setContent(String content) { 23 | this.content = content; 24 | } 25 | 26 | @Override 27 | public String toString() { 28 | return "ResponseMessage{" + 29 | "content='" + content + '\'' + 30 | '}'; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /springboot-websocket/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.thymeleaf.cache=false 2 | -------------------------------------------------------------------------------- /springboot-websocket/src/main/resources/templates/indexa.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | index_a 6 | 7 | 8 | 9 | 10 | 13 |
14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /springboot-websocket/src/main/resources/templates/indexb.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | index_b 6 | 7 | 8 | 9 | 10 | 13 |
14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /springboot-websocket/src/main/resources/templates/indexc.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | index_c 6 | 7 | 8 | 9 | 10 | 13 |
14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /springboot-websocket/src/test/java/com/example/springbootwebsocket/SpringbootWebsocketApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootwebsocket; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringbootWebsocketApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | --------------------------------------------------------------------------------