个人看了视频,来源
https://www.imooc.com/video/13589 整理了一些学习笔记忘各位大佬路过时提下宝贵意见谢谢~
springboot视频讲解:https://www.imooc.com/video/13589
springboot demo下载:https://start.spring.io/
1.启动main方法报错:Unregistering JMX-exposed beans on shutdown,原因pom.xml配置文件少了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
这样的配置
2.eclipse创建spring boot项目加载不到application.properties配置文件的问题
把Build Path->Source下面的Excluded的**去掉
3.spring-boot 在application.properties配置文件中修改访问项目访问路径
spring-boot配置文件中server.context-path=/XXXXXXX不起作用:
原因是更新后写法变成了server.servlet.context-path=/XXXXXX,这样写即可
4.关于正式与测试配置不同所以需要进行以下操作
application-dev.properties
application-pro.properties
application.properties 中写入spring.profiles.active= dev或者spring.profiles.active= pro
5.springboot在配置文件中配置,启动项目自动创建表
application.properties中配置
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
编写对应的javabean
类上面注解@Entity
主键自动增长配置 @Id
@GeneratedValue
写明参数为空的构造方法
注:如果之前有这个表,并且有数据那么程序启动自动会建立一个空数据的表
操作过程为首先删除表,在创建一个新表
application.properties中配置
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
编写对应的javabean
类上面注解@Entity
主键自动增长配置 @Id
@GeneratedValue
写明参数为空的构造方法
注:如果之前有这个表,那么不会在去创建这个表
接下来呢是一些注解的使用
@RestController(web访问)
Spring4之后新加入的注解,原来返回json需要@ResponseBody和@Controller配合。
即@RestController是@ResponseBody和@Controller的组合注解。
@SpringBootApplication
启动类注解
@Value
resources下的配置文件application.properties为:
server.port=8080 //设置端口号为8080
server.servlet.context-path=/demo //加个demo路径
cupSize=B //设置cupSize为B ,后台获取方式:改类 定义 @Value("${cupSize}")
private String cupSize;
age=15 //设置age为B ,后台获取方式:改类 定义 @Value("${age}")
private String age;
context:"cupSize:${cupSize},age:${age}" //在配置文件中使用配置参数后台 @Value("${context}")
private String context; 输出结果 "cupSize:B,age:15"
springboot配置javabean
配置文件application.properties:
girl.cupSize=B
girl.age=15
javabean中注解
@Component
@ConfigurationProperties(prefix="girl")
获取此javabean: @Autowired
private javabean类名 javabean起名,然后直接可以用里面含有属性值
@RequestMapping
可以写在方法上面或者类上面,一般给用户增加一个url访问地址
例:@RequestMapping(value={"/hello","/hi"},method=RequestMethod.GET)访问方法默认是get,post都可以请求
用户可以通过hello或者hi,用get方式访问该方法
@PathVariable
主要用于获取参数
方法名上面注解写法:
@RequestMapping(value={"/hello/{id}","/hi/{id}"},method=RequestMethod.GET)
方法参数写法
@PathVariable("id") Integer id
@RequestParam
主要用于获取参数
方法参数写法
@RequestParam(value="id",required=false,defaultValue="2") Integer id
value:参数名
required:是否必传
defaultValue:默认值
访问url传参格式 url?id=2