springboot学习记录一

学习视频来源:https://www.imooc.com/learn/767

SpringBoot学习记录

1Springboot特点

o  化繁为简,简化配置

o  是下一代框架

o  微服务的入门级微框架

2、创建项目:

   使用idea软件来创建springboot项目;

   create new project -> spring initializr-> ..->选择 web

3、项目启动方式(三种启动方式):

   a) 直接在idea中run启动

   b) 使用终端进入到项目的目录,使用mvn启动

        cd/Users/yven/IdeaProjects/my_project_springboot

        mvnspring-boot:run

        control+ c 停止项目运行

   c) 首先编译项目mvn install

       编译完成之后进入target目录下

     使用java命令启动:java -jar my_project-0.0.1-SNAPSHOT.jar

4、项目属性配置

数据库配置:

spring.datasource.url:jdbc:mysql://localhost:3306/

spring.datasource.username:root

spring.datasource.password:root

spring.datasource.drice-class-name:com.mysql.jdbc

 

在application,properties中配置:

server.port=8081

server.context-path=/springboot

推荐使用另一种文件的配置application.yml,注意冒号之要空格

server:
 
port:
8081
 
servlet:
   
context-path:
/test

 

a) 可以在配置文件application.yml中添加变量,在程序中去获取:

server:
  port: 8081
  servlet:
    context-path: /test

cupSize: B

在HelloController.java 中去获取这个变量:

@RestController
public class HelloController {

    @Value("${cupSize}")
    private String cupSize;// 获取application.yml中配置的


    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String say(){

        return "the cupSize is "+cupSize;
    }
}

 

b) 还可以在变量中添加变量:

server:
  port: 8081
  servlet:
    context-path: /test

cupSize: B
age: 18
content: "cupSize: ${cupSize},age: ${age}"

 

@RestController
public class HelloController {

    @Value("${cupSize}")
    private String cupSize;// 获取application.yml中配置的

    @Value("${age}")
    private Integer age;

    @Value("${content}")
    private String content;


    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String say(){

        return "the cupSize is "+cupSize+" age is "+age +" and the content is "+content;
    }
}

 

c) 还有一种方式:配置文件映射到一个class

配置文件:

girl:
  cupSize: B
  age: 18

 

映射类:

@Component
@ConfigurationProperties(prefix = "girl")
public class GirlPreperties {

    private String cupSize;

    private Integer age;

    public String getCupSize() {
        return cupSize;
    }

    public void setCupSize(String cupSize) {
        this.cupSize = cupSize;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
    
    public Integer getAge() {
        return age;
    }
}

 

获取:

@RestController
public class HelloController {

    @Autowired
    private GirlPreperties girlPreperties;

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String say(){

        return "test "+girlPreperties.getCupSize();
    }
}

发环境和生产环境的配置

application.yml(选择哪环境可)

spring:
  profiles:
    active: dev

application-dev.yml 发环境

server:
  port: 8081
  servlet:
    context-path: /test
girl:
  cupSize: B
  age: 18

application-prod.yml 产环境

server:
  port: 8081
  servlet:
    context-path: /test
girl:
  cupSize: F
  age: 18

 

在启动的时候也可以选择环境的载:

使用mvn install 编译之后,

使用java -jartarget/proj.jar--spring.profiles.active=prod

 

5ccontroller的使用

注解

注意(模版的使用):

添加spring-boot-starter-thymeleaf 模版

在resources下新建templates 》index.html

在controller类用@Controller注解,方法中使用

return “index”,就会加载创的模板,现在不推荐使用珍重方式,因为现在开发方式一般为前后端分的,返回json格式的数给前端就可以

注解

@PathVariable获取请的URL可以为:../hello/23

@RequestParam获取请的URL可以为:../hello?id=22

@GetMapping为RequestMapping的GET方式,类有PostMapping、PutMapping

    // 请求参数的方式不一样
//    @RequestMapping(value = {"/say/{id}","/do/{id}"},method = RequestMethod.GET)
//    public String say(@PathVariable Integer id){
//   return "the id is "+id+"  "+girlPreperties.getCupSize();
//}
    @RequestMapping(value = {"/say","/do"},method = RequestMethod.GET)
    public String say(@RequestParam(value="id" , required = false , defaultValue = "1") Integer id){
        // 设置参数的传值为默认为1 ,非必要
        return "the id is "+id+"+girlPreperties.getCupSize();
    }
}

 

 

 

 

 

 

 

6、数据库的操作

 pom中添加依赖

1、     <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://xxx.xxx.xxx.xxx:3306/dbgirl
    username: xxx
    password: xxx

  jpa:
    hibernate:
      ddl-auto: update # create是 每次启动都会去创建表, update会保留之前的数据
    show-sql: true

 

务操作:

方法上使用Transactional注解修饰

@Transactional

数据表需要改InnoDB


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值