Spring Boot 2.0 入门指南
0x01 什么是 Spring Boot?
Spring Boot 是用来简化 Spring 应用初始搭建以及开发过程的全新框架,被认为是 Spring MVC 的“接班人”,和微服务紧密联系在一起。
0x02 为什么学习 Spring Boot?
微服务是如今各大企业都开始火热使用的技术,而 Spring Boot 是学习 Spring Cloud 的基础
0x03 Spring Boot 有什么特点?
1. 化繁为简
2. 备受关注,是下一代框架
3. 微服务的入门级微框架
0x04 Spring Boot 的目标?
为所有的 Spring 开发提供一个更快速、更广泛的入门体验
但是当需求和默认配置偏离时,请尽快放弃使用 Spring Boot.
提供一系列的非功能性的特点,是大类项目(如嵌入式服务器,安全,标准,健康检查,和外部配置)。
绝对不生成代码,也不需要 XML 配置。
0x05 准备环境
系统要求
Spring Boot 2.0.0.BUILD-SNAPSHOT 需要JDK1.8 以上和 Spring Framework 5.0.2.Release 以上版本
构建支持 Maven3.2+ 和 Gradle4
Servlet 容器
Tips: 也可以将 Spring Boot 应用程序部署到任何兼容 Servlet 3 容器中。
技能要求
熟悉 Maven 项目构建
熟悉 Spring 注解
熟悉 RESful API 的理念
IDE
本节课程使用 Intellij Idea 作为开发工具
0x06 创建我们的第一个应用程序
接下来我们将会创建一个带有 Spring-Data-JPA 功能的 Spring-Boot Sample。
1. 打开我们的 Intellij IDEA,选择 ‘Create New Project’
2. 选择 Spring Initializr 来帮助我们快速创建 Spring Boot 程序,JDK 最低 1.8,Intializr Service URL 保持默认。
3. 输入以下项目配置信息
4. 勾选 Web 模块
5.SQL 模块分类中勾选 MySQL 和 JPA 模块
6. 接下来工程信息一切保持默认即可
7. 修改 POM.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion><span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">></span>com.xingyun<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">></span>spring-boot-with-data-jpa-sample<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">></span>0.0.1-SNAPSHOT<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">packaging</span><span style="color: rgba(0, 0, 255, 1)">></span>jar<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">packaging</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">></span>spring-boot-with-data-jpa-sample<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">description</span><span style="color: rgba(0, 0, 255, 1)">></span>Demo project for Spring Boot<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">description</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">parent</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">></span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">></span>spring-boot-starter-parent<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">></span>2.0.0.RELEASE<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">relativePath</span><span style="color: rgba(0, 0, 255, 1)">/></span> <span style="color: rgba(0, 128, 0, 1)"><!--</span><span style="color: rgba(0, 128, 0, 1)"> lookup parent from repository </span><span style="color: rgba(0, 128, 0, 1)">--></span> <span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">parent</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">properties</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">project.build.sourceEncoding</span><span style="color: rgba(0, 0, 255, 1)">></span>UTF-8<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">project.build.sourceEncoding</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">project.reporting.outputEncoding</span><span style="color: rgba(0, 0, 255, 1)">></span>UTF-8<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">project.reporting.outputEncoding</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">java.version</span><span style="color: rgba(0, 0, 255, 1)">></span>1.8<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">java.version</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">properties</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">dependencies</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">></span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">></span>spring-boot-configuration-processor<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">optional</span><span style="color: rgba(0, 0, 255, 1)">></span>true<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">optional</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">></span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">></span>spring-boot-starter-data-jpa<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">></span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">></span>spring-boot-starter-web<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">></span>mysql<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">></span>mysql-connector-java<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">scope</span><span style="color: rgba(0, 0, 255, 1)">></span>runtime<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">scope</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">></span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">></span>spring-boot-starter-test<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">scope</span><span style="color: rgba(0, 0, 255, 1)">></span>test<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">scope</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">dependencies</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">build</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">plugins</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">plugin</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">></span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">></span>spring-boot-maven-plugin<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">plugin</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">plugins</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">build</span><span style="color: rgba(0, 0, 255, 1)">></span>
</project>
8. 默认的系统配置文件是 application.properties
# 配置应用程序的端口
server.port=8080
#配置应用程序的上下文
server.servlet.context-path=/
Tips: 我们可以通过这种文件来配置 web 项目的全局上下文路径和端口号等,但是这里我们需要注释或者删除掉他们,因为我们有一个更好的方式来实现他们。
9. 配置我们的 yml 文件
总的配置文件,我们可以通过 active:dev|prod 激活我们的开发环境配置或者生产环境配置
application.yml
spring:
profiles:
active: dev
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/dbgirl
username: root
password:
jpa:
hibernate:
ddl-auto: create
dialect: MySQLDialect
show-sql: true
Spring 2.0 更新变动
开发环境配置文件
application-dev.yml
server:
port: 8081
servlet:
context-path: /
cupSize: B
age: 18
content: "cupSize=${cupSize},age=${age}"
book:
name: 第二行代码
price: 66.00
type: 安卓
生产环境配置文件
application-prod.yml
server:
port: 8086
servlet:
context-path: /
cupSize: B
age: 18
content: "cupSize=${cupSize},age=${age}"
book:
name: 第二行代码
price: 66.00
type: 安卓
10. 代码结构图如图所示
10.1 代码中获取配置文件中的属性
我们可以读取刚才开发环境下配置文件中的定义的常量属性通过下面的方法
@Value("${cupSize}") private String cupSize;@Value(</span>"${age}"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Integer age; @Value(</span>"${content}"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String content; @RequestMapping(value </span>= "/girl",method =<span style="color: rgba(0, 0, 0, 1)"> RequestMethod.GET) </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String configurationString(){ </span><span style="color: rgba(0, 0, 255, 1)">return</span> cupSize+"-----------------"+age+"-------------"+<span style="color: rgba(0, 0, 0, 1)">content; }</span></pre></code-box>
当然如果要配置的常量比较多的时候我们就不能使用上面的方法了,但是我们也有更好的方式实现
10.2 代码中获取配置文件中的属性实体
首先创建实体类 BookProperties.java
package com.xingyun.springbootwithdatajpasample.model;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties(prefix = "book")
public class BookProperties {</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String name; </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Double price; </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String type; </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getName() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> name; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setName(String name) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name; } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Double getPrice() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> price; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setPrice(Double price) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.price =<span style="color: rgba(0, 0, 0, 1)"> price; } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getType() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> type; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setType(String type) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.type =<span style="color: rgba(0, 0, 0, 1)"> type; }
}
Tips: 注意此文件中用到的两个注解不可省略必须有才行
调用方法如下所示:
@Autowired private BookProperties bookProperties;@RequestMapping(value </span>= "/book",method =<span style="color: rgba(0, 0, 0, 1)"> RequestMethod.GET) </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String bookMethod(){ </span><span style="color: rgba(0, 0, 255, 1)">return</span> "---------------------"+bookProperties.getName()+bookProperties.getPrice()+<span style="color: rgba(0, 0, 0, 1)">bookProperties.getType(); }</span></pre></code-box>
10.3 默认的单个 URL 映射
@RequestMapping(value = "/",method = RequestMethod.GET) public String home(){ return "Hello Home Page"; }@RequestMapping(value </span>= "/hello",method =<span style="color: rgba(0, 0, 0, 1)"> RequestMethod.GET) </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String say(){ </span><span style="color: rgba(0, 0, 255, 1)">return</span> "Hello Spring Boot"<span style="color: rgba(0, 0, 0, 1)">; }</span></pre></code-box>
10.4 多个 URL 映射
@RequestMapping(value = {"/api","/API"},method = RequestMethod.GET)
public String api(){
return "Hello API";
}
10.5 Post 方式访问
@RequestMapping(value = "/post",method = RequestMethod.POST) public String PostMethod(){ return "Hello Post Page"; }
10.6 传统风格的 URL
@RequestMapping(value = "/url/b",method = RequestMethod.GET) public String urlWithQuestion(@RequestParam(value = "id",required = false,defaultValue = "0")Integer uid){ return "Hello URL -----/url/b?id=***------id="+uid; }
访问请求:
http://127.0.0.1:8081/url/b?id=18
10.7 Restful 风格的 URL
@RequestMapping(value = "/url/a/{id}",method = RequestMethod.GET) public String urlWithParams(@PathVariable("id")Integer uid){ return "Hello URL -----url/a/***------id="+uid; }
访问请求:
http://127.0.0.1:8081/url/a/18
10.8 组合注解
上面我们应该已经发现,既要配置 GET/POST 又要配置映射路径很麻烦,因此我们今后可以用组合注解
@GetMapping(value = "/url/c") public String urlWithQuestion2(@RequestParam(value = "id",required = false,defaultValue = "0")Integer uid){ return "Hello URL -----/url/c?id=***------id="+uid; }
访问请求:
http://127.0.0.1:8081/url/c?id=18
10.9 @JsonFormat Date 注解
关于 JsonFormat 时间如果希望格式化时间为 12 小时制的,则使用hh🇲🇲ss 如果希望格式化时间为 24 小时制的,则使用HH🇲🇲ss
@JsonFormat(pattern = "yyyy-mm-dd HH🇸🇸mm",locale = "zh",timezone = "GMT+8")//2018-40-07 16:24:40 Date publishedTime;//发表时间 @JsonFormat(pattern = "yyyy-mm-dd hh🇸🇸mm a",locale = "zh",timezone = "GMT+8")//2018-40-07 04:24:40 下午 Date updatedTime;//修改时间
10.10 使用 Spring-Data-JPA 组件结合 MySQL 实现数据库的增删改查操作
首先我们需要定义一个实体类 Girl.java
package com.xingyun.springbootwithdatajpasample.model;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;@Entity
public class Girl {@Id @GeneratedValue </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Integer id; </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String girlName; </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Integer girlAge; </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Girl() { } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Integer getId() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> id; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setId(Integer id) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.id =<span style="color: rgba(0, 0, 0, 1)"> id; } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getGirlName() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> girlName; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setGirlName(String girlName) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.girlName =<span style="color: rgba(0, 0, 0, 1)"> girlName; } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Integer getGirlAge() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> girlAge; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setGirlAge(Integer girlAge) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.girlAge =<span style="color: rgba(0, 0, 0, 1)"> girlAge; }
}
Tips: 特别注意,几个注解不能少,引入的包路径千万别导错包了,不然会报错。
然后需要创建一个接口类 GirlRepository.java
package com.xingyun.springbootwithdatajpasample.mmInterface;import com.xingyun.springbootwithdatajpasample.model.Girl;
import org.springframework.data.jpa.repository.JpaRepository;import java.util.List;
public interface GirlRepository extends JpaRepository<Girl,Integer>{
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">自定义接口</span> <span style="color: rgba(0, 0, 255, 1)">public</span> List<Girl><span style="color: rgba(0, 0, 0, 1)"> findByGirlAge(Integer girlAge);
}
Tips: 我们需要继承 JpaRepository<Girl,Integer>, 这样 Spring-Data-JPA 就会帮我们实现基本的增删改查。
当然这默认的增删改查无法满足我们的实际业务需求,所以我们也可以在这里扩展我们实现的接口。
增删改查调用代码如下所示:
package com.xingyun.springbootwithdatajpasample.controller;import com.xingyun.springbootwithdatajpasample.mmInterface.GirlRepository;
import com.xingyun.springbootwithdatajpasample.model.Girl;
import com.xingyun.springbootwithdatajpasample.service.GirlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;
import java.util.Optional;@RestController
public class GirlController {@Autowired </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> GirlRepository girlRepository; @Autowired </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> GirlService girlService; </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 查询所有女生列表 * @Return * </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> @GetMapping(</span>"/girls"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">public</span> List<Girl><span style="color: rgba(0, 0, 0, 1)"> getGirlList(){ </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> girlRepository.findAll(); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 添加一个女生 * </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> @PostMapping(</span>"/girls"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">public</span> Girl girlAdd(@RequestParam("girlName") String girlName,@RequestParam("girlAge"<span style="color: rgba(0, 0, 0, 1)">) Integer girlAge){ Girl girl</span>=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Girl(); girl.setGirlName(girlName); girl.setGirlAge(girlAge); girlRepository.save(girl); </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> girl; } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 添加两个女生 * </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> @GetMapping(</span>"/girls/two"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String girlAddTwo(){ girlService.insertTwo(); </span><span style="color: rgba(0, 0, 255, 1)">return</span> "success"<span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 通过Id查询一个女生 * </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> @GetMapping(</span>"/girls/{id}"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">public</span> Optional<Girl> girlFindById(@PathVariable("id"<span style="color: rgba(0, 0, 0, 1)">) Integer uid){ </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> girlRepository.findById(uid); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 通过年龄查询一个女生 * </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> @GetMapping(</span>"/girls/age/{girlAge}"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">public</span> List<Girl> girlFindByAge(@PathVariable("girlAge"<span style="color: rgba(0, 0, 0, 1)">) Integer age){ </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> girlRepository.findByGirlAge(age); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 修改一个女生 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> @PostMapping(value</span>="/girls/{id}"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">public</span> Girl girlUpdate(@PathVariable("id") Integer uid,@RequestParam("girlName") String girlName,@RequestParam("girlAge"<span style="color: rgba(0, 0, 0, 1)">) Integer girlAge){ Girl girl</span>=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Girl(); girl.setId(uid); girl.setGirlName(girlName); girl.setGirlAge(girlAge); </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> girlRepository.save(girl); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 删除一个Id * </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> @DeleteMapping(value </span>= "/girls/{id}"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> girlDelete(@PathVariable("id"<span style="color: rgba(0, 0, 0, 1)">) Integer uid){ girlRepository.deleteById(uid); }
}
Tips: 这里要注意的一点是我这个版本使用的是 Spring-Boot 2.0.0.RELEASE 版本,更新后有个方法做了修改。
public List<Girl> girlFindById(@PathVariable("id")Integer uid){ return girlRepository.findById(uid); }
这个返回集合不再有效,需要改成下面这种:
@GetMapping("/girls/{id}") public Optional<Girl> girlFindById(@PathVariable("id")Integer uid){ return girlRepository.findById(uid); }
11. 可能出现的问题
在初次学习时候可能会出现一些常见的异常,可以移步去我的 CSDN 博客看这篇文章
细数 Spring Boot 中容易中招的那些坑
https://blog.csdn.net/hadues/article/details/79334355
@ConfigurationProperties(prefix = "xxx") 的值取出为空
https://blog.csdn.net/hadues/article/details/79123645
真正解决方案:java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
https://blog.csdn.net/hadues/article/details/79188793
12. 学习源码下载
使用前请在本地创建 MySQL 数据库 dbgirl
dbgirl.sql
-- phpMyAdmin SQL Dump -- version 4.7.4 -- https://www.phpmyadmin.net/ -- -- Host: 127.0.0.1 -- Generation Time: 2018-03-28 03:43:53 -- 服务器版本: 10.1.30-MariaDB -- PHP Version: 7.2.1SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";/!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT /;
/!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS /;
/!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION /;
/!40101 SET NAMES utf8mb4 /;--
-- Database:dbgirl
--
--
-- 表的结构girl
--CREATE TABLE
girl
(id
int(11) NOT NULL,girl_age
int(11) DEFAULT NULL,girl_name
varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;--
-- Indexes for dumped tables
----
-- Indexes for tablegirl
--
ALTER TABLEgirl
ADD PRIMARY KEY (id
);--
-- 在导出的表使用 AUTO_INCREMENT
----
-- 使用表 AUTO_INCREMENTgirl
--
ALTER TABLEgirl
MODIFYid
int(11) NOT NULL AUTO_INCREMENT;
COMMIT;/!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT /;
/!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS /;
/!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION /;
调用参考 readme.txt
使用前需启动 My SQL 数据库
MySQL
default username:root
default password:
1. http://127.0.0.1:8081/
-
http://127.0.0.1:8081/hello
-
http://127.0.0.1:8081/girl
4.http://127.0.0.1:8081/book
5.http://127.0.0.1:8081/api
http://127.0.0.1:8081/API
Tips: Get 请求,多映射
7.http://127.0.0.1:8081/api
Tips:Post 请求
8.http://127.0.0.1:8081/url/a/18
9.http://127.0.0.1:8081/url/b?id=18
10.http://127.0.0.1:8081/url/c?id=18
Tips: 组合注解 @GetMapping(value = "/url/c")
11.http://127.0.0.1:8081/girls/
Tips:Get 获取所有女生列表
12. http://127.0.0.1:8081/girls/
Tips: Post girlName girlAge 添加一个女生
13.http://127.0.0.1:8081/girls/two
Tips: 添加两个女生
14. 根据 Id 查询女生
http://127.0.0.1:8081/girls/1
15. 根据年龄查询女生
http://127.0.0.1:8081/girls/age/16
- http://127.0.0.1:8081/girls/1
Tips:girlName girlAge Post 根据 Id 修改一个女生
17.http://127.0.0.1:8081/girls/1
Tips:Delete 请求根据 id 删除一个女生
创建数据库 bean 时候切记不要导错包名
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
本课程所有源码下载地址:https://github.com/geekxingyun/JavaEE-Framework-Sample/tree/master/SpringBoot-Sample/spring-boot-with-data-jpa-sample
推荐学习免费视频教程地址:http://www.imooc.com/learn/767
__EOF__