springboot

https://blog.csdn.net/tengshe789/article/details/81488477

 

最近再研究springboot的原理��颇有收获,现在让我分享一下springboot如何使用吧~

想要解锁更多新姿势?请访问我的博客

啥是Springboot
和书上理解的不同,我认为Springboot是一个优秀的快速搭建框架,他通过maven继承方式添加依赖来整合很多第三方工具,可以避免各种麻烦的配置,有各种内嵌容器简化Web项目,还能避免依赖的干扰,它内置tomcat,jetty容器,使用的是java app运行程序,而不是传统的用把war放在tomcat等容器中运行

和JFinal的区别
JFinal是国人出品的一个web + orm 框架 ,JFinal,优点是开发迅速、代码量少、学习简单、功能强大、轻量级、易扩展。核心就是极致简洁。他没有商业机构的支持,所以宣传不到位,少有人知。

Springboot相比与JFinal最大的优点就是支持的功能非常多,可以非常方便的将spring的各种框架如springframework , spring-mvc, spring-security, spring-data-jpa, spring-cache等等集成起来进行自动化配置 ,而且生态 比较好,很多产品都对Springboot做出一定支持。

与Springcloud的区别
可以这么理解,Springboot里面包含了Springcloud,Springcloud只是Springboot里面的一个组件而已。

Springcloud提供了相当完整的微服务架构。而微服务架构,本质来说就是分布式架构,意味着你要将原来是一个整体的项目拆分成一个个的小型项目,然后利用某种机制将其联合起来,例如服务治理、通信框架等基础设施。

SpringBoot和SpringMVC区别
SpringBoot的Web组件,默认集成的是SpringMVC框架。

快速使用
要往下看的话,注意了��

Springboot 2.x 要求 JDK 1.8 环境及以上版本。另外,Springboot 2.x 只兼容 Spring Framework 5.0 及以上版本。
为 Springboot 2.x 提供了相关依赖构建工具是 Maven,版本需要 3.2 及以上版本。使用 Gradle 则需要 1.12 及以上版本。
建议用IntelliJ IDEA IntelliJ IDEA (简称 IDEA)
建立项目
我已经好久没用Eclipse了,要知道Eclipse是创建一个maven项目在引入Springboot依赖创建的。

下面我分享一下用IDEA创建Springboot的方法。

很简单,在这个界面里面就可以创建Springboot了。接下来在添加一些组件。

大功告成!

写一个DEMO
这里用我写的一个秒杀项目作为参考栗子。秒杀商城

创建一个conntroller包,编写一个样列。

package cn.tengshe789.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/demo")
public class SampleController {

