SpringBoot学习一

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: 中进行

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

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

server:
    port: 8888
    context-path: /demo
#以下是测试数据
level: A
age: 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: A
test.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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值