SpringBoot入门

参考:2小时学会Spring Boot

代码: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请求
@RestControllerSpring4之后新加的注解,原来返回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

http://127.0.0.1:8182/girl/hi

    @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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Spring Boot是一个用于快速构建基于Spring框架的应用程序的开源框架。它提供了一种简化的方法来配置和部署应用程序,使开发人员能够更快地开发和运行应用程序。 Spring Boot Actuator是Spring Boot的一个组件,它可以帮助我们监控和管理Spring Boot应用程序,包括健康检查、审计、统计和HTTP追踪等功能。要使用Spring Boot Actuator,只需引入相应的起步依赖,并在应用程序的入口点类上添加@SpringBootApplication注解即可。在该类中,使用@SpringBootApplication注解相当于同时添加了@Configuration、@EnableAutoConfiguration和@ComponentScan注解,它标识了当前应用程序是一个Spring Boot应用程序。要启动Spring Boot应用程序,只需在主启动类中编写main函数,通过调用SpringApplication.run(Application.class, args)方法来启动应用程序。在开发过程中,<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [SpringBoot入门](https://blog.youkuaiyun.com/weixin_45905210/article/details/121712027)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [spring boot 入门](https://blog.youkuaiyun.com/zhshx19900318/article/details/129476812)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值