├── README.md ├── docker-doc ├── Docker安装说明 │ └── README.md ├── Docker配置说明 │ └── README.md └── README.md ├── dubbo-doc ├── README.md ├── 安装zookeeper注册中心 │ └── README.md └── 搭建dubbo-admin控制台 │ └── README.md ├── spring-boot-docker ├── Dockerfile ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── springboottest │ │ │ ├── SpringBootTestApplication.java │ │ │ ├── controller │ │ │ └── UserController.java │ │ │ ├── domain │ │ │ └── User.java │ │ │ ├── repository │ │ │ └── UserRepository.java │ │ │ └── service │ │ │ ├── UserService.java │ │ │ └── impl │ │ │ └── UserServiceImpl.java │ └── resources │ │ ├── application-dev.yml │ │ ├── application-prod.yml │ │ ├── application.yml │ │ ├── static │ │ └── css │ │ │ └── bootstrap.css │ │ └── templates │ │ └── user │ │ ├── list.html │ │ ├── userAdd.html │ │ └── userEdit.html │ └── test │ └── java │ └── springboottest │ └── SpringBootTestApplicationTests.java ├── spring-boot-dubbo ├── README.md ├── integration-base │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── integration │ │ └── base │ │ ├── domain │ │ └── User.java │ │ └── service │ │ └── UserService.java ├── integration-service │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── integration │ │ │ ├── IntegrationServiceApplication.java │ │ │ └── service │ │ │ ├── config │ │ │ └── DubboConfig.java │ │ │ ├── repository │ │ │ └── UserRepository.java │ │ │ └── serviceimpl │ │ │ └── UserServiceImpl.java │ │ └── resources │ │ ├── application-dev.yml │ │ ├── application-prod.yml │ │ ├── application.yml │ │ └── dubbo │ │ ├── dubbo-provider.xml │ │ └── dubbo.properties ├── integration-web │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── integration │ │ │ ├── IntegrationWebApplication.java │ │ │ └── web │ │ │ ├── config │ │ │ └── DubboConfig.java │ │ │ └── controller │ │ │ └── UserController.java │ │ └── resources │ │ ├── application-dev.yml │ │ ├── application-prod.yml │ │ ├── application.yml │ │ ├── dubbo │ │ ├── dubbo-consumer.xml │ │ └── dubbo.properties │ │ ├── static │ │ └── css │ │ │ └── bootstrap.css │ │ └── templates │ │ └── user │ │ ├── list.html │ │ ├── userAdd.html │ │ └── userEdit.html └── pom.xml ├── spring-boot-integration ├── integration-base │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── integration │ │ └── base │ │ ├── domain │ │ └── User.java │ │ └── service │ │ └── UserService.java ├── integration-service │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── integration │ │ └── service │ │ ├── repository │ │ └── UserRepository.java │ │ └── serviceimpl │ │ └── UserServiceImpl.java ├── integration-web │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── integration │ │ │ ├── IntegrationWebApplication.java │ │ │ └── web │ │ │ └── controller │ │ │ └── UserController.java │ │ └── resources │ │ ├── application-dev.yml │ │ ├── application-prod.yml │ │ ├── application.yml │ │ ├── static │ │ └── css │ │ │ └── bootstrap.css │ │ └── templates │ │ └── user │ │ ├── list.html │ │ ├── userAdd.html │ │ └── userEdit.html └── pom.xml ├── spring-boot-jersey ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springbootjersey │ │ │ ├── SpringBootJerseyApplication.java │ │ │ ├── config │ │ │ └── MyResourceConfigCustomizer.java │ │ │ ├── controller │ │ │ └── UserController.java │ │ │ └── model │ │ │ └── User.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── example │ └── springbootjersey │ ├── SpringBootJerseyApplicationTests.java │ └── controller │ └── UserControllerTest.java ├── spring-boot-jpa-thymeleaf-crud ├── pom.xml └── src │ ├── main │ ├── java │ │ └── springboottest │ │ │ ├── SpringBootTestApplication.java │ │ │ ├── controller │ │ │ └── UserController.java │ │ │ ├── domain │ │ │ └── User.java │ │ │ ├── repository │ │ │ └── UserRepository.java │ │ │ └── service │ │ │ ├── UserService.java │ │ │ └── impl │ │ │ └── UserServiceImpl.java │ └── resources │ │ ├── application-dev.yml │ │ ├── application-prod.yml │ │ ├── application.yml │ │ ├── static │ │ └── css │ │ │ └── bootstrap.css │ │ └── templates │ │ └── user │ │ ├── list.html │ │ ├── userAdd.html │ │ └── userEdit.html │ └── test │ └── java │ └── springboottest │ └── SpringBootTestApplicationTests.java ├── spring-boot-multi-datasource ├── pom.xml ├── springboottest.iml └── src │ ├── main │ ├── java │ │ └── springboottest │ │ │ ├── SpringBootTestApplication.java │ │ │ ├── config │ │ │ ├── DataSourceConfig.java │ │ │ ├── MasterConfig.java │ │ │ └── SlaveConfig.java │ │ │ ├── controller │ │ │ └── UserController.java │ │ │ ├── database │ │ │ ├── master │ │ │ │ ├── User.java │ │ │ │ └── UserRepository.java │ │ │ └── slave │ │ │ │ ├── User2.java │ │ │ │ └── UserRepository2.java │ │ │ └── service │ │ │ ├── UserService.java │ │ │ └── impl │ │ │ └── UserServiceImpl.java │ └── resources │ │ └── application.yml │ └── test │ └── java │ └── springboottest │ └── SpringBootTestApplicationTests.java ├── spring-boot-mybatis-thymeleaf-crud ├── pom.xml └── src │ ├── main │ ├── java │ │ └── springboottest │ │ │ ├── SpringboottestApplication.java │ │ │ ├── controller │ │ │ └── UserController.java │ │ │ ├── mapper │ │ │ └── UserMapper.java │ │ │ ├── model │ │ │ ├── BaseEntity.java │ │ │ └── User.java │ │ │ ├── service │ │ │ ├── UserService.java │ │ │ └── impl │ │ │ │ └── UserServiceImpl.java │ │ │ └── utils │ │ │ └── MyMapper.java │ └── resources │ │ ├── application-dev.properties │ │ ├── application.yml │ │ ├── generator │ │ └── generatorConfig.xml │ │ ├── mapper │ │ └── UserMapper.xml │ │ ├── static │ │ └── css │ │ │ └── bootstrap.css │ │ └── templates │ │ └── user │ │ ├── list.html │ │ ├── userAdd.html │ │ └── userEdit.html │ └── test │ └── java │ └── springboottest │ └── SpringboottestApplicationTests.java ├── spring-boot-swagger ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springbootswagger │ │ │ ├── SpringBootSwaggerApplication.java │ │ │ ├── config │ │ │ └── Swagger2Config.java │ │ │ ├── controller │ │ │ └── TestController.java │ │ │ └── model │ │ │ └── vo │ │ │ └── User.java │ └── resources │ │ └── application.yml │ └── test │ └── java │ └── com │ └── example │ └── springbootswagger │ └── SpringBootSwaggerApplicationTests.java └── spring-cloud-demo ├── demo-consumer ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── demo │ │ │ └── democonsumer │ │ │ ├── DemoConsumerApplication.java │ │ │ ├── client │ │ │ ├── InfoClient.java │ │ │ ├── config │ │ │ │ └── MyFeignConfig.java │ │ │ └── fallback │ │ │ │ └── InfoFallBack.java │ │ │ └── controller │ │ │ └── ConsumerController.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── demo │ └── democonsumer │ └── DemoConsumerApplicationTests.java ├── demo-eureka ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── demo │ │ │ └── demoeureka │ │ │ └── DemoEurekaApplication.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── demo │ └── demoeureka │ └── DemoEurekaApplicationTests.java ├── demo-provider ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── demo │ │ │ └── demoprovider │ │ │ ├── DemoProviderApplication.java │ │ │ └── controller │ │ │ └── MyController.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── demo │ └── demoprovider │ └── DemoProviderApplicationTests.java └── pom.xml /README.md: -------------------------------------------------------------------------------- 1 | # 基于Java的微服务实践 2 | 基于Java的微服务实践。包括Spring Boot开发Web应用、Sping Boot的Docker部署、Dubbo微服务实践等。 3 | 4 | --- 5 | 6 | ## 示例代码 7 | - [spring-boot-jpa-thymeleaf-crud](spring-boot-jpa-thymeleaf-crud/)  Spring Boot+JPA+Thymeleaf实现增删改查示例 8 | - [spring-boot-mybatis-thymeleaf-crud](spring-boot-mybatis-thymeleaf-crud/)  Spring Boot+MyBatis+Thymeleaf实现增删改查示例 9 | - [spring-boot-docker](spring-boot-docker/)  Sping Boot使用远程Docker部署 10 | - [spring-boot-integration](spring-boot-integration/)  Sping Boot多模块示例 11 | - [spring-boot-dubbo](spring-boot-dubbo/)  Sping Boot整合Dubbo 12 | - [spring-boot-mybatis](https://github.com/wander-chu/spring-boot-mybatis)  Spring Boot集成MyBatis,分页插件PageHelper,通用Mapper,MyBatis的关联表查询 13 | - [spring-boot-security](https://github.com/wander-chu/spring-boot-security)  Spring Boot基于Spring Security的安全管理 14 | - [spring-boot-jersey](spring-boot-jersey/)  Spring Boot集成Jersey 15 | - [spring-boot-swagger](spring-boot-swagger/)  Spring Boot集成Swagger2 16 | - [spring-cloud-demo](spring-cloud-demo/)  Spring Cloud示例 17 | - 持续更新…… 18 | 19 | ## 资源推荐 20 | - [Spring Boot 中文导航](http://springboot.fun/) 21 | - [Spring Cloud 中文网](https://springcloud.cc/) 22 | - [云收藏(Spring Boot 2.0 实战开源项目)](https://github.com/cloudfavorites/favorites-web) -------------------------------------------------------------------------------- /docker-doc/Docker安装说明/README.md: -------------------------------------------------------------------------------- 1 | ## Docker安装说明 2 | ### 一.CentOS上安装Docker 3 | **1.查看内核版本** 4 | 5 | Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker 。 6 | 通过 uname -r 命令查看你当前的内核版本。 7 | 8 | $ uname -r 9 | 10 | **2.更新yum** 11 | 12 | 使用 root 权限登录 Centos。确保 yum 包更新到最新。 13 | 14 | $ sudo yum update 15 | 16 | **3.卸载旧版本** 17 | 18 | $ sudo yum remove docker docker-common docker-selinux docker-engine 19 | 20 | **4.安装需要的软件包** 21 | 22 | yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的。 23 | 24 | $ sudo yum install -y yum-utils device-mapper-persistent-data lvm2 25 | 26 | **5.设置yum源** 27 | 28 | $ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo 29 | 30 | **6.查看docker版本** 31 | 32 | $ yum list docker-ce --showduplicates | sort -r 33 | 34 | **7.安装docker** 35 | 36 | 安装最新版本。 37 | 38 | $ sudo yum install docker-ce 39 | 40 | 安装特定版本(例如:sudo yum install docker-ce-17.12.0.ce)。 41 | 42 | $ sudo yum install 43 | 44 | **8.启动并加入开机启动** 45 | 46 | $ sudo systemctl start docker 47 | 48 | $ sudo systemctl enable docker 49 | 50 | **9.验证安装是否成功** 51 | 52 | 输出中有client和service两部分表示docker安装启动都成功了。 53 | 54 | $ docker version -------------------------------------------------------------------------------- /docker-doc/Docker配置说明/README.md: -------------------------------------------------------------------------------- 1 | ## Docker配置说明 2 | ### 一.CentOS上Docker允许远程 3 | 4 | **1.修改配置文件** 5 | 6 | $ vim /usr/lib/systemd/system/docker.service 7 | 8 | 编辑文件,"ExecStart=/usr/bin/dockerd……"这一行改为: 9 | 10 | ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock 11 | 12 | **2.Docker重新读取配置文件** 13 | 14 | $ systemctl daemon-reload 15 | 16 | **3.重新启动Docker服务** 17 | 18 | $ systemctl restart docker 19 | 20 | **4.查看Docker进程** 21 | 22 | 出现"tcp://0.0.0.0:2375"说明成功。 23 | 24 | $ ps aux | grep docker 25 | 26 | **5.查看系统的网络端口** 27 | 28 | 将会看到tcp的2375端口是docker的守护进程在监听。 29 | 30 | $ netstat -tulp 31 | -------------------------------------------------------------------------------- /docker-doc/README.md: -------------------------------------------------------------------------------- 1 | # Docker Document 2 | Docker学习文档。 3 | 4 | --- 5 | 6 | ## 学习文档 7 | - [Docker安装说明](Docker安装说明/) 8 | - [Docker配置说明](Docker配置说明/) 9 | 10 | ## 资源推荐 11 | - [Docker 中文社区](http://www.docker.org.cn/) -------------------------------------------------------------------------------- /dubbo-doc/README.md: -------------------------------------------------------------------------------- 1 | # Dubbo Document 2 | Dubbo学习文档。 3 | 4 | --- 5 | 6 | ## 学习文档 7 | - [安装zookeeper注册中心](安装zookeeper注册中心/) 8 | - [搭建dubbo-admin控制台](搭建dubbo-admin控制台/) 9 | 10 | ## 资源推荐 11 | - [Dubbo 官网](http://dubbo.apache.org/en-us/) -------------------------------------------------------------------------------- /dubbo-doc/安装zookeeper注册中心/README.md: -------------------------------------------------------------------------------- 1 | ## zookeeper安装说明 2 | ### 一.Windows上安装zookeeper 3 | 4 | **1.下载zookeeper** 5 | 6 | 下载地址:http://mirrors.shu.edu.cn/apache/zookeeper/stable/zookeeper-3.4.12.tar.gz 7 | 8 | 也可以去官网下载想要的版本,官网地址:http://zookeeper.apache.org/releases.html 9 | 10 | **2.修改配置** 11 | 12 | 解压 zookeeper-3.4.12.tar.gz,进入解压后conf目录下,复制zoo_sample.cfg文件,重命名为zoo.cfg即可。 13 | 14 | **3.运行zookeeper** 15 | 16 | 进入解压后bin目录,双击zkServer.cmd执行即可。 17 | 18 | -------------------------------------------------------------------------------- /dubbo-doc/搭建dubbo-admin控制台/README.md: -------------------------------------------------------------------------------- 1 | ## 搭建dubbo-admin控制台 2 | ### 一.Windows上搭建dubbo-admin控制台 3 | 4 | **1.下载源码** 5 | 6 | 下载地址:https://github.com/apache/incubator-dubbo/tree/dubbo-2.5.8 7 | 8 | 你也可以选择下载其它的版本。 9 | 10 | **2.生成war文件** 11 | 12 | 解压,打开cmd进入解压后的子目录dubbo-admin目录下,在cmd中输入命令: 13 | 14 | mvn clean install 15 | 16 | 在target目录下会生成dubbo-admin-2.5.8.war,然后将该war包放入tomcat的webapps目录下,启动tomcat。 17 | 18 | **3.访问dubbo-admin** 19 | 20 | 在浏览器中访问localhost:[端口]/dubbo-admin-2.5.8,输入默认的用户名和密码:root/root。 -------------------------------------------------------------------------------- /spring-boot-docker/Dockerfile: -------------------------------------------------------------------------------- 1 | # 基础镜像使用Java 2 | FROM java:8 3 | # 作者 4 | MAINTAINER wander 5 | # VOLUME指定了临时文件目录为/tmp 6 | # 其效果是在主机 /var/lib/docker 目录下创建了一个临时文件,并链接到容器的/tmp 7 | VOLUME /tmp 8 | # 将jar包添加到容器中并更名为app.jar 9 | ADD target/*.jar app.jar 10 | # 运行jar包 11 | RUN bash -c "touch /app.jar" 12 | # ENTRYPOINT是容器启动命令 13 | ENTRYPOINT ["java","-Dspring.profiles.active=prod","-jar","app.jar"] 14 | # 指定容器需要映射到主机的端口 15 | EXPOSE 8080 -------------------------------------------------------------------------------- /spring-boot-docker/README.md: -------------------------------------------------------------------------------- 1 | # Spring Boot的远程Docker部署 2 | 使用远程Docker发布Spring Boot应用。 3 | 4 | --- 5 | 6 | 7 | **1.CentOS服务器上安装Docker** 8 | 9 | 安装步骤请参考[Docker安装说明](../docker-doc/Docker安装说明/) 10 | 11 | **2.修改Docker配置允许远程** 12 | 13 | 配置修改方法请参考[Docker配置说明](../docker-doc/Docker配置说明/) 14 | 15 | **3.我们直接把我们创建的Spring Boot的CRUD程序拿过来,并通过IDEA打开,在此基础上增加远程Docker发布需要的配置。** 16 | 17 | **4.IDEA安装Docker插件** 18 | 19 | 打开Idea,从File->Settings->Plugins->Install JetBrains plugin进入插件安装界面,在搜索框中输入docker,可以看到Docker integration,点击右边的Install按钮进行安装。安装后重启Idea。 20 | 21 | **5.连接远程Docker** 22 | 23 | 从File->Settings->Build,Execution,Deployment->Docker打开配置界面。 24 | 25 | 点击+号添加一个docker配置,输入Name和Engine API URL,URL是docker服务地址。不出意外应该可以连接成功。 26 | 27 | **6.项目根目录下增加Dockerfile文件** 28 | 29 | 若要查看Dockerfile内容请点击[Dockerfile代码](Dockerfile) 30 | 31 | **7.生成jar包** 32 | 33 | 通过Maven Projects或直接执行mvn clean package命令生成jar包,并测试确定可以运行。 34 | 35 | **8.增加Docker创建镜像和容器的配置** 36 | 37 | 从Run->Edit Configureations打开配置界面。点击+号,选择Docker->Dockerfile,新增一个配置页,信息填写如下 38 | 39 | Name:Docker 40 | Server:Docker(这里是选择的第5步时增加的Docker) 41 | Dockerfile:Dockerfile(这里选择的是第6步时增加的根目录下的Dockerfile文件) 42 | Context folder:. 43 | Image tag:springboot/springboot-docker:1.0 44 | Container name:springboot-docker 45 | Command line options:--link mysql:mysql -p 8080:8080 46 | 47 | 以上设置仅供参考,可以根据你的习惯进行配置。 48 | 这里使用--link连接mysql容器,配置文件中spring.datasoure.url中应该是mysql:3306格式的配置,其中mysql为--link设置的我们部署mysql的容器的别名。当然这里并不是一定要这么设置,spring.datasoure.url依然可以保持host:port格式的设置,在Command line options中增加--net=host即可) 49 | 50 | 对应配置文件中的spring.datasoure.url配置请点击[application-prod.yml](src/main/resources/application-prod.yml)查看 51 | 52 | 保存后执行,就会根据我们的配置通过远程Docker创建镜像并创建容器。 53 | 54 | **9.测试** 55 | 56 | 浏览器中访问:http://IP:8080 57 | 58 | -------------------------------------------------------------------------------- /spring-boot-docker/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.2.RELEASE 9 | 10 | 11 | com.example 12 | springboottest 13 | 0.0.1-SNAPSHOT 14 | springboottest 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-data-jpa 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-thymeleaf 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-web 33 | 34 | 35 | 36 | mysql 37 | mysql-connector-java 38 | runtime 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-starter-test 43 | test 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.springframework.boot 51 | spring-boot-maven-plugin 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /spring-boot-docker/src/main/java/springboottest/SpringBootTestApplication.java: -------------------------------------------------------------------------------- 1 | package springboottest; 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 | 9 | @SpringBootApplication 10 | public class SpringBootTestApplication extends SpringBootServletInitializer { 11 | public static void main(String[] args) { 12 | SpringApplication.run(SpringBootTestApplication.class, args); 13 | } 14 | 15 | @Override 16 | protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { 17 | return builder.sources(SpringBootTestApplication.class); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /spring-boot-docker/src/main/java/springboottest/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package springboottest.controller; 2 | 3 | import springboottest.domain.User; 4 | import springboottest.service.UserService; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.ui.Model; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | 9 | import javax.annotation.Resource; 10 | import java.util.List; 11 | 12 | @Controller 13 | public class UserController { 14 | @Resource 15 | UserService userService; 16 | 17 | @RequestMapping("/") 18 | public String Index() { 19 | return "redirect:/list"; 20 | } 21 | 22 | @RequestMapping("/list") 23 | public String list(Model model) { 24 | List users = userService.getUserList(); 25 | model.addAttribute("users", users); 26 | return "user/list"; 27 | } 28 | 29 | @RequestMapping("/toAdd") 30 | public String toAdd() { 31 | return "user/userAdd"; 32 | } 33 | 34 | @RequestMapping("/add") 35 | public String add(User user) { 36 | userService.save(user); 37 | return "redirect:/list"; 38 | } 39 | 40 | @RequestMapping("/toEdit") 41 | public String toEdit(Model model, Long id) { 42 | User user = userService.findUserById(id); 43 | model.addAttribute("user", user); 44 | return "user/userEdit"; 45 | } 46 | 47 | @RequestMapping("/edit") 48 | public String edit(User user) { 49 | userService.edit(user); 50 | return "redirect:/list"; 51 | } 52 | 53 | @RequestMapping("/delete") 54 | public String delete(Long id) { 55 | userService.delete(id); 56 | return "redirect:/list"; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /spring-boot-docker/src/main/java/springboottest/domain/User.java: -------------------------------------------------------------------------------- 1 | package springboottest.domain; 2 | 3 | import net.bytebuddy.dynamic.loading.InjectionClassLoader; 4 | 5 | import javax.persistence.*; 6 | 7 | @Entity 8 | public class User { 9 | @Id 10 | @GeneratedValue(strategy = GenerationType.IDENTITY) 11 | private long id; 12 | 13 | @Column(nullable = false, unique = true) 14 | private String username; 15 | 16 | @Column(nullable = false) 17 | private String password; 18 | 19 | @Column(nullable = false) 20 | private int age; 21 | 22 | public long getId() { 23 | return id; 24 | } 25 | 26 | public void setId(long id) { 27 | this.id = id; 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 int getAge() { 47 | return age; 48 | } 49 | 50 | public void setAge(int age) { 51 | this.age = age; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /spring-boot-docker/src/main/java/springboottest/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package springboottest.repository; 2 | 3 | import springboottest.domain.User; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | 7 | public interface UserRepository extends JpaRepository { 8 | } -------------------------------------------------------------------------------- /spring-boot-docker/src/main/java/springboottest/service/UserService.java: -------------------------------------------------------------------------------- 1 | package springboottest.service; 2 | 3 | import springboottest.domain.User; 4 | 5 | import java.util.List; 6 | 7 | public interface UserService { 8 | List getUserList(); 9 | 10 | User findUserById(long id); 11 | 12 | void save(User user); 13 | 14 | void edit(User user); 15 | 16 | void delete(long id); 17 | } -------------------------------------------------------------------------------- /spring-boot-docker/src/main/java/springboottest/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package springboottest.service.impl; 2 | 3 | import springboottest.domain.User; 4 | import springboottest.repository.UserRepository; 5 | import springboottest.service.UserService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import java.util.List; 10 | 11 | @Service 12 | public class UserServiceImpl implements UserService { 13 | @Autowired 14 | private UserRepository userRepository; 15 | 16 | @Override 17 | public List getUserList() { 18 | return userRepository.findAll(); 19 | } 20 | 21 | @Override 22 | public User findUserById(long id) { 23 | return userRepository.findById(id).orElse(null); 24 | } 25 | 26 | @Override 27 | public void save(User user) { 28 | userRepository.save(user); 29 | } 30 | 31 | @Override 32 | public void edit(User user) { 33 | userRepository.save(user); 34 | } 35 | 36 | @Override 37 | public void delete(long id) { 38 | userRepository.deleteById(id); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /spring-boot-docker/src/main/resources/application-dev.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true 4 | username: root 5 | password: 123456 6 | jpa: 7 | hibernate: 8 | ddl-auto: update 9 | show-sql: true 10 | thymeleaf: 11 | cache: false -------------------------------------------------------------------------------- /spring-boot-docker/src/main/resources/application-prod.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | # 这里使用mysql:3306是因为我的mysql是部署到docker中的,我们的容器需link mysql容器 4 | # 即使mysql部署在容器中,这里mysql也可以以IP:Port的形式配置,不过我们的容器需要设置参数--net=host 5 | url: jdbc:mysql://mysql:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true 6 | username: root 7 | password: 123456 8 | jpa: 9 | hibernate: 10 | ddl-auto: validate 11 | show-sql: true 12 | thymeleaf: 13 | cache: true -------------------------------------------------------------------------------- /spring-boot-docker/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | profiles: 3 | active: dev -------------------------------------------------------------------------------- /spring-boot-docker/src/main/resources/templates/user/list.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | userList 6 | 7 | 8 | 9 |
10 |

