├── README.md ├── SpringBoot-参考指南.pdf ├── springboot-cross-orgin ├── origin-web │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── com │ │ │ └── hehe │ │ │ ├── OriginWebApplication.java │ │ │ └── entity │ │ │ └── User.java │ │ └── resources │ │ ├── application.yml │ │ └── static │ │ └── index.html ├── pom.xml └── target-web │ ├── pom.xml │ └── src │ └── main │ ├── java │ └── com │ │ └── hehe │ │ ├── TargetWebApplication.java │ │ ├── config │ │ └── GlobalCorsConfig.java │ │ ├── entity │ │ └── User.java │ │ └── web │ │ └── UserController.java │ └── resources │ └── application.yml ├── springboot-data-jpa-save ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hehe │ │ │ ├── JpaApplication.java │ │ │ ├── common │ │ │ └── SimpleJpaRepositoryImpl.java │ │ │ ├── controller │ │ │ └── UserController.java │ │ │ ├── entity │ │ │ └── User.java │ │ │ └── repository │ │ │ └── UserRepository.java │ └── resources │ │ ├── application.yml │ │ ├── t_user.sql │ │ └── templates │ │ └── 1.html │ └── test │ └── java │ └── com │ └── hehe │ └── SpringbootDataJpaSaveApplicationTests.java ├── springboot-data-jpa ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hehe │ │ │ ├── JpaApplication.java │ │ │ ├── controller │ │ │ └── UserController.java │ │ │ ├── entity │ │ │ ├── R.java │ │ │ └── User.java │ │ │ ├── repository │ │ │ └── UserRepository.java │ │ │ └── service │ │ │ ├── UserService.java │ │ │ └── UserServiceImpl.java │ └── resources │ │ ├── application.yml │ │ └── t_user.sql │ └── test │ └── java │ └── com │ └── hehe │ └── JpaApplicationTest.java ├── springboot-date-format-anno ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── hehe │ │ ├── DateFormatAnnoApplication.java │ │ ├── config │ │ └── DateFormatConfig.java │ │ ├── entity │ │ └── User.java │ │ └── web │ │ └── UserController.java │ └── resources │ └── application.yml ├── springboot-date-format ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── hehe │ │ ├── DateFormatApplication.java │ │ ├── config │ │ └── GlobalDateFormatConfig.java │ │ ├── entity │ │ └── User.java │ │ └── web │ │ └── UserController.java │ └── resources │ ├── application.yml │ └── views │ └── user │ └── user.html ├── springboot-error-controller ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hehe │ │ │ ├── ErrorControllerApplication.java │ │ │ └── error │ │ │ ├── ErrorInfo.java │ │ │ ├── ErrorInfoBuilder.java │ │ │ └── GlobalErrorController.java │ └── resources │ │ ├── application.yml │ │ └── templates │ │ └── error.html │ └── test │ └── java │ └── com │ └── hehe │ └── SpringBootErrorControllerApplicationTests.java ├── springboot-error-handler ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hehe │ │ │ ├── ErrorHandlerApplication.java │ │ │ └── error │ │ │ ├── ErrorInfo.java │ │ │ ├── ErrorInfoBuilder.java │ │ │ └── GlobalErrorHandler.java │ └── resources │ │ ├── application.yml │ │ └── templates │ │ └── error.html │ └── test │ └── java │ └── com │ └── hehe │ └── SpringbootErrorHandlerApplicationTests.java ├── springboot-externalized-configuration ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hehe │ │ │ ├── ExternalizedConfigurationApplication.java │ │ │ ├── config │ │ │ ├── HerDataSource.java │ │ │ └── MyDataSource.java │ │ │ └── controller │ │ │ └── MyController.java │ └── resources │ │ └── application.yml │ └── test │ └── java │ └── com │ └── hehe │ └── ExternalizedConfigurationApplicationTest.java ├── springboot-helloworld ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hehe │ │ │ └── HelloworldApplication.java │ └── resources │ │ ├── application.yml │ │ ├── banner.txt │ │ └── favicon.ico │ └── test │ └── java │ └── com │ └── hehe │ └── SpringbootHelloworldApplicationTests.java ├── springboot-integration ├── mm-entity │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── com │ │ └── hehe │ │ └── integration │ │ ├── common │ │ └── R.java │ │ └── user │ │ └── User.java ├── mm-repo │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── com │ │ └── hehe │ │ └── integration │ │ └── user │ │ └── UserRepository.java ├── mm-service │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── com │ │ └── hehe │ │ └── integration │ │ └── user │ │ ├── UserService.java │ │ └── UserServiceImpl.java ├── mm-web │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── com │ │ │ └── hehe │ │ │ └── integration │ │ │ ├── MmWebApplication.java │ │ │ ├── hello │ │ │ └── HelloController.java │ │ │ └── user │ │ │ └── UserController.java │ │ └── resources │ │ └── application.yml └── pom.xml ├── springboot-locale-i18n ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hehe │ │ │ ├── LocaleI18nApplication.java │ │ │ └── locale │ │ │ └── LocaleConfig.java │ └── resources │ │ ├── application.yml │ │ ├── static │ │ └── i18n │ │ │ ├── messages.properties │ │ │ ├── messages_en_US.properties │ │ │ ├── messages_zh_CN.properties │ │ │ └── messages_zh_TW.properties │ │ └── views │ │ └── user │ │ └── login.html │ └── test │ └── java │ └── com │ └── hehe │ └── LocaleI18NApplicationTests.java ├── springboot-mybatis-annotation ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hehe │ │ │ ├── MybatisAnnotationApplication.java │ │ │ ├── controller │ │ │ └── UserController.java │ │ │ ├── mapper │ │ │ ├── UserMapper.java │ │ │ └── UserSqlProvider.java │ │ │ └── pojo │ │ │ └── User.java │ └── resources │ │ └── application.yml │ └── test │ └── java │ └── com │ └── hehe │ └── MybatisAnnotationApplicationTests.java ├── springboot-mybatis-common ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── hehe │ │ ├── MybatisCommonApplication.java │ │ ├── controller │ │ └── UserController.java │ │ ├── entity │ │ └── User.java │ │ ├── mapper │ │ └── UserMapper.java │ │ ├── service │ │ ├── UserService.java │ │ └── UserServiceImpl.java │ │ └── tools │ │ ├── BaseService.java │ │ └── BaseServiceImpl.java │ └── resources │ ├── application.yml │ └── t_user.sql ├── springboot-mybatis ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hehe │ │ │ ├── MybatisApplication.java │ │ │ ├── controller │ │ │ └── UserController.java │ │ │ ├── mapper │ │ │ └── UserMapper.java │ │ │ ├── pojo │ │ │ ├── R.java │ │ │ └── User.java │ │ │ └── service │ │ │ ├── UserService.java │ │ │ └── UserServiceImpl.java │ └── resources │ │ ├── application.yml │ │ └── t_user.sql │ └── test │ └── java │ └── com │ └── hehe │ └── MybatisApplicationTest.java ├── springboot-nginx ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hehe │ │ │ └── SpringBootNginxApplication.java │ └── resources │ │ ├── application.properties │ │ └── nginx-1.9.9 │ │ ├── conf │ │ ├── fastcgi.conf │ │ ├── fastcgi_params │ │ ├── koi-utf │ │ ├── koi-win │ │ ├── mime.types │ │ ├── nginx.conf │ │ ├── scgi_params │ │ ├── uwsgi_params │ │ └── win-utf │ │ ├── contrib │ │ ├── README │ │ ├── geo2nginx.pl │ │ ├── unicode2nginx │ │ │ ├── koi-utf │ │ │ ├── unicode-to-nginx.pl │ │ │ └── win-utf │ │ └── vim │ │ │ ├── ftdetect │ │ │ └── nginx.vim │ │ │ ├── indent │ │ │ └── nginx.vim │ │ │ └── syntax │ │ │ └── nginx.vim │ │ ├── docs │ │ ├── CHANGES │ │ ├── CHANGES.ru │ │ ├── LICENSE │ │ ├── OpenSSL.LICENSE │ │ ├── PCRE.LICENCE │ │ ├── README │ │ └── zlib.LICENSE │ │ ├── html │ │ ├── 50x.html │ │ ├── hehe │ │ │ ├── index.html │ │ │ └── js │ │ │ │ └── jquery.js │ │ └── index.html │ │ ├── logs │ │ └── nginx.pid │ │ ├── 修改Nginx配置.lnk │ │ ├── 启动Nginx.lnk │ │ └── 结束Nginx.bat │ └── test │ └── java │ └── com │ └── hehe │ └── SpringBootNginxApplicationTests.java ├── springboot-schedule-task ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hehe │ │ │ ├── ScheduleTaskApplication.java │ │ │ └── config │ │ │ ├── CompleteScheduleConfig.java │ │ │ └── SimpleScheduleConfig.java │ └── resources │ │ └── application.yml │ └── test │ └── java │ └── com │ └── hehe │ └── ScheduleTaskApplicationTests.java ├── springboot-send-mail ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hehe │ │ │ ├── MailApplication.java │ │ │ ├── service │ │ │ └── MailService.java │ │ │ ├── vo │ │ │ └── MailVo.java │ │ │ └── web │ │ │ └── MailController.java │ └── resources │ │ ├── application.yml │ │ └── views │ │ └── mail │ │ └── sendMail.html │ └── test │ └── java │ └── com │ └── hehe │ └── springbootsendmail │ └── SpringbootSendMailApplicationTests.java ├── springboot-swagger2-auto-close ├── pom.xml ├── src │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── hehe │ │ │ │ ├── Swagger2AutoApplication.java │ │ │ │ ├── config │ │ │ │ └── Swagger2Config.java │ │ │ │ ├── controller │ │ │ │ └── UserController.java │ │ │ │ └── entity │ │ │ │ └── User.java │ │ └── resources │ │ │ ├── application-dev.yml │ │ │ ├── application-prod.yml │ │ │ └── application.yml │ └── test │ │ └── java │ │ └── com │ │ └── hehe │ │ └── SpringbootSwagger2AutoApplicationTests.java └── target │ └── classes │ ├── application.yml │ └── com │ └── hehe │ ├── config │ └── Swagger2Config.class │ ├── controller │ └── UserController.class │ └── entity │ └── User.class ├── springboot-swagger2 ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hehe │ │ │ ├── Swagger2Application.java │ │ │ ├── config │ │ │ └── Swagger2Config.java │ │ │ ├── controller │ │ │ ├── HelloController.java │ │ │ └── UserController.java │ │ │ └── entity │ │ │ └── User.java │ └── resources │ │ ├── META-INF │ │ └── resources │ │ │ ├── swagger-ui.html │ │ │ └── webjars │ │ │ └── springfox-swagger-ui │ │ │ └── lang │ │ │ └── zh-cn.js │ │ └── application.yml │ └── test │ └── java │ └── com │ └── hehe │ └── SpringbootSwagger2ApplicationTests.java ├── springboot-web-jsp ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hehe │ │ │ ├── WebJspApplication.java │ │ │ └── controller │ │ │ └── HelloController.java │ ├── resources │ │ ├── application.yml │ │ └── static │ │ │ └── doge.gif │ └── webapp │ │ └── WEB-INF │ │ └── views │ │ └── index.jsp │ └── test │ └── java │ └── com │ └── hehe │ └── WebJspApplicationTest.java ├── springboot-web-thymeleaf-enhance ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hehe │ │ │ ├── ThymeleafEnhanceApplication.java │ │ │ ├── entity │ │ │ └── User.java │ │ │ └── web │ │ │ └── UserController.java │ └── resources │ │ ├── application.yml │ │ └── views │ │ ├── common │ │ └── head.html │ │ └── user │ │ ├── user.html │ │ └── userForm.html │ └── test │ └── java │ └── com │ └── hehe │ └── ThymeleafEnhanceApplicationTests.java ├── springboot-web-thymeleaf ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── hehe │ │ ├── ThymeleafApplication.java │ │ ├── controller │ │ └── UserController.java │ │ └── entity │ │ └── User.java │ └── resources │ ├── application.yml │ └── views │ ├── index.html │ ├── user.html │ └── userList.html ├── springboot-webjars ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hehe │ │ │ └── SpringbootWebjarsApplication.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── hehe │ └── SpringbootWebjarsApplicationTests.java └── springboot-websocket-chat ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── hehe │ │ ├── WebSocketChatApplication.java │ │ └── chat │ │ ├── Message.java │ │ ├── WebSocketChatServer.java │ │ └── WebSocketConfig.java └── resources │ ├── application.yml │ ├── static │ └── img │ │ └── login-bg.jpg │ └── templates │ ├── chat.html │ └── login.html └── test └── java └── com └── hehe └── WebsocketChatApplicationTests.java /README.md: -------------------------------------------------------------------------------- 1 | 推荐阅读本教程的三大理由: 2 | 1. 文章内容均为原创,结合官方文档和实战经验编写。 3 | 2. 文章结构经过细致整理,对新人学习更加友好。 4 | 3. 精选常用技术,不求全面,但求精华!! 5 | 6 | # SpringBoot 源码精读 图文教程 7 | 8 | ##### 源码下载:[《SpringBoot 基础教程-Git 》](https://github.com/yizhiwazi/springboot-socks) 9 | 10 | — Hey Man,Don't forget to Star or Fork . — 11 | 12 | ##### 专题阅读:[《SpringBoot 从入门到上瘾 》](http://www.jianshu.com/p/964370d9374e) 13 | 14 | 15 | # 快速入门 16 | ##### [SpringBoot 快速入门 + 3分钟打造RestAPI](http://www.jianshu.com/p/17e0e55c88db) 17 | 18 | ##### [SpringBoot 使用Spring Initializr快速构建工程 ](http://www.jianshu.com/p/d2b08a671e27) 19 | 20 | ##### [SpringBoot 工程结构推荐(新手必看)](http://www.jianshu.com/p/6dcfe16d91d0) 21 | 22 | # 开发工具 23 | ##### [IntelliJ IDEA 使用教程(2017图文版) -- 从入门到上瘾](http://www.jianshu.com/p/9c65b7613c30) 24 | 25 | # 外部配置 26 | ##### [SpringBoot 配置文件详解(告别XML)](http://www.jianshu.com/p/60b34464ca58) 27 | 28 | ##### [SpringBoot 如何下载最新的版本依赖](http://www.jianshu.com/p/6654e9fec93b) 29 | 30 | 31 | # 数据库案例 32 | ##### [SpringBoot 快速整合MyBatis(去XML化+初体验)](http://www.jianshu.com/p/fa89b59ade40) 33 | 34 | ##### [SpringBoot 快速整合Mybatis(去XML化+注解进阶)](http://www.jianshu.com/p/828d2bd12b2f) 35 | 36 | ##### [SpringBoot 快速整合Mybatis(去XML化+通用Service)](http://www.jianshu.com/p/4b4e75952e74) 37 | 38 | ##### [SpringBoot 快速整合SpringDataJPA (优雅篇)](http://www.jianshu.com/p/71087bafdcdd) 39 | 40 | ##### [SpringBoot 快速开启事务(附常见坑点)](http://www.jianshu.com/p/380a9d980ca5) 41 | 42 | # 模板引擎 43 | ##### [SpringBoot 添加对JSP的支持(附常见坑点)](http://www.jianshu.com/p/de939365c472) 44 | 45 | ##### [SpringBoot Thymeleaf--基于HTML5的现代模板引擎](http://www.jianshu.com/p/8dc48fa74e7e) 46 | 47 | # 异常处理 48 | ##### [SpringBoot 统一异常处理(附核心工具类-ErrorInfoBuilder)](http://www.jianshu.com/p/3998ea8b53a8) 49 | 50 | # 接口文档 51 | ##### [SpringBoot 使用Swagger2打造在线接口文档(附汉化教程)](http://www.jianshu.com/p/7e543f0f0bd8) 52 | 53 | # 多模块划分(Maven) 54 | ##### [SpringBoot 多模块项目实践(附打包方法)](http://www.jianshu.com/p/59ceea4f029d) 55 | 56 | # 跨域访问 57 | ##### [SpringBoot 实现前后端分离的跨域访问(CORS)](http://www.jianshu.com/p/477e7eaa6c2f) 58 | 59 | ##### [SpringBoot 实现前后端分离的跨域访问(Nginx)](http://www.jianshu.com/p/520021853827) 60 | 61 | # 定时任务 62 | 63 | ##### [SpringBoot 创建定时任务(配合数据库动态执行)](http://www.jianshu.com/p/d160f2536de7) 64 | 65 | # 趣味阅读 66 | ##### [【团队必备】阿里巴巴Java开发规约插件(附使用手册)](http://www.jianshu.com/p/cd19d42b00c8) 67 | -------------------------------------------------------------------------------- /SpringBoot-参考指南.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yizhiwazi/springboot-socks/763dce7c4a4960dc6389eb96087a39ed3a278896/SpringBoot-参考指南.pdf -------------------------------------------------------------------------------- /springboot-cross-orgin/origin-web/pom.xml: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="UTF-8"?> 2 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 | <modelVersion>4.0.0</modelVersion> 5 | 6 | <!--基本信息--> 7 | <groupId>com.hehe</groupId> 8 | <artifactId>origin-web</artifactId> 9 | <version>0.0.1-SNAPSHOT</version> 10 | <packaging>jar</packaging> 11 | <name>origin-web</name> 12 | <description>SpringBoot Origin Web</description> 13 | 14 | <!--继承信息--> 15 | <parent> 16 | <groupId>org.springframework.boot</groupId> 17 | <artifactId>spring-boot-starter-parent</artifactId> 18 | <version>2.1.0.RELEASE</version> 19 | <relativePath/> 20 | </parent> 21 | 22 | <!--依赖管理 --> 23 | <dependencies> 24 | <dependency><!--添加Web依赖 --> 25 | <groupId>org.springframework.boot</groupId> 26 | <artifactId>spring-boot-starter-web</artifactId> 27 | </dependency> 28 | <dependency><!--添加jQuery依赖 --> 29 | <groupId>org.webjars</groupId> 30 | <artifactId>jquery</artifactId> 31 | <version>3.3.1</version> 32 | </dependency> 33 | <dependency><!--添加Test依赖 --> 34 | <groupId>org.springframework.boot</groupId> 35 | <artifactId>spring-boot-starter-test</artifactId> 36 | <scope>test</scope> 37 | </dependency> 38 | </dependencies> 39 | 40 | <!--指定远程仓库(含插件)--> 41 | <repositories> 42 | <repository> 43 | <id>spring-snapshots</id> 44 | <url>http://repo.spring.io/snapshot</url> 45 | <snapshots> 46 | <enabled>true</enabled> 47 | </snapshots> 48 | </repository> 49 | <repository> 50 | <id>spring-milestones</id> 51 | <url>http://repo.spring.io/milestone</url> 52 | </repository> 53 | </repositories> 54 | <pluginRepositories> 55 | <pluginRepository> 56 | <id>spring-snapshots</id> 57 | <url>http://repo.spring.io/snapshot</url> 58 | </pluginRepository> 59 | <pluginRepository> 60 | <id>spring-milestones</id> 61 | <url>http://repo.spring.io/milestone</url> 62 | </pluginRepository> 63 | </pluginRepositories> 64 | </project> 65 | -------------------------------------------------------------------------------- /springboot-cross-orgin/origin-web/src/main/java/com/hehe/OriginWebApplication.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class OriginWebApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(OriginWebApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /springboot-cross-orgin/origin-web/src/main/java/com/hehe/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.hehe.entity; 2 | 3 | public class User { 4 | 5 | private String userId; 6 | private String username; 7 | private String password; 8 | 9 | public String getUserId() { 10 | return userId; 11 | } 12 | 13 | public void setUserId(String userId) { 14 | this.userId = userId; 15 | } 16 | 17 | public String getUsername() { 18 | return username; 19 | } 20 | 21 | public void setUsername(String username) { 22 | this.username = username; 23 | } 24 | 25 | public String getPassword() { 26 | return password; 27 | } 28 | 29 | public void setPassword(String password) { 30 | this.password = password; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "User{" + 36 | "userId='" + userId + '\'' + 37 | ", username='" + username + '\'' + 38 | ", password='" + password + '\'' + 39 | '}'; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /springboot-cross-orgin/origin-web/src/main/resources/application.yml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yizhiwazi/springboot-socks/763dce7c4a4960dc6389eb96087a39ed3a278896/springboot-cross-orgin/origin-web/src/main/resources/application.yml -------------------------------------------------------------------------------- /springboot-cross-orgin/origin-web/src/main/resources/static/index.html: -------------------------------------------------------------------------------- 1 | <!DOCTYPE html> 2 | <html> 3 | <head> 4 | <meta charset="UTF-8"/> 5 | <title>Origin Web</title> 6 | </head> 7 | <body> 8 | 9 | <h2>Origin Web</h2> 10 | 11 | <div id="user1"></div> 12 | 13 | <div id="user2"></div> 14 | 15 | 16 | </body> 17 | 18 | <script src="/webjars/jquery/3.3.1/jquery.js"></script> 19 | 20 | <script> 21 | $.ajax({ 22 | url: 'http://localhost:9000/user/findByObject', 23 | type: "post", 24 | data: {userId: '1'},//发送数据 25 | xhrFields: { 26 | withCredentials: true//表示跨域时要求客户端发送Cookie等认证信息 涉及登陆必须开启 27 | }, 28 | success: function (data) { 29 | $("#user1").html("跨域成功:" + data.username); 30 | }, 31 | error: function (xhr) { 32 | console.log(xhr); 33 | $("#user1").html("跨域失败:" + xhr.responseText); 34 | } 35 | }) 36 | 37 | $.ajax({ 38 | url: 'http://localhost:9000/user/findByJson', 39 | type: "post", 40 | contentType: 'application/json',//发送格式(JSON串) 41 | data: JSON.stringify({userId: '1'}),//发送数据(JSON串) 42 | xhrFields: { 43 | withCredentials: true //表示跨域时要求客户端发送Cookie等认证信息 涉及登陆必须开启 44 | }, 45 | success: function (data) { 46 | $("#user2").html("跨域成功:" + data.username); 47 | }, 48 | error: function (xhr) { 49 | console.log(xhr); 50 | $("#user2").html("跨域失败:" + xhr.responseText); 51 | } 52 | }) 53 | </script> 54 | 55 | </html> -------------------------------------------------------------------------------- /springboot-cross-orgin/pom.xml: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="UTF-8"?> 2 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 | <modelVersion>4.0.0</modelVersion> 5 | 6 | <groupId>com.hehe</groupId> 7 | <artifactId>springboot-cross-orgin</artifactId> 8 | <version>0.0.1-SNAPSHOT</version> 9 | <packaging>pom</packaging> 10 | 11 | <modules> 12 | <module>origin-web</module> 13 | <module>target-web</module> 14 | </modules> 15 | 16 | </project> 17 | -------------------------------------------------------------------------------- /springboot-cross-orgin/target-web/pom.xml: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="UTF-8"?> 2 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 | <modelVersion>4.0.0</modelVersion> 5 | 6 | <!--基本信息--> 7 | <groupId>com.hehe</groupId> 8 | <artifactId>target-web</artifactId> 9 | <version>0.0.1-SNAPSHOT</version> 10 | <packaging>jar</packaging> 11 | <name>target-web</name> 12 | <description>Spring Boot Target Web</description> 13 | 14 | <!--继承信息--> 15 | <parent> 16 | <groupId>org.springframework.boot</groupId> 17 | <artifactId>spring-boot-starter-parent</artifactId> 18 | <version>2.1.0.RELEASE</version> 19 | <relativePath/> 20 | </parent> 21 | 22 | <!--依赖管理 --> 23 | <dependencies> 24 | <dependency><!--添加Web依赖 --> 25 | <groupId>org.springframework.boot</groupId> 26 | <artifactId>spring-boot-starter-web</artifactId> 27 | </dependency> 28 | <dependency><!--添加Test依赖 --> 29 | <groupId>org.springframework.boot</groupId> 30 | <artifactId>spring-boot-starter-test</artifactId> 31 | <scope>test</scope> 32 | </dependency> 33 | </dependencies> 34 | 35 | <!--指定远程仓库(含插件)--> 36 | <repositories> 37 | <repository> 38 | <id>spring-snapshots</id> 39 | <url>http://repo.spring.io/snapshot</url> 40 | <snapshots> 41 | <enabled>true</enabled> 42 | </snapshots> 43 | </repository> 44 | <repository> 45 | <id>spring-milestones</id> 46 | <url>http://repo.spring.io/milestone</url> 47 | </repository> 48 | </repositories> 49 | <pluginRepositories> 50 | <pluginRepository> 51 | <id>spring-snapshots</id> 52 | <url>http://repo.spring.io/snapshot</url> 53 | </pluginRepository> 54 | <pluginRepository> 55 | <id>spring-milestones</id> 56 | <url>http://repo.spring.io/milestone</url> 57 | </pluginRepository> 58 | </pluginRepositories> 59 | 60 | </project> 61 | -------------------------------------------------------------------------------- /springboot-cross-orgin/target-web/src/main/java/com/hehe/TargetWebApplication.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class TargetWebApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(TargetWebApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /springboot-cross-orgin/target-web/src/main/java/com/hehe/config/GlobalCorsConfig.java: -------------------------------------------------------------------------------- 1 | package com.hehe.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.core.Ordered; 6 | import org.springframework.core.annotation.Order; 7 | import org.springframework.http.HttpHeaders; 8 | import org.springframework.web.servlet.config.annotation.CorsRegistry; 9 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 10 | 11 | @Configuration 12 | public class GlobalCorsConfig { 13 | 14 | /** 15 | * 设置全局跨域 16 | */ 17 | @Order(Ordered.HIGHEST_PRECEDENCE) 18 | @Bean 19 | public WebMvcConfigurer corsConfigurer() { 20 | return new WebMvcConfigurer() { 21 | @Override 22 | public void addCorsMappings(CorsRegistry registry) { 23 | //默认拦截路径 24 | registry.addMapping("/**") 25 | 26 | //表示允许那些原始域进行跨域访问,这里"*"表示允许任意网站,实际开发建议修改为配置项。 27 | .allowedOrigins("*") 28 | 29 | //表示是否允许客户端发送Cookie等凭证信息,这里"true"表示支持发送,涉及登陆此处必须开启。 30 | .allowCredentials(true) 31 | 32 | //表示允许原始域发起哪些请求方式,这里"*"表示支持GET/POST等全部提交方式。 33 | .allowedMethods("*") 34 | 35 | //表示允许原始域携带哪些请求头 这里"*"表示支持全部请求头 36 | .allowedHeaders("*") 37 | 38 | //表示允许暴露哪些响应头,这里特指那些非简单的头部信息,所以用"*"无效。 39 | .exposedHeaders(HttpHeaders.AUTHORIZATION); 40 | } 41 | }; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /springboot-cross-orgin/target-web/src/main/java/com/hehe/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.hehe.entity; 2 | 3 | public class User { 4 | 5 | private String userId; 6 | private String username; 7 | private String password; 8 | 9 | public String getUserId() { 10 | return userId; 11 | } 12 | 13 | public void setUserId(String userId) { 14 | this.userId = userId; 15 | } 16 | 17 | public String getUsername() { 18 | return username; 19 | } 20 | 21 | public void setUsername(String username) { 22 | this.username = username; 23 | } 24 | 25 | public String getPassword() { 26 | return password; 27 | } 28 | 29 | public void setPassword(String password) { 30 | this.password = password; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "User{" + 36 | "userId='" + userId + '\'' + 37 | ", username='" + username + '\'' + 38 | ", password='" + password + '\'' + 39 | '}'; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /springboot-cross-orgin/target-web/src/main/java/com/hehe/web/UserController.java: -------------------------------------------------------------------------------- 1 | package com.hehe.web; 2 | 3 | import com.hehe.entity.User; 4 | import org.springframework.web.bind.annotation.PostMapping; 5 | import org.springframework.web.bind.annotation.RequestBody; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | @RestController 9 | public class UserController { 10 | 11 | /** 12 | * 查询用户(要求前端传递用户对象) 13 | */ 14 | @PostMapping("/user/findByObject") 15 | public User findByObject(User user) { 16 | user.setUsername("admin"); 17 | return user; 18 | } 19 | 20 | /** 21 | * 查询用户(要求前端传递用户JSON) 22 | */ 23 | @PostMapping("/user/findByJson") 24 | public User findByJson(@RequestBody User user) { 25 | user.setUsername("jack"); 26 | return user; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /springboot-cross-orgin/target-web/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 9000 -------------------------------------------------------------------------------- /springboot-data-jpa-save/pom.xml: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="UTF-8"?> 2 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 | <modelVersion>4.0.0</modelVersion> 5 | 6 | <groupId>com.hehe</groupId> 7 | <artifactId>springboot-data-jpa-save</artifactId> 8 | <version>0.0.1-SNAPSHOT</version> 9 | <packaging>jar</packaging> 10 | 11 | <name>springboot-data-jpa-save</name> 12 | <description>使用JPA Save方法不保存Null属性</description> 13 | 14 | <parent> 15 | <groupId>org.springframework.boot</groupId> 16 | <artifactId>spring-boot-starter-parent</artifactId> 17 | <version>2.0.1.RELEASE</version> 18 | <relativePath/> <!-- lookup parent from repository --> 19 | </parent> 20 | 21 | <properties> 22 | <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 | <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 | <java.version>1.8</java.version> 25 | </properties> 26 | 27 | <dependencies> 28 | <dependency> 29 | <groupId>org.springframework.boot</groupId> 30 | <artifactId>spring-boot-starter-web</artifactId> 31 | </dependency> 32 | 33 | <dependency> 34 | <groupId>org.springframework.boot</groupId> 35 | <artifactId>spring-boot-starter-data-jpa</artifactId> 36 | </dependency> 37 | 38 | <dependency> 39 | <groupId>mysql</groupId> 40 | <artifactId>mysql-connector-java</artifactId> 41 | <scope>runtime</scope> 42 | </dependency> 43 | <dependency> 44 | <groupId>org.springframework.boot</groupId> 45 | <artifactId>spring-boot-starter-test</artifactId> 46 | <scope>test</scope> 47 | </dependency> 48 | </dependencies> 49 | 50 | <build> 51 | <plugins> 52 | <plugin> 53 | <groupId>org.springframework.boot</groupId> 54 | <artifactId>spring-boot-maven-plugin</artifactId> 55 | </plugin> 56 | </plugins> 57 | </build> 58 | 59 | 60 | </project> 61 | -------------------------------------------------------------------------------- /springboot-data-jpa-save/src/main/java/com/hehe/JpaApplication.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import com.hehe.common.SimpleJpaRepositoryImpl; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 7 | 8 | @EnableJpaRepositories(value = "com.hehe.repository", repositoryBaseClass = SimpleJpaRepositoryImpl.class) 9 | @SpringBootApplication 10 | public class JpaApplication { 11 | 12 | public static void main(String[] args) { 13 | SpringApplication.run(JpaApplication.class, args); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /springboot-data-jpa-save/src/main/java/com/hehe/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.hehe.controller; 2 | 3 | import com.hehe.entity.User; 4 | import com.hehe.repository.UserRepository; 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 | import java.util.List; 10 | 11 | @RestController 12 | public class UserController { 13 | 14 | @Autowired 15 | private UserRepository userRepository; 16 | 17 | @RequestMapping("/") 18 | public List<User> get() { 19 | 20 | userRepository.save(new User("1", "", null)); 21 | 22 | return userRepository.findAll(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /springboot-data-jpa-save/src/main/java/com/hehe/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.hehe.entity; 2 | 3 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 4 | import org.hibernate.annotations.GenericGenerator; 5 | 6 | import javax.persistence.Entity; 7 | import javax.persistence.GeneratedValue; 8 | import javax.persistence.Id; 9 | import javax.persistence.Table; 10 | 11 | @Entity 12 | @Table(name = "T_USER") 13 | @JsonIgnoreProperties({"handler","hibernateLazyInitializer"}) 14 | public class User { 15 | 16 | @Id 17 | @GenericGenerator(name = "idg",strategy = "uuid") 18 | @GeneratedValue(generator = "idg") 19 | private String userId; 20 | private String username; 21 | private String password; 22 | 23 | public User() { 24 | 25 | } 26 | 27 | public User(String userId, String username, String password) { 28 | this.userId = userId; 29 | this.username = username; 30 | this.password = password; 31 | } 32 | 33 | public String getUserId() { 34 | return userId; 35 | } 36 | 37 | public void setUserId(String userId) { 38 | this.userId = userId; 39 | } 40 | 41 | public String getUsername() { 42 | return username; 43 | } 44 | 45 | public void setUsername(String username) { 46 | this.username = username; 47 | } 48 | 49 | public String getPassword() { 50 | return password; 51 | } 52 | 53 | public void setPassword(String password) { 54 | this.password = password; 55 | } 56 | 57 | @Override 58 | public String toString() { 59 | return "User{" + 60 | "userId='" + userId + '\'' + 61 | ", username='" + username + '\'' + 62 | ", password='" + password + '\'' + 63 | '}'; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /springboot-data-jpa-save/src/main/java/com/hehe/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.hehe.repository; 2 | 3 | import com.hehe.entity.User; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | 7 | public interface UserRepository extends JpaRepository<User, String> { 8 | 9 | 10 | } 11 | -------------------------------------------------------------------------------- /springboot-data-jpa-save/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | url: jdbc:mysql://localhost:3306/socks?useSSL=false 4 | username: root 5 | password: root 6 | driver-class-name: com.mysql.jdbc.Driver 7 | 8 | -------------------------------------------------------------------------------- /springboot-data-jpa-save/src/main/resources/t_user.sql: -------------------------------------------------------------------------------- 1 | drop table if exists t_user; 2 | create table t_user ( 3 | user_id varchar(50), 4 | username varchar(50), 5 | password varchar(50) 6 | ); 7 | 8 | insert into t_user values ('1', 'admin', 'admin'); 9 | insert into t_user values ('2', 'yizhiwazi', '123456'); -------------------------------------------------------------------------------- /springboot-data-jpa-save/src/main/resources/templates/1.html: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yizhiwazi/springboot-socks/763dce7c4a4960dc6389eb96087a39ed3a278896/springboot-data-jpa-save/src/main/resources/templates/1.html -------------------------------------------------------------------------------- /springboot-data-jpa-save/src/test/java/com/hehe/SpringbootDataJpaSaveApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringbootDataJpaSaveApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /springboot-data-jpa/pom.xml: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="UTF-8"?> 2 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 | <modelVersion>4.0.0</modelVersion> 5 | <!--基本信息 --> 6 | <groupId>com.hehe</groupId> 7 | <artifactId>springboot-data-jpa</artifactId> 8 | <version>0.0.1-SNAPSHOT</version> 9 | <packaging>jar</packaging> 10 | <name>springboot-data-jpa</name> 11 | <description>SpringBoot使用JPA快速访问数据库</description> 12 | 13 | <!--继承信息 --> 14 | <parent> 15 | <groupId>org.springframework.boot</groupId> 16 | <artifactId>spring-boot-starter-parent</artifactId> 17 | <version>2.0.0.M4</version> 18 | <relativePath/> 19 | </parent> 20 | 21 | <!--依赖管理 --> 22 | <dependencies> 23 | <dependency> 24 | <groupId>org.springframework.boot</groupId> 25 | <artifactId>spring-boot-starter-web</artifactId> 26 | </dependency> 27 | <dependency> 28 | <groupId>org.springframework.boot</groupId> 29 | <artifactId>spring-boot-starter-data-jpa</artifactId> 30 | </dependency> 31 | <dependency> 32 | <groupId>mysql</groupId> 33 | <artifactId>mysql-connector-java</artifactId> 34 | <scope>runtime</scope> 35 | </dependency> 36 | <dependency> 37 | <groupId>org.springframework.boot</groupId> 38 | <artifactId>spring-boot-starter-test</artifactId> 39 | <scope>test</scope> 40 | </dependency> 41 | </dependencies> 42 | 43 | <!--指定远程仓库(含插件)--> 44 | <repositories> 45 | <repository> 46 | <id>spring-snapshots</id> 47 | <url>http://repo.spring.io/snapshot</url> 48 | <snapshots><enabled>true</enabled></snapshots> 49 | </repository> 50 | <repository> 51 | <id>spring-milestones</id> 52 | <url>http://repo.spring.io/milestone</url> 53 | </repository> 54 | </repositories> 55 | <pluginRepositories> 56 | <pluginRepository> 57 | <id>spring-snapshots</id> 58 | <url>http://repo.spring.io/snapshot</url> 59 | </pluginRepository> 60 | <pluginRepository> 61 | <id>spring-milestones</id> 62 | <url>http://repo.spring.io/milestone</url> 63 | </pluginRepository> 64 | </pluginRepositories> 65 | 66 | <!--构建插件 --> 67 | <build> 68 | <plugins> 69 | <plugin> 70 | <groupId>org.springframework.boot</groupId> 71 | <artifactId>spring-boot-maven-plugin</artifactId> 72 | </plugin> 73 | </plugins> 74 | </build> 75 | 76 | 77 | </project> 78 | -------------------------------------------------------------------------------- /springboot-data-jpa/src/main/java/com/hehe/JpaApplication.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import com.fasterxml.jackson.databind.SerializationFeature; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 9 | 10 | @SpringBootApplication 11 | @EnableJpaRepositories("com.hehe.repository") 12 | public class JpaApplication { 13 | 14 | //解决JPA因为懒加载导致JSON转换错误的问题 15 | @Bean 16 | public ObjectMapper objectMapper() { 17 | return new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); 18 | } 19 | 20 | public static void main(String[] args) { 21 | SpringApplication.run(JpaApplication.class, args); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /springboot-data-jpa/src/main/java/com/hehe/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.hehe.controller; 2 | 3 | import com.hehe.entity.R; 4 | import com.hehe.service.UserService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.PathVariable; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | @RestController 12 | @RequestMapping("/user/*") 13 | public class UserController { 14 | 15 | @Autowired 16 | UserService userService; 17 | 18 | @GetMapping("list") 19 | public R list() { 20 | try { 21 | return R.isOk().data(userService.list()); 22 | } catch (Exception e) { 23 | return R.isFail(e); 24 | } 25 | } 26 | 27 | @GetMapping("list/{username}") 28 | public R listbyname(@PathVariable("username") String username) { 29 | try { 30 | return R.isOk().data(userService.findByUsername(username)); 31 | } catch (Exception e) { 32 | return R.isFail(e); 33 | } 34 | } 35 | 36 | @GetMapping("get/{userId}") 37 | public R get(@PathVariable("userId") String userId) { 38 | try { 39 | return R.isOk().data(userService.get(userId)); 40 | } catch (Exception e) { 41 | return R.isFail(e); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /springboot-data-jpa/src/main/java/com/hehe/entity/R.java: -------------------------------------------------------------------------------- 1 | package com.hehe.entity; 2 | 3 | import java.io.Serializable; 4 | public class R<T> implements Serializable { 5 | 6 | private static final long serialVersionUID = -4577255781088498763L; 7 | private static final int OK = 0; 8 | private static final int FAIL = 1; 9 | private static final int UNAUTHORIZED = 2; 10 | 11 | private T data; //服务端数据 12 | private int status = OK; //状态码 13 | private String msg = ""; //描述信息 14 | 15 | //APIS 16 | public static R isOk(){ 17 | return new R(); 18 | } 19 | public static R isFail(){ 20 | return new R().status(FAIL); 21 | } 22 | public static R isFail(Throwable e){ 23 | return isFail().msg(e); 24 | } 25 | public R msg(Throwable e){ 26 | this.setMsg(e.toString()); 27 | return this; 28 | } 29 | public R data(T data){ 30 | this.setData(data); 31 | return this; 32 | } 33 | public R status(int status){ 34 | this.setStatus(status); 35 | return this; 36 | } 37 | 38 | 39 | //Constructors 40 | public R() { 41 | 42 | } 43 | 44 | //Getter&Setters 45 | public String getMsg() { 46 | return msg; 47 | } 48 | 49 | public void setMsg(String msg) { 50 | this.msg = msg; 51 | } 52 | 53 | public int getStatus() { 54 | return status; 55 | } 56 | 57 | public void setStatus(int status) { 58 | this.status = status; 59 | } 60 | 61 | public T getData() { 62 | return data; 63 | } 64 | 65 | public void setData(T data) { 66 | this.data = data; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /springboot-data-jpa/src/main/java/com/hehe/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.hehe.entity; 2 | 3 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 4 | 5 | import javax.persistence.Column; 6 | import javax.persistence.Entity; 7 | import javax.persistence.Id; 8 | import javax.persistence.Table; 9 | 10 | @Entity 11 | @Table(name = "T_USER") 12 | @JsonIgnoreProperties({"handler","hibernateLazyInitializer"}) 13 | public class User { 14 | 15 | @Id 16 | @Column(name = "USER_ID") 17 | private String userId; 18 | @Column(name = "USERNAME") 19 | private String username; 20 | @Column(name = "PASSWORD") 21 | private String password; 22 | 23 | public User() { 24 | 25 | } 26 | 27 | public User(String userId, String username, String password) { 28 | this.userId = userId; 29 | this.username = username; 30 | this.password = password; 31 | } 32 | 33 | public String getUserId() { 34 | return userId; 35 | } 36 | 37 | public void setUserId(String userId) { 38 | this.userId = userId; 39 | } 40 | 41 | public String getUsername() { 42 | return username; 43 | } 44 | 45 | public void setUsername(String username) { 46 | this.username = username; 47 | } 48 | 49 | public String getPassword() { 50 | return password; 51 | } 52 | 53 | public void setPassword(String password) { 54 | this.password = password; 55 | } 56 | 57 | @Override 58 | public String toString() { 59 | return "User{" + 60 | "userId='" + userId + '\'' + 61 | ", username='" + username + '\'' + 62 | ", password='" + password + '\'' + 63 | '}'; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /springboot-data-jpa/src/main/java/com/hehe/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.hehe.repository; 2 | 3 | import com.hehe.entity.User; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | import java.util.List; 7 | 8 | 9 | public interface UserRepository extends JpaRepository<User, String> { 10 | 11 | //接口方法的名称,符合约定则无需实现即可访问 12 | //@Query("select u from User u where u.name = ?1") 13 | List<User> findByUsername(String username); 14 | } 15 | -------------------------------------------------------------------------------- /springboot-data-jpa/src/main/java/com/hehe/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.hehe.service; 2 | 3 | import com.hehe.entity.User; 4 | import java.util.List; 5 | 6 | public interface UserService { 7 | 8 | 9 | List<User> list(); 10 | 11 | List<User> findByUsername(String username); 12 | 13 | 14 | User get(String userId); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /springboot-data-jpa/src/main/java/com/hehe/service/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.hehe.service; 2 | 3 | import com.hehe.entity.User; 4 | import com.hehe.repository.UserRepository; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | import java.util.List; 9 | 10 | @Service 11 | public class UserServiceImpl implements UserService { 12 | 13 | @Autowired 14 | UserRepository userRepository; 15 | 16 | @Override 17 | public List<User> list() { 18 | return userRepository.findAll(); 19 | } 20 | 21 | @Override 22 | public List<User> findByUsername(String username) { 23 | return userRepository.findByUsername(username); 24 | } 25 | 26 | @Override 27 | public User get(String userId) { 28 | return userRepository.getOne(userId); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /springboot-data-jpa/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | url: jdbc:mysql://localhost:3306/socks?useSSL=false 4 | username: root 5 | password: root 6 | driver-class-name: com.mysql.jdbc.Driver 7 | 8 | -------------------------------------------------------------------------------- /springboot-data-jpa/src/main/resources/t_user.sql: -------------------------------------------------------------------------------- 1 | DROP DATABASE IF EXISTS `socks`; 2 | CREATE DATABASE `socks`; 3 | USE `SOCKS`; 4 | DROP TABLE IF EXISTS `t_user`; 5 | CREATE TABLE `t_user` ( 6 | `USER_ID` varchar(50) , 7 | `USERNAME` varchar(50) , 8 | `PASSWORD` varchar(50) 9 | ) ; 10 | 11 | INSERT INTO `t_user` VALUES ('1', 'admin', 'admin'); 12 | INSERT INTO `t_user` VALUES ('2', 'yizhiwazi', '123456'); -------------------------------------------------------------------------------- /springboot-data-jpa/src/test/java/com/hehe/JpaApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import com.hehe.entity.User; 4 | import com.hehe.repository.UserRepository; 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.boot.test.context.SpringBootTest; 9 | import org.springframework.test.context.junit4.SpringRunner; 10 | 11 | import static org.assertj.core.api.Assertions.assertThat; 12 | 13 | @RunWith(SpringRunner.class) 14 | @SpringBootTest 15 | public class JpaApplicationTest { 16 | 17 | @Autowired 18 | UserRepository userRepository; 19 | 20 | @Test 21 | public void testuser() { 22 | 23 | //添加测试数据 24 | userRepository.save(new User("1111","test-data-jpa","first-pass")); 25 | userRepository.save(new User("9527","test-data-jpa","second-pass")); 26 | 27 | //开始进行测试 28 | assertThat(userRepository.existsById("9527")).isEqualTo(true); 29 | assertThat(userRepository.findByUsername("test-data-jpa").size()).isEqualTo(2); 30 | 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /springboot-date-format-anno/pom.xml: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="UTF-8"?> 2 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 | <modelVersion>4.0.0</modelVersion> 5 | 6 | <!--基本信息--> 7 | <groupId>com.hehe</groupId> 8 | <artifactId>springboot-date-format</artifactId> 9 | <version>0.0.1-SNAPSHOT</version> 10 | <packaging>jar</packaging> 11 | <name>springboot-date-format-anno</name> 12 | <description>Spring Boot 全局日期格式化(基于注解)</description> 13 | 14 | <!--继承信息--> 15 | <parent> 16 | <groupId>org.springframework.boot</groupId> 17 | <artifactId>spring-boot-starter-parent</artifactId> 18 | <version>2.1.0.RELEASE</version> 19 | <relativePath/> 20 | </parent> 21 | 22 | 23 | <!--依赖管理--> 24 | <dependencies> 25 | <dependency><!--Web相关依赖--> 26 | <groupId>org.springframework.boot</groupId> 27 | <artifactId>spring-boot-starter-web</artifactId> 28 | </dependency> 29 | <dependency><!--测试依赖--> 30 | <groupId>org.springframework.boot</groupId> 31 | <artifactId>spring-boot-starter-test</artifactId> 32 | <scope>test</scope> 33 | </dependency> 34 | </dependencies> 35 | 36 | <!--构建管理--> 37 | <build> 38 | <plugins> 39 | <plugin> 40 | <groupId>org.springframework.boot</groupId> 41 | <artifactId>spring-boot-maven-plugin</artifactId> 42 | </plugin> 43 | </plugins> 44 | </build> 45 | 46 | 47 | </project> 48 | -------------------------------------------------------------------------------- /springboot-date-format-anno/src/main/java/com/hehe/DateFormatAnnoApplication.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class DateFormatAnnoApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(DateFormatAnnoApplication.class, args); 11 | } 12 | 13 | } 14 | 15 | -------------------------------------------------------------------------------- /springboot-date-format-anno/src/main/java/com/hehe/config/DateFormatConfig.java: -------------------------------------------------------------------------------- 1 | package com.hehe.config; 2 | 3 | import com.fasterxml.jackson.core.JsonGenerator; 4 | import com.fasterxml.jackson.core.JsonParser; 5 | import com.fasterxml.jackson.core.JsonProcessingException; 6 | import com.fasterxml.jackson.databind.DeserializationContext; 7 | import com.fasterxml.jackson.databind.JsonDeserializer; 8 | import com.fasterxml.jackson.databind.JsonSerializer; 9 | import com.fasterxml.jackson.databind.SerializerProvider; 10 | import org.springframework.boot.jackson.JsonComponent; 11 | 12 | import java.io.IOException; 13 | import java.text.ParseException; 14 | import java.text.SimpleDateFormat; 15 | import java.util.Date; 16 | 17 | /** 18 | * 全局日期格式化 19 | */ 20 | @JsonComponent 21 | public class DateFormatConfig { 22 | 23 | private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 24 | 25 | /** 26 | * 日期格式化 27 | */ 28 | public static class DateJsonSerializer extends JsonSerializer<Date> { 29 | @Override 30 | public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { 31 | jsonGenerator.writeString(dateFormat.format(date)); 32 | } 33 | } 34 | 35 | /** 36 | * 解析日期字符串 37 | */ 38 | public static class DateJsonDeserializer extends JsonDeserializer<Date> { 39 | @Override 40 | public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { 41 | try { 42 | return dateFormat.parse(jsonParser.getText()); 43 | } catch (ParseException e) { 44 | throw new RuntimeException(e); 45 | } 46 | 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /springboot-date-format-anno/src/main/java/com/hehe/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.hehe.entity; 2 | 3 | import java.util.Date; 4 | 5 | public class User { 6 | 7 | private String userId; 8 | private String username; 9 | private String password; 10 | private Date createTime; 11 | private String timezone; 12 | 13 | public User(String userId, String username, String password, Date createTime, String timezone) { 14 | this.userId = userId; 15 | this.username = username; 16 | this.password = password; 17 | this.createTime = createTime; 18 | this.timezone = timezone; 19 | } 20 | 21 | public String getUserId() { 22 | return userId; 23 | } 24 | 25 | public void setUserId(String userId) { 26 | this.userId = userId; 27 | } 28 | 29 | public String getUsername() { 30 | return username; 31 | } 32 | 33 | public void setUsername(String username) { 34 | this.username = username; 35 | } 36 | 37 | public String getPassword() { 38 | return password; 39 | } 40 | 41 | public void setPassword(String password) { 42 | this.password = password; 43 | } 44 | 45 | public Date getCreateTime() { 46 | return createTime; 47 | } 48 | 49 | public void setCreateTime(Date createTime) { 50 | this.createTime = createTime; 51 | } 52 | 53 | public String getTimezone() { 54 | return timezone; 55 | } 56 | 57 | public void setTimezone(String timezone) { 58 | this.timezone = timezone; 59 | } 60 | 61 | @Override 62 | public String toString() { 63 | return "User{" + 64 | "userId='" + userId + '\'' + 65 | ", username='" + username + '\'' + 66 | ", password='" + password + '\'' + 67 | ", createTime=" + createTime + 68 | ", timezone='" + timezone + '\'' + 69 | '}'; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /springboot-date-format-anno/src/main/java/com/hehe/web/UserController.java: -------------------------------------------------------------------------------- 1 | package com.hehe.web; 2 | 3 | import com.hehe.entity.User; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | import java.util.Date; 8 | 9 | @RestController 10 | public class UserController { 11 | 12 | /** 13 | * 查询用户信息 14 | */ 15 | @GetMapping("/") 16 | public User get() { 17 | return new User("1", "socks", "123456", new Date(), "GMT"); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /springboot-date-format-anno/src/main/resources/application.yml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yizhiwazi/springboot-socks/763dce7c4a4960dc6389eb96087a39ed3a278896/springboot-date-format-anno/src/main/resources/application.yml -------------------------------------------------------------------------------- /springboot-date-format/pom.xml: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="UTF-8"?> 2 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 | <modelVersion>4.0.0</modelVersion> 5 | 6 | <!--基本信息--> 7 | <groupId>com.hehe</groupId> 8 | <artifactId>springboot-date-format</artifactId> 9 | <version>0.0.1-SNAPSHOT</version> 10 | <packaging>jar</packaging> 11 | <name>springboot-date-format</name> 12 | <description>Spring Boot 全局日期格式化</description> 13 | 14 | <!--继承信息--> 15 | <parent> 16 | <groupId>org.springframework.boot</groupId> 17 | <artifactId>spring-boot-starter-parent</artifactId> 18 | <version>2.1.0.RELEASE</version> 19 | <relativePath/> 20 | </parent> 21 | 22 | 23 | <!--依赖管理--> 24 | <dependencies> 25 | <dependency><!--Web相关依赖--> 26 | <groupId>org.springframework.boot</groupId> 27 | <artifactId>spring-boot-starter-web</artifactId> 28 | </dependency> 29 | <dependency><!--Thymeleaf依赖--> 30 | <groupId>org.springframework.boot</groupId> 31 | <artifactId>spring-boot-starter-thymeleaf</artifactId> 32 | </dependency> 33 | <dependency><!--JSON 解析工具类--> 34 | <groupId>com.fasterxml.jackson.core</groupId> 35 | <artifactId>jackson-databind</artifactId> 36 | </dependency> 37 | <dependency><!--XML 解析工具类--> 38 | <groupId>com.fasterxml.jackson.dataformat</groupId> 39 | <artifactId>jackson-dataformat-xml</artifactId> 40 | <optional>true</optional> 41 | </dependency> 42 | <dependency><!--热部署依赖--> 43 | <groupId>org.springframework.boot</groupId> 44 | <artifactId>spring-boot-devtools</artifactId> 45 | </dependency> 46 | <dependency><!--测试依赖--> 47 | <groupId>org.springframework.boot</groupId> 48 | <artifactId>spring-boot-starter-test</artifactId> 49 | <scope>test</scope> 50 | </dependency> 51 | </dependencies> 52 | 53 | <!--构建管理--> 54 | <build> 55 | <plugins> 56 | <plugin> 57 | <groupId>org.springframework.boot</groupId> 58 | <artifactId>spring-boot-maven-plugin</artifactId> 59 | </plugin> 60 | </plugins> 61 | </build> 62 | 63 | 64 | </project> 65 | -------------------------------------------------------------------------------- /springboot-date-format/src/main/java/com/hehe/DateFormatApplication.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class DateFormatApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(DateFormatApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /springboot-date-format/src/main/java/com/hehe/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.hehe.entity; 2 | 3 | import java.util.Date; 4 | 5 | public class User { 6 | 7 | private String userId; 8 | private String username; 9 | private String password; 10 | private Date createTime; 11 | 12 | public User(String userId, String username, String password, Date createTime) { 13 | this.userId = userId; 14 | this.username = username; 15 | this.password = password; 16 | this.createTime = createTime; 17 | } 18 | 19 | public String getUserId() { 20 | return userId; 21 | } 22 | 23 | public void setUserId(String userId) { 24 | this.userId = userId; 25 | } 26 | 27 | public String getUsername() { 28 | return username; 29 | } 30 | 31 | public void setUsername(String username) { 32 | this.username = username; 33 | } 34 | 35 | public String getPassword() { 36 | return password; 37 | } 38 | 39 | public void setPassword(String password) { 40 | this.password = password; 41 | } 42 | 43 | public Date getCreateTime() { 44 | return createTime; 45 | } 46 | 47 | public void setCreateTime(Date createTime) { 48 | this.createTime = createTime; 49 | } 50 | 51 | @Override 52 | public String toString() { 53 | return "User{" + 54 | "userId='" + userId + '\'' + 55 | ", username='" + username + '\'' + 56 | ", password='" + password + '\'' + 57 | ", createTime=" + createTime + 58 | '}'; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /springboot-date-format/src/main/java/com/hehe/web/UserController.java: -------------------------------------------------------------------------------- 1 | package com.hehe.web; 2 | 3 | import com.hehe.entity.User; 4 | import org.springframework.http.MediaType; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | import org.springframework.web.servlet.ModelAndView; 8 | 9 | import java.util.Date; 10 | 11 | /** 12 | * 用户管理 13 | */ 14 | @RestController 15 | public class UserController { 16 | 17 | /** 18 | * 打开主页 19 | */ 20 | @GetMapping("/") 21 | public ModelAndView index() { 22 | ModelAndView mv = new ModelAndView("user/user"); 23 | mv.addObject("user", new User("1", "admin", "123456", new Date())); 24 | return mv; 25 | } 26 | 27 | /** 28 | * 自动根据请求来判断返回用户JSON或XML 29 | */ 30 | @GetMapping("/user") 31 | public User get() { 32 | return new User("1", "admin", "123456", new Date()); 33 | } 34 | 35 | /** 36 | * 返回用户JSON 37 | */ 38 | @GetMapping(value = "/user/json", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) 39 | public User getJson() { 40 | return new User("1", "admin", "123456", new Date()); 41 | } 42 | 43 | /** 44 | * 返回用户XML 45 | */ 46 | @GetMapping(value = "/user/xml", produces = MediaType.APPLICATION_XML_VALUE) 47 | public User getXml() { 48 | return new User("1", "admin", "123456", new Date()); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /springboot-date-format/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | thymeleaf: 3 | cache: false 4 | prefix: classpath:/views/ -------------------------------------------------------------------------------- /springboot-date-format/src/main/resources/views/user/user.html: -------------------------------------------------------------------------------- 1 | <!DOCTYPE html> 2 | <html xmlns:th="http://www.thymeleaf.org"> 3 | <head> 4 | <meta charset="UTF-8"> 5 | <title>日期格式化</title> 6 | </head> 7 | <body> 8 | <h3><a th:href="@{/}">1.在页面中对日期格式化</a></h3> 9 | <form th:object="${user}"> 10 | <input th:value="*{userId}" type="hidden"> 11 | 账号:<input th:value="*{username}"> 12 | 密码:<input th:value="*{password}" type="password"> 13 | 时间:<input th:value="*{createTime}" type="text"> 14 | </form> 15 | <form th:object="${user}"> 16 | 账号:<input th:value="*{username}"> 17 | 密码:<input th:value="*{password}" type="password"> 18 | 时间:<input th:value="*{#dates.format(createTime,'yyyy-MM-dd HH:mm:ss')}"> 19 | </form> 20 | 21 | <h3><a th:href="@{/user/json}">2.点击获取JSON信息</a></h3> 22 | <pre> 23 | { 24 | "userId": "1", 25 | "username": "admin", 26 | "password": "123456", 27 | "createTime": "2018-11-18 00:58:48" 28 | } 29 | </pre> 30 | 31 | <h3><a th:href="@{/user/xml}">3.点击获取XML信息</a></h3> 32 | <xmp> 33 | <User> 34 | <userId>1</userId> 35 | <username>admin</username> 36 | <password>123456</password> 37 | <createTime>2018-11-18 00:59:02</createTime> 38 | </User> 39 | </xmp> 40 | 41 | </body> 42 | </html> -------------------------------------------------------------------------------- /springboot-error-controller/pom.xml: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="UTF-8"?> 2 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 | <modelVersion>4.0.0</modelVersion> 5 | <!--基本信息 --> 6 | <groupId>com.hehe</groupId> 7 | <artifactId>springboot-error-controller</artifactId> 8 | <version>0.0.1-SNAPSHOT</version> 9 | <packaging>jar</packaging> 10 | <name>spring-boot-error-controller</name> 11 | <description>SpringBoot 统一异常处理</description> 12 | 13 | <!--继承信息 --> 14 | <parent> 15 | <groupId>org.springframework.boot</groupId> 16 | <artifactId>spring-boot-starter-parent</artifactId> 17 | <version>2.0.0.M4</version> 18 | <relativePath/> 19 | </parent> 20 | 21 | <!--依赖管理 --> 22 | <dependencies> 23 | <dependency> <!--添加Web依赖 --> 24 | <groupId>org.springframework.boot</groupId> 25 | <artifactId>spring-boot-starter-web</artifactId> 26 | </dependency> 27 | <dependency> <!--添加Thymeleaf依赖 --> 28 | <groupId>org.springframework.boot</groupId> 29 | <artifactId>spring-boot-starter-thymeleaf</artifactId> 30 | </dependency> 31 | <dependency><!--添加Test依赖 --> 32 | <groupId>org.springframework.boot</groupId> 33 | <artifactId>spring-boot-starter-test</artifactId> 34 | <scope>test</scope> 35 | </dependency> 36 | </dependencies> 37 | 38 | <!--指定远程仓库(含插件)--> 39 | <repositories> 40 | <repository> 41 | <id>spring-snapshots</id> 42 | <url>http://repo.spring.io/snapshot</url> 43 | <snapshots> 44 | <enabled>true</enabled> 45 | </snapshots> 46 | </repository> 47 | <repository> 48 | <id>spring-milestones</id> 49 | <url>http://repo.spring.io/milestone</url> 50 | </repository> 51 | </repositories> 52 | <pluginRepositories> 53 | <pluginRepository> 54 | <id>spring-snapshots</id> 55 | <url>http://repo.spring.io/snapshot</url> 56 | </pluginRepository> 57 | <pluginRepository> 58 | <id>spring-milestones</id> 59 | <url>http://repo.spring.io/milestone</url> 60 | </pluginRepository> 61 | </pluginRepositories> 62 | 63 | <!--构建插件 --> 64 | <build> 65 | <plugins> 66 | <plugin> 67 | <groupId>org.springframework.boot</groupId> 68 | <artifactId>spring-boot-maven-plugin</artifactId> 69 | </plugin> 70 | </plugins> 71 | </build> 72 | </project> 73 | 74 | -------------------------------------------------------------------------------- /springboot-error-controller/src/main/java/com/hehe/ErrorControllerApplication.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | import java.sql.SQLException; 9 | import java.util.Arrays; 10 | import java.util.List; 11 | 12 | /** 13 | * @author yizhiwazi 14 | */ 15 | @SpringBootApplication 16 | @RestController 17 | public class ErrorControllerApplication { 18 | /** 19 | * 随机抛出异常. 20 | */ 21 | private void randomException() throws Exception { 22 | Exception[] exceptions = { //异常集合 23 | new NullPointerException(), 24 | new ArrayIndexOutOfBoundsException(), 25 | new NumberFormatException(), 26 | new SQLException()}; 27 | //发生概率 28 | double probability = 0.75; 29 | if (Math.random() < probability) { 30 | //情况1:要么抛出异常 31 | throw exceptions[(int) (Math.random() * exceptions.length)]; 32 | } else { 33 | //情况2:要么继续运行 34 | } 35 | 36 | } 37 | 38 | /** 39 | * 模拟用户数据访问. 40 | */ 41 | @GetMapping("/") 42 | public List index() throws Exception { 43 | randomException(); 44 | return Arrays.asList("正常用户数据1!", "正常用户数据2! 请按F5刷新!!"); 45 | } 46 | 47 | public static void main(String[] args) { 48 | SpringApplication.run(ErrorControllerApplication.class, args); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /springboot-error-controller/src/main/java/com/hehe/error/ErrorInfo.java: -------------------------------------------------------------------------------- 1 | package com.hehe.error; 2 | /** 3 | * 主要用途:存储错误信息的Bean. 4 | * 设计说明: 5 | * 1.官方提供了默认错误存储{@link DefaultErrorAttributes},但其最终返回的是存储结构是Map<String,Object>。 6 | * 2.为了避免采用Map这种模糊的数据结构,故统一使用{@link ErrorInfo}来表示错误信息。 7 | * 3.作为存储Bean,最好不要耦合业务逻辑,故统一使用{@link ErrorInfoBuilder}来构建错误信息. 8 | * 9 | * @author yizhiwazi 10 | */ 11 | public class ErrorInfo { 12 | /** 13 | * 发生时间 14 | */ 15 | private String time; 16 | /** 17 | * 访问Url 18 | */ 19 | private String url; 20 | /** 21 | * 错误类型 22 | */ 23 | private String error; 24 | /** 25 | * 错误的堆栈轨迹 26 | */ 27 | String stackTrace; 28 | /** 29 | * 状态码 30 | */ 31 | private int statusCode; 32 | /** 33 | * 状态码-描述 34 | */ 35 | private String reasonPhrase; 36 | 37 | 38 | /** 39 | * Getters And Setters 40 | */ 41 | public String getTime() { 42 | return time; 43 | } 44 | 45 | public void setTime(String time) { 46 | this.time = time; 47 | } 48 | 49 | public String getUrl() { 50 | return url; 51 | } 52 | 53 | public void setUrl(String url) { 54 | this.url = url; 55 | } 56 | 57 | public String getError() { 58 | return error; 59 | } 60 | 61 | public void setError(String error) { 62 | this.error = error; 63 | } 64 | 65 | public String getStackTrace() { 66 | return stackTrace; 67 | } 68 | 69 | public void setStackTrace(String stackTrace) { 70 | this.stackTrace = stackTrace; 71 | } 72 | 73 | public int getStatusCode() { 74 | return statusCode; 75 | } 76 | 77 | public void setStatusCode(int statusCode) { 78 | this.statusCode = statusCode; 79 | } 80 | 81 | public String getReasonPhrase() { 82 | return reasonPhrase; 83 | } 84 | 85 | public void setReasonPhrase(String reasonPhrase) { 86 | this.reasonPhrase = reasonPhrase; 87 | } 88 | } -------------------------------------------------------------------------------- /springboot-error-controller/src/main/java/com/hehe/error/GlobalErrorController.java: -------------------------------------------------------------------------------- 1 | package com.hehe.error; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.boot.web.servlet.error.ErrorController; 5 | import org.springframework.http.MediaType; 6 | import org.springframework.stereotype.Controller; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.ResponseBody; 9 | import org.springframework.web.servlet.ModelAndView; 10 | 11 | import javax.servlet.http.HttpServletRequest; 12 | 13 | /** 14 | * 主要用途:统一处理错误/异常(针对控制层) 15 | * 16 | * @author yizhiwazi 17 | */ 18 | @Controller 19 | @RequestMapping("${server.error.path:/error}") 20 | public class GlobalErrorController implements ErrorController { 21 | 22 | /** 23 | * 错误信息的构建工具. 24 | */ 25 | @Autowired 26 | private ErrorInfoBuilder errorInfoBuilder; 27 | 28 | /** 29 | * 错误信息页的路径 30 | */ 31 | private final static String DEFAULT_ERROR_VIEW = "error"; 32 | 33 | /** 34 | * 获取错误控制器的映射路径. 35 | */ 36 | @Override 37 | public String getErrorPath() { 38 | return errorInfoBuilder.getErrorProperties().getPath(); 39 | } 40 | 41 | /** 42 | * 情况1:若预期返回类型为text/html,s则返回错误信息页(View). 43 | */ 44 | @RequestMapping(produces = MediaType.TEXT_HTML_VALUE) 45 | @ResponseBody 46 | public ModelAndView errorHtml(HttpServletRequest request) { 47 | return new ModelAndView(DEFAULT_ERROR_VIEW, "errorInfo", errorInfoBuilder.getErrorInfo(request)); 48 | } 49 | 50 | /** 51 | * 情况2:其它预期类型 则返回详细的错误信息(JSON). 52 | */ 53 | @RequestMapping 54 | @ResponseBody 55 | public ErrorInfo error(HttpServletRequest request) { 56 | return errorInfoBuilder.getErrorInfo(request); 57 | } 58 | 59 | 60 | } 61 | -------------------------------------------------------------------------------- /springboot-error-controller/src/main/resources/application.yml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yizhiwazi/springboot-socks/763dce7c4a4960dc6389eb96087a39ed3a278896/springboot-error-controller/src/main/resources/application.yml -------------------------------------------------------------------------------- /springboot-error-controller/src/main/resources/templates/error.html: -------------------------------------------------------------------------------- 1 | <!DOCTYPE html> 2 | <html xmlns:th="http://www.thymeleaf.org"> 3 | <head> 4 | <title>GlobalError</title> 5 | </head> 6 | <h1>统一祖国 振兴中华</h1> 7 | <h2>服务异常,请稍后再试。</h2> 8 | <div th:object="${errorInfo}"> 9 | <h3 th:text="*{'发生时间:'+time}"></h3> 10 | <h3 th:text="*{'访问地址:'+url}"></h3> 11 | <h3 th:text="*{'问题类型:'+error}"></h3> 12 | <h3 th:text="*{'通信状态:'+statusCode+','+reasonPhrase}"></h3> 13 | <h3 th:text="*{'堆栈信息:'+stackTrace}"></h3> 14 | </div> 15 | </body> 16 | </html> -------------------------------------------------------------------------------- /springboot-error-controller/src/test/java/com/hehe/SpringBootErrorControllerApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringBootErrorControllerApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /springboot-error-handler/pom.xml: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="UTF-8"?> 2 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 | <modelVersion>4.0.0</modelVersion> 5 | <!--基本信息 --> 6 | <groupId>com.hehe</groupId> 7 | <artifactId>springboot-error-handler</artifactId> 8 | <version>0.0.1-SNAPSHOT</version> 9 | <packaging>jar</packaging> 10 | <name>spring-boot-error-handler</name> 11 | <description>SpringBoot 统一异常处理</description> 12 | 13 | <!--继承信息 --> 14 | <parent> 15 | <groupId>org.springframework.boot</groupId> 16 | <artifactId>spring-boot-starter-parent</artifactId> 17 | <version>2.0.0.M4</version> 18 | <relativePath/> 19 | </parent> 20 | 21 | <!--依赖管理 --> 22 | <dependencies> 23 | <dependency> <!--添加Web依赖 --> 24 | <groupId>org.springframework.boot</groupId> 25 | <artifactId>spring-boot-starter-web</artifactId> 26 | </dependency> 27 | <dependency> <!--添加Thymeleaf依赖 --> 28 | <groupId>org.springframework.boot</groupId> 29 | <artifactId>spring-boot-starter-thymeleaf</artifactId> 30 | </dependency> 31 | <dependency><!--添加Test依赖 --> 32 | <groupId>org.springframework.boot</groupId> 33 | <artifactId>spring-boot-starter-test</artifactId> 34 | <scope>test</scope> 35 | </dependency> 36 | </dependencies> 37 | 38 | <!--指定远程仓库(含插件)--> 39 | <repositories> 40 | <repository> 41 | <id>spring-snapshots</id> 42 | <url>http://repo.spring.io/snapshot</url> 43 | <snapshots> 44 | <enabled>true</enabled> 45 | </snapshots> 46 | </repository> 47 | <repository> 48 | <id>spring-milestones</id> 49 | <url>http://repo.spring.io/milestone</url> 50 | </repository> 51 | </repositories> 52 | <pluginRepositories> 53 | <pluginRepository> 54 | <id>spring-snapshots</id> 55 | <url>http://repo.spring.io/snapshot</url> 56 | </pluginRepository> 57 | <pluginRepository> 58 | <id>spring-milestones</id> 59 | <url>http://repo.spring.io/milestone</url> 60 | </pluginRepository> 61 | </pluginRepositories> 62 | 63 | <!--构建插件 --> 64 | <build> 65 | <plugins> 66 | <plugin> 67 | <groupId>org.springframework.boot</groupId> 68 | <artifactId>spring-boot-maven-plugin</artifactId> 69 | </plugin> 70 | </plugins> 71 | </build> 72 | </project> 73 | -------------------------------------------------------------------------------- /springboot-error-handler/src/main/java/com/hehe/ErrorHandlerApplication.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | import java.sql.SQLException; 9 | import java.util.Arrays; 10 | import java.util.List; 11 | 12 | /** 13 | * @author yizhiwazi 14 | */ 15 | @SpringBootApplication 16 | @RestController 17 | public class ErrorHandlerApplication { 18 | /** 19 | * 随机抛出异常. 20 | */ 21 | private void randomException() throws Exception { 22 | Exception[] exceptions = { //异常集合 23 | new NullPointerException(), 24 | new ArrayIndexOutOfBoundsException(), 25 | new NumberFormatException(), 26 | new SQLException()}; 27 | //发生概率 28 | double probability = 0.75; 29 | if (Math.random() < probability) { 30 | //情况1:要么抛出异常 31 | throw exceptions[(int) (Math.random() * exceptions.length)]; 32 | } else { 33 | //情况2:要么继续运行 34 | } 35 | 36 | } 37 | 38 | /** 39 | * 模拟用户数据访问. 40 | */ 41 | @GetMapping("/") 42 | public List index() throws Exception { 43 | randomException(); 44 | return Arrays.asList("正常用户数据1!", "正常用户数据2! 请按F5刷新!!"); 45 | } 46 | 47 | public static void main(String[] args) { 48 | SpringApplication.run(ErrorHandlerApplication.class, args); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /springboot-error-handler/src/main/java/com/hehe/error/ErrorInfo.java: -------------------------------------------------------------------------------- 1 | package com.hehe.error; 2 | /** 3 | * 主要用途:存储错误信息的Bean. 4 | * 设计说明: 5 | * 1.官方提供了默认错误存储{@link DefaultErrorAttributes},但其最终返回的是存储结构是Map<String,Object>。 6 | * 2.为了避免采用Map这种模糊的数据结构,故统一使用{@link ErrorInfo}来表示错误信息。 7 | * 3.作为存储Bean,最好不要耦合业务逻辑,故统一使用{@link ErrorInfoBuilder}来构建错误信息. 8 | * 9 | * @author yizhiwazi 10 | */ 11 | public class ErrorInfo { 12 | /** 13 | * 发生时间 14 | */ 15 | private String time; 16 | /** 17 | * 访问Url 18 | */ 19 | private String url; 20 | /** 21 | * 错误类型 22 | */ 23 | private String error; 24 | /** 25 | * 错误的堆栈轨迹 26 | */ 27 | String stackTrace; 28 | /** 29 | * 状态码 30 | */ 31 | private int statusCode; 32 | /** 33 | * 状态码-描述 34 | */ 35 | private String reasonPhrase; 36 | 37 | 38 | /** 39 | * Getters And Setters 40 | */ 41 | public String getTime() { 42 | return time; 43 | } 44 | 45 | public void setTime(String time) { 46 | this.time = time; 47 | } 48 | 49 | public String getUrl() { 50 | return url; 51 | } 52 | 53 | public void setUrl(String url) { 54 | this.url = url; 55 | } 56 | 57 | public String getError() { 58 | return error; 59 | } 60 | 61 | public void setError(String error) { 62 | this.error = error; 63 | } 64 | 65 | public String getStackTrace() { 66 | return stackTrace; 67 | } 68 | 69 | public void setStackTrace(String stackTrace) { 70 | this.stackTrace = stackTrace; 71 | } 72 | 73 | public int getStatusCode() { 74 | return statusCode; 75 | } 76 | 77 | public void setStatusCode(int statusCode) { 78 | this.statusCode = statusCode; 79 | } 80 | 81 | public String getReasonPhrase() { 82 | return reasonPhrase; 83 | } 84 | 85 | public void setReasonPhrase(String reasonPhrase) { 86 | this.reasonPhrase = reasonPhrase; 87 | } 88 | } -------------------------------------------------------------------------------- /springboot-error-handler/src/main/java/com/hehe/error/GlobalErrorHandler.java: -------------------------------------------------------------------------------- 1 | package com.hehe.error; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.web.bind.annotation.ControllerAdvice; 5 | import org.springframework.web.bind.annotation.ExceptionHandler; 6 | import org.springframework.web.bind.annotation.ResponseBody; 7 | import org.springframework.web.servlet.ModelAndView; 8 | 9 | import javax.servlet.http.HttpServletRequest; 10 | 11 | /** 12 | * 主要用途:统一处理错误/异常(针对控制层) 13 | * 使用说明: 14 | * {@link ControllerAdvice 默认扫描路径:例如com.hehe.controller} 15 | * {@link ExceptionHandler 指定捕捉异常} 16 | * {@link ModelAndView 返回异常信息页(View)} 17 | * {@link ResponseBody 返回异常信息(JSON)} 18 | * <p> 19 | * 使用@ExceptionHandler时候需注意如下几点: 20 | * 1.获取异常:直接在方法参数注入 21 | * 2.常见缺点:无法捕捉404类异常 22 | * 3.替代方案:实现ErrorController 23 | * 24 | * @author yizhiwazi 25 | */ 26 | 27 | @ControllerAdvice 28 | public class GlobalErrorHandler { 29 | /** 30 | * 错误信息页 31 | */ 32 | private final static String DEFAULT_ERROR_VIEW = "error"; 33 | 34 | /** 35 | * 错误信息构建器 36 | */ 37 | @Autowired 38 | private ErrorInfoBuilder errorInfoBuilder; 39 | 40 | /** 41 | * 根据业务规则,统一处理异常。 42 | */ 43 | @ExceptionHandler(Exception.class) 44 | @ResponseBody 45 | public Object exceptionHandler(HttpServletRequest request, Throwable error) { 46 | 47 | //1.若为AJAX请求,则返回异常信息(JSON) 48 | if (isAjaxRequest(request)) { 49 | return errorInfoBuilder.getErrorInfo(request,error); 50 | } 51 | //2.其余请求,则返回指定的异常信息页(View). 52 | return new ModelAndView(DEFAULT_ERROR_VIEW, "errorInfo", errorInfoBuilder.getErrorInfo(request, error)); 53 | } 54 | 55 | private boolean isAjaxRequest(HttpServletRequest request) { 56 | 57 | return "XMLHttpRequest".equals(request.getHeader("X-Requested-With")); 58 | } 59 | 60 | } -------------------------------------------------------------------------------- /springboot-error-handler/src/main/resources/application.yml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yizhiwazi/springboot-socks/763dce7c4a4960dc6389eb96087a39ed3a278896/springboot-error-handler/src/main/resources/application.yml -------------------------------------------------------------------------------- /springboot-error-handler/src/main/resources/templates/error.html: -------------------------------------------------------------------------------- 1 | <!DOCTYPE html> 2 | <html xmlns:th="http://www.thymeleaf.org"> 3 | <head> 4 | <title>GlobalError</title> 5 | </head> 6 | <h1>统一祖国 振兴中华</h1> 7 | <h2>服务异常,请稍后再试。</h2> 8 | <div th:object="${errorInfo}"> 9 | <h3 th:text="*{'发生时间:'+time}"></h3> 10 | <h3 th:text="*{'访问地址:'+url}"></h3> 11 | <h3 th:text="*{'问题类型:'+error}"></h3> 12 | <h3 th:text="*{'通信状态:'+statusCode+','+reasonPhrase}"></h3> 13 | <h3 th:text="*{'堆栈信息:'+stackTrace}"></h3> 14 | </div> 15 | </body> 16 | </html> -------------------------------------------------------------------------------- /springboot-error-handler/src/test/java/com/hehe/SpringbootErrorHandlerApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringbootErrorHandlerApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /springboot-externalized-configuration/pom.xml: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="UTF-8"?> 2 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 | 5 | <!--基本信息 --> 6 | <modelVersion>4.0.0</modelVersion> 7 | <groupId>com.hehe</groupId> 8 | <artifactId>springboot-externalized-configuration</artifactId> 9 | <version>0.0.1-SNAPSHOT</version> 10 | <packaging>jar</packaging> 11 | <name>spring-boot-externalized-configuration</name> 12 | <description>SpringBoot 配置文件详解</description> 13 | 14 | <!--继承信息 --> 15 | <parent> 16 | <groupId>org.springframework.boot</groupId> 17 | <artifactId>spring-boot-starter-parent</artifactId> 18 | <version>2.0.0.M4</version> 19 | </parent> 20 | 21 | <!--依赖管理 --> 22 | <dependencies> 23 | <dependency><!--添加Web依赖--> 24 | <groupId>org.springframework.boot</groupId> 25 | <artifactId>spring-boot-starter-web</artifactId> 26 | </dependency> 27 | <dependency><!--添加MySQL驱动依赖--> 28 | <groupId>mysql</groupId> 29 | <artifactId>mysql-connector-java</artifactId> 30 | <version>5.1.42</version> 31 | </dependency> 32 | <dependency><!--添加配置处理器依赖--> 33 | <groupId>org.springframework.boot</groupId> 34 | <artifactId>spring-boot-configuration-processor</artifactId> 35 | <optional>true</optional> 36 | </dependency> 37 | <dependency><!--添加测试依赖--> 38 | <groupId>org.springframework.boot</groupId> 39 | <artifactId>spring-boot-starter-test</artifactId> 40 | <scope>test</scope> 41 | </dependency> 42 | </dependencies> 43 | 44 | <!--指定远程仓库(含插件)--> 45 | <repositories> 46 | <repository> 47 | <id>spring-snapshots</id> 48 | <url>http://repo.spring.io/snapshot</url> 49 | <snapshots><enabled>true</enabled></snapshots> 50 | </repository> 51 | <repository> 52 | <id>spring-milestones</id> 53 | <url>http://repo.spring.io/milestone</url> 54 | </repository> 55 | </repositories> 56 | <pluginRepositories> 57 | <pluginRepository> 58 | <id>spring-snapshots</id> 59 | <url>http://repo.spring.io/snapshot</url> 60 | </pluginRepository> 61 | <pluginRepository> 62 | <id>spring-milestones</id> 63 | <url>http://repo.spring.io/milestone</url> 64 | </pluginRepository> 65 | </pluginRepositories> 66 | 67 | <!--构建插件--> 68 | <build> 69 | <plugins> 70 | <plugin> 71 | <groupId>org.springframework.boot</groupId> 72 | <artifactId>spring-boot-maven-plugin</artifactId> 73 | </plugin> 74 | </plugins> 75 | </build> 76 | 77 | </project> 78 | -------------------------------------------------------------------------------- /springboot-externalized-configuration/src/main/java/com/hehe/ExternalizedConfigurationApplication.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class ExternalizedConfigurationApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(ExternalizedConfigurationApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /springboot-externalized-configuration/src/main/java/com/hehe/config/HerDataSource.java: -------------------------------------------------------------------------------- 1 | package com.hehe.config; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import org.springframework.stereotype.Component; 5 | 6 | @Component 7 | public class HerDataSource { 8 | 9 | @Value("${spring.datasource.url}") 10 | private String url; 11 | @Value("${spring.datasource.username}") 12 | private String username; 13 | @Value("${spring.datasource.password}") 14 | private String password; 15 | @Value("${spring.datasource.driver-class-name}") 16 | private String driverClassName; 17 | 18 | // 提供Setter 和 Getter 方法 19 | 20 | public String getUrl() { 21 | return url; 22 | } 23 | 24 | public void setUrl(String url) { 25 | this.url = url; 26 | } 27 | 28 | public String getUsername() { 29 | return username; 30 | } 31 | 32 | public void setUsername(String username) { 33 | this.username = username; 34 | } 35 | 36 | public String getPassword() { 37 | return password; 38 | } 39 | 40 | public void setPassword(String password) { 41 | this.password = password; 42 | } 43 | 44 | public String getDriverClassName() { 45 | return driverClassName; 46 | } 47 | 48 | public void setDriverClassName(String driverClassName) { 49 | this.driverClassName = driverClassName; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /springboot-externalized-configuration/src/main/java/com/hehe/config/MyDataSource.java: -------------------------------------------------------------------------------- 1 | package com.hehe.config; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | import org.springframework.stereotype.Component; 5 | 6 | @Component 7 | @ConfigurationProperties(prefix = "spring.datasource") 8 | public class MyDataSource { 9 | 10 | private String url; 11 | 12 | private String username; 13 | 14 | private String password; 15 | 16 | private String driverClassName; 17 | 18 | // 提供equals方法 方便测试 19 | 20 | @Override 21 | public boolean equals(Object o) { 22 | if (this == o) return true; 23 | if (o == null || getClass() != o.getClass()) return false; 24 | 25 | MyDataSource that = (MyDataSource) o; 26 | 27 | if (url != null ? !url.equals(that.url) : that.url != null) return false; 28 | if (username != null ? !username.equals(that.username) : that.username != null) return false; 29 | if (password != null ? !password.equals(that.password) : that.password != null) return false; 30 | return driverClassName != null ? driverClassName.equals(that.driverClassName) : that.driverClassName == null; 31 | } 32 | 33 | @Override 34 | public int hashCode() { 35 | int result = url != null ? url.hashCode() : 0; 36 | result = 31 * result + (username != null ? username.hashCode() : 0); 37 | result = 31 * result + (password != null ? password.hashCode() : 0); 38 | result = 31 * result + (driverClassName != null ? driverClassName.hashCode() : 0); 39 | return result; 40 | } 41 | 42 | 43 | // 提供Setter 和 Getter 方法 44 | 45 | public String getUrl() { 46 | return url; 47 | } 48 | 49 | public void setUrl(String url) { 50 | this.url = url; 51 | } 52 | 53 | public String getUsername() { 54 | return username; 55 | } 56 | 57 | public void setUsername(String username) { 58 | this.username = username; 59 | } 60 | 61 | public String getPassword() { 62 | return password; 63 | } 64 | 65 | public void setPassword(String password) { 66 | this.password = password; 67 | } 68 | 69 | public String getDriverClassName() { 70 | return driverClassName; 71 | } 72 | 73 | public void setDriverClassName(String driverClassName) { 74 | this.driverClassName = driverClassName; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /springboot-externalized-configuration/src/main/java/com/hehe/controller/MyController.java: -------------------------------------------------------------------------------- 1 | package com.hehe.controller; 2 | 3 | import com.hehe.config.HerDataSource; 4 | import com.hehe.config.MyDataSource; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.core.env.Environment; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | import java.util.Arrays; 11 | import java.util.List; 12 | 13 | @RestController 14 | public class MyController { 15 | 16 | @Autowired 17 | MyDataSource myDataSource; 18 | 19 | @Autowired 20 | HerDataSource herDataSource; 21 | 22 | @Autowired 23 | Environment environment; 24 | 25 | @GetMapping("/") 26 | public List source(){ 27 | return Arrays.asList(myDataSource,herDataSource); 28 | } 29 | 30 | @GetMapping("/env") 31 | public List env(){ 32 | MyDataSource dataSource = new MyDataSource(); 33 | dataSource.setUrl(environment.getProperty("spring.datasource.url")); 34 | dataSource.setUsername(environment.getProperty("spring.datasource.username")); 35 | dataSource.setPassword(environment.getProperty("spring.datasource.password")); 36 | dataSource.setDriverClassName(environment.getProperty("spring.datasource.driver-class-name")); 37 | return Arrays.asList(new MyDataSource(),dataSource); 38 | } 39 | 40 | 41 | } 42 | -------------------------------------------------------------------------------- /springboot-externalized-configuration/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | url: jdbc:mysql://localhost:3306/socks?useSSL=false 4 | username: root 5 | password: root 6 | driver-class-name: com.mysql.jdbc.Driver -------------------------------------------------------------------------------- /springboot-externalized-configuration/src/test/java/com/hehe/ExternalizedConfigurationApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import com.hehe.config.MyDataSource; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | import org.springframework.test.context.junit4.SpringRunner; 9 | 10 | import static org.assertj.core.api.Assertions.assertThat; 11 | 12 | @RunWith(SpringRunner.class) 13 | @SpringBootTest 14 | public class ExternalizedConfigurationApplicationTest { 15 | 16 | @Autowired 17 | MyDataSource myDataSource; 18 | 19 | @Test 20 | public void testconfig() { 21 | MyDataSource dataSource = new MyDataSource(); 22 | dataSource.setUrl("jdbc:mysql://localhost:3306/socks?useSSL=false"); 23 | dataSource.setUsername("root"); 24 | dataSource.setPassword("root"); 25 | dataSource.setDriverClassName("com.mysql.jdbc.Driver"); 26 | assertThat(myDataSource).isEqualTo(dataSource); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /springboot-helloworld/pom.xml: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="UTF-8"?> 2 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 | 5 | <!--基本信息 --> 6 | <modelVersion>4.0.0</modelVersion> 7 | <groupId>com.hehe</groupId> 8 | <artifactId>springboot-helloworld</artifactId> 9 | <version>0.0.1-SNAPSHOT</version> 10 | <packaging>jar</packaging> 11 | <name>springboot-helloworld</name> 12 | <description>Demo project for Spring Boot</description> 13 | 14 | <!--继承信息 --> 15 | <parent> 16 | <groupId>org.springframework.boot</groupId> 17 | <artifactId>spring-boot-starter-parent</artifactId> 18 | <version>2.0.0.M4</version> 19 | <relativePath/> 20 | </parent> 21 | 22 | <!--依赖管理 --> 23 | <dependencies> 24 | <!-- 添加WEB支持 --> 25 | <dependency> 26 | <groupId>org.springframework.boot</groupId> 27 | <artifactId>spring-boot-starter-web</artifactId> 28 | </dependency> 29 | <!-- 添加Test支持 --> 30 | <dependency> 31 | <groupId>org.springframework.boot</groupId> 32 | <artifactId>spring-boot-starter-test</artifactId> 33 | <scope>test</scope> 34 | </dependency> 35 | </dependencies> 36 | 37 | <!--指定远程仓库(含插件)--> 38 | <repositories> 39 | <repository> 40 | <id>spring-snapshots</id> 41 | <url>http://repo.spring.io/snapshot</url> 42 | <snapshots><enabled>true</enabled></snapshots> 43 | </repository> 44 | <repository> 45 | <id>spring-milestones</id> 46 | <url>http://repo.spring.io/milestone</url> 47 | </repository> 48 | </repositories> 49 | <pluginRepositories> 50 | <pluginRepository> 51 | <id>spring-snapshots</id> 52 | <url>http://repo.spring.io/snapshot</url> 53 | </pluginRepository> 54 | <pluginRepository> 55 | <id>spring-milestones</id> 56 | <url>http://repo.spring.io/milestone</url> 57 | </pluginRepository> 58 | </pluginRepositories> 59 | 60 | <!--构建插件 --> 61 | <build> 62 | <plugins> 63 | <plugin> 64 | <groupId>org.springframework.boot</groupId> 65 | <artifactId>spring-boot-maven-plugin</artifactId> 66 | </plugin> 67 | </plugins> 68 | </build> 69 | </project> 70 | -------------------------------------------------------------------------------- /springboot-helloworld/src/main/java/com/hehe/HelloworldApplication.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | import org.springframework.context.annotation.ComponentScan; 8 | import org.springframework.context.annotation.Configuration; 9 | import org.springframework.stereotype.Controller; 10 | import org.springframework.web.bind.annotation.GetMapping; 11 | import org.springframework.web.bind.annotation.ResponseBody; 12 | 13 | /** 14 | * 为了让小伙伴们对Boot有更直观的了解 可以先简单阅读这些说明。 15 | * 16 | * 一、SpringBoot 特性 17 | 1.座靠Spring (未来颠覆者) 18 | 2.内嵌Tomcat、Jetty (无需部署) 19 | 3.提供各种Starter (简化依赖) 20 | 4.提供自动配置 (开发神器) 21 | 5.告别XML (妈妈再也不用担我写错配置了) 22 | 23 | * 二、快速学习-便携式入口 24 | * {@link SpringBootApplication } 25 | * 26 | * {@link EnableAutoConfiguration 1.开启自动配置 } 例如在ClassPath看到MVC包则自动去初始化WEB环境 27 | * {@link Configuration 2.标记配置类 } 例如制定方法为上下文提供Bean. 28 | * {@link ComponentScan 3.开启组件扫描 } 例如@Controller,@Service等. 29 | * 30 | */ 31 | 32 | @SpringBootApplication 33 | @Controller 34 | public class HelloworldApplication { 35 | 36 | @GetMapping("/") 37 | @ResponseBody 38 | public String index() { 39 | return "Hello World!"; 40 | } 41 | 42 | public static void main(String[] args) { 43 | SpringApplication.run(HelloworldApplication.class, args); //启动项目 44 | } 45 | 46 | 47 | } 48 | -------------------------------------------------------------------------------- /springboot-helloworld/src/main/resources/application.yml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yizhiwazi/springboot-socks/763dce7c4a4960dc6389eb96087a39ed3a278896/springboot-helloworld/src/main/resources/application.yml -------------------------------------------------------------------------------- /springboot-helloworld/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | ${AnsiColor.BRIGHT_YELLOW} 2 | //////////////////////////////////////////////////////////////////// 3 | // _ooOoo_ // 4 | // o8888888o // 5 | // 88" . "88 // 6 | // (| ^_^ |) // 7 | // O\ = /O // 8 | // ____/`---'\____ // 9 | // .' \\| |// `. // 10 | // / \\||| : |||// \ // 11 | // / _||||| -:- |||||- \ // 12 | // | | \\\ - /// | | // 13 | // | \_| ''\---/'' | | // 14 | // \ .-\__ `-` ___/-. / // 15 | // ___`. .' /--.--\ `. . ___ // 16 | // ."" '< `.___\_<|>_/___.' >'"". // 17 | // | | : `- \`.;`\ _ /`;.`/ - ` : | | // 18 | // \ \ `-. \_ __\ /__ _/ .-` / / // 19 | // ========`-.____`-.___\_____/___.-`____.-'======== // 20 | // `=---=' // 21 | // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // 22 | // Welcome to SpringBoot !! // 23 | //////////////////////////////////////////////////////////////////// -------------------------------------------------------------------------------- /springboot-helloworld/src/main/resources/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yizhiwazi/springboot-socks/763dce7c4a4960dc6389eb96087a39ed3a278896/springboot-helloworld/src/main/resources/favicon.ico -------------------------------------------------------------------------------- /springboot-helloworld/src/test/java/com/hehe/SpringbootHelloworldApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; 8 | import org.springframework.boot.test.context.SpringBootTest; 9 | import org.springframework.test.context.junit4.SpringRunner; 10 | import org.springframework.test.web.servlet.MockMvc; 11 | 12 | import static org.hamcrest.Matchers.containsString; 13 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 14 | import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; 15 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; 16 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 17 | 18 | 19 | /** 20 | * 三、快速单元测试 21 | * {@link SpringBootTest} 基于Boot环境 22 | * {@link MockMvc} 基于SpringMVC进行测试 23 | */ 24 | 25 | @RunWith(SpringRunner.class) 26 | @SpringBootTest 27 | @AutoConfigureMockMvc //开启MockMvc 28 | public class SpringbootHelloworldApplicationTests { 29 | 30 | @Autowired 31 | private MockMvc mockMvc; //注入MockMvc 32 | 33 | @Test 34 | public void testHelloController() throws Exception { 35 | 36 | mockMvc.perform(get("/")) //请求方式+地址 37 | .andDo(print()) //打印效果 38 | .andExpect(status().isOk()) //预期状态 39 | .andExpect(content().string(containsString("Hello World")));//预期返回值 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /springboot-integration/mm-entity/pom.xml: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="UTF-8"?> 2 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 | <modelVersion>4.0.0</modelVersion> 5 | 6 | <!-- 基本信息 --> 7 | <groupId>com.hehe</groupId> 8 | <artifactId>mm-entity</artifactId> 9 | <version>0.0.1-SNAPSHOT</version> 10 | <packaging>jar</packaging> 11 | <name>mm-entity</name> 12 | 13 | <!-- 继承本项目的父工程 --> 14 | <parent> 15 | <groupId>com.hehe</groupId> 16 | <artifactId>springboot-integration</artifactId> 17 | <version>1.0.0.RELEASE</version> 18 | </parent> 19 | 20 | <!-- Entity模块相关依赖 --> 21 | <dependencies> 22 | <dependency> 23 | <groupId>org.springframework.boot</groupId> 24 | <artifactId>spring-boot-starter-data-jpa</artifactId> 25 | </dependency> 26 | </dependencies> 27 | 28 | </project> -------------------------------------------------------------------------------- /springboot-integration/mm-entity/src/main/java/com/hehe/integration/common/R.java: -------------------------------------------------------------------------------- 1 | package com.hehe.integration.common; 2 | 3 | import java.io.Serializable; 4 | 5 | public class R<T> implements Serializable { 6 | 7 | private static final long serialVersionUID = -4577255781088498763L; 8 | private static final int OK = 0; 9 | private static final int FAIL = 1; 10 | private static final int UNAUTHORIZED = 2; 11 | 12 | private T data; //服务端数据 13 | private int status = OK; //状态码 14 | private String msg = ""; //描述信息 15 | 16 | //APIS 17 | public static R isOk() { 18 | return new R(); 19 | } 20 | 21 | public static R isFail() { 22 | return new R().status(FAIL); 23 | } 24 | 25 | public static R isFail(Throwable e) { 26 | return isFail().msg(e); 27 | } 28 | 29 | public R msg(Throwable e) { 30 | this.setMsg(e.toString()); 31 | return this; 32 | } 33 | 34 | public R data(T data) { 35 | this.setData(data); 36 | return this; 37 | } 38 | 39 | public R status(int status) { 40 | this.setStatus(status); 41 | return this; 42 | } 43 | 44 | 45 | //Constructors 46 | public R() { 47 | 48 | } 49 | 50 | //Getter&Setters 51 | 52 | public T getData() { 53 | return data; 54 | } 55 | 56 | public void setData(T data) { 57 | this.data = data; 58 | } 59 | 60 | public int getStatus() { 61 | return status; 62 | } 63 | 64 | public void setStatus(int status) { 65 | this.status = status; 66 | } 67 | 68 | public String getMsg() { 69 | return msg; 70 | } 71 | 72 | public void setMsg(String msg) { 73 | this.msg = msg; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /springboot-integration/mm-entity/src/main/java/com/hehe/integration/user/User.java: -------------------------------------------------------------------------------- 1 | package com.hehe.integration.user; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.Id; 6 | import javax.persistence.Table; 7 | 8 | @Entity 9 | @Table(name = "T_USER") 10 | public class User { 11 | 12 | @Id 13 | @Column(name = "USERID") 14 | private String userId; 15 | @Column(name = "USERNAME") 16 | private String username; 17 | @Column(name = "PASSWORD") 18 | private String password; 19 | 20 | public String getUserId() { 21 | return userId; 22 | } 23 | 24 | public void setUserId(String userId) { 25 | this.userId = userId; 26 | } 27 | 28 | public String getUsername() { 29 | return username; 30 | } 31 | 32 | public void setUsername(String username) { 33 | this.username = username; 34 | } 35 | 36 | public String getPassword() { 37 | return password; 38 | } 39 | 40 | public void setPassword(String password) { 41 | this.password = password; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /springboot-integration/mm-repo/pom.xml: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="UTF-8"?> 2 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 | <modelVersion>4.0.0</modelVersion> 5 | 6 | <!-- 基本信息 --> 7 | <groupId>com.hehe</groupId> 8 | <artifactId>mm-repo</artifactId> 9 | <version>0.0.1-SNAPSHOT</version> 10 | <packaging>jar</packaging> 11 | <name>mm-repo</name> 12 | 13 | <!-- 继承本项目的父工程 --> 14 | <parent> 15 | <groupId>com.hehe</groupId> 16 | <artifactId>springboot-integration</artifactId> 17 | <version>1.0.0.RELEASE</version> 18 | </parent> 19 | 20 | <!-- Repo模块相关依赖 --> 21 | <dependencies> 22 | <dependency> 23 | <groupId>com.hehe</groupId> 24 | <artifactId>mm-entity</artifactId> 25 | </dependency> 26 | 27 | <dependency> 28 | <groupId>mysql</groupId> 29 | <artifactId>mysql-connector-java</artifactId> 30 | </dependency> 31 | 32 | </dependencies> 33 | 34 | </project> -------------------------------------------------------------------------------- /springboot-integration/mm-repo/src/main/java/com/hehe/integration/user/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.hehe.integration.user; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | public interface UserRepository extends JpaRepository<User,String> { 6 | } -------------------------------------------------------------------------------- /springboot-integration/mm-service/pom.xml: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="UTF-8"?> 2 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 | <modelVersion>4.0.0</modelVersion> 5 | 6 | <!-- 基本信息 --> 7 | <groupId>com.hehe</groupId> 8 | <artifactId>mm-service</artifactId> 9 | <version>0.0.1-SNAPSHOT</version> 10 | <packaging>jar</packaging> 11 | <name>mm-service</name> 12 | 13 | <!-- 继承本项目的父工程 --> 14 | <parent> 15 | <groupId>com.hehe</groupId> 16 | <artifactId>springboot-integration</artifactId> 17 | <version>1.0.0.RELEASE</version> 18 | </parent> 19 | 20 | <!-- Web模块相关依赖 --> 21 | <dependencies> 22 | <dependency> 23 | <groupId>com.hehe</groupId> 24 | <artifactId>mm-repo</artifactId> 25 | </dependency> 26 | <dependency> 27 | <groupId>com.hehe</groupId> 28 | <artifactId>mm-entity</artifactId> 29 | </dependency> 30 | </dependencies> 31 | 32 | </project> -------------------------------------------------------------------------------- /springboot-integration/mm-service/src/main/java/com/hehe/integration/user/UserService.java: -------------------------------------------------------------------------------- 1 | package com.hehe.integration.user; 2 | 3 | import java.util.List; 4 | 5 | public interface UserService { 6 | 7 | List<User> list(); 8 | } 9 | -------------------------------------------------------------------------------- /springboot-integration/mm-service/src/main/java/com/hehe/integration/user/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.hehe.integration.user; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | 6 | import java.util.List; 7 | 8 | @Service 9 | public class UserServiceImpl implements UserService { 10 | 11 | @Autowired 12 | UserRepository userRepository; 13 | 14 | @Override 15 | public List<User> list() { 16 | return userRepository.findAll(); 17 | } 18 | } -------------------------------------------------------------------------------- /springboot-integration/mm-web/pom.xml: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="UTF-8"?> 2 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 | <modelVersion>4.0.0</modelVersion> 5 | 6 | <!-- 基本信息 --> 7 | <groupId>com.hehe</groupId> 8 | <artifactId>mm-web</artifactId> 9 | <version>0.0.1-SNAPSHOT</version> 10 | <packaging>jar</packaging> 11 | <name>mm-web</name> 12 | 13 | <!-- 继承本项目的父工程 --> 14 | <parent> 15 | <groupId>com.hehe</groupId> 16 | <artifactId>springboot-integration</artifactId> 17 | <version>1.0.0.RELEASE</version> 18 | </parent> 19 | 20 | <!-- Web模块相关依赖 --> 21 | <dependencies> 22 | <dependency> 23 | <groupId>com.hehe</groupId> 24 | <artifactId>mm-service</artifactId> 25 | </dependency> 26 | <dependency> 27 | <groupId>com.hehe</groupId> 28 | <artifactId>mm-entity</artifactId> 29 | </dependency> 30 | <dependency> 31 | <groupId>org.springframework.boot</groupId> 32 | <artifactId>spring-boot-starter-web</artifactId> 33 | </dependency> 34 | 35 | <dependency> 36 | <groupId>org.springframework.boot</groupId> 37 | <artifactId>spring-boot-starter-test</artifactId> 38 | <scope>test</scope> 39 | </dependency> 40 | </dependencies> 41 | 42 | </project> -------------------------------------------------------------------------------- /springboot-integration/mm-web/src/main/java/com/hehe/integration/MmWebApplication.java: -------------------------------------------------------------------------------- 1 | package com.hehe.integration; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | /** 7 | * 注:为了避免扫描路径不一致,请将启动类放在Root Package 即 com.hehe.integration 8 | */ 9 | 10 | @SpringBootApplication 11 | public class MmWebApplication { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(MmWebApplication.class, args); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /springboot-integration/mm-web/src/main/java/com/hehe/integration/hello/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.hehe.integration.hello; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | 6 | @Controller 7 | public class HelloController { 8 | 9 | @GetMapping("/") 10 | public String index(){ 11 | return "redirect:/user/list"; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /springboot-integration/mm-web/src/main/java/com/hehe/integration/user/UserController.java: -------------------------------------------------------------------------------- 1 | package com.hehe.integration.user; 2 | 3 | import com.hehe.integration.common.R; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | @RestController 10 | @RequestMapping("/user/*") 11 | public class UserController { 12 | 13 | @Autowired 14 | UserService userService; 15 | 16 | @GetMapping("list") 17 | public R list() { 18 | try { 19 | return R.isOk().data(userService.list()); 20 | } catch (Exception e) { 21 | return R.isFail(e); 22 | } 23 | 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /springboot-integration/mm-web/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | url: jdbc:mysql://localhost:3306/socks?useSSL=false 4 | username: root 5 | password: root 6 | driver-class-name: com.mysql.jdbc.Driver -------------------------------------------------------------------------------- /springboot-integration/pom.xml: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="UTF-8"?> 2 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 | 5 | <!-- 基本信息 --> 6 | <description>SpringBoot 多模块构建示例</description> 7 | <modelVersion>4.0.0</modelVersion> 8 | <name>spring-boot-integration</name> 9 | <packaging>pom</packaging> 10 | 11 | <!-- 项目说明:这里作为聚合工程的父工程 --> 12 | <groupId>com.hehe</groupId> 13 | <artifactId>springboot-integration</artifactId> 14 | <version>1.0.0.RELEASE</version> 15 | 16 | <!-- 继承说明:这里继承SpringBoot提供的父工程 --> 17 | <parent> 18 | <groupId>org.springframework.boot</groupId> 19 | <artifactId>spring-boot-starter-parent</artifactId> 20 | <version>1.5.7.RELEASE</version> 21 | <relativePath/> 22 | </parent> 23 | 24 | <!-- 模块说明:这里声明多个子模块 --> 25 | <modules> 26 | <module>mm-web</module> 27 | <module>mm-service</module> 28 | <module>mm-repo</module> 29 | <module>mm-entity</module> 30 | </modules> 31 | 32 | <!-- 版本说明:这里统一管理依赖的版本号 --> 33 | <dependencyManagement> 34 | <dependencies> 35 | <dependency> 36 | <groupId>com.hehe</groupId> 37 | <artifactId>mm-web</artifactId> 38 | <version>0.0.1-SNAPSHOT</version> 39 | </dependency> 40 | <dependency> 41 | <groupId>com.hehe</groupId> 42 | <artifactId>mm-service</artifactId> 43 | <version>0.0.1-SNAPSHOT</version> 44 | </dependency> 45 | <dependency> 46 | <groupId>com.hehe</groupId> 47 | <artifactId>mm-repo</artifactId> 48 | <version>0.0.1-SNAPSHOT</version> 49 | </dependency> 50 | <dependency> 51 | <groupId>com.hehe</groupId> 52 | <artifactId>mm-entity</artifactId> 53 | <version>0.0.1-SNAPSHOT</version> 54 | </dependency> 55 | </dependencies> 56 | </dependencyManagement> 57 | 58 | </project> -------------------------------------------------------------------------------- /springboot-locale-i18n/src/main/java/com/hehe/LocaleI18nApplication.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | import org.springframework.web.servlet.ModelAndView; 8 | 9 | @SpringBootApplication 10 | @RestController 11 | public class LocaleI18nApplication { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(LocaleI18nApplication.class, args); 15 | } 16 | 17 | /** 18 | * 登陆页面 19 | */ 20 | @GetMapping("/") 21 | public ModelAndView index() { 22 | return new ModelAndView("user/login"); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /springboot-locale-i18n/src/main/java/com/hehe/locale/LocaleConfig.java: -------------------------------------------------------------------------------- 1 | package com.hehe.locale; 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.LocaleChangeInterceptor; 9 | import org.springframework.web.servlet.i18n.SessionLocaleResolver; 10 | 11 | import java.util.Locale; 12 | 13 | /** 14 | * 配置国际化语言 15 | */ 16 | @Configuration 17 | public class LocaleConfig { 18 | 19 | /** 20 | * 默认解析器 其中locale表示默认语言 21 | */ 22 | @Bean 23 | public LocaleResolver localeResolver() { 24 | SessionLocaleResolver localeResolver = new SessionLocaleResolver(); 25 | localeResolver.setDefaultLocale(Locale.US); 26 | return localeResolver; 27 | } 28 | 29 | /** 30 | * 默认拦截器 其中lang表示切换语言的参数名 31 | */ 32 | @Bean 33 | public WebMvcConfigurer localeInterceptor() { 34 | return new WebMvcConfigurer() { 35 | @Override 36 | public void addInterceptors(InterceptorRegistry registry) { 37 | LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor(); 38 | localeInterceptor.setParamName("lang"); 39 | registry.addInterceptor(localeInterceptor); 40 | } 41 | }; 42 | } 43 | } -------------------------------------------------------------------------------- /springboot-locale-i18n/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | thymeleaf: 3 | cache: false #关闭页面缓存 4 | prefix: classpath:/views/ #页面拼接路径 5 | messages: 6 | basename: static/i18n/messages #相对路径 首部请勿添加斜杠 7 | -------------------------------------------------------------------------------- /springboot-locale-i18n/src/main/resources/static/i18n/messages.properties: -------------------------------------------------------------------------------- 1 | #默认文件不能删除 可以不填属性 2 | user.title= -------------------------------------------------------------------------------- /springboot-locale-i18n/src/main/resources/static/i18n/messages_en_US.properties: -------------------------------------------------------------------------------- 1 | #这里填写英语翻译 2 | user.title=User Login 3 | user.welcome=Welcome 4 | user.username=Username 5 | user.password=Password 6 | user.login=Sign In -------------------------------------------------------------------------------- /springboot-locale-i18n/src/main/resources/static/i18n/messages_zh_CN.properties: -------------------------------------------------------------------------------- 1 | #这里填写中文翻译 2 | user.title=用户登陆 3 | user.welcome=欢迎 4 | user.username=登陆用户 5 | user.password=登陆密码 6 | user.login=登陆 -------------------------------------------------------------------------------- /springboot-locale-i18n/src/main/resources/static/i18n/messages_zh_TW.properties: -------------------------------------------------------------------------------- 1 | #这里填写繁体翻译 2 | user.title=用戶登陸 3 | user.welcome=歡迎 4 | user.username=登陸用戶 5 | user.password=登陸密碼 6 | user.login=登陸 -------------------------------------------------------------------------------- /springboot-locale-i18n/src/test/java/com/hehe/LocaleI18NApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class LocaleI18NApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /springboot-mybatis-annotation/src/main/java/com/hehe/MybatisAnnotationApplication.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class MybatisAnnotationApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(MybatisAnnotationApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /springboot-mybatis-annotation/src/main/java/com/hehe/controller/UserController.java: -------------------------------------------------------------------------------- 1 | 2 | package com.hehe.controller; 3 | 4 | import com.hehe.mapper.UserMapper; 5 | import com.hehe.pojo.User; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.PathVariable; 9 | import org.springframework.web.bind.annotation.RequestMapping; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | import java.util.List; 13 | 14 | @RestController 15 | @RequestMapping("/user/*") 16 | public class UserController { 17 | 18 | @SuppressWarnings("all") 19 | @Autowired 20 | UserMapper userMapper; 21 | 22 | @GetMapping("list") 23 | public List<User> list() { 24 | return userMapper.list(); 25 | } 26 | 27 | @GetMapping("list/{username}") 28 | public List<User> listByUsername(@PathVariable("username") String username) { 29 | return userMapper.listByUsername(username); 30 | } 31 | 32 | @GetMapping("get/{username}/{password}") 33 | public User get(@PathVariable("username") String username, @PathVariable("password") String password) { 34 | return userMapper.get(username, password); 35 | } 36 | 37 | @GetMapping("get/bad/{username}/{password}") 38 | public User getBadUser(@PathVariable("username") String username, @PathVariable("password") String password) { 39 | return userMapper.getBadUser(username, password); 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /springboot-mybatis-annotation/src/main/java/com/hehe/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | 2 | package com.hehe.mapper; 3 | 4 | import com.hehe.pojo.User; 5 | import org.apache.ibatis.annotations.*; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * 主要用途:介绍Mybatis注解的常见使用方式. 11 | */ 12 | @Mapper 13 | public interface UserMapper { 14 | /** 15 | * 方式1:使用注解编写SQL。 16 | */ 17 | @Select("select * from t_user") 18 | @Results({ 19 | @Result(property = "userId", column = "USER_ID"), 20 | @Result(property = "username", column = "USERNAME"), 21 | @Result(property = "password", column = "PASSWORD"), 22 | @Result(property = "mobileNum", column = "PHONE_NUM") 23 | }) 24 | List<User> list(); 25 | 26 | /** 27 | * 方式2:使用注解指定某个工具类的方法来动态编写SQL. 28 | */ 29 | @SelectProvider(type = UserSqlProvider.class, method = "listByUsername") 30 | List<User> listByUsername(String username); 31 | 32 | /** 33 | * 延伸:上述两种方式都可以附加@Results注解来指定结果集的映射关系. 34 | * 35 | * PS:如果符合下划线转驼峰的匹配项可以直接省略不写。 36 | */ 37 | @Results({ 38 | @Result(property = "userId", column = "USER_ID"), 39 | @Result(property = "username", column = "USERNAME"), 40 | @Result(property = "password", column = "PASSWORD"), 41 | @Result(property = "mobileNum", column = "PHONE_NUM") 42 | }) 43 | @Select("select * from t_user") 44 | List<User> listSample(); 45 | 46 | /** 47 | * 延伸:无论什么方式,如果涉及多个参数,则必须加上@Param注解,否则无法使用EL表达式获取参数。 48 | */ 49 | @Select("select * from t_user where username like #{username} and password like #{password}") 50 | User get(@Param("username") String username, @Param("password") String password); 51 | 52 | @SelectProvider(type = UserSqlProvider.class, method = "getBadUser") 53 | User getBadUser(@Param("username") String username, @Param("password") String password); 54 | 55 | } -------------------------------------------------------------------------------- /springboot-mybatis-annotation/src/main/java/com/hehe/mapper/UserSqlProvider.java: -------------------------------------------------------------------------------- 1 | package com.hehe.mapper; 2 | 3 | import com.fasterxml.jackson.databind.PropertyNamingStrategy; 4 | import com.hehe.pojo.User; 5 | import org.apache.ibatis.annotations.Param; 6 | import org.apache.ibatis.jdbc.SQL; 7 | 8 | import java.lang.reflect.Field; 9 | 10 | /** 11 | * 主要用途:根据复杂的业务需求来动态生成SQL. 12 | * <p> 13 | * 目标:使用Java工具类来替代传统的XML文件.(例如:UserSqlProvider.java <-- UserMapper.xml) 14 | */ 15 | public class UserSqlProvider { 16 | /** 17 | * 方式1:在工具类的方法里,可以自己手工编写SQL。 18 | */ 19 | public String listByUsername(String username) { 20 | return "select * from t_user where username =#{username}"; 21 | } 22 | 23 | /** 24 | * 方式2:也可以根据官方提供的API来编写动态SQL。 25 | */ 26 | public String getBadUser(@Param("username") String username, @Param("password") String password) { 27 | return new SQL() {{ 28 | SELECT("*"); 29 | FROM("t_user"); 30 | if (username != null && password != null) { 31 | WHERE("username like #{username} and password like #{password}"); 32 | } else { 33 | WHERE("1=2"); 34 | } 35 | }}.toString(); 36 | } 37 | 38 | /** 39 | * 1.用于获取结果集的映射关系 40 | */ 41 | public static String getResultsStr(Class origin) { 42 | StringBuilder stringBuilder = new StringBuilder(); 43 | stringBuilder.append("@Results({\n"); 44 | for (Field field : origin.getDeclaredFields()) { 45 | String property = field.getName(); 46 | //映射关系:对象属性(驼峰)->数据库字段(下划线) 47 | String column = new PropertyNamingStrategy.SnakeCaseStrategy().translate(field.getName()).toUpperCase(); 48 | stringBuilder.append(String.format("@Result(property = \"%s\", column = \"%s\"),\n", property, column)); 49 | } 50 | stringBuilder.append("})"); 51 | return stringBuilder.toString(); 52 | } 53 | 54 | /** 55 | * 2.打印结果集的映射关系. 例如: 56 | * 57 | * @Results({ 58 | * @Result(property = "userId", column = "USER_ID"), 59 | * @Result(property = "username", column = "USERNAME"), 60 | * @Result(property = "password", column = "PASSWORD"), 61 | * @Result(property = "mobileNum", column = "PHONE_NUM"), 62 | * }) 63 | */ 64 | public static void main(String[] args) { 65 | System.out.println(getResultsStr(User.class)); 66 | } 67 | 68 | } -------------------------------------------------------------------------------- /springboot-mybatis-annotation/src/main/java/com/hehe/pojo/User.java: -------------------------------------------------------------------------------- 1 | package com.hehe.pojo; 2 | 3 | public class User { 4 | 5 | private String userId; 6 | private String username; 7 | private String password; 8 | private String mobileNum; 9 | 10 | public String getUserId() { 11 | return userId; 12 | } 13 | 14 | public void setUserId(String userId) { 15 | this.userId = userId; 16 | } 17 | 18 | public String getUsername() { 19 | return username; 20 | } 21 | 22 | public void setUsername(String username) { 23 | this.username = username; 24 | } 25 | 26 | public String getPassword() { 27 | return password; 28 | } 29 | 30 | public void setPassword(String password) { 31 | this.password = password; 32 | } 33 | 34 | public String getMobileNum() { 35 | return mobileNum; 36 | } 37 | 38 | public void setMobileNum(String mobileNum) { 39 | this.mobileNum = mobileNum; 40 | } 41 | } -------------------------------------------------------------------------------- /springboot-mybatis-annotation/src/main/resources/application.yml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yizhiwazi/springboot-socks/763dce7c4a4960dc6389eb96087a39ed3a278896/springboot-mybatis-annotation/src/main/resources/application.yml -------------------------------------------------------------------------------- /springboot-mybatis-annotation/src/test/java/com/hehe/MybatisAnnotationApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class MybatisAnnotationApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /springboot-mybatis-common/src/main/java/com/hehe/MybatisCommonApplication.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 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.hehe.mapper") 9 | public class MybatisCommonApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(MybatisCommonApplication.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /springboot-mybatis-common/src/main/java/com/hehe/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.hehe.controller; 2 | 3 | import com.hehe.entity.User; 4 | import com.hehe.service.UserService; 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 | import java.util.List; 10 | 11 | @RestController 12 | @RequestMapping("/user/*") 13 | public class UserController { 14 | 15 | @Autowired 16 | UserService userService; 17 | 18 | @RequestMapping("list") 19 | public List<User> list(User user) { 20 | return userService.list(user); 21 | } 22 | 23 | @RequestMapping("get") 24 | public User get(User user) { 25 | return userService.get(user); 26 | } 27 | 28 | @RequestMapping("update") 29 | public int update(User user) { 30 | return userService.update(user); 31 | } 32 | 33 | @RequestMapping("save") 34 | public int save(User user) { 35 | return userService.save(user); 36 | } 37 | 38 | @RequestMapping("delete") 39 | public int delete(User user) { 40 | return userService.delete(user); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /springboot-mybatis-common/src/main/java/com/hehe/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.hehe.entity; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Id; 5 | import javax.persistence.Table; 6 | import javax.persistence.Transient; 7 | 8 | @Table(name = "T_USER") 9 | public class User { 10 | @Id 11 | @Column(name = "USER_ID") 12 | private String userId; 13 | @Column(name = "USERNAME") 14 | private String username; 15 | @Column(name = "PASSWORD") 16 | private String password; 17 | @Column(name = "PHONE_NUM") 18 | private String mobileNum; 19 | @Transient 20 | private String other; //跟数据库无关的字段使用@Transient标记或移至VO类。 21 | 22 | public String getUserId() { 23 | return userId; 24 | } 25 | 26 | public void setUserId(String userId) { 27 | this.userId = userId; 28 | } 29 | 30 | public String getUsername() { 31 | return username; 32 | } 33 | 34 | public void setUsername(String username) { 35 | this.username = username; 36 | } 37 | 38 | public String getPassword() { 39 | return password; 40 | } 41 | 42 | public void setPassword(String password) { 43 | this.password = password; 44 | } 45 | 46 | public String getMobileNum() { 47 | return mobileNum; 48 | } 49 | 50 | public void setMobileNum(String mobileNum) { 51 | this.mobileNum = mobileNum; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /springboot-mybatis-common/src/main/java/com/hehe/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.hehe.mapper; 2 | 3 | import com.hehe.entity.User; 4 | import tk.mybatis.mapper.common.Mapper; 5 | 6 | /** 7 | * 继承通用Mapper获取CURD方法 8 | */ 9 | public interface UserMapper extends Mapper<User> { 10 | 11 | } 12 | -------------------------------------------------------------------------------- /springboot-mybatis-common/src/main/java/com/hehe/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.hehe.service; 2 | 3 | import com.hehe.entity.User; 4 | import com.hehe.tools.BaseService; 5 | 6 | public interface UserService extends BaseService<User> { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /springboot-mybatis-common/src/main/java/com/hehe/service/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.hehe.service; 2 | 3 | import com.hehe.entity.User; 4 | import com.hehe.tools.BaseServiceImpl; 5 | import org.springframework.stereotype.Service; 6 | 7 | @Service 8 | public class UserServiceImpl extends BaseServiceImpl<User> implements UserService { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /springboot-mybatis-common/src/main/java/com/hehe/tools/BaseService.java: -------------------------------------------------------------------------------- 1 | package com.hehe.tools; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * 通用Service 7 | */ 8 | public interface BaseService<T> { 9 | 10 | List<T> list(T entity); 11 | 12 | T get(T entity); 13 | 14 | int update(T entity); 15 | 16 | int save(T entity); 17 | 18 | int delete(T entity); 19 | 20 | } 21 | -------------------------------------------------------------------------------- /springboot-mybatis-common/src/main/java/com/hehe/tools/BaseServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.hehe.tools; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import tk.mybatis.mapper.common.Mapper; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * 主要用途:通用Service 封装常用的CRUD方法 10 | */ 11 | public class BaseServiceImpl<T> implements BaseService<T> { 12 | 13 | @Autowired 14 | private Mapper<T> mapper;//泛型装配 15 | 16 | @Override 17 | public List<T> list(T entity) { 18 | return mapper.select(entity); 19 | } 20 | 21 | @Override 22 | public T get(T entity) { 23 | return mapper.selectOne(entity); 24 | } 25 | 26 | @Override 27 | public int update(T entity) { 28 | return mapper.updateByPrimaryKeySelective(entity); 29 | } 30 | 31 | @Override 32 | public int save(T entity) { 33 | return mapper.insertSelective(entity); 34 | } 35 | 36 | @Override 37 | public int delete(T entity) { 38 | return mapper.deleteByPrimaryKey(entity); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /springboot-mybatis-common/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | #连接MySQL 4 | url: jdbc:mysql://localhost:3306/socks?useSSL=false 5 | username: root 6 | password: root 7 | driver-class-name: com.mysql.jdbc.Driver 8 | 9 | mybatis: 10 | configuration: 11 | #配置项:开启下划线到驼峰的自动转换. 作用:将数据库字段根据驼峰规则自动注入到对象属性。 12 | map-underscore-to-camel-case: true 13 | 14 | logging: 15 | level: 16 | #打印SQL信息 17 | com.hehe.mapper: debug -------------------------------------------------------------------------------- /springboot-mybatis-common/src/main/resources/t_user.sql: -------------------------------------------------------------------------------- 1 | USE `SOCKS`; 2 | DROP TABLE IF EXISTS `t_user`; 3 | CREATE TABLE `t_user` ( 4 | `USER_ID` varchar(50) , 5 | `USERNAME` varchar(50) , 6 | `PASSWORD` varchar(50) , 7 | `PHONE_NUM` varchar(15) 8 | ) ; 9 | 10 | INSERT INTO `t_user` VALUES ('1', 'admin', 'admin','15011791234'); 11 | INSERT INTO `t_user` VALUES ('2', 'roots', 'roots','18812342017'); -------------------------------------------------------------------------------- /springboot-mybatis/pom.xml: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="UTF-8"?> 2 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 | <modelVersion>4.0.0</modelVersion> 5 | <!--基本信息 --> 6 | <groupId>com.hehe</groupId> 7 | <artifactId>springboot-mybatis</artifactId> 8 | <version>0.0.1-SNAPSHOT</version> 9 | <packaging>jar</packaging> 10 | <name>spring-boot-mybatis</name> 11 | <description>SpringBoot快速整合Mybatis案例</description> 12 | 13 | <!--继承信息 --> 14 | <parent> 15 | <groupId>org.springframework.boot</groupId> 16 | <artifactId>spring-boot-starter-parent</artifactId> 17 | <version>2.0.0.M4</version> 18 | <relativePath/> 19 | </parent> 20 | 21 | <!--依赖管理 --> 22 | <dependencies> 23 | <dependency> <!--添加Web依赖 --> 24 | <groupId>org.springframework.boot</groupId> 25 | <artifactId>spring-boot-starter-web</artifactId> 26 | </dependency> 27 | <dependency> <!--添加Mybatis依赖 --> 28 | <groupId>org.mybatis.spring.boot</groupId> 29 | <artifactId>mybatis-spring-boot-starter</artifactId> 30 | <version>1.3.1</version> 31 | </dependency> 32 | <dependency><!--添加MySQL驱动依赖 --> 33 | <groupId>mysql</groupId> 34 | <artifactId>mysql-connector-java</artifactId> 35 | <scope>runtime</scope> 36 | </dependency> 37 | <dependency><!--添加Test依赖 --> 38 | <groupId>org.springframework.boot</groupId> 39 | <artifactId>spring-boot-starter-test</artifactId> 40 | <scope>test</scope> 41 | </dependency> 42 | </dependencies> 43 | 44 | <!--指定远程仓库(含插件)--> 45 | <repositories> 46 | <repository> 47 | <id>spring-snapshots</id> 48 | <url>http://repo.spring.io/snapshot</url> 49 | <snapshots><enabled>true</enabled></snapshots> 50 | </repository> 51 | <repository> 52 | <id>spring-milestones</id> 53 | <url>http://repo.spring.io/milestone</url> 54 | </repository> 55 | </repositories> 56 | <pluginRepositories> 57 | <pluginRepository> 58 | <id>spring-snapshots</id> 59 | <url>http://repo.spring.io/snapshot</url> 60 | </pluginRepository> 61 | <pluginRepository> 62 | <id>spring-milestones</id> 63 | <url>http://repo.spring.io/milestone</url> 64 | </pluginRepository> 65 | </pluginRepositories> 66 | 67 | <!--构建插件 --> 68 | <build> 69 | <plugins> 70 | <plugin> 71 | <groupId>org.springframework.boot</groupId> 72 | <artifactId>spring-boot-maven-plugin</artifactId> 73 | </plugin> 74 | </plugins> 75 | </build> 76 | 77 | </project> 78 | -------------------------------------------------------------------------------- /springboot-mybatis/src/main/java/com/hehe/MybatisApplication.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 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.hehe.mapper") 9 | public class MybatisApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(MybatisApplication.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /springboot-mybatis/src/main/java/com/hehe/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.hehe.controller; 2 | 3 | import com.hehe.pojo.R; 4 | import com.hehe.service.UserService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.PathVariable; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | @RestController 12 | @RequestMapping("/user/*") 13 | public class UserController { 14 | 15 | @Autowired 16 | UserService userService; 17 | 18 | @GetMapping("list") 19 | public R list() { 20 | try { 21 | return R.isOk().data(userService.list()); 22 | } catch (Exception e) { 23 | return R.isFail(e); 24 | } 25 | } 26 | 27 | @GetMapping("list/{username}") 28 | public R listbyname(@PathVariable("username") String username) { 29 | try { 30 | return R.isOk().data(userService.findByUsername(username)); 31 | } catch (Exception e) { 32 | return R.isFail(e); 33 | } 34 | } 35 | 36 | @GetMapping("get/{userId}") 37 | public R get(@PathVariable("userId") String userId) { 38 | try { 39 | return R.isOk().data(userService.get(userId)); 40 | } catch (Exception e) { 41 | return R.isFail(e); 42 | } 43 | } 44 | 45 | @GetMapping("del/{userId}") 46 | public R delete(@PathVariable("userId") String userId) { 47 | try { 48 | return R.isOk().data(userService.delete(userId)); 49 | } catch (Exception e) { 50 | return R.isFail(e); 51 | } 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /springboot-mybatis/src/main/java/com/hehe/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.hehe.mapper; 2 | 3 | import com.hehe.pojo.User; 4 | import org.apache.ibatis.annotations.Delete; 5 | import org.apache.ibatis.annotations.Select; 6 | 7 | import java.util.List; 8 | 9 | public interface UserMapper { 10 | 11 | @Select("select * from t_user where 1=1") 12 | List<User> list(); 13 | 14 | @Select("select * from t_user where username like #{username}") 15 | List<User> findByUsername(String username); 16 | 17 | @Select("select * from t_user where user_id like #{userId}") 18 | User getOne(String userId); 19 | 20 | @Delete("delete from t_user where user_id like #{userId}") 21 | int delete(String userId); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /springboot-mybatis/src/main/java/com/hehe/pojo/R.java: -------------------------------------------------------------------------------- 1 | package com.hehe.pojo; 2 | 3 | import java.io.Serializable; 4 | 5 | public class R<T> implements Serializable { 6 | 7 | private static final long serialVersionUID = -4577255781088498763L; 8 | private static final int OK = 0; 9 | private static final int FAIL = 1; 10 | private static final int UNAUTHORIZED = 2; 11 | 12 | private T data; //服务端数据 13 | private int status = OK; //状态码 14 | private String msg = ""; //描述信息 15 | 16 | //APIS 17 | public static R isOk(){ 18 | return new R(); 19 | } 20 | public static R isFail(){ 21 | return new R().status(FAIL); 22 | } 23 | public static R isFail(Throwable e){ 24 | return isFail().msg(e); 25 | } 26 | public R msg(Throwable e){ 27 | this.setMsg(e.toString()); 28 | return this; 29 | } 30 | public R data(T data){ 31 | this.setData(data); 32 | return this; 33 | } 34 | public R status(int status){ 35 | this.setStatus(status); 36 | return this; 37 | } 38 | 39 | 40 | //Constructors 41 | public R() { 42 | 43 | } 44 | 45 | //Getter&Setters 46 | public String getMsg() { 47 | return msg; 48 | } 49 | 50 | public void setMsg(String msg) { 51 | this.msg = msg; 52 | } 53 | 54 | public int getStatus() { 55 | return status; 56 | } 57 | 58 | public void setStatus(int status) { 59 | this.status = status; 60 | } 61 | 62 | public T getData() { 63 | return data; 64 | } 65 | 66 | public void setData(T data) { 67 | this.data = data; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /springboot-mybatis/src/main/java/com/hehe/pojo/User.java: -------------------------------------------------------------------------------- 1 | package com.hehe.pojo; 2 | 3 | public class User { 4 | 5 | private String userId; 6 | private String username; 7 | private String password; 8 | 9 | public User() { 10 | 11 | } 12 | 13 | public User(String userId, String username, String password) { 14 | this.userId = userId; 15 | this.username = username; 16 | this.password = password; 17 | } 18 | 19 | public String getUserId() { 20 | return userId; 21 | } 22 | 23 | public void setUserId(String userId) { 24 | this.userId = userId; 25 | } 26 | 27 | public String getUsername() { 28 | return username; 29 | } 30 | 31 | public void setUsername(String username) { 32 | this.username = username; 33 | } 34 | 35 | public String getPassword() { 36 | return password; 37 | } 38 | 39 | public void setPassword(String password) { 40 | this.password = password; 41 | } 42 | 43 | @Override 44 | public String toString() { 45 | return "User{" + 46 | "userId='" + userId + '\'' + 47 | ", username='" + username + '\'' + 48 | ", password='" + password + '\'' + 49 | '}'; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /springboot-mybatis/src/main/java/com/hehe/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.hehe.service; 2 | 3 | import com.hehe.pojo.User; 4 | import java.util.List; 5 | 6 | public interface UserService { 7 | 8 | List<User> list(); 9 | 10 | List<User> findByUsername(String username); 11 | 12 | User get(String userId); 13 | 14 | int delete(String userId); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /springboot-mybatis/src/main/java/com/hehe/service/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.hehe.service; 2 | 3 | import com.hehe.pojo.User; 4 | import com.hehe.mapper.UserMapper; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | import java.util.List; 9 | 10 | @Service 11 | public class UserServiceImpl implements UserService { 12 | 13 | @SuppressWarnings("all") 14 | @Autowired 15 | UserMapper userMapper; 16 | 17 | @Override 18 | public List<User> list() { 19 | return userMapper.list(); 20 | } 21 | 22 | @Override 23 | public List<User> findByUsername(String username) { 24 | return userMapper.findByUsername(username); 25 | } 26 | 27 | @Override 28 | public User get(String userId) { 29 | return userMapper.getOne(userId); 30 | } 31 | 32 | @Override 33 | public int delete(String userId) { 34 | return userMapper.delete(userId); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /springboot-mybatis/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | url: jdbc:mysql://localhost:3306/socks?useSSL=false 4 | username: root 5 | password: root 6 | driver-class-name: com.mysql.jdbc.Driver 7 | 8 | mybatis: 9 | configuration: 10 | map-underscore-to-camel-case: true #开启驼峰映射 11 | 12 | -------------------------------------------------------------------------------- /springboot-mybatis/src/main/resources/t_user.sql: -------------------------------------------------------------------------------- 1 | DROP DATABASE IF EXISTS `socks`; 2 | CREATE DATABASE `socks`; 3 | USE `SOCKS`; 4 | DROP TABLE IF EXISTS `t_user`; 5 | CREATE TABLE `t_user` ( 6 | `USER_ID` varchar(50) , 7 | `USERNAME` varchar(50) , 8 | `PASSWORD` varchar(50) 9 | ) ; 10 | 11 | INSERT INTO `t_user` VALUES ('1', 'admin', 'admin'); 12 | INSERT INTO `t_user` VALUES ('2', 'yizhiwazi', '123456'); -------------------------------------------------------------------------------- /springboot-mybatis/src/test/java/com/hehe/MybatisApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import com.hehe.mapper.UserMapper; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | import org.springframework.test.context.junit4.SpringRunner; 9 | import static org.assertj.core.api.Assertions.assertThat; 10 | 11 | @RunWith(SpringRunner.class) 12 | @SpringBootTest 13 | public class MybatisApplicationTest { 14 | 15 | @SuppressWarnings("all") 16 | @Autowired 17 | UserMapper userMapper; 18 | 19 | @Test 20 | public void test_db() { 21 | 22 | //开始进行测试 23 | assertThat(userMapper.list().size()).isGreaterThan(1); 24 | assertThat(userMapper.getOne("1")).isNotEqualTo(null); 25 | assertThat(userMapper.getOne("xxx")).isEqualTo(null); 26 | 27 | 28 | 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /springboot-nginx/pom.xml: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="UTF-8"?> 2 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 | <modelVersion>4.0.0</modelVersion> 5 | <!--基本信息 --> 6 | <groupId>com.hehe</groupId> 7 | <artifactId>springboot-nginx</artifactId> 8 | <version>0.0.1-SNAPSHOT</version> 9 | <packaging>jar</packaging> 10 | <name>spring-boot-nginx</name> 11 | <description>SpringBoot 使用Nginx实现跨域访问</description> 12 | 13 | <!--继承信息 --> 14 | <parent> 15 | <groupId>org.springframework.boot</groupId> 16 | <artifactId>spring-boot-starter-parent</artifactId> 17 | <version>2.0.0.M5</version> 18 | <relativePath/> 19 | </parent> 20 | 21 | <!--依赖管理 --> 22 | <dependencies> 23 | <dependency> <!--添加Web依赖 --> 24 | <groupId>org.springframework.boot</groupId> 25 | <artifactId>spring-boot-starter-web</artifactId> 26 | </dependency> 27 | <dependency><!--添加Test依赖 --> 28 | <groupId>org.springframework.boot</groupId> 29 | <artifactId>spring-boot-starter-test</artifactId> 30 | <scope>test</scope> 31 | </dependency> 32 | </dependencies> 33 | 34 | <!--指定远程仓库(含插件)--> 35 | <repositories> 36 | <repository> 37 | <id>spring-snapshots</id> 38 | <url>http://repo.spring.io/snapshot</url> 39 | <snapshots> 40 | <enabled>true</enabled> 41 | </snapshots> 42 | </repository> 43 | <repository> 44 | <id>spring-milestones</id> 45 | <url>http://repo.spring.io/milestone</url> 46 | </repository> 47 | </repositories> 48 | <pluginRepositories> 49 | <pluginRepository> 50 | <id>spring-snapshots</id> 51 | <url>http://repo.spring.io/snapshot</url> 52 | </pluginRepository> 53 | <pluginRepository> 54 | <id>spring-milestones</id> 55 | <url>http://repo.spring.io/milestone</url> 56 | </pluginRepository> 57 | </pluginRepositories> 58 | 59 | <!--构建插件 --> 60 | <build> 61 | <plugins> 62 | <plugin> 63 | <groupId>org.springframework.boot</groupId> 64 | <artifactId>spring-boot-maven-plugin</artifactId> 65 | </plugin> 66 | </plugins> 67 | </build> 68 | </project> 69 | -------------------------------------------------------------------------------- /springboot-nginx/src/main/java/com/hehe/SpringBootNginxApplication.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.lang.Nullable; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | import org.springframework.web.servlet.HandlerInterceptor; 10 | import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 11 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 12 | import org.springframework.web.util.WebUtils; 13 | 14 | import javax.servlet.http.HttpServletRequest; 15 | import javax.servlet.http.HttpServletResponse; 16 | 17 | @SpringBootApplication 18 | @RestController 19 | @RequestMapping("/user/login/*") 20 | public class SpringBootNginxApplication { 21 | 22 | //在拦截器打印访问URL 23 | @Bean 24 | public WebMvcConfigurer webMvcConfigurer() { 25 | return new WebMvcConfigurer() { 26 | @Override 27 | public void addInterceptors(InterceptorRegistry registry) { 28 | registry.addInterceptor(new HandlerInterceptor() { 29 | @Override 30 | public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception { 31 | if(response.getStatus()/100>=4){ 32 | System.err.println("访问URL:"+request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)); 33 | }else { 34 | System.out.println("访问URL:"+request.getRequestURI()); 35 | } 36 | } 37 | }); 38 | } 39 | }; 40 | } 41 | 42 | 43 | //提供验证码 44 | @RequestMapping("verifyCode") 45 | public String verifyCode(HttpServletRequest request) { 46 | request.getSession().setAttribute("verifyCode", "N7GX"); 47 | return request.getSession().getId() + ":" + request.getSession().getAttribute("verifyCode"); 48 | } 49 | 50 | //核对验证码 51 | @RequestMapping("checkVerifyCode") 52 | public String checkVerifyCode(HttpServletRequest request) { 53 | return request.getSession().getId() + ":" + request.getSession().getAttribute("verifyCode"); 54 | } 55 | 56 | public static void main(String[] args) { 57 | SpringApplication.run(SpringBootNginxApplication.class, args); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /springboot-nginx/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yizhiwazi/springboot-socks/763dce7c4a4960dc6389eb96087a39ed3a278896/springboot-nginx/src/main/resources/application.properties -------------------------------------------------------------------------------- /springboot-nginx/src/main/resources/nginx-1.9.9/conf/fastcgi.conf: -------------------------------------------------------------------------------- 1 | 2 | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 3 | fastcgi_param QUERY_STRING $query_string; 4 | fastcgi_param REQUEST_METHOD $request_method; 5 | fastcgi_param CONTENT_TYPE $content_type; 6 | fastcgi_param CONTENT_LENGTH $content_length; 7 | 8 | fastcgi_param SCRIPT_NAME $fastcgi_script_name; 9 | fastcgi_param REQUEST_URI $request_uri; 10 | fastcgi_param DOCUMENT_URI $document_uri; 11 | fastcgi_param DOCUMENT_ROOT $document_root; 12 | fastcgi_param SERVER_PROTOCOL $server_protocol; 13 | fastcgi_param REQUEST_SCHEME $scheme; 14 | fastcgi_param HTTPS $https if_not_empty; 15 | 16 | fastcgi_param GATEWAY_INTERFACE CGI/1.1; 17 | fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; 18 | 19 | fastcgi_param REMOTE_ADDR $remote_addr; 20 | fastcgi_param REMOTE_PORT $remote_port; 21 | fastcgi_param SERVER_ADDR $server_addr; 22 | fastcgi_param SERVER_PORT $server_port; 23 | fastcgi_param SERVER_NAME $server_name; 24 | 25 | # PHP only, required if PHP was built with --enable-force-cgi-redirect 26 | fastcgi_param REDIRECT_STATUS 200; 27 | -------------------------------------------------------------------------------- /springboot-nginx/src/main/resources/nginx-1.9.9/conf/fastcgi_params: -------------------------------------------------------------------------------- 1 | 2 | fastcgi_param QUERY_STRING $query_string; 3 | fastcgi_param REQUEST_METHOD $request_method; 4 | fastcgi_param CONTENT_TYPE $content_type; 5 | fastcgi_param CONTENT_LENGTH $content_length; 6 | 7 | fastcgi_param SCRIPT_NAME $fastcgi_script_name; 8 | fastcgi_param REQUEST_URI $request_uri; 9 | fastcgi_param DOCUMENT_URI $document_uri; 10 | fastcgi_param DOCUMENT_ROOT $document_root; 11 | fastcgi_param SERVER_PROTOCOL $server_protocol; 12 | fastcgi_param REQUEST_SCHEME $scheme; 13 | fastcgi_param HTTPS $https if_not_empty; 14 | 15 | fastcgi_param GATEWAY_INTERFACE CGI/1.1; 16 | fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; 17 | 18 | fastcgi_param REMOTE_ADDR $remote_addr; 19 | fastcgi_param REMOTE_PORT $remote_port; 20 | fastcgi_param SERVER_ADDR $server_addr; 21 | fastcgi_param SERVER_PORT $server_port; 22 | fastcgi_param SERVER_NAME $server_name; 23 | 24 | # PHP only, required if PHP was built with --enable-force-cgi-redirect 25 | fastcgi_param REDIRECT_STATUS 200; 26 | -------------------------------------------------------------------------------- /springboot-nginx/src/main/resources/nginx-1.9.9/conf/koi-win: -------------------------------------------------------------------------------- 1 | 2 | charset_map koi8-r windows-1251 { 3 | 4 | 80 88 ; # euro 5 | 6 | 95 95 ; # bullet 7 | 8 | 9A A0 ; # 9 | 10 | 9E B7 ; # · 11 | 12 | A3 B8 ; # small yo 13 | A4 BA ; # small Ukrainian ye 14 | 15 | A6 B3 ; # small Ukrainian i 16 | A7 BF ; # small Ukrainian yi 17 | 18 | AD B4 ; # small Ukrainian soft g 19 | AE A2 ; # small Byelorussian short u 20 | 21 | B0 B0 ; # ° 22 | 23 | B3 A8 ; # capital YO 24 | B4 AA ; # capital Ukrainian YE 25 | 26 | B6 B2 ; # capital Ukrainian I 27 | B7 AF ; # capital Ukrainian YI 28 | 29 | B9 B9 ; # numero sign 30 | 31 | BD A5 ; # capital Ukrainian soft G 32 | BE A1 ; # capital Byelorussian short U 33 | 34 | BF A9 ; # (C) 35 | 36 | C0 FE ; # small yu 37 | C1 E0 ; # small a 38 | C2 E1 ; # small b 39 | C3 F6 ; # small ts 40 | C4 E4 ; # small d 41 | C5 E5 ; # small ye 42 | C6 F4 ; # small f 43 | C7 E3 ; # small g 44 | C8 F5 ; # small kh 45 | C9 E8 ; # small i 46 | CA E9 ; # small j 47 | CB EA ; # small k 48 | CC EB ; # small l 49 | CD EC ; # small m 50 | CE ED ; # small n 51 | CF EE ; # small o 52 | 53 | D0 EF ; # small p 54 | D1 FF ; # small ya 55 | D2 F0 ; # small r 56 | D3 F1 ; # small s 57 | D4 F2 ; # small t 58 | D5 F3 ; # small u 59 | D6 E6 ; # small zh 60 | D7 E2 ; # small v 61 | D8 FC ; # small soft sign 62 | D9 FB ; # small y 63 | DA E7 ; # small z 64 | DB F8 ; # small sh 65 | DC FD ; # small e 66 | DD F9 ; # small shch 67 | DE F7 ; # small ch 68 | DF FA ; # small hard sign 69 | 70 | E0 DE ; # capital YU 71 | E1 C0 ; # capital A 72 | E2 C1 ; # capital B 73 | E3 D6 ; # capital TS 74 | E4 C4 ; # capital D 75 | E5 C5 ; # capital YE 76 | E6 D4 ; # capital F 77 | E7 C3 ; # capital G 78 | E8 D5 ; # capital KH 79 | E9 C8 ; # capital I 80 | EA C9 ; # capital J 81 | EB CA ; # capital K 82 | EC CB ; # capital L 83 | ED CC ; # capital M 84 | EE CD ; # capital N 85 | EF CE ; # capital O 86 | 87 | F0 CF ; # capital P 88 | F1 DF ; # capital YA 89 | F2 D0 ; # capital R 90 | F3 D1 ; # capital S 91 | F4 D2 ; # capital T 92 | F5 D3 ; # capital U 93 | F6 C6 ; # capital ZH 94 | F7 C2 ; # capital V 95 | F8 DC ; # capital soft sign 96 | F9 DB ; # capital Y 97 | FA C7 ; # capital Z 98 | FB D8 ; # capital SH 99 | FC DD ; # capital E 100 | FD D9 ; # capital SHCH 101 | FE D7 ; # capital CH 102 | FF DA ; # capital hard sign 103 | } 104 | -------------------------------------------------------------------------------- /springboot-nginx/src/main/resources/nginx-1.9.9/conf/nginx.conf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yizhiwazi/springboot-socks/763dce7c4a4960dc6389eb96087a39ed3a278896/springboot-nginx/src/main/resources/nginx-1.9.9/conf/nginx.conf -------------------------------------------------------------------------------- /springboot-nginx/src/main/resources/nginx-1.9.9/conf/scgi_params: -------------------------------------------------------------------------------- 1 | 2 | scgi_param REQUEST_METHOD $request_method; 3 | scgi_param REQUEST_URI $request_uri; 4 | scgi_param QUERY_STRING $query_string; 5 | scgi_param CONTENT_TYPE $content_type; 6 | 7 | scgi_param DOCUMENT_URI $document_uri; 8 | scgi_param DOCUMENT_ROOT $document_root; 9 | scgi_param SCGI 1; 10 | scgi_param SERVER_PROTOCOL $server_protocol; 11 | scgi_param REQUEST_SCHEME $scheme; 12 | scgi_param HTTPS $https if_not_empty; 13 | 14 | scgi_param REMOTE_ADDR $remote_addr; 15 | scgi_param REMOTE_PORT $remote_port; 16 | scgi_param SERVER_PORT $server_port; 17 | scgi_param SERVER_NAME $server_name; 18 | -------------------------------------------------------------------------------- /springboot-nginx/src/main/resources/nginx-1.9.9/conf/uwsgi_params: -------------------------------------------------------------------------------- 1 | 2 | uwsgi_param QUERY_STRING $query_string; 3 | uwsgi_param REQUEST_METHOD $request_method; 4 | uwsgi_param CONTENT_TYPE $content_type; 5 | uwsgi_param CONTENT_LENGTH $content_length; 6 | 7 | uwsgi_param REQUEST_URI $request_uri; 8 | uwsgi_param PATH_INFO $document_uri; 9 | uwsgi_param DOCUMENT_ROOT $document_root; 10 | uwsgi_param SERVER_PROTOCOL $server_protocol; 11 | uwsgi_param REQUEST_SCHEME $scheme; 12 | uwsgi_param HTTPS $https if_not_empty; 13 | 14 | uwsgi_param REMOTE_ADDR $remote_addr; 15 | uwsgi_param REMOTE_PORT $remote_port; 16 | uwsgi_param SERVER_PORT $server_port; 17 | uwsgi_param SERVER_NAME $server_name; 18 | -------------------------------------------------------------------------------- /springboot-nginx/src/main/resources/nginx-1.9.9/contrib/README: -------------------------------------------------------------------------------- 1 | 2 | geo2nginx.pl by Andrei Nigmatulin 3 | 4 | The perl script to convert CSV geoip database ( free download 5 | at http://www.maxmind.com/app/geoip_country ) to format, suitable 6 | for use by the ngx_http_geo_module. 7 | 8 | 9 | unicode2nginx by Maxim Dounin 10 | 11 | The perl script to convert unicode mappings ( available 12 | at http://www.unicode.org/Public/MAPPINGS/ ) to the nginx 13 | configuration file format. 14 | Two generated full maps for windows-1251 and koi8-r. 15 | 16 | 17 | vim by Evan Miller 18 | 19 | Syntax highlighting of nginx configuration for vim, to be 20 | placed into ~/.vim/. 21 | 22 | -------------------------------------------------------------------------------- /springboot-nginx/src/main/resources/nginx-1.9.9/contrib/geo2nginx.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | 3 | # (c) Andrei Nigmatulin, 2005 4 | # 5 | # this script provided "as is", without any warranties. use it at your own risk. 6 | # 7 | # special thanx to Andrew Sitnikov for perl port 8 | # 9 | # this script converts CSV geoip database (free download at http://www.maxmind.com/app/geoip_country) 10 | # to format, suitable for use with nginx_http_geo module (http://sysoev.ru/nginx) 11 | # 12 | # for example, line with ip range 13 | # 14 | # "62.16.68.0","62.16.127.255","1041253376","1041268735","RU","Russian Federation" 15 | # 16 | # will be converted to four subnetworks: 17 | # 18 | # 62.16.68.0/22 RU; 19 | # 62.16.72.0/21 RU; 20 | # 62.16.80.0/20 RU; 21 | # 62.16.96.0/19 RU; 22 | 23 | 24 | use warnings; 25 | use strict; 26 | 27 | while( <STDIN> ){ 28 | if (/"[^"]+","[^"]+","([^"]+)","([^"]+)","([^"]+)"/){ 29 | print_subnets($1, $2, $3); 30 | } 31 | } 32 | 33 | sub print_subnets { 34 | my ($a1, $a2, $c) = @_; 35 | my $l; 36 | while ($a1 <= $a2) { 37 | for ($l = 0; ($a1 & (1 << $l)) == 0 && ($a1 + ((1 << ($l + 1)) - 1)) <= $a2; $l++){}; 38 | print long2ip($a1) . "/" . (32 - $l) . " " . $c . ";\n"; 39 | $a1 += (1 << $l); 40 | } 41 | } 42 | 43 | sub long2ip { 44 | my $ip = shift; 45 | 46 | my $str = 0; 47 | 48 | $str = ($ip & 255); 49 | 50 | $ip >>= 8; 51 | $str = ($ip & 255).".$str"; 52 | 53 | $ip >>= 8; 54 | $str = ($ip & 255).".$str"; 55 | 56 | $ip >>= 8; 57 | $str = ($ip & 255).".$str"; 58 | } 59 | -------------------------------------------------------------------------------- /springboot-nginx/src/main/resources/nginx-1.9.9/contrib/unicode2nginx/unicode-to-nginx.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | 3 | # Convert unicode mappings to nginx configuration file format. 4 | 5 | # You may find useful mappings in various places, including 6 | # unicode.org official site: 7 | # 8 | # http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1251.TXT 9 | # http://www.unicode.org/Public/MAPPINGS/VENDORS/MISC/KOI8-R.TXT 10 | 11 | # Needs perl 5.6 or later. 12 | 13 | # Written by Maxim Dounin, mdounin@rambler-co.ru 14 | 15 | ############################################################################### 16 | 17 | require 5.006; 18 | 19 | while (<>) { 20 | # Skip comments and empty lines 21 | 22 | next if /^#/; 23 | next if /^\s*$/; 24 | chomp; 25 | 26 | # Convert mappings 27 | 28 | if (/^\s*0x(..)\s*0x(....)\s*(#.*)/) { 29 | # Mapping <from-code> <unicode-code> "#" <unicode-name> 30 | my $cs_code = $1; 31 | my $un_code = $2; 32 | my $un_name = $3; 33 | 34 | # Produce UTF-8 sequence from character code; 35 | 36 | my $un_utf8 = join('', map { sprintf("%02X", $_) } unpack("C*", pack("U", hex($un_code)))); 37 | 38 | print " $cs_code $un_utf8 ; $un_name\n"; 39 | 40 | } else { 41 | warn "Unrecognized line: '$_'"; 42 | } 43 | } 44 | 45 | ############################################################################### 46 | -------------------------------------------------------------------------------- /springboot-nginx/src/main/resources/nginx-1.9.9/contrib/vim/ftdetect/nginx.vim: -------------------------------------------------------------------------------- 1 | au BufRead,BufNewFile *.nginx set ft=nginx 2 | au BufRead,BufNewFile */etc/nginx/* set ft=nginx 3 | au BufRead,BufNewFile */usr/local/nginx/conf/* set ft=nginx 4 | au BufRead,BufNewFile nginx.conf set ft=nginx 5 | -------------------------------------------------------------------------------- /springboot-nginx/src/main/resources/nginx-1.9.9/contrib/vim/indent/nginx.vim: -------------------------------------------------------------------------------- 1 | if exists("b:did_indent") 2 | finish 3 | endif 4 | let b:did_indent = 1 5 | 6 | setlocal indentexpr= 7 | 8 | " cindent actually works for nginx' simple file structure 9 | setlocal cindent 10 | " Just make sure that the comments are not reset as defs would be. 11 | setlocal cinkeys-=0# 12 | -------------------------------------------------------------------------------- /springboot-nginx/src/main/resources/nginx-1.9.9/docs/LICENSE: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2002-2015 Igor Sysoev 3 | * Copyright (C) 2011-2015 Nginx, Inc. 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 9 | * 1. Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 | * SUCH DAMAGE. 26 | */ 27 | -------------------------------------------------------------------------------- /springboot-nginx/src/main/resources/nginx-1.9.9/docs/README: -------------------------------------------------------------------------------- 1 | 2 | Documentation is available at http://nginx.org 3 | 4 | -------------------------------------------------------------------------------- /springboot-nginx/src/main/resources/nginx-1.9.9/docs/zlib.LICENSE: -------------------------------------------------------------------------------- 1 | (C) 1995-2013 Jean-loup Gailly and Mark Adler 2 | 3 | This software is provided 'as-is', without any express or implied 4 | warranty. In no event will the authors be held liable for any damages 5 | arising from the use of this software. 6 | 7 | Permission is granted to anyone to use this software for any purpose, 8 | including commercial applications, and to alter it and redistribute it 9 | freely, subject to the following restrictions: 10 | 11 | 1. The origin of this software must not be misrepresented; you must not 12 | claim that you wrote the original software. If you use this software 13 | in a product, an acknowledgment in the product documentation would be 14 | appreciated but is not required. 15 | 2. Altered source versions must be plainly marked as such, and must not be 16 | misrepresented as being the original software. 17 | 3. This notice may not be removed or altered from any source distribution. 18 | 19 | Jean-loup Gailly Mark Adler 20 | jloup@gzip.org madler@alumni.caltech.edu 21 | -------------------------------------------------------------------------------- /springboot-nginx/src/main/resources/nginx-1.9.9/html/50x.html: -------------------------------------------------------------------------------- 1 | <!DOCTYPE html> 2 | <html> 3 | <head> 4 | <title>Error</title> 5 | <style> 6 | body { 7 | width: 35em; 8 | margin: 0 auto; 9 | font-family: Tahoma, Verdana, Arial, sans-serif; 10 | } 11 | </style> 12 | </head> 13 | <body> 14 | <h1>An error occurred.</h1> 15 | <p>Sorry, the page you are looking for is currently unavailable.<br/> 16 | Please try again later.</p> 17 | <p>If you are the system administrator of this resource then you should check 18 | the <a href="http://nginx.org/r/error_log">error log</a> for details.</p> 19 | <p><em>Faithfully yours, nginx.</em></p> 20 | </body> 21 | </html> 22 | -------------------------------------------------------------------------------- /springboot-nginx/src/main/resources/nginx-1.9.9/html/hehe/index.html: -------------------------------------------------------------------------------- 1 | <!DOCTYPE html> 2 | <html lang="en"> 3 | <head> 4 | <meta charset="UTF-8"/> 5 | <title>Page Index</title> 6 | </head> 7 | <body> 8 | <h2>前台系统7000</h2> 9 | <p id="info1"></p> 10 | <p id="info2"></p> 11 | </body> 12 | 13 | <script src="js/jquery.js"></script> 14 | <script> 15 | $.ajax({ 16 | url: 'http://localhost:7000/api/user/login/verifyCode', 17 | type: "POST", 18 | success: function (data) { 19 | //1.获取验证码 20 | $("#info1").html("跨域访问成功:verifyCode:" + data); 21 | //2.核对验证码 22 | $.ajax({ 23 | url: 'http://localhost:7000/api/user/login/checkVerifyCode', 24 | type: "POST", 25 | success: function (data) { 26 | $("#info2").html("跨域访问成功:checkVerifyCode:" + data); 27 | } 28 | }); 29 | }, 30 | error: function (data) { 31 | $("#info1").html("跨域失败!!"); 32 | } 33 | }); 34 | </script> 35 | 36 | </html> -------------------------------------------------------------------------------- /springboot-nginx/src/main/resources/nginx-1.9.9/html/index.html: -------------------------------------------------------------------------------- 1 | <!DOCTYPE html> 2 | <html> 3 | <head> 4 | <title>Welcome to nginx!</title> 5 | <style> 6 | body { 7 | width: 35em; 8 | margin: 0 auto; 9 | font-family: Tahoma, Verdana, Arial, sans-serif; 10 | } 11 | </style> 12 | </head> 13 | <body> 14 | <h1>Welcome to nginx!</h1> 15 | <p>If you see this page, the nginx web server is successfully installed and 16 | working. Further configuration is required.</p> 17 | 18 | <p>For online documentation and support please refer to 19 | <a href="http://nginx.org/">nginx.org</a>.<br/> 20 | Commercial support is available at 21 | <a href="http://nginx.com/">nginx.com</a>.</p> 22 | 23 | <p><em>Thank you for using nginx.</em></p> 24 | </body> 25 | </html> 26 | -------------------------------------------------------------------------------- /springboot-nginx/src/main/resources/nginx-1.9.9/logs/nginx.pid: -------------------------------------------------------------------------------- 1 | 7012 2 | -------------------------------------------------------------------------------- /springboot-nginx/src/main/resources/nginx-1.9.9/修改Nginx配置.lnk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yizhiwazi/springboot-socks/763dce7c4a4960dc6389eb96087a39ed3a278896/springboot-nginx/src/main/resources/nginx-1.9.9/修改Nginx配置.lnk -------------------------------------------------------------------------------- /springboot-nginx/src/main/resources/nginx-1.9.9/启动Nginx.lnk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yizhiwazi/springboot-socks/763dce7c4a4960dc6389eb96087a39ed3a278896/springboot-nginx/src/main/resources/nginx-1.9.9/启动Nginx.lnk -------------------------------------------------------------------------------- /springboot-nginx/src/main/resources/nginx-1.9.9/结束Nginx.bat: -------------------------------------------------------------------------------- 1 | taskkill /f /im nginx.exe -------------------------------------------------------------------------------- /springboot-nginx/src/test/java/com/hehe/SpringBootNginxApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringBootNginxApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /springboot-schedule-task/src/main/java/com/hehe/ScheduleTaskApplication.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class ScheduleTaskApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(ScheduleTaskApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /springboot-schedule-task/src/main/java/com/hehe/config/CompleteScheduleConfig.java: -------------------------------------------------------------------------------- 1 | package com.hehe.config; 2 | 3 | import org.apache.ibatis.annotations.Mapper; 4 | import org.apache.ibatis.annotations.Select; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.context.annotation.Configuration; 7 | import org.springframework.scheduling.annotation.EnableScheduling; 8 | import org.springframework.scheduling.annotation.SchedulingConfigurer; 9 | import org.springframework.scheduling.config.ScheduledTaskRegistrar; 10 | import org.springframework.scheduling.support.CronTrigger; 11 | import org.springframework.util.StringUtils; 12 | 13 | import java.time.LocalDateTime; 14 | 15 | @Configuration 16 | @EnableScheduling 17 | public class CompleteScheduleConfig implements SchedulingConfigurer { 18 | 19 | @Mapper 20 | public interface CronMapper { 21 | @Select("select cron from cron limit 1") 22 | String getCron(); 23 | } 24 | 25 | @Autowired 26 | @SuppressWarnings("all") 27 | CronMapper cronMapper; 28 | 29 | /** 30 | * 执行定时任务. 31 | */ 32 | @Override 33 | public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { 34 | taskRegistrar.addTriggerTask( 35 | //1.添加任务内容(Runnable) 36 | () -> System.out.println("执行定时任务2: " + LocalDateTime.now().toLocalTime()), 37 | //2.设置执行周期(Trigger) 38 | triggerContext -> { 39 | //2.1 从数据库获取执行周期 40 | String cron = cronMapper.getCron(); 41 | //2.2 合法性校验. 42 | if (StringUtils.isEmpty(cron)) { 43 | // Omitted Code .. 44 | } 45 | //2.3 返回执行周期(Date) 46 | return new CronTrigger(cron).nextExecutionTime(triggerContext); 47 | } 48 | ); 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /springboot-schedule-task/src/main/java/com/hehe/config/SimpleScheduleConfig.java: -------------------------------------------------------------------------------- 1 | package com.hehe.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.scheduling.annotation.EnableScheduling; 5 | import org.springframework.scheduling.annotation.Scheduled; 6 | 7 | import java.time.LocalDateTime; 8 | 9 | @Configuration //1.主要用于标记配置类,兼备Component的效果。 10 | @EnableScheduling // 2.开启定时任务 11 | public class SimpleScheduleConfig { 12 | //3.添加定时任务 13 | @Scheduled(cron = "0/5 * * * * ?") 14 | private void configureTasks() { 15 | System.err.println("执行定时任务1: " + LocalDateTime.now()); 16 | } 17 | } -------------------------------------------------------------------------------- /springboot-schedule-task/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | url: jdbc:mysql://localhost:3306/socks?useSSL=false 4 | username: root 5 | password: root -------------------------------------------------------------------------------- /springboot-schedule-task/src/test/java/com/hehe/ScheduleTaskApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class ScheduleTaskApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /springboot-send-mail/src/main/java/com/hehe/MailApplication.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class MailApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(MailApplication.class); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /springboot-send-mail/src/main/java/com/hehe/web/MailController.java: -------------------------------------------------------------------------------- 1 | package com.hehe.web; 2 | 3 | import com.hehe.service.MailService; 4 | import com.hehe.vo.MailVo; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.PostMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | import org.springframework.web.multipart.MultipartFile; 10 | import org.springframework.web.servlet.ModelAndView; 11 | 12 | @RestController 13 | public class MailController { 14 | 15 | @Autowired 16 | private MailService mailService; 17 | 18 | 19 | /** 20 | * 发送邮件的主界面 21 | */ 22 | @GetMapping("/") 23 | public ModelAndView index() { 24 | ModelAndView mv = new ModelAndView("mail/sendMail");//打开发送邮件的页面 25 | mv.addObject("from", mailService.getMailSendFrom());//邮件发信人 26 | return mv; 27 | } 28 | 29 | 30 | /** 31 | * 发送邮件 32 | */ 33 | @PostMapping("/mail/send") 34 | public MailVo sendMail(MailVo mailVo, MultipartFile[] files) { 35 | mailVo.setMultipartFiles(files); 36 | return mailService.sendMail(mailVo); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /springboot-send-mail/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | mail: 3 | host: smtp.163.com #SMTP服务器地址 4 | username: socks #登陆账号 5 | password: 123456 #登陆密码(或授权码) 6 | properties: 7 | from: socks@163.com #邮件发信人(即真实邮箱) 8 | thymeleaf: 9 | cache: false 10 | prefix: classpath:/views/ 11 | servlet: 12 | multipart: 13 | max-file-size: 10MB #限制单个文件大小 14 | max-request-size: 50MB #限制请求总量 -------------------------------------------------------------------------------- /springboot-send-mail/src/test/java/com/hehe/springbootsendmail/SpringbootSendMailApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.hehe.springbootsendmail; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringbootSendMailApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | 18 | -------------------------------------------------------------------------------- /springboot-swagger2-auto-close/src/main/java/com/hehe/Swagger2AutoApplication.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Swagger2AutoApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Swagger2AutoApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /springboot-swagger2-auto-close/src/main/java/com/hehe/config/Swagger2Config.java: -------------------------------------------------------------------------------- 1 | package com.hehe.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.context.annotation.Profile; 6 | import springfox.documentation.builders.ApiInfoBuilder; 7 | import springfox.documentation.builders.PathSelectors; 8 | import springfox.documentation.builders.RequestHandlerSelectors; 9 | import springfox.documentation.service.Contact; 10 | import springfox.documentation.spi.DocumentationType; 11 | import springfox.documentation.spring.web.plugins.Docket; 12 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 13 | 14 | /** 15 | * Swagger2 接口配置 16 | */ 17 | 18 | @Configuration 19 | @EnableSwagger2 20 | @Profile({"dev","test"}) 21 | //@ConditionalOnProperty(name = "swagger.enable", havingValue = "true") 22 | public class Swagger2Config { 23 | /** 24 | * 添加摘要信息(Docket) 25 | */ 26 | @Bean 27 | public Docket controllerApi() { 28 | return new Docket(DocumentationType.SWAGGER_2) 29 | .apiInfo(new ApiInfoBuilder() 30 | .title("标题:某公司_用户信息管理系统_接口文档") 31 | .description("描述:用于管理集团旗下公司的人员信息,具体包括XXX,XXX模块...") 32 | .contact(new Contact("Socks", null, null)) 33 | .version("版本号:1.0") 34 | .build()) 35 | .select() 36 | .apis(RequestHandlerSelectors.basePackage("com.hehe.controller")) 37 | .paths(PathSelectors.any()) 38 | .build(); 39 | } 40 | } -------------------------------------------------------------------------------- /springboot-swagger2-auto-close/src/main/java/com/hehe/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.hehe.controller; 2 | 3 | import com.hehe.entity.User; 4 | import io.swagger.annotations.Api; 5 | import io.swagger.annotations.ApiImplicitParam; 6 | import io.swagger.annotations.ApiImplicitParams; 7 | import io.swagger.annotations.ApiOperation; 8 | import org.springframework.web.bind.annotation.*; 9 | import springfox.documentation.annotations.ApiIgnore; 10 | 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | 14 | /** 15 | * 主要用途: 用户信息管理 16 | * 17 | * @author yizhiwazi 18 | * @see Api 描述类/接口的主要用途 19 | * @see ApiOperation 描述方法用途 20 | * @see ApiImplicitParams 描述方法的参数(Multi-Params) 21 | * @see ApiImplicitParam 描述方法的参数 22 | * @see ApiIgnore 忽略某类/方法/参数的文档 23 | */ 24 | @Api("用户信息管理") 25 | @RestController 26 | @RequestMapping("/user/*") 27 | public class UserController { 28 | 29 | private final static List<User> userList = new ArrayList<>(); 30 | 31 | { 32 | userList.add(new User("1", "admin", "123456")); 33 | userList.add(new User("2", "jacks", "111111")); 34 | } 35 | 36 | @ApiOperation("获取列表") 37 | @GetMapping("list") 38 | public List userList() { 39 | return userList; 40 | } 41 | 42 | @ApiOperation("新增用户") 43 | @PostMapping("save") 44 | public boolean save(User user) { 45 | return userList.add(user); 46 | } 47 | 48 | @ApiOperation("更新用户") 49 | @ApiImplicitParam(name = "user", value = "单个用户信息", dataType = "User") 50 | @PutMapping("update") 51 | public boolean update(User user) { 52 | return userList.remove(user) && userList.add(user); 53 | } 54 | 55 | @ApiOperation("批量删除") 56 | @ApiImplicitParam(name = "users", value = "N个用户信息", dataType = "List<User>") 57 | @DeleteMapping("delete") 58 | public boolean delete(@RequestBody List<User> users) { 59 | return userList.removeAll(users); 60 | } 61 | } -------------------------------------------------------------------------------- /springboot-swagger2-auto-close/src/main/java/com/hehe/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.hehe.entity; 2 | 3 | public class User { 4 | 5 | private String userId; 6 | private String username; 7 | private String password; 8 | 9 | public User() { 10 | 11 | } 12 | 13 | public User(String userId, String username, String password) { 14 | this.userId = userId; 15 | this.username = username; 16 | this.password = password; 17 | } 18 | 19 | @Override 20 | public boolean equals(Object o) { 21 | if (this == o) { 22 | return true; 23 | } 24 | if (o == null || getClass() != o.getClass()) { 25 | return false; 26 | } 27 | 28 | User user = (User) o; 29 | 30 | return userId != null ? userId.equals(user.userId) : user.userId == null; 31 | } 32 | 33 | @Override 34 | public int hashCode() { 35 | int result = userId != null ? userId.hashCode() : 0; 36 | result = 31 * result + (username != null ? username.hashCode() : 0); 37 | result = 31 * result + (password != null ? password.hashCode() : 0); 38 | return result; 39 | } 40 | 41 | public String getUserId() { 42 | return userId; 43 | } 44 | 45 | public void setUserId(String userId) { 46 | this.userId = userId; 47 | } 48 | 49 | public String getUsername() { 50 | return username; 51 | } 52 | 53 | public void setUsername(String username) { 54 | this.username = username; 55 | } 56 | 57 | public String getPassword() { 58 | return password; 59 | } 60 | 61 | public void setPassword(String password) { 62 | this.password = password; 63 | } 64 | } -------------------------------------------------------------------------------- /springboot-swagger2-auto-close/src/main/resources/application-dev.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8081 3 | 4 | swagger.enable: true 5 | 6 | -------------------------------------------------------------------------------- /springboot-swagger2-auto-close/src/main/resources/application-prod.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8082 3 | 4 | 5 | -------------------------------------------------------------------------------- /springboot-swagger2-auto-close/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | profiles: 3 | active: prod -------------------------------------------------------------------------------- /springboot-swagger2-auto-close/src/test/java/com/hehe/SpringbootSwagger2AutoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringbootSwagger2AutoApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /springboot-swagger2-auto-close/target/classes/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | profiles: 3 | active: prod -------------------------------------------------------------------------------- /springboot-swagger2-auto-close/target/classes/com/hehe/config/Swagger2Config.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yizhiwazi/springboot-socks/763dce7c4a4960dc6389eb96087a39ed3a278896/springboot-swagger2-auto-close/target/classes/com/hehe/config/Swagger2Config.class -------------------------------------------------------------------------------- /springboot-swagger2-auto-close/target/classes/com/hehe/controller/UserController.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yizhiwazi/springboot-socks/763dce7c4a4960dc6389eb96087a39ed3a278896/springboot-swagger2-auto-close/target/classes/com/hehe/controller/UserController.class -------------------------------------------------------------------------------- /springboot-swagger2-auto-close/target/classes/com/hehe/entity/User.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yizhiwazi/springboot-socks/763dce7c4a4960dc6389eb96087a39ed3a278896/springboot-swagger2-auto-close/target/classes/com/hehe/entity/User.class -------------------------------------------------------------------------------- /springboot-swagger2/src/main/java/com/hehe/Swagger2Application.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Swagger2Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Swagger2Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /springboot-swagger2/src/main/java/com/hehe/config/Swagger2Config.java: -------------------------------------------------------------------------------- 1 | package com.hehe.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import springfox.documentation.builders.ApiInfoBuilder; 6 | import springfox.documentation.builders.PathSelectors; 7 | import springfox.documentation.builders.RequestHandlerSelectors; 8 | import springfox.documentation.service.Contact; 9 | import springfox.documentation.spi.DocumentationType; 10 | import springfox.documentation.spring.web.plugins.Docket; 11 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 12 | /** 13 | * 主要用途:开启在线接口文档和添加相关配置 14 | * 15 | * @author yizhiwazi 16 | */ 17 | 18 | @Configuration 19 | @EnableSwagger2 20 | public class Swagger2Config { 21 | /** 22 | * 添加摘要信息(Docket) 23 | */ 24 | @Bean 25 | public Docket controllerApi() { 26 | return new Docket(DocumentationType.SWAGGER_2) 27 | .apiInfo(new ApiInfoBuilder() 28 | .title("标题:某公司_用户信息管理系统_接口文档") 29 | .description("描述:用于管理集团旗下公司的人员信息,具体包括XXX,XXX模块...") 30 | .contact(new Contact("一只袜子",null,null)) 31 | .version("版本号:1.0") 32 | .build()) 33 | .select() 34 | .apis(RequestHandlerSelectors.basePackage("com.hehe.controller")) 35 | .paths(PathSelectors.any()) 36 | .build(); 37 | } 38 | } -------------------------------------------------------------------------------- /springboot-swagger2/src/main/java/com/hehe/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.hehe.controller; 2 | 3 | import io.swagger.annotations.Api; 4 | import io.swagger.annotations.ApiOperation; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | 8 | @Api("首页管理") 9 | @Controller 10 | public class HelloController { 11 | 12 | @ApiOperation("欢迎界面") 13 | @GetMapping("/") 14 | public String hehe(){ 15 | return "redirect:/swagger-ui.html"; 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /springboot-swagger2/src/main/java/com/hehe/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.hehe.controller; 2 | 3 | import com.hehe.entity.User; 4 | import io.swagger.annotations.Api; 5 | import io.swagger.annotations.ApiImplicitParam; 6 | import io.swagger.annotations.ApiImplicitParams; 7 | import io.swagger.annotations.ApiOperation; 8 | import org.springframework.web.bind.annotation.*; 9 | import springfox.documentation.annotations.ApiIgnore; 10 | 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | 14 | /** 15 | * 主要用途: 用户信息管理 16 | * 17 | * @author yizhiwazi 18 | * @see Api 描述类/接口的主要用途 19 | * @see ApiOperation 描述方法用途 20 | * @see ApiImplicitParams 描述方法的参数(Multi-Params) 21 | * @see ApiImplicitParam 描述方法的参数 22 | * @see ApiIgnore 忽略某类/方法/参数的文档 23 | */ 24 | @Api("用户信息管理") 25 | @RestController 26 | @RequestMapping("/user/*") 27 | public class UserController { 28 | 29 | private final static List<User> userList = new ArrayList<>(); 30 | 31 | { 32 | userList.add(new User("1", "admin", "123456")); 33 | userList.add(new User("2", "jacks", "111111")); 34 | } 35 | 36 | @ApiOperation("获取列表") 37 | @GetMapping("list") 38 | public List userList() { 39 | return userList; 40 | } 41 | 42 | @ApiOperation("新增用户") 43 | @PostMapping("save") 44 | public boolean save(User user) { 45 | return userList.add(user); 46 | } 47 | 48 | @ApiOperation("更新用户") 49 | @ApiImplicitParam(name = "user", value = "单个用户信息", dataType = "User") 50 | @PutMapping("update") 51 | public boolean update(User user) { 52 | return userList.remove(user) && userList.add(user); 53 | } 54 | 55 | @ApiOperation("批量删除") 56 | @ApiImplicitParam(name = "users", value = "N个用户信息", dataType = "List<User>") 57 | @DeleteMapping("delete") 58 | public boolean delete(@RequestBody List<User> users) { 59 | return userList.removeAll(users); 60 | } 61 | } -------------------------------------------------------------------------------- /springboot-swagger2/src/main/java/com/hehe/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.hehe.entity; 2 | 3 | public class User { 4 | 5 | private String userId; 6 | private String username; 7 | private String password; 8 | 9 | public User() { 10 | 11 | } 12 | 13 | public User(String userId, String username, String password) { 14 | this.userId = userId; 15 | this.username = username; 16 | this.password = password; 17 | } 18 | 19 | @Override 20 | public boolean equals(Object o) { 21 | if (this == o) { 22 | return true; 23 | } 24 | if (o == null || getClass() != o.getClass()) { 25 | return false; 26 | } 27 | 28 | User user = (User) o; 29 | 30 | return userId != null ? userId.equals(user.userId) : user.userId == null; 31 | } 32 | 33 | @Override 34 | public int hashCode() { 35 | int result = userId != null ? userId.hashCode() : 0; 36 | result = 31 * result + (username != null ? username.hashCode() : 0); 37 | result = 31 * result + (password != null ? password.hashCode() : 0); 38 | return result; 39 | } 40 | 41 | public String getUserId() { 42 | return userId; 43 | } 44 | 45 | public void setUserId(String userId) { 46 | this.userId = userId; 47 | } 48 | 49 | public String getUsername() { 50 | return username; 51 | } 52 | 53 | public void setUsername(String username) { 54 | this.username = username; 55 | } 56 | 57 | public String getPassword() { 58 | return password; 59 | } 60 | 61 | public void setPassword(String password) { 62 | this.password = password; 63 | } 64 | } -------------------------------------------------------------------------------- /springboot-swagger2/src/main/resources/META-INF/resources/webjars/springfox-swagger-ui/lang/zh-cn.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /* jshint quotmark: double */ 4 | window.SwaggerTranslator.learn({ 5 | "Warning: Deprecated":"警告:已过时", 6 | "Implementation Notes":"实现备注", 7 | "Response Class":"响应类", 8 | "Status":"状态", 9 | "Parameters":"参数", 10 | "Parameter":"参数", 11 | "Value":"值", 12 | "Description":"描述", 13 | "Parameter Type":"参数类型", 14 | "Data Type":"数据类型", 15 | "Response Messages":"响应消息", 16 | "HTTP Status Code":"HTTP状态码", 17 | "Reason":"原因", 18 | "Response Model":"响应模型", 19 | "Request URL":"请求URL", 20 | "Response Body":"响应体", 21 | "Response Code":"响应码", 22 | "Response Headers":"响应头", 23 | "Hide Response":"隐藏响应", 24 | "Headers":"头", 25 | "Try it out!":"试一下!", 26 | "Show/Hide":"显示/隐藏", 27 | "List Operations":"显示列表", 28 | "Expand Operations":"展开操作", 29 | "Raw":"原始", 30 | "can't parse JSON. Raw result":"无法解析JSON. 原始结果", 31 | "Example Value":"示例", 32 | "Click to set as parameter value":"点击设置参数", 33 | "Model Schema":"模型架构", 34 | "Model":"模型", 35 | "apply":"应用", 36 | "Username":"用户名", 37 | "Password":"密码", 38 | "Terms of service":"服务条款", 39 | "Created by":"创建者", 40 | "See more at":"查看更多:", 41 | "Contact the developer":"联系开发者", 42 | "api version":"api版本", 43 | "Response Content Type":"响应Content Type", 44 | "Parameter content type:":"参数类型:", 45 | "fetching resource":"正在获取资源", 46 | "fetching resource list":"正在获取资源列表", 47 | "Explore":"浏览", 48 | "Show Swagger Petstore Example Apis":"显示 Swagger Petstore 示例 Apis", 49 | "Can't read from server. It may not have the appropriate access-control-origin settings.":"无法从服务器读取。可能没有正确设置access-control-origin。", 50 | "Please specify the protocol for":"请指定协议:", 51 | "Can't read swagger JSON from":"无法读取swagger JSON于", 52 | "Finished Loading Resource Information. Rendering Swagger UI":"已加载资源信息。正在渲染Swagger UI", 53 | "Unable to read api":"无法读取api", 54 | "from path":"从路径", 55 | "server returned":"服务器返回" 56 | }); 57 | -------------------------------------------------------------------------------- /springboot-swagger2/src/main/resources/application.yml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yizhiwazi/springboot-socks/763dce7c4a4960dc6389eb96087a39ed3a278896/springboot-swagger2/src/main/resources/application.yml -------------------------------------------------------------------------------- /springboot-swagger2/src/test/java/com/hehe/SpringbootSwagger2ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringbootSwagger2ApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /springboot-web-jsp/pom.xml: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="UTF-8"?> 2 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 | 5 | <!--基本信息 --> 6 | <modelVersion>4.0.0</modelVersion> 7 | <groupId>com.hehe</groupId> 8 | <artifactId>springboot-web-jsp</artifactId> 9 | <version>0.0.1-SNAPSHOT</version> 10 | 11 | <!--打包格式:SpringBoot使用JSP时需打包为war类型 --> 12 | <packaging>war</packaging> 13 | 14 | <!--继承父工程--> 15 | <parent> 16 | <groupId>org.springframework.boot</groupId> 17 | <artifactId>spring-boot-starter-parent</artifactId> 18 | <version>2.0.0.M4</version> 19 | <relativePath/> 20 | </parent> 21 | 22 | <!--依赖管理 --> 23 | <dependencies> 24 | <dependency> 25 | <groupId>org.springframework.boot</groupId> 26 | <artifactId>spring-boot-starter-web</artifactId> 27 | </dependency> 28 | <dependency> 29 | <groupId>org.springframework.boot</groupId> 30 | <artifactId>spring-boot-starter-tomcat</artifactId> 31 | </dependency> 32 | <dependency> 33 | <groupId>org.apache.tomcat.embed</groupId> 34 | <artifactId>tomcat-embed-jasper</artifactId> 35 | </dependency> 36 | <dependency> 37 | <groupId>javax.servlet</groupId> 38 | <artifactId>jstl</artifactId> 39 | </dependency> 40 | <dependency> 41 | <groupId>org.springframework.boot</groupId> 42 | <artifactId>spring-boot-devtools</artifactId> 43 | </dependency> 44 | <dependency> 45 | <groupId>org.springframework.boot</groupId> 46 | <artifactId>spring-boot-starter-test</artifactId> 47 | <scope>test</scope> 48 | </dependency> 49 | </dependencies> 50 | 51 | <!--指定远程仓库(含插件) --> 52 | <repositories> 53 | <repository> 54 | <id>spring-snapshots</id> 55 | <url>http://repo.spring.io/snapshot</url> 56 | <snapshots><enabled>true</enabled></snapshots> 57 | </repository> 58 | <repository> 59 | <id>spring-milestones</id> 60 | <url>http://repo.spring.io/milestone</url> 61 | </repository> 62 | </repositories> 63 | <pluginRepositories> 64 | <pluginRepository> 65 | <id>spring-snapshots</id> 66 | <url>http://repo.spring.io/snapshot</url> 67 | </pluginRepository> 68 | <pluginRepository> 69 | <id>spring-milestones</id> 70 | <url>http://repo.spring.io/milestone</url> 71 | </pluginRepository> 72 | </pluginRepositories> 73 | 74 | <!--构建插件 --> 75 | <build> 76 | <plugins> 77 | <plugin> 78 | <groupId>org.springframework.boot</groupId> 79 | <artifactId>spring-boot-maven-plugin</artifactId> 80 | </plugin> 81 | </plugins> 82 | </build> 83 | 84 | 85 | </project> 86 | -------------------------------------------------------------------------------- /springboot-web-jsp/src/main/java/com/hehe/WebJspApplication.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.builder.SpringApplicationBuilder; 6 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 7 | 8 | @SpringBootApplication 9 | public class WebJspApplication extends SpringBootServletInitializer { 10 | 11 | @Override 12 | protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { 13 | return builder.sources(WebJspApplication.class); 14 | } 15 | 16 | public static void main(String[] args) { 17 | SpringApplication.run(WebJspApplication.class, args); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /springboot-web-jsp/src/main/java/com/hehe/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.hehe.controller; 2 | 3 | 4 | import org.springframework.stereotype.Controller; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | 7 | @Controller 8 | public class HelloController { 9 | 10 | @GetMapping("/") 11 | public String index (){ 12 | return "index"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /springboot-web-jsp/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | mvc: 3 | view: 4 | prefix: /WEB-INF/views/ 5 | suffix: .jsp 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /springboot-web-jsp/src/main/resources/static/doge.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yizhiwazi/springboot-socks/763dce7c4a4960dc6389eb96087a39ed3a278896/springboot-web-jsp/src/main/resources/static/doge.gif -------------------------------------------------------------------------------- /springboot-web-jsp/src/main/webapp/WEB-INF/views/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ page contentType="text/html;charset=UTF-8"%> 2 | <!DOCTYPE html> 3 | <html> 4 | <head> 5 | <title>Title</title> 6 | </head> 7 | <body> 8 | <marquee><p style="font-size: 100px">Hello JSP!!</p> 9 | <img src="${pageContext.servletContext.contextPath}/doge.gif"> 10 | </marquee> 11 | 12 | </body> 13 | </html> 14 | -------------------------------------------------------------------------------- /springboot-web-jsp/src/test/java/com/hehe/WebJspApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; 9 | import org.springframework.boot.test.web.client.TestRestTemplate; 10 | import org.springframework.http.HttpStatus; 11 | import org.springframework.http.ResponseEntity; 12 | import org.springframework.test.annotation.DirtiesContext; 13 | import org.springframework.test.context.junit4.SpringRunner; 14 | 15 | import static org.assertj.core.api.Assertions.assertThat; 16 | 17 | @RunWith(SpringRunner.class) 18 | @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) 19 | @DirtiesContext 20 | public class WebJspApplicationTest { 21 | 22 | @Autowired 23 | private TestRestTemplate restTemplate; 24 | 25 | @Test 26 | public void testJspWithEl() throws Exception { 27 | ResponseEntity<String> entity = restTemplate.getForEntity("/", String.class); 28 | assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); 29 | assertThat(entity.getBody()).contains("Hello JSP"); 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /springboot-web-thymeleaf-enhance/pom.xml: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="UTF-8"?> 2 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 | <modelVersion>4.0.0</modelVersion> 5 | <!--基本信息--> 6 | <groupId>com.hehe</groupId> 7 | <artifactId>springboot-web-thymeleaf-enhance</artifactId> 8 | <version>0.0.1-SNAPSHOT</version> 9 | <packaging>jar</packaging> 10 | <name>springboot-web-thymeleaf-enhance</name> 11 | <description>SpringBoot Thymeleaf使用教程(实用版)</description> 12 | 13 | <!--继承信息--> 14 | <parent> 15 | <groupId>org.springframework.boot</groupId> 16 | <artifactId>spring-boot-starter-parent</artifactId> 17 | <version>2.1.0.RELEASE</version> 18 | </parent> 19 | 20 | <!--依赖管理--> 21 | <dependencies> 22 | <dependency><!--Web相关依赖--> 23 | <groupId>org.springframework.boot</groupId> 24 | <artifactId>spring-boot-starter-web</artifactId> 25 | </dependency> 26 | <dependency><!--页面模板依赖--> 27 | <groupId>org.springframework.boot</groupId> 28 | <artifactId>spring-boot-starter-thymeleaf</artifactId> 29 | </dependency> 30 | <dependency><!--热部署依赖--> 31 | <groupId>org.springframework.boot</groupId> 32 | <artifactId>spring-boot-devtools</artifactId> 33 | <scope>runtime</scope> 34 | </dependency> 35 | <dependency><!--Webjars省略版本号--> 36 | <groupId>org.webjars</groupId> 37 | <artifactId>webjars-locator-core</artifactId> 38 | </dependency> 39 | <dependency> 40 | <groupId>org.webjars</groupId> 41 | <artifactId>jquery</artifactId> 42 | <version>3.3.1</version> 43 | </dependency> 44 | <dependency> 45 | <groupId>org.webjars</groupId> 46 | <artifactId>bootstrap</artifactId> 47 | <version>4.1.3</version> 48 | </dependency> 49 | <dependency> 50 | <groupId>org.springframework.boot</groupId> 51 | <artifactId>spring-boot-starter-test</artifactId> 52 | <scope>test</scope> 53 | </dependency> 54 | </dependencies> 55 | 56 | </project> 57 | -------------------------------------------------------------------------------- /springboot-web-thymeleaf-enhance/src/main/java/com/hehe/ThymeleafEnhanceApplication.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class ThymeleafEnhanceApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(ThymeleafEnhanceApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /springboot-web-thymeleaf-enhance/src/main/java/com/hehe/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.hehe.entity; 2 | 3 | 4 | import java.util.Date; 5 | 6 | public class User { 7 | 8 | private String id; 9 | 10 | private String username; 11 | 12 | private String password; 13 | 14 | private Date createTime; 15 | 16 | public User(String id, String username, String password, Date createTime) { 17 | this.id = id; 18 | this.username = username; 19 | this.password = password; 20 | this.createTime = createTime; 21 | } 22 | 23 | public String getId() { 24 | return id; 25 | } 26 | 27 | public void setId(String id) { 28 | this.id = id; 29 | } 30 | 31 | public String getUsername() { 32 | return username; 33 | } 34 | 35 | public void setUsername(String username) { 36 | this.username = username; 37 | } 38 | 39 | public String getPassword() { 40 | return password; 41 | } 42 | 43 | public void setPassword(String password) { 44 | this.password = password; 45 | } 46 | 47 | public Date getCreateTime() { 48 | return createTime; 49 | } 50 | 51 | public void setCreateTime(Date createTime) { 52 | this.createTime = createTime; 53 | } 54 | 55 | @Override 56 | public String toString() { 57 | return "User{" + 58 | "id='" + id + '\'' + 59 | ", username='" + username + '\'' + 60 | ", password='" + password + '\'' + 61 | ", createTime=" + createTime + 62 | '}'; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /springboot-web-thymeleaf-enhance/src/main/java/com/hehe/web/UserController.java: -------------------------------------------------------------------------------- 1 | package com.hehe.web; 2 | 3 | import com.hehe.entity.User; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.PathVariable; 6 | import org.springframework.web.bind.annotation.RestController; 7 | import org.springframework.web.servlet.ModelAndView; 8 | 9 | import java.util.ArrayList; 10 | import java.util.Date; 11 | import java.util.List; 12 | 13 | @RestController 14 | public class UserController { 15 | private List<User> userList = new ArrayList<>(); 16 | 17 | { 18 | userList.add(new User("1", "socks", "123456", new Date())); 19 | userList.add(new User("2", "admin", "111111", new Date())); 20 | userList.add(new User("3", "jacks", "222222", null)); 21 | } 22 | 23 | @GetMapping("/") 24 | public ModelAndView index() { 25 | return new ModelAndView("user/user", "userList", userList); 26 | } 27 | 28 | @GetMapping("/user/{userId}") 29 | public ModelAndView userForm(@PathVariable String userId) { 30 | return new ModelAndView("user/userForm", "user", userList.get(0)); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /springboot-web-thymeleaf-enhance/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | thymeleaf: 3 | cache: false #关闭缓存 4 | prefix: classpath:/views/ #调整页面路径 -------------------------------------------------------------------------------- /springboot-web-thymeleaf-enhance/src/main/resources/views/common/head.html: -------------------------------------------------------------------------------- 1 | <!DOCTYPE html> 2 | <html xmlns:th="http://www.thymeleaf.org"> 3 | <!--声明static为页面片段名称--> 4 | <head th:fragment="static"> 5 | <link th:href="@{/webjars/bootstrap/css/bootstrap.css}" rel="stylesheet" type="text/css"/> 6 | <script th:src="@{/webjars/jquery/jquery.min.js}"></script> 7 | <link th:href="@{/webjars/bootstrap/js/bootstrap.min.js}" rel="stylesheet" type="text/css"/> 8 | </head> 9 | </html> -------------------------------------------------------------------------------- /springboot-web-thymeleaf-enhance/src/main/resources/views/user/user.html: -------------------------------------------------------------------------------- 1 | <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title th:text="用户列表">User</title> <!--默认拼接前缀路径,开头请勿再添加斜杠,防止部署运行报错!--> <script th:replace="common/head::static"></script> </head> <body> <h3>用户列表</h3> <div th:each="user,userStat:${userList}" th:class="${userStat.even}?'even':'odd'"> 序号:<input type="text" th:value="${userStat.count}"/> 账号:<input type="text" th:value="${user.username}"/> 密码:<input type="password" th:value="${user.password}"/> 时间:<input type="text" th:value="${user.createTime}"/> 时间:<input type="text" th:value="${#dates.format(user.createTime,'yyyy-MM-dd HH:mm:ss')}"/> </div> <script th:inline="javascript"> //通过内联表达式获取用户信息 var userList = [[${userList}]]; console.log(userList) </script> </body> </html> -------------------------------------------------------------------------------- /springboot-web-thymeleaf-enhance/src/main/resources/views/user/userForm.html: -------------------------------------------------------------------------------- 1 | <!DOCTYPE html> 2 | <html xmlns:th="http://www.thymeleaf.org"> 3 | <head> 4 | <meta charset="UTF-8"/> 5 | <title th:text="用户信息">User</title> 6 | <script th:replace="common/head::static"></script> 7 | </head> 8 | <body> 9 | <h3>用户表单</h3> 10 | <form class="form-horizontal" th:object="${user}"> 11 | <input type="hidden" id="id" name="id" th:value="*{id}"> 12 | <div class="form-group"> 13 | <label class="col-md-2 control-label">账号:</label> 14 | <div class="col-md-4"> 15 | <input class="form-control" id="username" name="username" th:value="*{username}"/> 16 | </div> 17 | </div> 18 | <div class="form-group"> 19 | <label class="col-md-2 control-label">密码:</label> 20 | <div class="col-md-4"> 21 | <input class="form-control" id="password" name="password" th:value="*{password}" type="password"/> 22 | </div> 23 | </div> 24 | <div class="form-group"> 25 | <label class="col-md-2 control-label">时间:</label> 26 | <div class="col-md-4"> 27 | <input class="form-control" id="createTime" name="createTime" th:value="*{#dates.format(createTime,'yyyy-MM-dd HH:mm:ss')}" /> 28 | </div> 29 | </div> 30 | </form> 31 | 32 | </body> 33 | </html> -------------------------------------------------------------------------------- /springboot-web-thymeleaf-enhance/src/test/java/com/hehe/ThymeleafEnhanceApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class ThymeleafEnhanceApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /springboot-web-thymeleaf/src/main/java/com/hehe/ThymeleafApplication.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class ThymeleafApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(ThymeleafApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /springboot-web-thymeleaf/src/main/java/com/hehe/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.hehe.controller; 2 | 3 | import com.hehe.entity.User; 4 | import org.springframework.stereotype.Controller; 5 | import org.springframework.ui.Model; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | import java.util.UUID; 11 | 12 | @Controller 13 | public class UserController { 14 | 15 | @GetMapping("/") 16 | public String index(Model model) { 17 | model.addAttribute("info", "user/list"); 18 | return "index"; 19 | } 20 | 21 | @GetMapping("/user") 22 | public String hehe(Model model) { 23 | model.addAttribute("user", new User(UUID.randomUUID().toString(), "yizhiwazi", "20170928")); 24 | return "user"; 25 | } 26 | 27 | @GetMapping("/user/list") 28 | public String userlist(Model model) { 29 | List<User> userList = new ArrayList<>(); 30 | userList.add(new User(UUID.randomUUID().toString(), "yizhiwazi", "20170928")); 31 | userList.add(new User(UUID.randomUUID().toString(), "kumamon", "123456")); 32 | userList.add(new User(UUID.randomUUID().toString(), "admin", "admin")); 33 | model.addAttribute("userList", userList); 34 | return "userList"; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /springboot-web-thymeleaf/src/main/java/com/hehe/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.hehe.entity; 2 | 3 | public class User { 4 | 5 | private String userId; 6 | private String username; 7 | private String password; 8 | 9 | public User( String userId, String username, String password){ 10 | this.userId=userId; 11 | this.username=username; 12 | this.password=password; 13 | } 14 | 15 | public String getUserId() { 16 | return userId; 17 | } 18 | 19 | public void setUserId(String userId) { 20 | this.userId = userId; 21 | } 22 | 23 | public String getUsername() { 24 | return username; 25 | } 26 | 27 | public void setUsername(String username) { 28 | this.username = username; 29 | } 30 | 31 | public String getPassword() { 32 | return password; 33 | } 34 | 35 | public void setPassword(String password) { 36 | this.password = password; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /springboot-web-thymeleaf/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | thymeleaf: 3 | cache: false #关闭缓存 4 | prefix: classpath:/views/ #添加路径前缀 -------------------------------------------------------------------------------- /springboot-web-thymeleaf/src/main/resources/views/index.html: -------------------------------------------------------------------------------- 1 | <!DOCTYPE html> 2 | <html xmlns:th="http://www.thymeleaf.org"> 3 | <head> 4 | <title>Thymeleaf</title> 5 | </head> 6 | <body> 7 | <h2>欢迎使用Thymeleaf!!</h2> 8 | <a href="http://www.baidu.com" th:href="${info}">戳我有惊喜</a> 9 | </body> 10 | </html> -------------------------------------------------------------------------------- /springboot-web-thymeleaf/src/main/resources/views/user.html: -------------------------------------------------------------------------------- 1 | <!DOCTYPE html> 2 | <html xmlns:th="http://www.thymeleaf.org"> 3 | <head> 4 | <title>Thymeleaf</title> 5 | </head> 6 | <body> 7 | <h2>查询用户信息</h2> 8 | <!-- 使用th:object 搭配*{} 可以省略对象名 --> 9 | <form action="/" th:object="${user}" > 10 | <input type="hidden" name="userId" th:value="*{userId}" /> 11 | <input type="text" name="username" th:value="*{username}" /> 12 | <input type="text" name="password" th:value="*{password}" /> 13 | </form> 14 | 15 | </body> 16 | </html> -------------------------------------------------------------------------------- /springboot-web-thymeleaf/src/main/resources/views/userList.html: -------------------------------------------------------------------------------- 1 | <!DOCTYPE html> 2 | <html xmlns:th="http://www.thymeleaf.org"> 3 | <head> 4 | <title>Thymeleaf</title> 5 | </head> 6 | <body> 7 | <h2>用户列表</h2> 8 | <!--说明: th:each="obj,stat:${objects}" 分别代表单个实例,状态(可省略),待遍历对象--> 9 | <div th:each="user:${userList}"> 10 | <input type="hidden" name="userId" th:value="${user.userId}"/> 11 | 用户姓名:<input type="text" name="password" th:value="${user.username}"/> 12 | 登录密码:<input type="text" name="username" th:value="${user.password}"/> 13 | </div> 14 | 15 | <a href="/user">查询单个用户</a> 16 | </body> 17 | </html> -------------------------------------------------------------------------------- /springboot-webjars/pom.xml: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="UTF-8"?> 2 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 | <modelVersion>4.0.0</modelVersion> 5 | 6 | <groupId>com.hehe</groupId> 7 | <artifactId>springboot-webjars</artifactId> 8 | <version>0.0.1-SNAPSHOT</version> 9 | <packaging>jar</packaging> 10 | 11 | <name>springboot-webjars</name> 12 | <description>Spring Boot使用WebJars统一管理静态资源</description> 13 | 14 | <parent> 15 | <groupId>org.springframework.boot</groupId> 16 | <artifactId>spring-boot-starter-parent</artifactId> 17 | <version>2.0.3.RELEASE</version> 18 | <relativePath/> 19 | </parent> 20 | 21 | <properties> 22 | <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 | <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 | <java.version>1.8</java.version> 25 | </properties> 26 | 27 | <dependencies> 28 | <dependency><!--Webjars版本定位工具(前端)--> 29 | <groupId>org.webjars</groupId> 30 | <artifactId>webjars-locator-core</artifactId> 31 | </dependency> 32 | <dependency><!--jQuery组件(前端)--> 33 | <groupId>org.webjars</groupId> 34 | <artifactId>jquery</artifactId> 35 | <version>3.3.1</version> 36 | </dependency> 37 | <dependency><!--Bootstrap框架(前端)--> 38 | <groupId>org.webjars</groupId> 39 | <artifactId>bootstrap</artifactId> 40 | <version>4.1.1</version> 41 | </dependency> 42 | <dependency> 43 | <groupId>org.springframework.boot</groupId> 44 | <artifactId>spring-boot-starter-web</artifactId> 45 | </dependency> 46 | <dependency> 47 | <groupId>org.springframework.boot</groupId> 48 | <artifactId>spring-boot-starter-test</artifactId> 49 | <scope>test</scope> 50 | </dependency> 51 | </dependencies> 52 | 53 | 54 | </project> 55 | -------------------------------------------------------------------------------- /springboot-webjars/src/main/java/com/hehe/SpringbootWebjarsApplication.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringbootWebjarsApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringbootWebjarsApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /springboot-webjars/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yizhiwazi/springboot-socks/763dce7c4a4960dc6389eb96087a39ed3a278896/springboot-webjars/src/main/resources/application.properties -------------------------------------------------------------------------------- /springboot-webjars/src/test/java/com/hehe/SpringbootWebjarsApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringbootWebjarsApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /springboot-websocket-chat/pom.xml: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="UTF-8"?> 2 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 | <modelVersion>4.0.0</modelVersion> 5 | 6 | <groupId>com.hehe</groupId> 7 | <artifactId>springboot-websocket-chat</artifactId> 8 | <version>0.0.1-SNAPSHOT</version> 9 | <packaging>jar</packaging> 10 | 11 | <name>springboot-websocket-chat</name> 12 | <description>Spring Boot 简单聊天室</description> 13 | 14 | <parent> 15 | <groupId>org.springframework.boot</groupId> 16 | <artifactId>spring-boot-starter-parent</artifactId> 17 | <version>2.0.4.RELEASE</version> 18 | <relativePath/> 19 | </parent> 20 | 21 | <dependencies> 22 | <dependency> 23 | <groupId>org.springframework.boot</groupId> 24 | <artifactId>spring-boot-starter-web</artifactId> 25 | </dependency> 26 | <dependency> 27 | <groupId>org.springframework.boot</groupId> 28 | <artifactId>spring-boot-starter-websocket</artifactId> 29 | </dependency> 30 | <dependency> 31 | <groupId>org.springframework.boot</groupId> 32 | <artifactId>spring-boot-starter-thymeleaf</artifactId> 33 | </dependency> 34 | <dependency><!--Webjars版本定位工具--> 35 | <groupId>org.webjars</groupId> 36 | <artifactId>webjars-locator-core</artifactId> 37 | </dependency> 38 | <dependency> 39 | <groupId>org.webjars.npm</groupId> 40 | <artifactId>mdui</artifactId> 41 | <version>0.4.0</version> 42 | </dependency> 43 | <dependency> 44 | <groupId>org.webjars</groupId> 45 | <artifactId>jquery</artifactId> 46 | <version>3.3.1</version> 47 | </dependency> 48 | <dependency> 49 | <groupId>org.springframework.boot</groupId> 50 | <artifactId>spring-boot-devtools</artifactId> 51 | <scope>runtime</scope> 52 | </dependency> 53 | <dependency> 54 | <groupId>com.alibaba</groupId> 55 | <artifactId>fastjson</artifactId> 56 | <version>1.2.49</version> 57 | </dependency> 58 | <dependency> 59 | <groupId>org.springframework.boot</groupId> 60 | <artifactId>spring-boot-starter-test</artifactId> 61 | <scope>test</scope> 62 | </dependency> 63 | </dependencies> 64 | 65 | <build> 66 | <plugins> 67 | <plugin> 68 | <groupId>org.springframework.boot</groupId> 69 | <artifactId>spring-boot-maven-plugin</artifactId> 70 | </plugin> 71 | </plugins> 72 | </build> 73 | 74 | 75 | </project> 76 | -------------------------------------------------------------------------------- /springboot-websocket-chat/src/main/java/com/hehe/WebSocketChatApplication.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | import org.springframework.web.servlet.ModelAndView; 8 | import org.thymeleaf.util.StringUtils; 9 | 10 | import javax.servlet.http.HttpServletRequest; 11 | import java.net.InetAddress; 12 | import java.net.UnknownHostException; 13 | 14 | @SpringBootApplication 15 | @RestController 16 | public class WebSocketChatApplication { 17 | 18 | /** 19 | * 登陆界面 20 | */ 21 | @GetMapping("/") 22 | public ModelAndView login() { 23 | return new ModelAndView("/login"); 24 | } 25 | 26 | /** 27 | * 聊天界面 28 | */ 29 | @GetMapping("/index") 30 | public ModelAndView index(String username, String password, HttpServletRequest request) throws UnknownHostException { 31 | if (StringUtils.isEmpty(username)) { 32 | username = "匿名用户"; 33 | } 34 | ModelAndView mav = new ModelAndView("/chat"); 35 | mav.addObject("username", username); 36 | mav.addObject("webSocketUrl", "ws://"+InetAddress.getLocalHost().getHostAddress()+":"+request.getServerPort()+request.getContextPath()+"/chat"); 37 | return mav; 38 | } 39 | 40 | public static void main(String[] args) { 41 | SpringApplication.run(WebSocketChatApplication.class, args); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /springboot-websocket-chat/src/main/java/com/hehe/chat/Message.java: -------------------------------------------------------------------------------- 1 | package com.hehe.chat; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | 5 | /** 6 | * WebSocket 聊天消息类 7 | */ 8 | public class Message { 9 | 10 | public static final String ENTER = "ENTER"; 11 | public static final String SPEAK = "SPEAK"; 12 | public static final String QUIT = "QUIT"; 13 | 14 | private String type;//消息类型 15 | 16 | private String username; //发送人 17 | 18 | private String msg; //发送消息 19 | 20 | private int onlineCount; //在线用户数 21 | 22 | public static String jsonStr(String type, String username, String msg, int onlineTotal) { 23 | return JSON.toJSONString(new Message(type, username, msg, onlineTotal)); 24 | } 25 | 26 | public Message(String type, String username, String msg, int onlineCount) { 27 | this.type = type; 28 | this.username = username; 29 | this.msg = msg; 30 | this.onlineCount = onlineCount; 31 | } 32 | 33 | public static String getENTER() { 34 | return ENTER; 35 | } 36 | 37 | public static String getSPEAK() { 38 | return SPEAK; 39 | } 40 | 41 | public static String getQUIT() { 42 | return QUIT; 43 | } 44 | 45 | public String getType() { 46 | return type; 47 | } 48 | 49 | public void setType(String type) { 50 | this.type = type; 51 | } 52 | 53 | public String getUsername() { 54 | return username; 55 | } 56 | 57 | public void setUsername(String username) { 58 | this.username = username; 59 | } 60 | 61 | public String getMsg() { 62 | return msg; 63 | } 64 | 65 | public void setMsg(String msg) { 66 | this.msg = msg; 67 | } 68 | 69 | public int getOnlineCount() { 70 | return onlineCount; 71 | } 72 | 73 | public void setOnlineCount(int onlineCount) { 74 | this.onlineCount = onlineCount; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /springboot-websocket-chat/src/main/java/com/hehe/chat/WebSocketChatServer.java: -------------------------------------------------------------------------------- 1 | package com.hehe.chat; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import org.springframework.stereotype.Component; 5 | import javax.websocket.*; 6 | import javax.websocket.server.ServerEndpoint; 7 | import java.io.IOException; 8 | import java.util.Map; 9 | import java.util.concurrent.ConcurrentHashMap; 10 | 11 | /** 12 | * WebSocket 聊天服务端 13 | * 14 | * @see ServerEndpoint WebSocket服务端 需指定端点的访问路径 15 | * @see Session WebSocket会话对象 通过它给客户端发送消息 16 | */ 17 | 18 | @Component 19 | @ServerEndpoint("/chat") 20 | public class WebSocketChatServer { 21 | 22 | /** 23 | * 全部在线会话 PS: 基于场景考虑 这里使用线程安全的Map存储会话对象。 24 | */ 25 | private static Map<String, Session> onlineSessions = new ConcurrentHashMap<>(); 26 | 27 | 28 | /** 29 | * 当客户端打开连接:1.添加会话对象 2.更新在线人数 30 | */ 31 | @OnOpen 32 | public void onOpen(Session session) { 33 | onlineSessions.put(session.getId(), session); 34 | sendMessageToAll(Message.jsonStr(Message.ENTER, "", "", onlineSessions.size())); 35 | } 36 | 37 | /** 38 | * 当客户端发送消息:1.获取它的用户名和消息 2.发送消息给所有人 39 | * <p> 40 | * PS: 这里约定传递的消息为JSON字符串 方便传递更多参数! 41 | */ 42 | @OnMessage 43 | public void onMessage(Session session, String jsonStr) { 44 | Message message = JSON.parseObject(jsonStr, Message.class); 45 | sendMessageToAll(Message.jsonStr(Message.SPEAK, message.getUsername(), message.getMsg(), onlineSessions.size())); 46 | } 47 | 48 | /** 49 | * 当关闭连接:1.移除会话对象 2.更新在线人数 50 | */ 51 | @OnClose 52 | public void onClose(Session session) { 53 | onlineSessions.remove(session.getId()); 54 | sendMessageToAll(Message.jsonStr(Message.QUIT, "", "", onlineSessions.size())); 55 | } 56 | 57 | /** 58 | * 当通信发生异常:打印错误日志 59 | */ 60 | @OnError 61 | public void onError(Session session, Throwable error) { 62 | error.printStackTrace(); 63 | } 64 | 65 | /** 66 | * 公共方法:发送信息给所有人 67 | */ 68 | private static void sendMessageToAll(String msg) { 69 | onlineSessions.forEach((id, session) -> { 70 | try { 71 | session.getBasicRemote().sendText(msg); 72 | } catch (IOException e) { 73 | e.printStackTrace(); 74 | } 75 | }); 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /springboot-websocket-chat/src/main/java/com/hehe/chat/WebSocketConfig.java: -------------------------------------------------------------------------------- 1 | package com.hehe.chat; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.web.socket.server.standard.ServerEndpointExporter; 6 | 7 | @Configuration 8 | public class WebSocketConfig { 9 | 10 | /** 11 | * 用于扫描和注册所有携带ServerEndPoint注解的实例。 12 | * <p> 13 | * PS:若部署到外部容器 则无需提供此类。 14 | */ 15 | @Bean 16 | public ServerEndpointExporter serverEndpointExporter() { 17 | 18 | return new ServerEndpointExporter(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /springboot-websocket-chat/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | thymeleaf: 3 | cache: false 4 | -------------------------------------------------------------------------------- /springboot-websocket-chat/src/main/resources/static/img/login-bg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yizhiwazi/springboot-socks/763dce7c4a4960dc6389eb96087a39ed3a278896/springboot-websocket-chat/src/main/resources/static/img/login-bg.jpg -------------------------------------------------------------------------------- /springboot-websocket-chat/src/test/java/com/hehe/WebsocketChatApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.hehe; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class WebsocketChatApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | --------------------------------------------------------------------------------