在启动类中启动项目需用@SpringBootApplication这个注解
第二种启动项目的方式:找到项目存放路径下,使用命令mvn spring-boot:run
第三种启动方式:(1)mvn install (2)cd target (3)java -jar xxx.jar
SpringBoot项目不用配置tomcat、xml等等等等,直接通过方法启动项目,可以配置项目的一些属性:开发环境、生产环境
1、Springboot访问路径:server.servlet.context-path=/girl为这个
Spring boot默认是/,这样直接通过http://ip:port/就可以访问到index页面
如果要修改为http://ip:port/path/访问的话,那么需要在Application.properties文件中加入
server.context-path= /你的path
比如:spring-boot,那么访问地址就是http://ip:port/spring-boot路径。
2、SpringBoot Application配置:.yml格式文件
server:
port: 8082
servlet:
path: /girl
3、
通过注解把.yml文件的值注入到属性中
@Value("${cupSize}")
private String cupSize;
通过注解把.yml文件的值注入到类中
@ConfigurationProperties(prefix = "girl")
4、解决开发环境、生产环境的不同的配置问题
创建多个 Application-*.yml文件
Spring:
profiles:
active: * //改变环境
多种启动方式、启动不同环境
5、Controller的使用
@pathVariable 获取url中数据
@RequestMapping(value = {"/{id}/hello"}, method = RequestMethod.GET)
public String say(@PathVariable("id") Integer id)
@RequestParam 获取请求参数的值
@RequestParam(value="",required=false,defaultValue=0)
@GetMapping 组合注解
代替@RequestMapping的get请求
6、Spring-Data-Jpa
JPA(Java Persistence API)定义一系列对象持久化的标准
目前实行这一规范的产品有Hibernate、TopLink
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/dbgirl
username: root
password: 123
jqa:
hibernate:
ddl-auto: create
show-sql: true
/*
这个类代表数据库一张表
*/
@Entity
7、Jpa的使用
//接口继承JpaRepository
public interface GirlRepository extends JpaRepository<Girl,Integer>{
//通过年龄来查询
public List<Girl> findByAge(Integer age);
}
//使用
girlRepository.save(girl);
girlRepositoryfindByAge(age);
8、
/**
* 事务管理
*/
@Transactional
慕课网2小时学会Spring Boot第一讲笔记
最新推荐文章于 2020-07-11 10:59:32 发布