springboot及idea学习笔记

方式1用idea启动
然后防止不停下载安装,在mvn seting.xml里配置阿里云镜像:
<mirrors>
 <mirror>
   
   <id>nexus-aliyun</id>
   <mirrorOf>*</mirrorOf>
   <name>Nexus aliyun</name>
   <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
<mirrors>




springboot启动方式1:
切换到:E:\workspace2017\grel路径下
查看文件dir
然后输入命令启动:mvn spring-boot:run


在浏览器访问:localhost:8080/hello


方式2:
编译命令:mvn install
然后进入到target目录:cd target
然后启动:java -jar grel-0.0.1-SNAPSHOT.jar




springboot配置文件
方式1:
application.properties文件内容:
server.port=8082
server.context-path=/girl


方式2:
application.yml文件,8082前加空格
server:
  port: 8080
  context-path: /girl #使用localhost:8081/girl/hello
cupSize: B
age: 18
content: "cupSize:${cupSize},age:${age}"


线上和生产环境配置
1.application.yml文件内容


spring:
  profiles:
    active: pro  //跳到dev,  active: dev
    
2.application-dev.yml文件内容


server:
  port: 8082
gril:
  cupSize: B
  age: 18




3.application-pro.yml文件内容


server:
  port: 8082
gril:
  cupSize: B
  age: 18






controller:
连接:https://www.cnblogs.com/cxxjohnson/p/7732087.html


@RestController
public class HelloController {
    @Value("${cupSize}")//获取application.yml文件中名为ip的value值
    private String cupSize;
    @Value("${age}")
    private Integer age;
    @Value("${content}")
    private String content;


    @RequestMapping(value = "hello",method = RequestMethod.GET)
    public String say(){
        // return  capSize + age;
        return content;
    }
}




cmd启动生产环境:




idea 启动线上环境:
编译命令:mvn install
然后进入到target目录:cd target
然后启动:java -jar grel-0.0.1-SNAPSHOT.jar  --spring.profiles.active=pro


1.get set 快捷键


fn + alt +insert
2.main方法


psvm


3.for循环

fori
4.打印


sout




 
http://localhost:8082/hello/say?id=2 问号传参;
controller用 
@RequestMapping(value = "/say", method = RequestMethod.GET)
public String say(@RequestParem (value="id") Integer myId){} 注:myId是变量可随意命名;
http://localhost:8082/hello/say/2传参 ;


controller用 
 @RequestMapping(value = "/say/{id}", method = RequestMethod.GET)
public String say(@PathVariable (value="id") Integer id){} ;注:PathVariable是url传参


如果url不传值的hua;
@RequestMapping(value = "/say", method = RequestMethod.GET)//value = "say/{id}"
    public String say(@RequestParam  (value="id",required= true, defaultValue="0") Integer myId){}
    http://localhost:8082/hello/say?id=    结果:id 0
    http://localhost:8082/hello/say?id=88  结果 :id 88
    
 简化代码:@RequestMapping(value = "/say", method = RequestMethod.GET)//value = "say/{id}"
 
 用注解1:@GetMapping("/say")
 用注
 
2:或者@PostMapping("/say")   








数据库操作:


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>


<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
在导入spring-boot-starter-data-jpa报错(缺少数据库驱动导致);
idea添加数据库驱动方法:
View → Tool Windows → database → Data Source Properties → MySQL


最下面的Driver files,添加一下就行了。不过IDEA也可以自动下载






连接数据库:
application.yml
server:
  port: 8888
spring:
  profiles:
    active: product
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    username: root
    password: root
  jpa:
    hibernate:
      ddl-auto: create
    show-sql: true
    
    
    注释: ddl-auto: create 每次程序跑时,会创建对应的表结构,但是有表,应用停下时会删掉表,会创建空的表
           ddl-auto: update 会创建对应的表结构,会保留原来的数据
    ddl-auto: none  什么都不操作
    ddl-auto: validate 会验证类的熟悉和表结构一致,不一致会报错
    ddl-auto:  create-drop 会创建对应的表结构,但是有数据,应用停下时会删掉表
    
    
   创建实体:
   
   
     报错:cannot resolve symbol Entity
     解决:File->Invalidate Caches/Restart 清除缓存重启 还不行就maven -> Reinport
     
     手动删除Project Settings里面的Libraries内容;
在Maven Project的试图里clean一下,删除之前编译过的文件;
项目右键-》Maven-》Reimport


package com.example.demo;                                       
                                                                
      import org.springframework.boot.autoconfigure.domain.EntityScan;
                                                                
      import javax.persistence.Entity;                                
      import javax.persistence.GeneratedValue;                        
      import javax.persistence.Id;                                    
                                                                
      /**                                                             
       * Created by wangmeng on 2017/12/18.                           
       */                                                             
      @Entity                                                         
      public class Girl {                                             
                                                                
          @Id                                                         
          @GeneratedValue                                             
          private  Integer id;                                        
                                                                
          private String cupSize;                                     
                                                                
          private  Integer age;                                       
                                                                
          public Integer getAge() {                                   
              return age;                                             
          }                                                           
                                                                
          public void setAge(Integer age) {                           
              this.age = age;                                         
          }                                                           
                                                                
          public String getCupSize() {                                
              return cupSize;                                         
          }                                                           
                                                                
          public void setCupSize(String cupSize) {                    
              this.cupSize = cupSize;                                 
          }                                                           
                                                                
          public Integer getId() {                                    
              return id;                                              
          }                                                           
                                                                
          public void setId(Integer id) {                             
              this.id = id;                                           
          }                                                           
                                                                
          public Girl() {                                             
          }                                                           
      }   
      
      
      注释:然后启动项目刷新数据库,需要切换一下其他数据库内容,再返回刷新
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值