    @RequestMapping("/hello")
    public String index() {
        return "Hello World";
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
接下来在他同级包或者上一级的包内,创建一个主方法MainApplication。方法内容;

@SpringBootApplication
@EnableAsync
//@ComponentScan("cn.tengshe789.controller")
//@EnableAutoConfiguration
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}
1
2
3
4
5
6
7
8
9
在浏览器输入http://127.0.0.1:8080/demo/hello/,就可以启动了!

SpringApplication.run
Springboot将他标识为启动类,用它启动Springboot项目

基础注解解释
@RestController
在上加上RestController 表示修饰该Controller所有的方法返回JSON格式,直接可以编写Restful接口。就相当于@Controller+@ResponseBody这种实现

@SpringBootApplication
用在启动Springboot中,相当于@ComponentScan+@EnableAutoConfiguration+@Configuration

@ComponentScan(“cn.tengshe789.controller”)
控制器扫包范围。

@EnableAutoConfiguration
他让 Spring Boot 根据咱应用所声明的依赖来对 Spring 框架进行自动配置。意思是,创建项目时添加的spring-boot-starter-web添加了Tomcat和Spring MVC,所以auto-configuration将假定你正在开发一个web应用并相应地对Spring进行设置。

配置文件
properties
规则:

1、名用大写比较规范

2、=两边别打空格

3、名值对写完后别打分号

自定义参数
name=tengshe789
1
多环境配置
spring.profiles.active=pre

application-dev.properties:开发环境
application-test.properties:测试环境
application-prod.properties:生产环境
1
2
3
4
5
修改端口号
server.port=8888 
server.context-path=/tengshe789
1
2
yaml
规则:

使用空格 Space 缩进表示分层,不同层次之间的缩进可以使用不同的空格数目,但是同层元素一定左对齐,即前面空格数目相同(不能使用 Tab,各个系统 Tab对应的 Space 数目可能不同,导致层次混乱)
‘#’表示注释,只能单行注释,从#开始处到行尾
破折号后面跟一个空格(a dash and space)表示列表
用冒号和空格表示键值对 key: value
简单数据(scalars,标量数据)可以不使用引号括起来,包括字符串数据。用单引号或者双引号括起来的被当作字符串数据,在单引号或双引号中使用C风格的转义字符
server:
  port:  8080
  context-path: /springboot
1
2
3
xml
Springboot官方不推荐xml,略

Web开发
一个项目用Springboot,十有八九就是用于Web开发。首先让我们看看Springboot怎么快速开发Web把

如何访问静态资源
请在resources目录下创建static文件夹,在该位置放置一个静态资源。

目录:src/main/resources/static

启动程序后,尝试访问http://localhost:8080/img.xxx/。就可以访问了。

关于渲染Web页面
在之前的快速使用的示例中,我们都是通过添加@RestController来处理请求,所以返回的内容为json对象。那么如果需要渲染html页面的时候,要如何实现呢?

模板引擎方法
Springboot依然可以实现动态HTML,并且提供了多种模板引擎的默认配置支持,Springboot官方文档有如下推荐的模板引擎:

· Thymeleaf

· FreeMarker

· Velocity

· Groovy

· Mustache

Springboot官方建议避免使用JSP,若一定要使用JSP将无法实现Spring Boot的多种特性。

在Springboot中,默认的模板配置路径都时:src/main/resources/templates。当然也可以修改这个路径,具体如何修改,可在各模板引擎的配置属性中查询并修改。

Thymeleaf(胸腺)
这里还是用我写的一个秒杀项目作为参考栗子。秒杀商城

POM
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
1
2
3
4
配置文件
在application.properties中添加:

#thymeleaf
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.enabled=true
spring.thymeleaf.encoding=UTF-8
# 一代填 spring.thymeleaf.mode=HTML5
spring.thymeleaf.mode=HTML
1
2
3
4
5
6
7
8
9
后台
在src/main/resources/创建一个templates文件夹,新网页后缀为*.html

 @RequestMapping("/to_list")
    public String list(Model model,MiaoshaUser user) {
        model.addAttribute("user", user);
        //查询商品列表
        List<GoodsVo> goodsList = goodsService.listGoodsVo();
        model.addAttribute("goodsList", goodsList);
        return "goods_list";
    }
1
2
3
4
5
6
7
8
前台
这里注意Thymeleaf语法,Thymeleaf很像HTML,不同之处在标签加了一个th前缀

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>商品列表</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <!-- jquery -->
    <script type="text/javascript" th:src="@{/js/jquery.min.js}"></script>
</head>
<body>

<div class="panel panel-default" >
    <div class="panel-heading">秒杀商品列表</div>
    <table class="table" id="goodslist">
        <tr><td>商品名称</td><td>商品图片</td><td>商品原价</td><td>秒杀价</td><td>库存数量</td><td>详情</td></tr>
        <tr  th:each="goods,goodsStat : ${goodsList}">
            <td th:text="${goods.goodsName}"></td>
            <td ><img th:src="@{${goods.goodsImg}}" width="100" height="100" /></td>
            <td th:text="${goods.goodsPrice}"></td>
            <td th:text="${goods.miaoshaPrice}"></td>
            <td th:text="${goods.stockCount}"></td>
            <td><a th:href="'/goods_detail.htm?goodsId='+${goods.id}">详情</a></td>
        </tr>
    </table>
</div>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值