用户列表

11 |

12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 |
#User NamePasswordAgeEditDelete
1neoOtto6editdelete
35 |
36 |
37 |
38 | add 39 |
40 |
41 | 42 | -------------------------------------------------------------------------------- /spring-boot-docker/src/main/resources/templates/user/userAdd.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | user 6 | 7 | 8 | 9 |
10 |

添加用户

11 |

12 |
13 |
14 |
15 | 16 |
17 | 18 |
19 |
20 |
21 | 22 |
23 | 24 |
25 |
26 |
27 | 28 |
29 | 30 |
31 |
32 |
33 |
34 | 35 |       36 | 37 |
38 | 39 |
40 |
41 |
42 | 43 | -------------------------------------------------------------------------------- /spring-boot-docker/src/main/resources/templates/user/userEdit.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | user 6 | 7 | 8 | 9 |
10 |

修改用户

11 |

12 |
13 |
14 | 15 |
16 | 17 |
18 | 20 |
21 |
22 |
23 | 24 |
25 | 27 |
28 |
29 |
30 | 31 |
32 | 33 |
34 |
35 |
36 |
37 | 38 |       39 | Back 40 |
41 | 42 |
43 |
44 |
45 | 46 | -------------------------------------------------------------------------------- /spring-boot-docker/src/test/java/springboottest/SpringBootTestApplicationTests.java: -------------------------------------------------------------------------------- 1 | package springboottest; 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 SpringBootTestApplicationTests { 11 | @Test 12 | public void contextLoads() { 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /spring-boot-dubbo/README.md: -------------------------------------------------------------------------------- 1 | # Spring Boot整合Dubbo 2 | Sping Boot整合Dubbo。 3 | 4 | --- 5 | 6 | **1.安装zookeeper** 7 | 8 | 安装步骤请参考[安装zookeeper注册中心](../dubbo-doc/安装zookeeper注册中心/) 9 | 10 | **2.搭建dubbo-admin控制台** 11 | 12 | 搭建步骤请参考[搭建dubbo-admin控制台](../dubbo-doc/搭建dubbo-admin控制台/) 13 | 14 | **3.工程搭建** 15 | 16 | 我们把之前搭建的Spring Boot多模块项目拷贝过来,稍加修改,改成web、service都可以启动。 17 | 18 | 修改内容: 19 | 20 | 1.service模块增加启动类 21 | 2.pom配置中web模块只依赖base模块,不再依赖service模块 22 | 3.web模块关于数据库的配置移动到service模块下面 23 | 4.base模块中的实体添加implements Serializable 24 | 5.web模块取消数据库自动配置,因为我们虽然引用了实体类中添加了@Entity注解的base模块,但是我们并不需要直接连数据库 25 | @SpringBootApplication(exclude = { 26 | DataSourceAutoConfiguration.class, 27 | DataSourceTransactionManagerAutoConfiguration.class, 28 | HibernateJpaAutoConfiguration.class}) 29 | 6.web、service分别设置不同的server.port 30 | 31 | 下面我们会把service当成生产者、web当成消费者注册到zookeeper。 32 | 33 | **4.增加dubbo配置** 34 | 35 | 我们实现的是xml的dubbo配置。 36 | 37 | 生产者添加内容如下: 38 | 39 | java/integration/service/config/DubboConfig.java 40 | resources/dubbo/dubbo-provider.xml 41 | resources/dubbo/dubbo.properties 42 | 43 | 消费者添加内容如下: 44 | 45 | java/integration/web/config/DubboConfig.java 46 | resources/dubbo/dubbo-consumer.xml 47 | resources/dubbo/dubbo.properties 48 | 49 | **5.测试** 50 | 51 | 运行service、web项目,在dubbo-admin查看注册情况,在浏览器中访问消费者页面查看运行情况。 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /spring-boot-dubbo/integration-base/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | com.example 8 | integration-base 9 | 0.0.1-SNAPSHOT 10 | jar 11 | integration-base 12 | Spring Boot Integration Base 13 | 14 | 15 | 16 | com.example 17 | spring-boot-integration 18 | 0.0.1-SNAPSHOT 19 | 20 | 21 | 22 | 1.8 23 | 24 | 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-data-jpa 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /spring-boot-dubbo/integration-base/src/main/java/integration/base/domain/User.java: -------------------------------------------------------------------------------- 1 | package integration.base.domain; 2 | 3 | import javax.persistence.*; 4 | import java.io.Serializable; 5 | 6 | @Entity 7 | public class User implements Serializable { 8 | @Id 9 | @GeneratedValue(strategy = GenerationType.IDENTITY) 10 | private long id; 11 | 12 | @Column(nullable = false, unique = true) 13 | private String username; 14 | 15 | @Column(nullable = false) 16 | private String password; 17 | 18 | @Column(nullable = false) 19 | private int age; 20 | 21 | public long getId() { 22 | return id; 23 | } 24 | 25 | public void setId(long id) { 26 | this.id = id; 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 int getAge() { 46 | return age; 47 | } 48 | 49 | public void setAge(int age) { 50 | this.age = age; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /spring-boot-dubbo/integration-base/src/main/java/integration/base/service/UserService.java: -------------------------------------------------------------------------------- 1 | package integration.base.service; 2 | 3 | import integration.base.domain.User; 4 | 5 | import java.util.List; 6 | 7 | public interface UserService { 8 | List getUserList(); 9 | 10 | User findUserById(long id); 11 | 12 | void save(User user); 13 | 14 | void edit(User user); 15 | 16 | void delete(long id); 17 | } -------------------------------------------------------------------------------- /spring-boot-dubbo/integration-service/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | com.example 8 | integration-service 9 | 0.0.1-SNAPSHOT 10 | jar 11 | integration-service 12 | Spring Boot Integration Service 13 | 14 | 15 | 16 | com.example 17 | spring-boot-integration 18 | 0.0.1-SNAPSHOT 19 | 20 | 21 | 22 | 1.8 23 | 24 | 25 | 26 | 27 | 28 | com.example 29 | integration-base 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-web 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-data-jpa 40 | 41 | 42 | 43 | mysql 44 | mysql-connector-java 45 | runtime 46 | 47 | 48 | 49 | 50 | com.alibaba 51 | dubbo 52 | 2.5.8 53 | 54 | 55 | log4j 56 | log4j 57 | 58 | 59 | 60 | 61 | 62 | 63 | org.apache.zookeeper 64 | zookeeper 65 | 3.4.9 66 | 67 | 68 | slf4j-api 69 | org.slf4j 70 | 71 | 72 | log4j 73 | log4j 74 | 75 | 76 | slf4j-log4j12 77 | org.slf4j 78 | 79 | 80 | 81 | 82 | 83 | com.101tec 84 | zkclient 85 | 0.2 86 | 87 | 88 | 89 | 90 | 91 | 92 | org.springframework.boot 93 | spring-boot-maven-plugin 94 | 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /spring-boot-dubbo/integration-service/src/main/java/integration/IntegrationServiceApplication.java: -------------------------------------------------------------------------------- 1 | package integration; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class IntegrationServiceApplication { 8 | public static void main(String[] args) { 9 | SpringApplication.run(IntegrationServiceApplication.class, args); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /spring-boot-dubbo/integration-service/src/main/java/integration/service/config/DubboConfig.java: -------------------------------------------------------------------------------- 1 | package integration.service.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.context.annotation.ImportResource; 5 | import org.springframework.context.annotation.PropertySource; 6 | 7 | @Configuration 8 | @PropertySource("classpath:dubbo/dubbo.properties") 9 | @ImportResource({ "classpath:dubbo/*.xml" }) 10 | public class DubboConfig { 11 | } 12 | 13 | 14 | -------------------------------------------------------------------------------- /spring-boot-dubbo/integration-service/src/main/java/integration/service/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package integration.service.repository; 2 | 3 | import integration.base.domain.User; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | public interface UserRepository extends JpaRepository { 7 | } -------------------------------------------------------------------------------- /spring-boot-dubbo/integration-service/src/main/java/integration/service/serviceimpl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package integration.service.serviceimpl; 2 | 3 | import integration.base.domain.User; 4 | import integration.base.service.UserService; 5 | import integration.service.repository.UserRepository; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import java.util.List; 10 | 11 | @Service 12 | public class UserServiceImpl implements UserService { 13 | 14 | @Autowired 15 | private UserRepository userRepository; 16 | 17 | @Override 18 | public List getUserList() { 19 | return userRepository.findAll(); 20 | } 21 | 22 | @Override 23 | public User findUserById(long id) { 24 | return userRepository.findById(id).orElse(null); 25 | } 26 | 27 | @Override 28 | public void save(User user) { 29 | userRepository.save(user); 30 | } 31 | 32 | @Override 33 | public void edit(User user) { 34 | userRepository.save(user); 35 | } 36 | 37 | @Override 38 | public void delete(long id) { 39 | userRepository.deleteById(id); 40 | } 41 | } -------------------------------------------------------------------------------- /spring-boot-dubbo/integration-service/src/main/resources/application-dev.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true 4 | username: root 5 | password: 123456 6 | jpa: 7 | hibernate: 8 | ddl-auto: update 9 | show-sql: true 10 | 11 | 12 | -------------------------------------------------------------------------------- /spring-boot-dubbo/integration-service/src/main/resources/application-prod.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | driver-class-name: com.mysql.cj.jdbc.Driver 4 | url: jdbc:mysql://mysql:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true 5 | username: root 6 | password: 123456 7 | jpa: 8 | hibernate: 9 | ddl-auto: validate 10 | show-sql: true -------------------------------------------------------------------------------- /spring-boot-dubbo/integration-service/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | profiles: 3 | active: dev 4 | 5 | server: 6 | port: 8081 -------------------------------------------------------------------------------- /spring-boot-dubbo/integration-service/src/main/resources/dubbo/dubbo-provider.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 21 | -------------------------------------------------------------------------------- /spring-boot-dubbo/integration-service/src/main/resources/dubbo/dubbo.properties: -------------------------------------------------------------------------------- 1 | dubbo.application.name=dubbo-provider 2 | dubbo.registry.protocol=zookeeper 3 | dubbo.registry.address=127.0.0.1:2181 4 | dubbo.protocol.name=dubbo 5 | dubbo.protocol.port=20880 6 | dubbo.admin.root.password=root 7 | dubbo.admin.guest.password=guest -------------------------------------------------------------------------------- /spring-boot-dubbo/integration-web/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | com.example 8 | integration-web 9 | 0.0.1-SNAPSHOT 10 | jar 11 | integration-web 12 | Spring Boot Integration Web 13 | 14 | 15 | 16 | com.example 17 | spring-boot-integration 18 | 0.0.1-SNAPSHOT 19 | 20 | 21 | 22 | 1.8 23 | 24 | 25 | 26 | 27 | 28 | com.example 29 | integration-base 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-thymeleaf 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-web 40 | 41 | 42 | 43 | 44 | com.alibaba 45 | dubbo 46 | 2.5.8 47 | 48 | 49 | log4j 50 | log4j 51 | 52 | 53 | 54 | 55 | 56 | 57 | org.apache.zookeeper 58 | zookeeper 59 | 3.4.9 60 | 61 | 62 | slf4j-api 63 | org.slf4j 64 | 65 | 66 | log4j 67 | log4j 68 | 69 | 70 | slf4j-log4j12 71 | org.slf4j 72 | 73 | 74 | 75 | 76 | 77 | com.101tec 78 | zkclient 79 | 0.2 80 | 81 | 82 | 83 | 84 | 85 | 86 | org.springframework.boot 87 | spring-boot-maven-plugin 88 | 89 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /spring-boot-dubbo/integration-web/src/main/java/integration/IntegrationWebApplication.java: -------------------------------------------------------------------------------- 1 | package integration; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 6 | import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; 7 | import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; 8 | 9 | @SpringBootApplication(exclude = { 10 | DataSourceAutoConfiguration.class, 11 | DataSourceTransactionManagerAutoConfiguration.class, 12 | HibernateJpaAutoConfiguration.class}) 13 | public class IntegrationWebApplication { 14 | 15 | public static void main(String[] args) { 16 | SpringApplication.run(IntegrationWebApplication.class, args); 17 | } 18 | 19 | } 20 | 21 | -------------------------------------------------------------------------------- /spring-boot-dubbo/integration-web/src/main/java/integration/web/config/DubboConfig.java: -------------------------------------------------------------------------------- 1 | package integration.web.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.context.annotation.ImportResource; 5 | import org.springframework.context.annotation.PropertySource; 6 | 7 | @Configuration 8 | @PropertySource("classpath:dubbo/dubbo.properties") 9 | @ImportResource({"classpath:dubbo/*.xml"}) 10 | public class DubboConfig { 11 | } 12 | -------------------------------------------------------------------------------- /spring-boot-dubbo/integration-web/src/main/java/integration/web/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package integration.web.controller; 2 | 3 | import integration.base.domain.User; 4 | import integration.base.service.UserService; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.ui.Model; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | 9 | import javax.annotation.Resource; 10 | import java.util.List; 11 | 12 | @Controller 13 | public class UserController { 14 | @Resource 15 | UserService userService; 16 | 17 | @RequestMapping("/") 18 | public String Index() { 19 | return "redirect:/list"; 20 | } 21 | 22 | @RequestMapping("/list") 23 | public String list(Model model) { 24 | List users = userService.getUserList(); 25 | model.addAttribute("users", users); 26 | return "user/list"; 27 | } 28 | 29 | @RequestMapping("/toAdd") 30 | public String toAdd() { 31 | return "user/userAdd"; 32 | } 33 | 34 | @RequestMapping("/add") 35 | public String add(User user) { 36 | userService.save(user); 37 | return "redirect:/list"; 38 | } 39 | 40 | @RequestMapping("/toEdit") 41 | public String toEdit(Model model, Long id) { 42 | User user = userService.findUserById(id); 43 | model.addAttribute("user", user); 44 | return "user/userEdit"; 45 | } 46 | 47 | @RequestMapping("/edit") 48 | public String edit(User user) { 49 | userService.edit(user); 50 | return "redirect:/list"; 51 | } 52 | 53 | @RequestMapping("/delete") 54 | public String delete(Long id) { 55 | userService.delete(id); 56 | return "redirect:/list"; 57 | } 58 | } -------------------------------------------------------------------------------- /spring-boot-dubbo/integration-web/src/main/resources/application-dev.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | thymeleaf: 3 | cache: false -------------------------------------------------------------------------------- /spring-boot-dubbo/integration-web/src/main/resources/application-prod.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | thymeleaf: 3 | cache: true -------------------------------------------------------------------------------- /spring-boot-dubbo/integration-web/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | profiles: 3 | active: dev 4 | 5 | server: 6 | port: 8082 -------------------------------------------------------------------------------- /spring-boot-dubbo/integration-web/src/main/resources/dubbo/dubbo-consumer.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /spring-boot-dubbo/integration-web/src/main/resources/dubbo/dubbo.properties: -------------------------------------------------------------------------------- 1 | dubbo.application.name=dubbo-consumer 2 | dubbo.registry.protocol=zookeeper 3 | dubbo.registry.address=127.0.0.1:2181 -------------------------------------------------------------------------------- /spring-boot-dubbo/integration-web/src/main/resources/templates/user/list.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | userList 6 | 7 | 8 | 9 |
10 |

用户列表

11 |

12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 |
#User NamePasswordAgeEditDelete
1neoOtto6editdelete
35 |
36 |
37 |
38 | add 39 |
40 |
41 | 42 | -------------------------------------------------------------------------------- /spring-boot-dubbo/integration-web/src/main/resources/templates/user/userAdd.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | user 6 | 7 | 8 | 9 |
10 |

添加用户

11 |

12 |
13 |
14 |
15 | 16 |
17 | 18 |
19 |
20 |
21 | 22 |
23 | 24 |
25 |
26 |
27 | 28 |
29 | 30 |
31 |
32 |
33 |
34 | 35 |       36 | 37 |
38 | 39 |
40 |
41 |
42 | 43 | -------------------------------------------------------------------------------- /spring-boot-dubbo/integration-web/src/main/resources/templates/user/userEdit.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | user 6 | 7 | 8 | 9 |
10 |

修改用户

11 |

12 |
13 |
14 | 15 |
16 | 17 |
18 | 20 |
21 |
22 |
23 | 24 |
25 | 27 |
28 |
29 |
30 | 31 |
32 | 33 |
34 |
35 |
36 |
37 | 38 |       39 | Back 40 |
41 | 42 |
43 |
44 |
45 | 46 | -------------------------------------------------------------------------------- /spring-boot-dubbo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | Spring Boot 多模块示例 6 | 4.0.0 7 | spring-boot-integration 8 | pom 9 | 10 | 11 | com.example 12 | spring-boot-integration 13 | 0.0.1-SNAPSHOT 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-parent 19 | 2.1.2.RELEASE 20 | 21 | 22 | 23 | 24 | 1.8 25 | 26 | 27 | 28 | 29 | integration-web 30 | integration-service 31 | integration-base 32 | 33 | 34 | 35 | 36 | 37 | 38 | com.example 39 | integration-web 40 | 0.0.1-SNAPSHOT 41 | 42 | 43 | 44 | com.example 45 | integration-service 46 | 0.0.1-SNAPSHOT 47 | 48 | 49 | 50 | com.example 51 | integration-base 52 | 0.0.1-SNAPSHOT 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /spring-boot-integration/integration-base/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | com.example 8 | integration-base 9 | 0.0.1-SNAPSHOT 10 | jar 11 | integration-base 12 | Spring Boot Integration Base 13 | 14 | 15 | 16 | com.example 17 | spring-boot-integration 18 | 0.0.1-SNAPSHOT 19 | 20 | 21 | 22 | 1.8 23 | 24 | 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-data-jpa 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /spring-boot-integration/integration-base/src/main/java/integration/base/domain/User.java: -------------------------------------------------------------------------------- 1 | package integration.base.domain; 2 | 3 | import javax.persistence.*; 4 | 5 | @Entity 6 | public class User { 7 | @Id 8 | @GeneratedValue(strategy = GenerationType.IDENTITY) 9 | private long id; 10 | 11 | @Column(nullable = false, unique = true) 12 | private String username; 13 | 14 | @Column(nullable = false) 15 | private String password; 16 | 17 | @Column(nullable = false) 18 | private int age; 19 | 20 | public long getId() { 21 | return id; 22 | } 23 | 24 | public void setId(long id) { 25 | this.id = id; 26 | } 27 | 28 | public String 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 int getAge() { 45 | return age; 46 | } 47 | 48 | public void setAge(int age) { 49 | this.age = age; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /spring-boot-integration/integration-base/src/main/java/integration/base/service/UserService.java: -------------------------------------------------------------------------------- 1 | package integration.base.service; 2 | 3 | import integration.base.domain.User; 4 | 5 | import java.util.List; 6 | 7 | public interface UserService { 8 | List getUserList(); 9 | 10 | User findUserById(long id); 11 | 12 | void save(User user); 13 | 14 | void edit(User user); 15 | 16 | void delete(long id); 17 | } -------------------------------------------------------------------------------- /spring-boot-integration/integration-service/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | com.example 8 | integration-service 9 | 0.0.1-SNAPSHOT 10 | jar 11 | integration-service 12 | Spring Boot Integration Service 13 | 14 | 15 | 16 | com.example 17 | spring-boot-integration 18 | 0.0.1-SNAPSHOT 19 | 20 | 21 | 22 | 1.8 23 | 24 | 25 | 26 | 27 | 28 | com.example 29 | integration-base 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-data-jpa 35 | 36 | 37 | 38 | mysql 39 | mysql-connector-java 40 | runtime 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /spring-boot-integration/integration-service/src/main/java/integration/service/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package integration.service.repository; 2 | 3 | import integration.base.domain.User; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | public interface UserRepository extends JpaRepository { 7 | } -------------------------------------------------------------------------------- /spring-boot-integration/integration-service/src/main/java/integration/service/serviceimpl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package integration.service.serviceimpl; 2 | 3 | import integration.base.domain.User; 4 | import integration.base.service.UserService; 5 | import integration.service.repository.UserRepository; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import java.util.List; 10 | 11 | @Service 12 | public class UserServiceImpl implements UserService { 13 | 14 | @Autowired 15 | private UserRepository userRepository; 16 | 17 | @Override 18 | public List getUserList() { 19 | return userRepository.findAll(); 20 | } 21 | 22 | @Override 23 | public User findUserById(long id) { 24 | return userRepository.findById(id).orElse(null); 25 | } 26 | 27 | @Override 28 | public void save(User user) { 29 | userRepository.save(user); 30 | } 31 | 32 | @Override 33 | public void edit(User user) { 34 | userRepository.save(user); 35 | } 36 | 37 | @Override 38 | public void delete(long id) { 39 | userRepository.deleteById(id); 40 | } 41 | } -------------------------------------------------------------------------------- /spring-boot-integration/integration-web/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | com.example 8 | integration-web 9 | 0.0.1-SNAPSHOT 10 | jar 11 | integration-web 12 | Spring Boot Integration Web 13 | 14 | 15 | 16 | com.example 17 | spring-boot-integration 18 | 0.0.1-SNAPSHOT 19 | 20 | 21 | 22 | 1.8 23 | 24 | 25 | 26 | 27 | 28 | com.example 29 | integration-service 30 | 31 | 32 | 33 | com.example 34 | integration-base 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-thymeleaf 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-starter-web 45 | 46 | 47 | 48 | 49 | 50 | 51 | org.springframework.boot 52 | spring-boot-maven-plugin 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /spring-boot-integration/integration-web/src/main/java/integration/IntegrationWebApplication.java: -------------------------------------------------------------------------------- 1 | package integration; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class IntegrationWebApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(IntegrationWebApplication.class, args); 11 | } 12 | 13 | } 14 | 15 | -------------------------------------------------------------------------------- /spring-boot-integration/integration-web/src/main/java/integration/web/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package integration.web.controller; 2 | 3 | import integration.base.domain.User; 4 | import integration.base.service.UserService; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.ui.Model; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | 9 | import javax.annotation.Resource; 10 | import java.util.List; 11 | 12 | @Controller 13 | public class UserController { 14 | @Resource 15 | UserService userService; 16 | 17 | @RequestMapping("/") 18 | public String Index() { 19 | return "redirect:/list"; 20 | } 21 | 22 | @RequestMapping("/list") 23 | public String list(Model model) { 24 | List users = userService.getUserList(); 25 | model.addAttribute("users", users); 26 | return "user/list"; 27 | } 28 | 29 | @RequestMapping("/toAdd") 30 | public String toAdd() { 31 | return "user/userAdd"; 32 | } 33 | 34 | @RequestMapping("/add") 35 | public String add(User user) { 36 | userService.save(user); 37 | return "redirect:/list"; 38 | } 39 | 40 | @RequestMapping("/toEdit") 41 | public String toEdit(Model model, Long id) { 42 | User user = userService.findUserById(id); 43 | model.addAttribute("user", user); 44 | return "user/userEdit"; 45 | } 46 | 47 | @RequestMapping("/edit") 48 | public String edit(User user) { 49 | userService.edit(user); 50 | return "redirect:/list"; 51 | } 52 | 53 | @RequestMapping("/delete") 54 | public String delete(Long id) { 55 | userService.delete(id); 56 | return "redirect:/list"; 57 | } 58 | } -------------------------------------------------------------------------------- /spring-boot-integration/integration-web/src/main/resources/application-dev.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | driver-class-name: com.mysql.cj.jdbc.Driver 4 | url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true 5 | username: root 6 | password: 123456 7 | jpa: 8 | hibernate: 9 | ddl-auto: update 10 | show-sql: true 11 | thymeleaf: 12 | cache: false -------------------------------------------------------------------------------- /spring-boot-integration/integration-web/src/main/resources/application-prod.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | driver-class-name: com.mysql.cj.jdbc.Driver 4 | url: jdbc:mysql://mysql:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true 5 | username: root 6 | password: 123456 7 | jpa: 8 | hibernate: 9 | ddl-auto: validate 10 | show-sql: true 11 | thymeleaf: 12 | cache: true -------------------------------------------------------------------------------- /spring-boot-integration/integration-web/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | profiles: 3 | active: dev -------------------------------------------------------------------------------- /spring-boot-integration/integration-web/src/main/resources/templates/user/list.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | userList 6 | 7 | 8 | 9 |
10 |

用户列表

11 |

12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 |
#User NamePasswordAgeEditDelete
1neoOtto6editdelete
35 |
36 |
37 |
38 | add 39 |
40 |
41 | 42 | -------------------------------------------------------------------------------- /spring-boot-integration/integration-web/src/main/resources/templates/user/userAdd.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | user 6 | 7 | 8 | 9 |
10 |

添加用户

11 |

12 |
13 |
14 |
15 | 16 |
17 | 18 |
19 |
20 |
21 | 22 |
23 | 24 |
25 |
26 |
27 | 28 |
29 | 30 |
31 |
32 |
33 |
34 | 35 |       36 | 37 |
38 | 39 |
40 |
41 |
42 | 43 | -------------------------------------------------------------------------------- /spring-boot-integration/integration-web/src/main/resources/templates/user/userEdit.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | user 6 | 7 | 8 | 9 |
10 |

修改用户

11 |

12 |
13 |
14 | 15 |
16 | 17 |
18 | 20 |
21 |
22 |
23 | 24 |
25 | 27 |
28 |
29 |
30 | 31 |
32 | 33 |
34 |
35 |
36 |
37 | 38 |       39 | Back 40 |
41 | 42 |
43 |
44 |
45 | 46 | -------------------------------------------------------------------------------- /spring-boot-integration/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | Spring Boot 多模块示例 6 | 4.0.0 7 | spring-boot-integration 8 | pom 9 | 10 | 11 | com.example 12 | spring-boot-integration 13 | 0.0.1-SNAPSHOT 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-parent 19 | 2.1.2.RELEASE 20 | 21 | 22 | 23 | 24 | 1.8 25 | 26 | 27 | 28 | 29 | integration-web 30 | integration-service 31 | integration-base 32 | 33 | 34 | 35 | 36 | 37 | 38 | com.example 39 | integration-web 40 | 0.0.1-SNAPSHOT 41 | 42 | 43 | 44 | com.example 45 | integration-service 46 | 0.0.1-SNAPSHOT 47 | 48 | 49 | 50 | com.example 51 | integration-base 52 | 0.0.1-SNAPSHOT 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /spring-boot-jersey/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.3.RELEASE 9 | 10 | 11 | com.example 12 | spring-boot-jersey 13 | 0.0.1-SNAPSHOT 14 | spring-boot-jersey 15 | Jersey demo for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-jersey 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | org.projectlombok 32 | lombok 33 | 1.18.6 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-test 38 | test 39 | 40 | 41 | 42 | 43 | 44 | 45 | org.springframework.boot 46 | spring-boot-maven-plugin 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /spring-boot-jersey/src/main/java/com/example/springbootjersey/SpringBootJerseyApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjersey; 2 | 3 | import org.glassfish.jersey.server.ResourceConfig; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.context.annotation.Bean; 7 | 8 | @SpringBootApplication 9 | public class SpringBootJerseyApplication { 10 | public static void main(String[] args) { 11 | SpringApplication.run(SpringBootJerseyApplication.class, args); 12 | } 13 | 14 | @Bean 15 | public ResourceConfig resourceConfig() { 16 | return new ResourceConfig(); 17 | } 18 | } -------------------------------------------------------------------------------- /spring-boot-jersey/src/main/java/com/example/springbootjersey/config/MyResourceConfigCustomizer.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjersey.config; 2 | 3 | import com.example.springbootjersey.controller.UserController; 4 | import org.glassfish.jersey.server.ResourceConfig; 5 | import org.springframework.boot.autoconfigure.jersey.ResourceConfigCustomizer; 6 | import org.springframework.stereotype.Component; 7 | 8 | @Component 9 | public class MyResourceConfigCustomizer implements ResourceConfigCustomizer { 10 | @Override 11 | public void customize(ResourceConfig config) { 12 | config.register(UserController.class); 13 | } 14 | } -------------------------------------------------------------------------------- /spring-boot-jersey/src/main/java/com/example/springbootjersey/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjersey.controller; 2 | 3 | import com.example.springbootjersey.model.User; 4 | import lombok.extern.slf4j.Slf4j; 5 | import org.springframework.stereotype.Component; 6 | 7 | import javax.ws.rs.*; 8 | import javax.ws.rs.core.MediaType; 9 | import javax.ws.rs.core.Response; 10 | import java.net.URI; 11 | import java.util.*; 12 | 13 | @Path("user") 14 | @Component 15 | @Slf4j 16 | public class UserController { 17 | @GET 18 | @Path("{id}") 19 | @Produces(MediaType.APPLICATION_JSON) 20 | public Response findUserById(@PathParam("id") Integer id) { 21 | User user = new User(); 22 | user.setId(id); 23 | user.setUsername("wander"); 24 | user.setSex("男"); 25 | user.setAge(35); 26 | return Response.ok(user).build(); 27 | } 28 | 29 | @GET 30 | @Produces(MediaType.APPLICATION_JSON) 31 | public Response findAllUsers() { 32 | List users = new ArrayList<>(); 33 | User user = new User(); 34 | user.setId(1); 35 | user.setUsername("wander"); 36 | user.setSex("男"); 37 | user.setAge(35); 38 | users.add(user); 39 | return Response.ok(users).build(); 40 | } 41 | 42 | @POST 43 | @Consumes(MediaType.APPLICATION_JSON) 44 | public Response addUser(User user) { 45 | if (user.getId() == 1) 46 | return Response.status(Response.Status.CONFLICT).build(); 47 | else 48 | return Response.created(URI.create("/user/" + user.getId())).build(); 49 | } 50 | 51 | @PUT 52 | @Produces(MediaType.APPLICATION_JSON) 53 | @Consumes(MediaType.APPLICATION_JSON) 54 | public Response updateUser(User user) { 55 | return Response.ok(user).build(); 56 | } 57 | 58 | @DELETE 59 | @Path("{id}") 60 | public Response deleteUser(@PathParam("id") Integer id) { 61 | return Response.noContent().build(); 62 | } 63 | } -------------------------------------------------------------------------------- /spring-boot-jersey/src/main/java/com/example/springbootjersey/model/User.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjersey.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class User implements Serializable { 6 | private Integer id; 7 | private String username; 8 | private String sex; 9 | private Integer age; 10 | 11 | public Integer getId() { 12 | return id; 13 | } 14 | 15 | public void setId(Integer id) { 16 | this.id = id; 17 | } 18 | 19 | public String getUsername() { 20 | return username; 21 | } 22 | 23 | public void setUsername(String username) { 24 | this.username = username; 25 | } 26 | 27 | public String getSex() { 28 | return sex; 29 | } 30 | 31 | public void setSex(String sex) { 32 | this.sex = sex; 33 | } 34 | 35 | public Integer getAge() { 36 | return age; 37 | } 38 | 39 | public void setAge(Integer age) { 40 | this.age = age; 41 | } 42 | } -------------------------------------------------------------------------------- /spring-boot-jersey/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | # spring.jersey.application-path=test -------------------------------------------------------------------------------- /spring-boot-jersey/src/test/java/com/example/springbootjersey/SpringBootJerseyApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjersey; 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 SpringBootJerseyApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-boot-jersey/src/test/java/com/example/springbootjersey/controller/UserControllerTest.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootjersey.controller; 2 | 3 | import com.example.springbootjersey.SpringBootJerseyApplication; 4 | import com.example.springbootjersey.model.User; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.beans.factory.annotation.Value; 9 | import org.springframework.boot.test.context.SpringBootTest; 10 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 11 | 12 | import javax.ws.rs.client.Client; 13 | import javax.ws.rs.client.ClientBuilder; 14 | import javax.ws.rs.client.Entity; 15 | import javax.ws.rs.client.WebTarget; 16 | import javax.ws.rs.core.GenericType; 17 | import javax.ws.rs.core.MediaType; 18 | import javax.ws.rs.core.Response; 19 | 20 | import java.util.List; 21 | 22 | import static org.junit.Assert.*; 23 | 24 | @RunWith(SpringJUnit4ClassRunner.class) 25 | @SpringBootTest(classes = SpringBootJerseyApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 26 | @Slf4j 27 | public class UserControllerTest { 28 | @Value("${local.server.port}") 29 | private int port; 30 | 31 | @Test 32 | public void findUserById() { 33 | Client client = ClientBuilder.newClient(); 34 | WebTarget target = client.target("http://localhost:" + port + "/user/1"); 35 | Response response = target.request(MediaType.APPLICATION_JSON).get(); 36 | User user = response.readEntity(User.class); 37 | assertTrue(response.getStatus() == 200); 38 | assertTrue(user.getId() == 1); 39 | } 40 | 41 | @Test 42 | public void findAllUsers() { 43 | Client client = ClientBuilder.newClient(); 44 | WebTarget target = client.target("http://localhost:" + port + "/user"); 45 | Response response = target.request(MediaType.APPLICATION_JSON).get(); 46 | List users = response.readEntity(new GenericType>() { 47 | }); 48 | assertTrue(response.getStatus() == 200); 49 | assertTrue(users.size() == 1); 50 | } 51 | 52 | @Test 53 | public void addUser() { 54 | Client client = ClientBuilder.newClient(); 55 | WebTarget target = client.target("http://localhost:" + port + "/user"); 56 | User user = new User(); 57 | user.setId(2); 58 | user.setUsername("wander"); 59 | user.setSex("男"); 60 | user.setAge(35); 61 | Response response = target.request(MediaType.APPLICATION_JSON).post(Entity.json(user)); 62 | assertTrue(response.getStatus() == 201); 63 | } 64 | 65 | @Test 66 | public void updateUser() { 67 | Client client = ClientBuilder.newClient(); 68 | WebTarget target = client.target("http://localhost:" + port + "/user"); 69 | User user = new User(); 70 | user.setId(1); 71 | user.setUsername("wander2"); 72 | user.setSex("男"); 73 | user.setAge(35); 74 | Response response = target.request(MediaType.APPLICATION_JSON).put(Entity.json(user)); 75 | user = response.readEntity(User.class); 76 | assertTrue(response.getStatus() == 200); 77 | assertEquals("wander2", user.getUsername()); 78 | } 79 | 80 | @Test 81 | public void deleteUser() { 82 | Client client = ClientBuilder.newClient(); 83 | WebTarget target = client.target("http://localhost:" + port + "/user/1"); 84 | Response response = target.request(MediaType.APPLICATION_JSON).delete(); 85 | assertTrue(response.getStatus() == 204); 86 | } 87 | } -------------------------------------------------------------------------------- /spring-boot-jpa-thymeleaf-crud/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.2.RELEASE 9 | 10 | 11 | com.example 12 | springboottest 13 | 0.0.1-SNAPSHOT 14 | springboottest 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-data-jpa 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-thymeleaf 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-web 33 | 34 | 35 | 36 | mysql 37 | mysql-connector-java 38 | runtime 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-starter-test 43 | test 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.springframework.boot 51 | spring-boot-maven-plugin 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /spring-boot-jpa-thymeleaf-crud/src/main/java/springboottest/SpringBootTestApplication.java: -------------------------------------------------------------------------------- 1 | package springboottest; 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 | 9 | @SpringBootApplication 10 | public class SpringBootTestApplication extends SpringBootServletInitializer { 11 | public static void main(String[] args) { 12 | SpringApplication.run(SpringBootTestApplication.class, args); 13 | } 14 | 15 | @Override 16 | protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { 17 | return builder.sources(SpringBootTestApplication.class); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /spring-boot-jpa-thymeleaf-crud/src/main/java/springboottest/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package springboottest.controller; 2 | 3 | import springboottest.domain.User; 4 | import springboottest.service.UserService; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.ui.Model; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | 9 | import javax.annotation.Resource; 10 | import java.util.List; 11 | 12 | @Controller 13 | public class UserController { 14 | @Resource 15 | UserService userService; 16 | 17 | @RequestMapping("/") 18 | public String Index() { 19 | return "redirect:/list"; 20 | } 21 | 22 | @RequestMapping("/list") 23 | public String list(Model model) { 24 | List users = userService.getUserList(); 25 | model.addAttribute("users", users); 26 | return "user/list"; 27 | } 28 | 29 | @RequestMapping("/toAdd") 30 | public String toAdd() { 31 | return "user/userAdd"; 32 | } 33 | 34 | @RequestMapping("/add") 35 | public String add(User user) { 36 | userService.save(user); 37 | return "redirect:/list"; 38 | } 39 | 40 | @RequestMapping("/toEdit") 41 | public String toEdit(Model model, Long id) { 42 | User user = userService.findUserById(id); 43 | model.addAttribute("user", user); 44 | return "user/userEdit"; 45 | } 46 | 47 | @RequestMapping("/edit") 48 | public String edit(User user) { 49 | userService.edit(user); 50 | return "redirect:/list"; 51 | } 52 | 53 | @RequestMapping("/delete") 54 | public String delete(Long id) { 55 | userService.delete(id); 56 | return "redirect:/list"; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /spring-boot-jpa-thymeleaf-crud/src/main/java/springboottest/domain/User.java: -------------------------------------------------------------------------------- 1 | package springboottest.domain; 2 | 3 | import net.bytebuddy.dynamic.loading.InjectionClassLoader; 4 | 5 | import javax.persistence.*; 6 | 7 | @Entity 8 | public class User { 9 | @Id 10 | @GeneratedValue(strategy = GenerationType.IDENTITY) 11 | private long id; 12 | 13 | @Column(nullable = false, unique = true) 14 | private String username; 15 | 16 | @Column(nullable = false) 17 | private String password; 18 | 19 | @Column(nullable = false) 20 | private int age; 21 | 22 | public long getId() { 23 | return id; 24 | } 25 | 26 | public void setId(long id) { 27 | this.id = id; 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 int getAge() { 47 | return age; 48 | } 49 | 50 | public void setAge(int age) { 51 | this.age = age; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /spring-boot-jpa-thymeleaf-crud/src/main/java/springboottest/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package springboottest.repository; 2 | 3 | import springboottest.domain.User; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | 7 | public interface UserRepository extends JpaRepository { 8 | } -------------------------------------------------------------------------------- /spring-boot-jpa-thymeleaf-crud/src/main/java/springboottest/service/UserService.java: -------------------------------------------------------------------------------- 1 | package springboottest.service; 2 | 3 | import springboottest.domain.User; 4 | 5 | import java.util.List; 6 | 7 | public interface UserService { 8 | List getUserList(); 9 | 10 | User findUserById(long id); 11 | 12 | void save(User user); 13 | 14 | void edit(User user); 15 | 16 | void delete(long id); 17 | } -------------------------------------------------------------------------------- /spring-boot-jpa-thymeleaf-crud/src/main/java/springboottest/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package springboottest.service.impl; 2 | 3 | import springboottest.domain.User; 4 | import springboottest.repository.UserRepository; 5 | import springboottest.service.UserService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import java.util.List; 10 | 11 | @Service 12 | public class UserServiceImpl implements UserService { 13 | @Autowired 14 | private UserRepository userRepository; 15 | 16 | @Override 17 | public List getUserList() { 18 | return userRepository.findAll(); 19 | } 20 | 21 | @Override 22 | public User findUserById(long id) { 23 | return userRepository.findById(id).orElse(null); 24 | } 25 | 26 | @Override 27 | public void save(User user) { 28 | userRepository.save(user); 29 | } 30 | 31 | @Override 32 | public void edit(User user) { 33 | userRepository.save(user); 34 | } 35 | 36 | @Override 37 | public void delete(long id) { 38 | userRepository.deleteById(id); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /spring-boot-jpa-thymeleaf-crud/src/main/resources/application-dev.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | driver-class-name: com.mysql.cj.jdbc.Driver 4 | url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true 5 | username: root 6 | password: 123456 7 | jpa: 8 | hibernate: 9 | ddl-auto: update 10 | show-sql: true 11 | thymeleaf: 12 | cache: false -------------------------------------------------------------------------------- /spring-boot-jpa-thymeleaf-crud/src/main/resources/application-prod.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | driver-class-name: com.mysql.cj.jdbc.Driver 4 | url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true 5 | username: root 6 | password: 123456 7 | jpa: 8 | hibernate: 9 | ddl-auto: validate 10 | show-sql: true 11 | thymeleaf: 12 | cache: true -------------------------------------------------------------------------------- /spring-boot-jpa-thymeleaf-crud/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | profiles: 3 | active: dev -------------------------------------------------------------------------------- /spring-boot-jpa-thymeleaf-crud/src/main/resources/templates/user/list.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | userList 6 | 7 | 8 | 9 |
10 |

用户列表

11 |

12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 |
#User NamePasswordAgeEditDelete
1neoOtto6editdelete
35 |
36 |
37 |
38 | add 39 |
40 |
41 | 42 | -------------------------------------------------------------------------------- /spring-boot-jpa-thymeleaf-crud/src/main/resources/templates/user/userAdd.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | user 6 | 7 | 8 | 9 |
10 |

添加用户

11 |

12 |
13 |
14 |
15 | 16 |
17 | 18 |
19 |
20 |
21 | 22 |
23 | 24 |
25 |
26 |
27 | 28 |
29 | 30 |
31 |
32 |
33 |
34 | 35 |       36 | 37 |
38 | 39 |
40 |
41 |
42 | 43 | -------------------------------------------------------------------------------- /spring-boot-jpa-thymeleaf-crud/src/main/resources/templates/user/userEdit.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | user 6 | 7 | 8 | 9 |
10 |

修改用户

11 |

12 |
13 |
14 | 15 |
16 | 17 |
18 | 20 |
21 |
22 |
23 | 24 |
25 | 27 |
28 |
29 |
30 | 31 |
32 | 33 |
34 |
35 |
36 |
37 | 38 |       39 | Back 40 |
41 | 42 |
43 |
44 |
45 | 46 | -------------------------------------------------------------------------------- /spring-boot-jpa-thymeleaf-crud/src/test/java/springboottest/SpringBootTestApplicationTests.java: -------------------------------------------------------------------------------- 1 | package springboottest; 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 SpringBootTestApplicationTests { 11 | @Test 12 | public void contextLoads() { 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /spring-boot-multi-datasource/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.2.RELEASE 9 | 10 | 11 | com.example 12 | springboottest 13 | 0.0.1-SNAPSHOT 14 | springboottest 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-data-jpa 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-thymeleaf 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-web 33 | 34 | 35 | 36 | mysql 37 | mysql-connector-java 38 | runtime 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-starter-test 43 | test 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.springframework.boot 51 | spring-boot-maven-plugin 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /spring-boot-multi-datasource/springboottest.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /spring-boot-multi-datasource/src/main/java/springboottest/SpringBootTestApplication.java: -------------------------------------------------------------------------------- 1 | package springboottest; 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 | 9 | @SpringBootApplication 10 | public class SpringBootTestApplication extends SpringBootServletInitializer { 11 | public static void main(String[] args) { 12 | SpringApplication.run(SpringBootTestApplication.class, args); 13 | } 14 | 15 | @Override 16 | protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { 17 | return builder.sources(SpringBootTestApplication.class); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /spring-boot-multi-datasource/src/main/java/springboottest/config/DataSourceConfig.java: -------------------------------------------------------------------------------- 1 | package springboottest.config; 2 | 3 | import org.springframework.beans.factory.annotation.Qualifier; 4 | import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; 5 | import org.springframework.boot.context.properties.ConfigurationProperties; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | import org.springframework.context.annotation.Primary; 9 | import javax.sql.DataSource; 10 | 11 | @Configuration 12 | public class DataSourceConfig { 13 | @Primary 14 | @Bean(name = "masterDataSourceProperties") 15 | @Qualifier("masterDataSourceProperties") 16 | @ConfigurationProperties(prefix = "spring.datasource.master") 17 | public DataSourceProperties masterDataSourceProperties() { 18 | return new DataSourceProperties(); 19 | } 20 | 21 | @Primary 22 | @Bean(name = "masterDataSource") 23 | @Qualifier("masterDataSource") 24 | public DataSource masterDataSource(@Qualifier("masterDataSourceProperties") DataSourceProperties dataSourceProperties) { 25 | return dataSourceProperties.initializeDataSourceBuilder().build(); 26 | } 27 | 28 | //slave库 29 | @Bean(name = "slaveDataSourceProperties") 30 | @Qualifier("slaveDataSourceProperties") 31 | @ConfigurationProperties(prefix = "spring.datasource.slave") 32 | public DataSourceProperties slaveDataSourceProperties() { 33 | return new DataSourceProperties(); 34 | } 35 | 36 | @Bean(name = "slaveDataSource") 37 | @Qualifier("slaveDataSource") 38 | public DataSource slaveDataSource(@Qualifier("slaveDataSourceProperties") DataSourceProperties dataSourceProperties) { 39 | return dataSourceProperties.initializeDataSourceBuilder().build(); 40 | } 41 | } -------------------------------------------------------------------------------- /spring-boot-multi-datasource/src/main/java/springboottest/config/MasterConfig.java: -------------------------------------------------------------------------------- 1 | package springboottest.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.beans.factory.annotation.Qualifier; 5 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 6 | import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties; 7 | import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings; 8 | import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties; 9 | import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; 10 | import org.springframework.context.annotation.Bean; 11 | import org.springframework.context.annotation.Configuration; 12 | import org.springframework.context.annotation.Primary; 13 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 14 | import org.springframework.orm.jpa.JpaTransactionManager; 15 | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 16 | import org.springframework.transaction.PlatformTransactionManager; 17 | import org.springframework.transaction.annotation.EnableTransactionManagement; 18 | 19 | import javax.annotation.Resource; 20 | import javax.persistence.EntityManager; 21 | import javax.sql.DataSource; 22 | import java.util.Map; 23 | 24 | @Configuration 25 | @EnableTransactionManagement 26 | @EnableJpaRepositories( 27 | entityManagerFactoryRef = "masterEntityManagerFactory", 28 | transactionManagerRef = "masterTransactionManager", 29 | basePackages = {"springboottest.database.master"}) 30 | @ConditionalOnProperty(prefix = "spring.datasource.master", name = "enable", havingValue = "true") 31 | public class MasterConfig { 32 | @Autowired 33 | private HibernateProperties hibernateProperties; 34 | @Resource 35 | @Qualifier("masterDataSource") 36 | private DataSource masterDataSource; 37 | 38 | @Primary 39 | @Bean(name = "masterEntityManager") 40 | public EntityManager entityManager(EntityManagerFactoryBuilder builder) { 41 | return masterEntityManagerFactory(builder).getObject().createEntityManager(); 42 | } 43 | 44 | @Resource 45 | private JpaProperties jpaProperties; 46 | 47 | /** 48 | * 设置实体类所在位置 49 | */ 50 | @Primary 51 | @Bean(name = "masterEntityManagerFactory") 52 | public LocalContainerEntityManagerFactoryBean masterEntityManagerFactory(EntityManagerFactoryBuilder builder) { 53 | 54 | Map properties = hibernateProperties.determineHibernateProperties( 55 | jpaProperties.getProperties(), new HibernateSettings()); 56 | return builder 57 | .dataSource(masterDataSource) 58 | .packages("springboottest.database.master") 59 | .persistenceUnit("masterPersistenceUnit") 60 | .properties(properties) 61 | .build(); 62 | } 63 | 64 | @Primary 65 | @Bean(name = "masterTransactionManager") 66 | public PlatformTransactionManager masterTransactionManager(EntityManagerFactoryBuilder builder) { 67 | return new JpaTransactionManager(masterEntityManagerFactory(builder).getObject()); 68 | } 69 | } -------------------------------------------------------------------------------- /spring-boot-multi-datasource/src/main/java/springboottest/config/SlaveConfig.java: -------------------------------------------------------------------------------- 1 | package springboottest.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.beans.factory.annotation.Qualifier; 5 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 6 | import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties; 7 | import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings; 8 | import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties; 9 | import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; 10 | import org.springframework.context.annotation.Bean; 11 | import org.springframework.context.annotation.Configuration; 12 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 13 | import org.springframework.orm.jpa.JpaTransactionManager; 14 | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 15 | import org.springframework.transaction.PlatformTransactionManager; 16 | import org.springframework.transaction.annotation.EnableTransactionManagement; 17 | 18 | import javax.annotation.Resource; 19 | import javax.persistence.EntityManager; 20 | import javax.sql.DataSource; 21 | import java.util.Map; 22 | 23 | 24 | @Configuration 25 | @EnableTransactionManagement 26 | @EnableJpaRepositories( 27 | entityManagerFactoryRef = "slaveEntityManagerFactory", 28 | transactionManagerRef = "slaveTransactionManager", 29 | basePackages = {"springboottest.database.slave"}) 30 | @ConditionalOnProperty(prefix = "spring.datasource.slave", name = "enable", havingValue = "true") 31 | public class SlaveConfig { 32 | @Autowired 33 | @Qualifier("slaveDataSource") 34 | private DataSource slaveDataSource; 35 | 36 | @Autowired 37 | private HibernateProperties hibernateProperties; 38 | 39 | @Bean(name = "slaveEntityManager") 40 | public EntityManager entityManager(EntityManagerFactoryBuilder builder) { 41 | return slaveEntityManagerFactory(builder).getObject().createEntityManager(); 42 | } 43 | 44 | @Resource 45 | private JpaProperties jpaProperties; 46 | 47 | @Bean(name = "slaveEntityManagerFactory") 48 | public LocalContainerEntityManagerFactoryBean slaveEntityManagerFactory(EntityManagerFactoryBuilder builder) { 49 | 50 | Map properties = hibernateProperties.determineHibernateProperties( 51 | jpaProperties.getProperties(), new HibernateSettings()); 52 | return builder 53 | .dataSource(slaveDataSource) 54 | .packages("springboottest.database.slave") 55 | .persistenceUnit("slavePersistenceUnit") 56 | .properties(properties) 57 | .build(); 58 | } 59 | 60 | @Bean(name = "slaveTransactionManager") 61 | PlatformTransactionManager slaveTransactionManager(EntityManagerFactoryBuilder builder) { 62 | return new JpaTransactionManager(slaveEntityManagerFactory(builder).getObject()); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /spring-boot-multi-datasource/src/main/java/springboottest/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package springboottest.controller; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.web.bind.annotation.RestController; 5 | import springboottest.database.master.User; 6 | import springboottest.database.slave.User2; 7 | import springboottest.service.UserService; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | 10 | import java.util.List; 11 | 12 | @RestController 13 | public class UserController { 14 | @Autowired 15 | UserService userService; 16 | 17 | @RequestMapping("/") 18 | public List master() { 19 | return userService.getUserList(); 20 | } 21 | 22 | @RequestMapping("slave") 23 | public List slave() { 24 | return userService.getUserList2(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /spring-boot-multi-datasource/src/main/java/springboottest/database/master/User.java: -------------------------------------------------------------------------------- 1 | package springboottest.database.master; 2 | 3 | import javax.persistence.*; 4 | 5 | @Entity 6 | public class User { 7 | @Id 8 | @GeneratedValue(strategy = GenerationType.IDENTITY) 9 | private long id; 10 | 11 | @Column(nullable = false, unique = true) 12 | private String username; 13 | 14 | @Column(nullable = false) 15 | private String password; 16 | 17 | @Column(nullable = false) 18 | private int age; 19 | 20 | public long getId() { 21 | return id; 22 | } 23 | 24 | public void setId(long id) { 25 | this.id = id; 26 | } 27 | 28 | public String 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 int getAge() { 45 | return age; 46 | } 47 | 48 | public void setAge(int age) { 49 | this.age = age; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /spring-boot-multi-datasource/src/main/java/springboottest/database/master/UserRepository.java: -------------------------------------------------------------------------------- 1 | package springboottest.database.master; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | public interface UserRepository extends JpaRepository { 6 | } -------------------------------------------------------------------------------- /spring-boot-multi-datasource/src/main/java/springboottest/database/slave/User2.java: -------------------------------------------------------------------------------- 1 | package springboottest.database.slave; 2 | 3 | import javax.persistence.*; 4 | 5 | @Entity 6 | @Table(name = "user") 7 | public class User2 { 8 | @Id 9 | @GeneratedValue(strategy = GenerationType.IDENTITY) 10 | private long id; 11 | 12 | @Column(nullable = false, unique = true) 13 | private String username; 14 | 15 | @Column(nullable = false) 16 | private String password; 17 | 18 | @Column(nullable = false) 19 | private int age; 20 | 21 | public long getId() { 22 | return id; 23 | } 24 | 25 | public void setId(long id) { 26 | this.id = id; 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 int getAge() { 46 | return age; 47 | } 48 | 49 | public void setAge(int age) { 50 | this.age = age; 51 | } 52 | } 53 | 54 | -------------------------------------------------------------------------------- /spring-boot-multi-datasource/src/main/java/springboottest/database/slave/UserRepository2.java: -------------------------------------------------------------------------------- 1 | package springboottest.database.slave; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | public interface UserRepository2 extends JpaRepository { 6 | } 7 | -------------------------------------------------------------------------------- /spring-boot-multi-datasource/src/main/java/springboottest/service/UserService.java: -------------------------------------------------------------------------------- 1 | package springboottest.service; 2 | 3 | import springboottest.database.master.User; 4 | import springboottest.database.slave.User2; 5 | 6 | import java.util.List; 7 | 8 | public interface UserService { 9 | List getUserList(); 10 | 11 | List getUserList2(); 12 | } -------------------------------------------------------------------------------- /spring-boot-multi-datasource/src/main/java/springboottest/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package springboottest.service.impl; 2 | 3 | import springboottest.database.master.User; 4 | import springboottest.database.master.UserRepository; 5 | import springboottest.database.slave.User2; 6 | import springboottest.database.slave.UserRepository2; 7 | import springboottest.service.UserService; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Service; 10 | 11 | import java.util.List; 12 | 13 | @Service 14 | public class UserServiceImpl implements UserService { 15 | @Autowired(required = false) 16 | private UserRepository userRepository; 17 | 18 | @Autowired(required = false) 19 | private UserRepository2 userRepository2; 20 | 21 | @Override 22 | public List getUserList() { 23 | return userRepository.findAll(); 24 | } 25 | 26 | @Override 27 | public List getUserList2() { 28 | return userRepository2.findAll(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /spring-boot-multi-datasource/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | master: 4 | enable: true 5 | driver-class-name: com.mysql.cj.jdbc.Driver 6 | url: jdbc:mysql://127.0.0.1:3306/ds01?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true 7 | username: root 8 | password: 123456 9 | slave: 10 | enable: true 11 | driver-class-name: com.mysql.cj.jdbc.Driver 12 | url: jdbc:mysql://127.0.0.1:3306/ds02?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true 13 | username: root 14 | password: 123456 15 | jpa: 16 | hibernate: 17 | ddl-auto: update 18 | show-sql: true 19 | properties: 20 | database-platform: org.hibernate.dialect.MySQLDialect 21 | -------------------------------------------------------------------------------- /spring-boot-multi-datasource/src/test/java/springboottest/SpringBootTestApplicationTests.java: -------------------------------------------------------------------------------- 1 | package springboottest; 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 SpringBootTestApplicationTests { 11 | @Test 12 | public void contextLoads() { 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /spring-boot-mybatis-thymeleaf-crud/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.2.RELEASE 9 | 10 | 11 | com.example 12 | springboottest 13 | 0.0.1-SNAPSHOT 14 | springboottest 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 2.0.3-beta1 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-thymeleaf 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-web 30 | 31 | 32 | org.mybatis.spring.boot 33 | mybatis-spring-boot-starter 34 | 2.0.0 35 | 36 | 37 | mysql 38 | mysql-connector-java 39 | runtime 40 | 41 | 42 | 43 | 44 | tk.mybatis 45 | mapper-spring-boot-starter 46 | ${mapper.starter.version} 47 | 48 | 49 | 50 | org.springframework.boot 51 | spring-boot-starter-test 52 | test 53 | 54 | 55 | 56 | 57 | 58 | 59 | org.springframework.boot 60 | spring-boot-maven-plugin 61 | 62 | 63 | 64 | org.mybatis.generator 65 | mybatis-generator-maven-plugin 66 | 1.3.2 67 | 68 | ${basedir}/src/main/resources/generator/generatorConfig.xml 69 | true 70 | true 71 | 72 | 73 | 74 | mysql 75 | mysql-connector-java 76 | ${mysql.version} 77 | 78 | 79 | tk.mybatis 80 | mapper-generator 81 | 1.0.0 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /spring-boot-mybatis-thymeleaf-crud/src/main/java/springboottest/SpringboottestApplication.java: -------------------------------------------------------------------------------- 1 | package springboottest; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.boot.CommandLineRunner; 6 | import org.springframework.boot.SpringApplication; 7 | import org.springframework.boot.autoconfigure.SpringBootApplication; 8 | import tk.mybatis.spring.annotation.MapperScan; 9 | 10 | @SpringBootApplication 11 | @MapperScan(basePackages = "springboottest.mapper") 12 | public class SpringboottestApplication implements CommandLineRunner { 13 | private Logger logger = LoggerFactory.getLogger(SpringboottestApplication.class); 14 | 15 | public static void main(String[] args) { 16 | SpringApplication.run(SpringboottestApplication.class, args); 17 | } 18 | 19 | @Override 20 | public void run(String... args) throws Exception { 21 | logger.info("服务启动完成!"); 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /spring-boot-mybatis-thymeleaf-crud/src/main/java/springboottest/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package springboottest.controller; 2 | 3 | import springboottest.model.User; 4 | import springboottest.service.UserService; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.ui.Model; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | 9 | import javax.annotation.Resource; 10 | import java.util.List; 11 | 12 | @Controller 13 | public class UserController { 14 | @Resource 15 | UserService userService; 16 | 17 | @RequestMapping("/") 18 | public String Index() { 19 | return "redirect:/list"; 20 | } 21 | 22 | @RequestMapping("/list") 23 | public String list(Model model) { 24 | List users = userService.getUserList(); 25 | model.addAttribute("users", users); 26 | return "user/list"; 27 | } 28 | 29 | @RequestMapping("/toAdd") 30 | public String toAdd() { 31 | return "user/userAdd"; 32 | } 33 | 34 | @RequestMapping("/add") 35 | public String add(User user) { 36 | userService.save(user); 37 | return "redirect:/list"; 38 | } 39 | 40 | @RequestMapping("/toEdit") 41 | public String toEdit(Model model, Long id) { 42 | User user = userService.findUserById(id); 43 | model.addAttribute("user", user); 44 | return "user/userEdit"; 45 | } 46 | 47 | @RequestMapping("/edit") 48 | public String edit(User user) { 49 | userService.edit(user); 50 | return "redirect:/list"; 51 | } 52 | 53 | @RequestMapping("/delete") 54 | public String delete(Long id) { 55 | userService.delete(id); 56 | return "redirect:/list"; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /spring-boot-mybatis-thymeleaf-crud/src/main/java/springboottest/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package springboottest.mapper; 2 | 3 | import springboottest.model.User; 4 | import springboottest.utils.MyMapper; 5 | 6 | public interface UserMapper extends MyMapper { 7 | } -------------------------------------------------------------------------------- /spring-boot-mybatis-thymeleaf-crud/src/main/java/springboottest/model/BaseEntity.java: -------------------------------------------------------------------------------- 1 | package springboottest.model; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.GeneratedValue; 5 | import javax.persistence.GenerationType; 6 | import javax.persistence.Id; 7 | 8 | public class BaseEntity { 9 | @Id 10 | @Column(name = "Id") 11 | @GeneratedValue(strategy = GenerationType.IDENTITY) 12 | private Integer id; 13 | 14 | public Integer getId() { 15 | return id; 16 | } 17 | 18 | public void setId(Integer id) { 19 | this.id = id; 20 | } 21 | } -------------------------------------------------------------------------------- /spring-boot-mybatis-thymeleaf-crud/src/main/java/springboottest/model/User.java: -------------------------------------------------------------------------------- 1 | package springboottest.model; 2 | 3 | public class User extends BaseEntity { 4 | private String username; 5 | private String password; 6 | private Integer age; 7 | 8 | public String getUsername() { 9 | return username; 10 | } 11 | 12 | public void setUsername(String username) { 13 | this.username = username; 14 | } 15 | 16 | public String getPassword() { 17 | return password; 18 | } 19 | 20 | public void setPassword(String password) { 21 | this.password = password; 22 | } 23 | 24 | public Integer getAge() { 25 | return age; 26 | } 27 | 28 | public void setAge(Integer age) { 29 | this.age = age; 30 | } 31 | } -------------------------------------------------------------------------------- /spring-boot-mybatis-thymeleaf-crud/src/main/java/springboottest/service/UserService.java: -------------------------------------------------------------------------------- 1 | package springboottest.service; 2 | 3 | import springboottest.model.User; 4 | 5 | import java.util.List; 6 | 7 | public interface UserService { 8 | List getUserList(); 9 | 10 | User findUserById(long id); 11 | 12 | void save(User user); 13 | 14 | void edit(User user); 15 | 16 | void delete(long id); 17 | } -------------------------------------------------------------------------------- /spring-boot-mybatis-thymeleaf-crud/src/main/java/springboottest/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package springboottest.service.impl; 2 | 3 | import springboottest.model.User; 4 | import springboottest.mapper.UserMapper; 5 | import springboottest.service.UserService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import java.util.List; 10 | 11 | @Service 12 | public class UserServiceImpl implements UserService { 13 | @Autowired 14 | private UserMapper userMapper; 15 | 16 | @Override 17 | public List getUserList() { 18 | return userMapper.selectAll(); 19 | } 20 | 21 | @Override 22 | public User findUserById(long id) { 23 | return userMapper.selectByPrimaryKey(id); 24 | } 25 | 26 | @Override 27 | public void save(User user) { 28 | userMapper.insert(user); 29 | } 30 | 31 | @Override 32 | public void edit(User user) { 33 | userMapper.updateByPrimaryKey(user); 34 | } 35 | 36 | @Override 37 | public void delete(long id) { 38 | userMapper.deleteByPrimaryKey(id); 39 | } 40 | } -------------------------------------------------------------------------------- /spring-boot-mybatis-thymeleaf-crud/src/main/java/springboottest/utils/MyMapper.java: -------------------------------------------------------------------------------- 1 | package springboottest.utils; 2 | 3 | import tk.mybatis.mapper.common.Mapper; 4 | import tk.mybatis.mapper.common.MySqlMapper; 5 | 6 | public interface MyMapper extends Mapper, MySqlMapper { 7 | //特别注意,该接口不能被扫描到,否则会出错 8 | } -------------------------------------------------------------------------------- /spring-boot-mybatis-thymeleaf-crud/src/main/resources/application-dev.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 2 | spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true 3 | spring.datasource.username=root 4 | spring.datasource.password=123456 -------------------------------------------------------------------------------- /spring-boot-mybatis-thymeleaf-crud/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 9090 3 | 4 | spring: 5 | datasource: 6 | driver-class-name: com.mysql.cj.jdbc.Driver 7 | url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true 8 | username: root 9 | password: 123456 10 | thymeleaf: 11 | cache: false 12 | 13 | mybatis: 14 | type-aliases-package: springboottest.model 15 | mapper-locations: classpath:mapper/*.xml 16 | 17 | mapper: 18 | mappers: springboottest.utils.MyMapper 19 | not-empty: false 20 | identity: MYSQL -------------------------------------------------------------------------------- /spring-boot-mybatis-thymeleaf-crud/src/main/resources/generator/generatorConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 29 | 30 | 31 | 32 |
33 |
34 |
-------------------------------------------------------------------------------- /spring-boot-mybatis-thymeleaf-crud/src/main/resources/mapper/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /spring-boot-mybatis-thymeleaf-crud/src/main/resources/templates/user/list.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | userList 6 | 7 | 8 | 9 |
10 |

用户列表

11 |

12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 |
#User NamePasswordAgeEditDelete
1neoOtto6editdelete
35 |
36 |
37 |
38 | add 39 |
40 |
41 | 42 | -------------------------------------------------------------------------------- /spring-boot-mybatis-thymeleaf-crud/src/main/resources/templates/user/userAdd.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | user 6 | 7 | 8 | 9 |
10 |

添加用户

11 |

12 |
13 |
14 |
15 | 16 |
17 | 18 |
19 |
20 |
21 | 22 |
23 | 24 |
25 |
26 |
27 | 28 |
29 | 30 |
31 |
32 |
33 |
34 | 35 |       36 | 37 |
38 | 39 |
40 |
41 |
42 | 43 | -------------------------------------------------------------------------------- /spring-boot-mybatis-thymeleaf-crud/src/main/resources/templates/user/userEdit.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | user 6 | 7 | 8 | 9 |
10 |

修改用户

11 |

12 |
13 |
14 | 15 |
16 | 17 |
18 | 20 |
21 |
22 |
23 | 24 |
25 | 27 |
28 |
29 |
30 | 31 |
32 | 33 |
34 |
35 |
36 |
37 | 38 |       39 | Back 40 |
41 | 42 |
43 |
44 |
45 | 46 | -------------------------------------------------------------------------------- /spring-boot-mybatis-thymeleaf-crud/src/test/java/springboottest/SpringboottestApplicationTests.java: -------------------------------------------------------------------------------- 1 | package springboottest; 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 SpringboottestApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | 18 | -------------------------------------------------------------------------------- /spring-boot-swagger/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.example 12 | spring-boot-swagger 13 | 0.0.1-SNAPSHOT 14 | spring-boot-swagger 15 | Swagger demo for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-web 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-test 30 | test 31 | 32 | 33 | 34 | 35 | io.springfox 36 | springfox-swagger2 37 | 2.7.0 38 | 39 | 40 | io.springfox 41 | springfox-swagger-ui 42 | 2.7.0 43 | 44 | 45 | 46 | 47 | 48 | 49 | org.springframework.boot 50 | spring-boot-maven-plugin 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /spring-boot-swagger/src/main/java/com/example/springbootswagger/SpringBootSwaggerApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootswagger; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootSwaggerApplication { 8 | public static void main(String[] args) { 9 | SpringApplication.run(SpringBootSwaggerApplication.class, args); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /spring-boot-swagger/src/main/java/com/example/springbootswagger/config/Swagger2Config.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootswagger.config; 2 | 3 | import io.swagger.annotations.ApiOperation; 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.boot.context.properties.ConfigurationProperties; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.web.bind.annotation.RequestMethod; 10 | import springfox.documentation.builders.*; 11 | import springfox.documentation.schema.ModelRef; 12 | import springfox.documentation.service.ApiInfo; 13 | import springfox.documentation.service.Contact; 14 | import springfox.documentation.service.Parameter; 15 | import springfox.documentation.service.ResponseMessage; 16 | import springfox.documentation.spi.DocumentationType; 17 | import springfox.documentation.spring.web.plugins.Docket; 18 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 19 | 20 | import java.time.LocalDate; 21 | import java.util.ArrayList; 22 | import java.util.List; 23 | 24 | /** 25 | * 对Swagger2的配置信息 26 | * 27 | * @author wander 28 | */ 29 | @Configuration 30 | @EnableSwagger2 31 | @ConfigurationProperties(prefix = "swagger") 32 | public class Swagger2Config { 33 | private String title; 34 | private String desc; 35 | private String version; 36 | private String termsOfServiceUrl; 37 | private String license; 38 | private String licenseUrl; 39 | private String basePackage; 40 | private String groupName; 41 | private String contactName; 42 | private String contactUrl; 43 | private String contactEmail; 44 | 45 | private ApiInfo apiInfo() { 46 | return new ApiInfoBuilder().title(title).description(desc).version(version).termsOfServiceUrl(termsOfServiceUrl) 47 | .licenseUrl(licenseUrl).license(license).contact(new Contact(contactName, contactUrl, contactEmail)) 48 | .build(); 49 | } 50 | 51 | @Bean 52 | public Docket swaggerApi() { 53 | //==================== 需要的参数 START ==================== 54 | List pars = new ArrayList<>(); 55 | ParameterBuilder token = new ParameterBuilder(); 56 | token.name("token").description("token").modelRef(new ModelRef("string")).parameterType("header").required(false).build(); 57 | pars.add(token.build()); 58 | //==================== 需要的参数 END ==================== 59 | return new Docket(DocumentationType.SWAGGER_2) 60 | .apiInfo(apiInfo()) 61 | .groupName(groupName) 62 | .globalOperationParameters(pars) // 全局参数 63 | .directModelSubstitute(LocalDate.class, String.class) 64 | .genericModelSubstitutes(ResponseEntity.class) 65 | .useDefaultResponseMessages(false) 66 | .globalResponseMessage(RequestMethod.POST, customerResponseMessage()) 67 | .globalResponseMessage(RequestMethod.GET, customerResponseMessage()) 68 | .forCodeGeneration(true).select() 69 | .apis(RequestHandlerSelectors.basePackage(basePackage)) 70 | .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any()) 71 | .build(); 72 | } 73 | 74 | private List customerResponseMessage() { 75 | List list = new ArrayList<>(); 76 | list.add(new ResponseMessageBuilder().code(200).message("请求成功").build()); 77 | list.add(new ResponseMessageBuilder().code(201).message("资源创建成功").build()); 78 | list.add(new ResponseMessageBuilder().code(204).message("服务器成功处理了请求,但不需要返回任何实体内容").build()); 79 | list.add(new ResponseMessageBuilder().code(400).message("请求失败,具体查看返回业务状态码与对应消息").build()); 80 | list.add(new ResponseMessageBuilder().code(401).message("请求失败,未经过身份认证").build()); 81 | list.add(new ResponseMessageBuilder().code(405).message("请求方法不支持").build()); 82 | list.add(new ResponseMessageBuilder().code(415).message("请求媒体类型不支持").build()); 83 | list.add(new ResponseMessageBuilder().code(500).message("服务器遇到了一个未曾预料的状况,导致了它无法完成对请求的处理").build()); 84 | list.add(new ResponseMessageBuilder().code(503).message("服务器当前无法处理请求,这个状况是临时的,并且将在一段时间以后恢复").build()); 85 | return list; 86 | } 87 | 88 | public void setTitle(String title) { 89 | this.title = title; 90 | } 91 | 92 | public void setDesc(String desc) { 93 | this.desc = desc; 94 | } 95 | 96 | public void setVersion(String version) { 97 | this.version = version; 98 | } 99 | 100 | public void setTermsOfServiceUrl(String termsOfServiceUrl) { 101 | this.termsOfServiceUrl = termsOfServiceUrl; 102 | } 103 | 104 | public void setLicense(String license) { 105 | this.license = license; 106 | } 107 | 108 | public void setLicenseUrl(String licenseUrl) { 109 | this.licenseUrl = licenseUrl; 110 | } 111 | 112 | public void setBasePackage(String basePackage) { 113 | this.basePackage = basePackage; 114 | } 115 | 116 | public void setGroupName(String groupName) { 117 | this.groupName = groupName; 118 | } 119 | 120 | public void setContactName(String contactName) { 121 | this.contactName = contactName; 122 | } 123 | 124 | public void setContactUrl(String contactUrl) { 125 | this.contactUrl = contactUrl; 126 | } 127 | 128 | public void setContactEmail(String contactEmail) { 129 | this.contactEmail = contactEmail; 130 | } 131 | } -------------------------------------------------------------------------------- /spring-boot-swagger/src/main/java/com/example/springbootswagger/controller/TestController.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootswagger.controller; 2 | 3 | import com.example.springbootswagger.model.vo.User; 4 | import com.fasterxml.jackson.databind.util.JSONPObject; 5 | import io.swagger.annotations.Api; 6 | import io.swagger.annotations.ApiImplicitParam; 7 | import io.swagger.annotations.ApiImplicitParams; 8 | import io.swagger.annotations.ApiOperation; 9 | import org.springframework.http.MediaType; 10 | import org.springframework.http.ResponseEntity; 11 | import org.springframework.util.StringUtils; 12 | import org.springframework.web.bind.annotation.*; 13 | 14 | import javax.print.attribute.standard.Media; 15 | 16 | /** 17 | * 测试接口 - controller类 18 | * 19 | * @author wander 20 | */ 21 | @Api(tags = "测试接口") 22 | @RestController 23 | @RequestMapping(value = "tests") 24 | public class TestController { 25 | @ApiOperation(value = "GET请求参数的测试", notes = "@RequestParam用来获得静态的URL请求参数;@PathVariable用来获得动态的URL请求入参") 26 | @ApiImplicitParams({ 27 | @ApiImplicitParam(name = "id", value = "编号", required = true, paramType = "path"), 28 | @ApiImplicitParam(name = "name", value = "名称", required = false, paramType = "query") 29 | }) 30 | @GetMapping(value = "{id}") 31 | public ResponseEntity testGet(@PathVariable(value = "id") Long id, @RequestParam(required = false) String name) { 32 | if (StringUtils.isEmpty(name)) 33 | name = "wander"; 34 | 35 | return ResponseEntity.ok("编号: " + id + "\t名称: " + name); 36 | } 37 | 38 | @ApiOperation(value = "POST请求参数的测试", notes = "@RequestBody用来获取post请求参数", 39 | produces = MediaType.APPLICATION_JSON_UTF8_VALUE) 40 | @PostMapping 41 | public ResponseEntity testPost(@RequestBody User user) { 42 | return ResponseEntity.ok(user); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /spring-boot-swagger/src/main/java/com/example/springbootswagger/model/vo/User.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootswagger.model.vo; 2 | 3 | import io.swagger.annotations.ApiModel; 4 | import io.swagger.annotations.ApiModelProperty; 5 | 6 | @ApiModel(value = "用户信息") 7 | public class User { 8 | @ApiModelProperty(value = "姓名", required = true, position = 1) 9 | private String name; 10 | @ApiModelProperty(value = "年龄", required = false, position = 2) 11 | private Integer age; 12 | 13 | public String getName() { 14 | return name; 15 | } 16 | 17 | public void setName(String name) { 18 | this.name = name; 19 | } 20 | 21 | public Integer getAge() { 22 | return age; 23 | } 24 | 25 | public void setAge(Integer age) { 26 | this.age = age; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /spring-boot-swagger/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | # swagger 配置 2 | swagger: 3 | title: API示例 4 | desc: 基于springBoot编写的RESful-API 5 | version: 0.0.1.SNAPSHOT 6 | termsOfServiceUrl: javascript:void(0) 7 | license: Apache 2.0 8 | licenseUrl: http://www.apache.org/licenses/LICENSE-2.0.html 9 | basePackage: com.example.springbootswagger.controller 10 | groupName: 默认API示例分组 11 | contactName: wander 12 | contactUrl: https://github.com/wander-chu 13 | contactEmail: 362207958@qq.com -------------------------------------------------------------------------------- /spring-boot-swagger/src/test/java/com/example/springbootswagger/SpringBootSwaggerApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springbootswagger; 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 SpringBootSwaggerApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-cloud-demo/demo-consumer/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.demo 12 | demo-consumer 13 | 0.0.1-SNAPSHOT 14 | demo-consumer 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | Greenwich.SR1 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | org.springframework.cloud 29 | spring-cloud-starter-netflix-eureka-client 30 | 31 | 32 | org.springframework.cloud 33 | spring-cloud-starter-openfeign 34 | 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-test 39 | test 40 | 41 | 42 | 43 | 44 | 45 | 46 | org.springframework.cloud 47 | spring-cloud-dependencies 48 | ${spring-cloud.version} 49 | pom 50 | import 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | org.springframework.boot 59 | spring-boot-maven-plugin 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /spring-cloud-demo/demo-consumer/src/main/java/com/demo/democonsumer/DemoConsumerApplication.java: -------------------------------------------------------------------------------- 1 | package com.demo.democonsumer; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 6 | import org.springframework.cloud.openfeign.EnableFeignClients; 7 | 8 | @SpringBootApplication 9 | @EnableEurekaClient 10 | @EnableFeignClients 11 | public class DemoConsumerApplication { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(DemoConsumerApplication.class, args); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /spring-cloud-demo/demo-consumer/src/main/java/com/demo/democonsumer/client/InfoClient.java: -------------------------------------------------------------------------------- 1 | package com.demo.democonsumer.client; 2 | 3 | import com.demo.democonsumer.client.config.MyFeignConfig; 4 | import com.demo.democonsumer.client.fallback.InfoFallBack; 5 | import org.springframework.cloud.openfeign.FeignClient; 6 | import org.springframework.web.bind.annotation.PathVariable; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RequestMethod; 9 | 10 | /** 11 | * FeignClient 配置说明 12 | * 1.name:被调用的服务应用名称 13 | * 2.fallback: InfoFallBack作为熔断实现,当请求provider失败时调用其中的方法 14 | * 3.configuration: feign配置 15 | */ 16 | @FeignClient(name = "provider", fallback = InfoFallBack.class, configuration = MyFeignConfig.class) 17 | public interface InfoClient { 18 | 19 | @RequestMapping(value = "/info", method = RequestMethod.GET) 20 | String getDefaultInfo(); 21 | 22 | @RequestMapping(value = "/info/{name}", method = RequestMethod.POST) 23 | String getInfo(@PathVariable("name") String name); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /spring-cloud-demo/demo-consumer/src/main/java/com/demo/democonsumer/client/config/MyFeignConfig.java: -------------------------------------------------------------------------------- 1 | package com.demo.democonsumer.client.config; 2 | 3 | import feign.Logger; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | 7 | @Configuration 8 | public class MyFeignConfig { 9 | 10 | /** 11 | * Feign打印日志等级 12 | * 13 | * @return 14 | */ 15 | @Bean 16 | public Logger.Level feignLoggerLevel() { 17 | return Logger.Level.FULL; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /spring-cloud-demo/demo-consumer/src/main/java/com/demo/democonsumer/client/fallback/InfoFallBack.java: -------------------------------------------------------------------------------- 1 | package com.demo.democonsumer.client.fallback; 2 | 3 | import com.demo.democonsumer.client.InfoClient; 4 | import org.springframework.stereotype.Component; 5 | import org.springframework.web.bind.annotation.PathVariable; 6 | 7 | @Component 8 | public class InfoFallBack implements InfoClient { 9 | 10 | @Override 11 | public String getDefaultInfo() { 12 | return "fallback info"; 13 | } 14 | 15 | @Override 16 | public String getInfo(String name) { 17 | return "fallback default info"; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /spring-cloud-demo/demo-consumer/src/main/java/com/demo/democonsumer/controller/ConsumerController.java: -------------------------------------------------------------------------------- 1 | package com.demo.democonsumer.controller; 2 | 3 | import com.demo.democonsumer.client.InfoClient; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.web.bind.annotation.PathVariable; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RequestMethod; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | @RestController 11 | public class ConsumerController { 12 | 13 | @Autowired 14 | private InfoClient infoClient; 15 | 16 | @RequestMapping(value = "/call/info", method = RequestMethod.GET) 17 | public String consumerInfo(){ 18 | return infoClient.getDefaultInfo(); 19 | } 20 | 21 | @RequestMapping(value = "/call/info/{name}", method = RequestMethod.GET) 22 | public String consumerInfo(@PathVariable String name){ 23 | return infoClient.getInfo(name); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /spring-cloud-demo/demo-consumer/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wander-chu/java-microservice-demo/7fdde02d32aaec3d117619afbd085ff1462171b6/spring-cloud-demo/demo-consumer/src/main/resources/application.properties -------------------------------------------------------------------------------- /spring-cloud-demo/demo-consumer/src/test/java/com/demo/democonsumer/DemoConsumerApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.demo.democonsumer; 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 DemoConsumerApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-cloud-demo/demo-eureka/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.demo 12 | demo-eureka 13 | 0.0.1-SNAPSHOT 14 | demo-eureka 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | Greenwich.SR1 20 | 21 | 22 | 23 | 24 | org.springframework.cloud 25 | spring-cloud-starter-netflix-eureka-server 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-test 31 | test 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.springframework.cloud 39 | spring-cloud-dependencies 40 | ${spring-cloud.version} 41 | pom 42 | import 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.springframework.boot 51 | spring-boot-maven-plugin 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /spring-cloud-demo/demo-eureka/src/main/java/com/demo/demoeureka/DemoEurekaApplication.java: -------------------------------------------------------------------------------- 1 | package com.demo.demoeureka; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 | 7 | @SpringBootApplication 8 | @EnableEurekaServer 9 | public class DemoEurekaApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(DemoEurekaApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /spring-cloud-demo/demo-eureka/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wander-chu/java-microservice-demo/7fdde02d32aaec3d117619afbd085ff1462171b6/spring-cloud-demo/demo-eureka/src/main/resources/application.properties -------------------------------------------------------------------------------- /spring-cloud-demo/demo-eureka/src/test/java/com/demo/demoeureka/DemoEurekaApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.demo.demoeureka; 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 DemoEurekaApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-cloud-demo/demo-provider/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.demo 12 | demo-provider 13 | 0.0.1-SNAPSHOT 14 | demo-provider 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | Greenwich.SR1 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | org.springframework.cloud 29 | spring-cloud-starter-netflix-eureka-client 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-test 35 | test 36 | 37 | 38 | 39 | 40 | 41 | 42 | org.springframework.cloud 43 | spring-cloud-dependencies 44 | ${spring-cloud.version} 45 | pom 46 | import 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | org.springframework.boot 55 | spring-boot-maven-plugin 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /spring-cloud-demo/demo-provider/src/main/java/com/demo/demoprovider/DemoProviderApplication.java: -------------------------------------------------------------------------------- 1 | package com.demo.demoprovider; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | @SpringBootApplication 8 | @EnableDiscoveryClient 9 | public class DemoProviderApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(DemoProviderApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /spring-cloud-demo/demo-provider/src/main/java/com/demo/demoprovider/controller/MyController.java: -------------------------------------------------------------------------------- 1 | package com.demo.demoprovider.controller; 2 | 3 | import org.springframework.web.bind.annotation.PathVariable; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RequestMethod; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | @RestController 9 | public class MyController { 10 | 11 | @RequestMapping(value = "/info", method = RequestMethod.GET) 12 | public String getDefaultInfo() { 13 | try { 14 | //休眠2秒,测试超时服务熔断[直接关闭服务提供者亦可] 15 | Thread.sleep(2000); 16 | } catch (InterruptedException e) { 17 | e.printStackTrace(); 18 | } 19 | 20 | return "Hello provider"; 21 | } 22 | 23 | @RequestMapping(value = "/info/{name}", method = RequestMethod.POST) 24 | public String getInfo(@PathVariable String name) { 25 | return "Hello " + name; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /spring-cloud-demo/demo-provider/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wander-chu/java-microservice-demo/7fdde02d32aaec3d117619afbd085ff1462171b6/spring-cloud-demo/demo-provider/src/main/resources/application.properties -------------------------------------------------------------------------------- /spring-cloud-demo/demo-provider/src/test/java/com/demo/demoprovider/DemoProviderApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.demo.demoprovider; 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 DemoProviderApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-cloud-demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.demo 8 | spring-cloud-demo 9 | 1.0-SNAPSHOT 10 | 11 | --------------------------------------------------------------------------------