代码:https://git.oschina.net/liaoshixiong/girl
工具:Intellij IDEA 旗舰版
新建项目
1、选择Spring Initializr;
1、
2、填写项目相关信息
3、Spring Boot相关配置(1.4.1)
设置保存项目路径。
4、MAVEN配置路径:使用阿里云的路径。
settings.xml: mirrors
5、删除项目中不需要的文件:
directory:.mvn
file:mvnw,mvnw.cmd
启动项目
右击,选中并单击RUN "GirlApplication"
7、写一个HelloController,重启项目,并写URL:http://localhost:8080/hello
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* Created By IntelliJ Idea 2017.1
* Company: LZY
* Author: Administrator
* Date&Time: 2018/9/2 17:32
*/
@RestController
public class HelloController {
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String say(){
return "Hello Spring Boot!";
}
}
启动项目方法2
1、到项目目录下
2、执行命令:mvn spring-boot:run
启动项目方法3
1、编译:mvn install
2、到目录target:cd target
执行命令:java -jar girl-0.0.1-SNAPSHOT.jar
项目属性配置
1、application.properties
server.port=8081
server.servlet.context-path=/girl
重启项目,URL:http://localhost:8081/girl/hello
2、application.yml
server:
port: 8082
servlet:
context-path: /girl
3、application.yml的属性配置及使用。URL:http://localhost:8082/girl/hello/cupSize
#application.yml
cupSize: B
//HelloController
@RestController
public class HelloController {
@Value("${cupSize}")
private String cupSize;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String say(){
return "Hello Spring Boot!";
}
@RequestMapping(value = "/hello/cupSize",method = RequestMethod.GET)
public String sayCupSize(){
return cupSize;
}
}
4、application.yml的属性配置:引用其他属性的值;
#application.yml
cupSize: B
age: 18
content: "cupSize:${cupSize},age:${age}"
5、application.yml的属性配置:赋值给一个实体类;
#application.yml
girl:
cupSize: B
age: 18
实体类:
@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;
}
@Override
public String toString() {
return "GirlProperties{" +
"cupSize='" + cupSize + '\'' +
", age=" + age +
'}';
}
}
引用实体类的HelloController;URL:http://localhost:8082/girl/hello/girl
@Autowired
private GirlProperties girlProperties;
@RequestMapping(value = "/hello/girl",method = RequestMethod.GET)
public String sayGirl(){
return this.girlProperties.toString();
}
6、application.yml的属性配置:测试环境与生产环境的切换
复制application.yml为application-dev.yml与application-prod.yml;
如果使用application-dev.yml的配置,修改application.yml:
#application.yml
spring:
profiles:
active: dev
Controller注解的使用
@Controller | 处理http请求 |
@RestController | Spring4之后新加的注解,原来返回json需要@ResponseBody配合@Controller |
@RequestMapping | 配置url映射 |
@PathVariable | 获取url中的数据 |
@RequestParam | 获取请求参数的值 |
@GetMapping | 组合注解,相当于:@RequestMapping(value = ...,method = RequestMethod.GET) |
1、到页面。
添加maven 依赖thymeleaf
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
添加\resources\templates\index.html
写一个controller.URL:http://localhost:8182/girl/index(或http://127.0.0.1:8182/girl/index)
@Controller
public class IndexController {
@RequestMapping(value = "index",method = RequestMethod.GET)
public String toIndex(){
return "index";
}
}
2、@RestController等同于@Controller加上@ResponseBody
3、两个路径,指向同一个地方。
http://127.0.0.1:8182/girl/hello
@RequestMapping(value = {"/hello","hi"},method = RequestMethod.GET)
public String say(){
return "Hello Spring Boot!";
}
4、给整个Controller的路径加个前缀“/hello”,URL:http://127.0.0.1:8182/girl/hello/hello
@RestController
@RequestMapping("/hello")
public class HelloController {
}
5、GET方式与POST方式。
method = RequestMethod.POST
@RequestMapping(value = {"/hello","hi"},method = RequestMethod.POST)
public String sayPost(){
return "POST:Hello Spring Boot!";
}
使用POSTMAN
如果不写“method = RequestMethod.POST”,默认所有,但是不建议这样做。
6、从路径中获取值,URL:http://127.0.0.1:8182/girl/hello/say/1111
@RequestMapping(value = {"/say/{id}"},method = RequestMethod.GET)
public String sayPathId(@PathVariable("id") Integer id){
return "say id:"+id;
}
7、获取参数值,URL:http://127.0.0.1:8182/girl/hello/say?id=1111
@RequestMapping(value = {"/say"},method = RequestMethod.GET)
public String sayGetParam(@RequestParam("id") Integer myId){
return "Param id:"+myId;
}
8、获取参数值,设定不是必须传的,有默认值,URL:http://127.0.0.1:8182/girl/hello/saydefault
@RequestMapping(value = {"/saydefault"},method = RequestMethod.GET)
public String sayGetParamDefault(@RequestParam(value = "id",required = false,defaultValue = "2222") Integer myId){
return "Param id:"+myId;
}
9、简化@RequestMapping为@GetMapping,URL:http://127.0.0.1:8182/girl/hello/saydefaultbbbr
@GetMapping(value = {"/saydefaultbbbr"})
public String sayGetParamDefaultAbbr(@RequestParam(value = "id",required = false,defaultValue = "2222") Integer myId){
return "Param id:"+myId;
}
数据库操作
Spring-Data-Jpa
JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。
Hibernate3.2+、TopLink 10.1.3以及OpenJPA都提供了JPA的实现。
RESTful API设计
请求类型 | 请求路径 | 功能 |
---|---|---|
GET | /girls | 获取女生列表 |
POST | /girls | 创建一个女生 |
GET | /girls/id | 通过id查询一个女生 |
PUT | /girls/id | 通过id更新一个女生 |
DELETE | /girls/id | 通过id删除一个女生 |
添加到pom.xml中的两个依赖
<!--数据库依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
添加注解到配置文件application.yml中
spring:
profiles:
active: dev
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/dbgirl
username: root
password: root
jpa:
hibernate:
ddl-auto: create
use-new-id-generator-mappings: false
show-sql: true
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
新建实体类Girl
@Entity
public class Girl {
@Id
@GeneratedValue
private Integer id;
private String cupSize;
private Integer age;
public Girl() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}
@Override
public String toString() {
return "Girl{" +
"id=" + id +
", cupSize='" + cupSize + '\'' +
", age=" + age +
'}';
}
}
启动项目,系统会自动创建girl表。
但是因为 如下注解,会每次启动项目时,都重新生成表。
jpa:
hibernate:
ddl-auto: create
如果将create更改为update,只会在没有表的情况下生成表。
其他CURD操作:
代码:https://gitee.com/Yenn-2017_admin/girl