SpringBoot 快速入门笔记
一、环境准备
配置好 java, maven , 并给 maven 设置国内镜像(阿里)
在 maven 安装目录/conf 下,找到 settings.xml,配置如下代码
<mirrors> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors>
编辑器: IDEA
二、用 IDEA 创建项目
打开 IDEA 新建项目,选择
Spring Initializr,勾选
Web依赖。
三、启动 SpringBoot 项目的三种方式
1. IDEA 启动
在 IDEA 中,找到有
@SpringBootApplication注解的类,右键,
run xxxApplication
或点击 IDEA 中的快捷按钮
2. maven命令直接启动
命令行进入项目目录,执行命令: mvn spring-boot:run
3. maven命令先编译成 jar
再启动
命令行进入项目目录,先编译程序执行: mvn install 然后 cd target 进入生成的 target 目录,看到 test-0.0.1-SNAPSHOT.jar java -jar test-0.0.1-SNAPSHOT.jar
启动时指定运行端口
java -jar -Dserver.port=9999 test-0.0.1-SNAPSHOT.jar
## 四、SpringBoot 配置
4.1 两种配置文件
SpringBoot 的配置文件有两种:
application.properties和
application.yml
两种文件效果一样,只是写法不同。
application.properties文件配置:
server.port=8080 server.servlet.context-path=/test
application.yml文件配置
server: port: 8081 servlet: context-path: /test
注意:冒号后面必须加个空格,不能写成port:8081,需要在 port: 和 8081 之间加空格
对比来看
application.yml文件写法更精简,建议使用。
4.2 属性配置
可在
application.yml配置文件里自定义配置信息并在项目中读取。
4.2.1 单个属性读取
配置信息
cupSize和
age
server: port: 8081 cupSize: B age: 20
在 Controller 中读取
@RestController public class HelloController { @Value("${cupSize}") private String cupSize; @Value("${age}") private Integer age; @RequestMapping(value = "/hello", method = RequestMethod.GET) public String say(){ return cupSize + age; } }
在 Controller 中通过 @Value(“${age}”) 注解读取配置文件中的属性
4.2.2 通过对象多属性一起读取
application.yml
server: port: 8081 girl: cupSize: B age: 20
新建类
GirlProperties.java
@Component @ConfigurationProperties(prefix = "girl") public class GirlProperties { private String cupSize; private Integer age; public String getCupSize() { return cupSize; } public void setCupSize(String cupSize) { this.cupSize = cupSize; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
Controller 中使用
@RestController public class HelloController { @Autowired private GirlProperties girl; @RequestMapping(value = "/hello", method = RequestMethod.GET) public String say(){ return girl.getCupSize() + girl.getAge(); } }
4.2.3 多环境配置
配置开发环境和生产环境
新建
application-dev.yml作为开发环境配置
server: port: 8082 girl: cupSize: F age: 24
新建
application-prod.yml作为生产环境配置
server: port: 8081 girl: cupSize: B age: 20
修改
application.yml, 配置为开发环境
spring: profiles: active: dev
如需配置为生产环境,将
active: dev改为
active: prod
spring: profiles: active: prod
五、Controller 的使用
注解 | 说明 |
---|---|
@Controller | 处理 http 请求 |
@RestController | Spring4 之后新加的注解,原来返回 json 需要 @ResponseBody 配合 @Controller |
@RequestMapping | 配置 url 映射 |
@PathVariable | 获取 url 中的数据 |
@RequestParam | 获取请求参数的值 |
@GetMapping | 组合注解 |
5.1 @RestController
@RestController public class HelloController { @Autowired private GirlProperties girl; @RequestMapping(value = "/hello", method = RequestMethod.GET) public String say(){ return girl.getCupSize() + girl.getAge(); } }
注:同时使用 @ResponseBody 和 @Controller 与单独使用
@RestController效果相同
5.2 @RequestMapping
5.2.1 给方法配置多个访问路径
给 value 配置多个路径的集合
@RequestMapping(value = {"/happy", "/hi"}, method = RequestMethod.GET) public String sayHi(){ return "happy or Hi"; }
访问
http://localhost:8082/hi或者
http://localhost:8082/happy均可进入方法
sayHi()
5.2.2 给类配置访问路径
@RestController @RequestMapping(value = "/girl") public class HelloController { @RequestMapping(value = {"/happy", "/hi"}, method = RequestMethod.GET) public String sayHi(){ return "happy or Hi"; } }
需要访问
http://localhost:8082/girl/hi
5.3 @PathVariable
@PathVariable用来获取 url 中的参数
@RequestMapping(value = "/go/{id}", method = RequestMethod.GET) public String go(@PathVariable("id") String id){ return "id: " + id; }
访问地址
http://localhost:8082/girl/go/123
id 也可以放前面,效果一样
@RequestMapping(value = "/{id}/go", method = RequestMethod.GET) public String go(@PathVariable("id") String id){ return "id: " + id; }
5.4 @RequstParam
获取请求参数的值
@RequestMapping(value = "/hei", method = RequestMethod.GET) public String getRequestParam(@RequestParam("id") String id){ return "id: " + id; }
访问地址:
http://localhost:8082/girl/hei?id=123
给参数加默认值
@RequestMapping(value = "/hei", method = RequestMethod.GET) public String getRequestParam(@RequestParam(value = "id", required = false, defaultValue = "0") String id){ return "id: " + id; }
当
id不传时默认是 0。
5.5 @GetMapping
@GetMapping(value = "/hei") public String getRequestParam(@RequestParam(value = "id", required = false, defaultValue = "0") String id){ return "id: " + id; }
简化
@RequestMapping的写法,还有
@PostMapping等。
六、数据库操作 JPA
JPA (Java Persistence API) 定义了一系列的对象持久化的标准,目前实现这一规范的产品有
Hibernate、
TopLink等。
6.1 配置引入 MySQL 和 JPA
修改
pom.xml文件添加
JPA和
MySQL依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies>
修改
application.yml文件,配置
JPA和
MySQL
spring: profiles: active: dev datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://0.0.0.0:3306/dbgirl username: root password: root jpa: hibernate: ddl-auto: update show-sql: true
注:url 中最后的 dbgirl 是你的数据库名字
ddl-auto可选参数有五种:
create 启动时删数据库中的表,然后创建,退出时不删除数据表
create-drop 启动时删数据库中的表,然后创建,退出时删除数据表 如果表不存在报错
update 如果启动时表格式不一致则更新表,原有数据保留
none 不进行配置
validate 项目启动表结构进行校验 如果不一致则报错
6.2 创建数据库和表
创建数据库
dbgirl,建数据库时编码应选用
utf-8 utf8mb4,以便能存储表情符号等。
然后在项目中新建 java 类
Girl
package com.solo.test01; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Girl { @Id @GeneratedValue private Integer id; private String cupSize; private Integer age; //需要有空参构造函数 public Girl() { } //getter, setter 方法省略 }
运行项目,数据库会自动创建表
girl
6.3 增删改查
创建 java 类
GirlRepository
package com.solo.test01.girl; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; public interface GirlRepository extends JpaRepository<Girl, Integer> { List<Girl> findAllByAge(Integer age); }
创建 Controller 类
GirlController
package com.solo.test01.girl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController public class GirlController { @Autowired private GirlRepository repository; /** * 查询所有女生 * * @return */ @GetMapping(value = "/girls") public List<Girl> getAll() { return repository.findAll(); } /** * 添加一位女生 * * @param cupSize * @param age * @return 返回新添加的对象 */ @PostMapping(value = "/girl/add") public Girl add(@RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age) { Girl girl = new Girl(); girl.setAge(age); girl.setCupSize(cupSize); return repository.save(girl); } /** * 更新 * * @param id * @param cupSize * @param age * @return */ @PutMapping(value = "/girls/{id}") public Girl update(@PathVariable("id") Integer id, @RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age) { Girl girl = new Girl(); girl.setId(id); girl.setCupSize(cupSize); girl.setAge(age); return repository.save(girl); } /** * 通过id查询一个女生 * * @param id * @return */ @GetMapping(value = "/girls/{id}") public Girl findOne(@PathVariable("id") Integer id) { return repository.findById(id).get(); } /** * 删除 * * @param id */ @DeleteMapping(value = "/girls/{id}") public void deleteById(@PathVariable("id") Integer id) { repository.deleteById(id); } /** * 通过年龄查 * * @param age * @return */ @GetMapping(value = "/girls/age/{age}") public List<Girl> findByAge(@PathVariable("age") Integer age) { return repository.findAllByAge(age); } }
以上通过 JPA 完成了增删改查。
阅读更多- 快速入门1springboot
- 基于Spring Boot构建的Spring MVC快速入门
- JAVAEE——SpringBoot入门:简介、微服务、环境准备、helloworld与探究、快速构建项目
- 01.Spring Cloud学习笔记之使用IDEA+Spring Boot快速构建Rest服务
- Spring Boot 快速入门
- Spring Boot快速入门 (一)
- Spring Boot实战与原理分析之快速入门
- SpringBoot快速入门
- Spring Boot快速入门
- Spring Boot快速入门
- Spring Boot 入门笔记
- Spring Boot快速入门及实例
- Spring Boot快速入门
- Spring Boot快速入门
- Spring Boot快速入门
- 【SpringBoot】的快速入门程序
- 深入Spring Boot (一):快速入门
- Spring Boot快速入门教程
- Spring Boot 快速入门
- 【原创】Spring-boot快速入门(二)JPA数据源--转载请注明出处