SpringBoot学习一

来源:互联网 发布:朱敏希28分钟 知乎 编辑:程序博客网 时间:2024/05/29 03:54

1、使用IDEA创建springboot项目
2、初次使用,下载jar会很慢,使用阿里云的镜像:.m2/settings.xml:

<mirror>    <id>nexus-aliyun</id>    <mirrorOf>*</mirrorOf>    <name>Nexus aliyun</name>    <url>http://maven.aliyun.com/nexus/content/groups/public</url></mirror>

3、项目下的pom.xml文件
4、主类:DemoApplication:

@SpringBootApplication //启动需要的注解public class DemoApplication {    public static void main(String[] args) {        SpringApplication.run(DemoApplication.class, args);    }}

5、配置
在appliction.properties: 中进行

#项目运行端口号,默认8080server.port=8888#项目名称server.context-path=/demo#其他配置

访问:http://localhost:8888/demo/xxx
使用application.yml配置。(格式简便,注意:后面必须有一个空格)

server:    port: 8888    context-path: /demo#以下是测试数据level: Aage: 13#配置文件中变量调用其他变量content: "level: ${level}, age: ${age}"

6、Controller:
class上使用注解@RestConroller(Spring4之后的注解,相当于@ResponseBody配合@Controller)。
method上使用注解@RequestMapping,我们可以用简洁的:@GetMapping, @PostMapping。
我们可以使用@Value属性可以获取配置文件中的值:

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

如果获取多个配置,可以封装:
application.yml:

test.level: Atest.age: 13
@Componet@ConfigurationProperties(prefix="test")public class TestProperties {    private String level;    private Integer age;    // ....}// 其他类中使用,直接用@Autowired注入该类

了解:使用teymeleaf依赖,在resources/thymeleaf/index.html. 控制器方法中直接返回 return “index”; 可以看到访问到了界面。
如今开发前后端分离,后端返回json给前端即可。

7、数据库:
Spring-Data-Jpa:
JPA定义了一系列对象持久化的标准,目前实现这一规范的产品有Hibernate、TopLink等。

0 0
原创粉丝点击