引言:
Spring Boot是一个强大的框架,用于创建Java应用程序和微服务。它提供了快速开发的能力,允许您轻松创建独立的、可运行的应用程序,而无需太多的配置和样板代码。本篇博客文章将向您展示如何使用Spring Boot创建一个简单的Web应用程序。我们将介绍Spring Boot的基本概念,以及如何设置和运行您的第一个Spring Boot应用程序。
正文:
什么是Spring Boot?
Spring Boot是Spring框架的扩展,旨在简化Spring应用程序的开发和部署。它通过提供一组开箱即用的功能和自动配置来加速应用程序开发。Spring Boot的目标是让开发人员更专注于业务逻辑,而不是配置。
创建您的第一个Spring Boot应用程序
首先,确保您已经安装了Java开发工具和Spring Boot。接下来,我们将创建一个简单的Spring Boot应用程序:
-
使用Spring Initializr初始化项目。
Spring Initializr是一个Web界面,允许您轻松创建Spring Boot项目。在浏览器中访问https://start.spring.io/,选择项目的基本设置,然后点击“Generate”按钮。这将生成一个包含所有必要依赖项的项目框架。
-
导入项目到您的集成开发环境(IDE)。
将生成的项目导入您喜欢的IDE中。这里我们使用IntelliJ IDEA。
-
创建一个简单的控制器。
在项目中创建一个新的Java类,作为一个简单的控制器。这个控制器将处理Web请求并返回响应。
@RestController
public class HelloWorldController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, Spring Boot!";
}
}
4.运行应用程序。
运行您的Spring Boot应用程序,访问http://localhost:8080/hello,您应该会看到 "Hello, Spring Boot!" 的响应。
自定义您的Spring Boot应用程序
Spring Boot允许您轻松自定义应用程序的行为。您可以修改应用程序属性、添加新的依赖项和创建自定义配置。这使您能够构建适应特定需求的应用程序。
结论:
Spring Boot是一个出色的工具,用于创建Java Web应用程序和微服务。它简化了开发和部署过程,让开发人员更专注于业务逻辑。希望本篇博客文章帮助您入门Spring Boot,并激发您开始构建自己的应用程序。
如果您想要深入了解Spring Boot,可以查看Spring Boot的官方文档和示例项目。开始编写您自己的Spring Boot应用程序,探索其强大的功能,以及如何将其应用到实际项目中。
这是我的用户界面
@Service @Transactional public class ArticleServiceImpl implements IArticleService { @Autowired private ArticleMapper articleMapper; @Autowired private StatisticMapper statisticMapper; // 分页查询文章列表 @Override public PageInfo<Article> selectArticleWithPage(Integer page, Integer count) { PageHelper.startPage(page, count); List<Article> articleList = articleMapper.selectArticleWithPage(); // 封装文章统计数据 for (int i = 0; i < articleList.size(); i++) { Article article = articleList.get(i); Statistic statistic = statisticMapper.selectStatisticWithArticleId(article.getId()); article.setHits(statistic.getHits()); article.setCommentsNum(statistic.getCommentsNum()); } PageInfo<Article> pageInfo=new PageInfo<>(articleList); return pageInfo; } // 统计前10的热度文章信息 @Override public List<Article> getHeatArticles( ) { List<Statistic> list = statisticMapper.getStatistic(); List<Article> articlelist=new ArrayList<>(); for (int i = 0; i < list.size(); i++) { Article article = articleMapper.selectArticleWithId(list.get(i).getArticleId()); article.setHits(list.get(i).getHits()); article.setCommentsNum(list.get(i).getCommentsNum()); articlelist.add(article); if(i>=9){ break; } } return articlelist; } }这是我的一些代码
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org" th:with="title='发布文章',active='publish'"> <header th:replace="back/header::headerFragment(${title},${active})"></header> <link th:href="@{/back/plugins/tagsinput/jquery.tagsinput.css}" rel="stylesheet"/> <link th:href="@{/back/plugins/select2.dist.css/select2-bootstrap.css}" rel="stylesheet"/> <link th:href="@{/back/plugins/toggles/toggles.css}" rel="stylesheet"/> <link th:href="@{/back/plugins/mditor/css/mditor.min.css}" rel="stylesheet"/> <link th:href="@{/back/plugins/dropzone/4.3.0/min/dropzone.min.css}" rel="stylesheet"> <style> #tags_tagsinput { background-color: #fafafa; border: 1px solid #eeeeee; } #tags_addTag input { width: 100%; } #tags_addTag { margin-top: -5px; } .mditor .editor{ font-size: 14px; font-family: "Helvetica Neue",Helvetica,Arial,sans-serif; } .mditor .backdrop, .mditor .textarea, .mditor .viewer{ font-size: 14px; } .markdown-body{ font-size: 14px; } .note-toolbar { text-align: center; } .note-editor.note-frame { border: none; } .note-editor .note-toolbar { background-color: #f5f5f5; padding-bottom: 10px; } .note-toolbar .note-btn-group { margin: 0; } .note-toolbar .note-btn { border: none; } #articleForm #dropzone { min-height: 200px; background-color: #dbdde0; line-height:200px; margin-bottom: 10px; } #articleForm .dropzone { border: 1px dashed #8662c6; border-radius: 5px; background: white; } #articleForm .dropzone .dz-message { font-weight: 400; } #articleForm .dropzone .dz-message .note { font-size: 0.8em; font-weight: 200; display: block; margin-top: 1.4rem; } </style> <body class="fixed-left"> <div id="wrapper"> <div th:replace="back/header::header-body"></div> <div class="content-page"> <div class="content"> <div class="container"> <div class="row"> <div class="col-sm-12"> <h4 class="page-title"> <th:block th:if="${null != contents}"> 编辑文章 </th:block> <th:block th:unless="${null != contents}"> 发布文章 </th:block> </h4> </div> <div class="col-md-12"> <form id="articleForm"> <input type="hidden" name="id" th:value="${contents!=null and contents.id!=null}?${contents.id}: ''" id="id"/> <input type="hidden" name="allowComment" th:value="${contents!=null and contents.allowComment !=null} ?${contents.allowComment}: true" id="allow_comment"/> <input type="hidden" name="content" id="content-editor"/> <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/> <div class="form-group col-md-6" style="padding: 0 10px 0 0;"> <th:block th:if="${null != contents}"> <input type="text" class="form-control" name="title" required="required" aria-required="true" th:value="${contents.title}"/> </th:block> <th:block th:unless="${null != contents}"> <input type="text" class="form-control" placeholder="请输入文章标题(必须)" name="title" required="required" aria-required="true"/> </th:block> </div> <div class="form-group col-md-6" style="padding: 0 10px 0 0;"> <th:block th:if="${null != contents}"> <input name="tags" id="tags" type="text" class="form-control" th:value="${contents.tags}" /> </th:block> <th:block th:unless="${null != contents}"> <input name="tags" id="tags" type="text" class="form-control" placeholder="请输入文章标签" /> </th:block> </div> <div class="clearfix"></div> <div id="md-container" class="form-group"> <textarea id="md-editor" th:utext="${contents!=null and contents.content !=null}?${contents.content}: ''"></textarea> </div> <!-- <div class="form-group col-md-3 col-sm-4"> <label class="col-sm-4">开启评论</label> <div class="col-sm-8"> <div th:class="${contents!=null and contents.allowComment!=null }?'toggle toggle-success allow-'+${contents.allowComment}:'toggle toggle-success allow-true'"></div> </div> </div>--> <div class="clearfix"></div> <div class="text-right"> <a class="btn btn-default waves-effect waves-light" th:href="@{/admin/article}">返回列表</a> <button type="button" class="btn btn-primary waves-effect waves-light" οnclick="subArticle('publish');"> 保存文章 </button> </div> </form> </div> </div> <div th:replace="back/footer :: footer-content"></div> </div> </div> </div> </div> <div th:replace="back/footer :: footer"></div> <script th:src="@{/back/plugins/tagsinput/jquery.tagsinput.min.js}"></script> <script th:src="@{/back/plugins/jquery-multi-select/jquery.quicksearch.js}"></script> <script th:src="@{/back/plugins/mditor/js/mditor.min.js}"></script> <script th:src="@{/back/plugins/multi-select/0.9.12/js/jquery.multi-select.min.js}"></script> <script th:src="@{/back/plugins/select2/3.4.8/select2.min.js}"></script> <script th:src="@{//cdn.bootcss.com/jquery-toggles/2.0.4/toggles.min.js}"></script> <script th:src="@{/back/plugins/dropzone/4.3.0/min/dropzone.min.js}"></script> <script th:src="@{/back/plugins/to-markdown/3.1.0/to-markdown.min.js}"></script> <script th:src="@{/back/js/article.js}"></script> </body> </html>跟前端页面的一些代码