你的第一个Spring Boot应用
Posted 疯狂Java图书
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了你的第一个Spring Boot应用相关的知识,希望对你有一定的参考价值。
创建可执行的JAR包
<build>
<plugins>
<!--定义Spring BootMaven插件,可用于运行SpringBoot应用 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>
mvnclean
mvn package
使用命令行窗口。
使用IntelliJ IDEA的Terminal窗口。
使用Run Anything。
使用Maven面板。
使用运行配置。
[3.2.0:jar (default-jar) @ firstboot --- ] ---maven-jar-plugin:
[01\1.2\firstboot\target\firstboot-0.0.1-SNAPSHOT.jar ] Buildingjar: G:\publish\codes\
[ ]
[2.4.2:repackage(repackage) @ firstboot --- ] ---spring-boot-maven-plugin:
[ ] Replacing main artifact with repackaged archive
[ ]------------------------------------------------------------------------
[ ] BUILD SUCCESS
[ ] ------------------------------------------------------------------------
java -jar firstboot-0.0.1-SNAPSHOT.jar
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:
3.2.0:resources (default-resources) on project firstboot:
Input length = 1 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors,re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enablefull debug logging.
开发业务组件
public interface BookService
{
List<Book>getAllBooks();
Integer addBook(Book book);
void deleteBook(Integer id);
}
5) (propagation =Propagation.REQUIRED, timeout =
public class BookServiceImplimplements BookService
{
// 依赖注入容器中的BookDao组件
private BookDao bookDao;
publicList<Book> getAllBooks()
{
return(List<Book>) bookDao.findAll();
}
publicInteger addBook(Book book)
{
bookDao.save(book);
returnbook.getId();
}
publicvoid deleteBook(Integer id)
{
bookDao.deleteById(id);
}
}
public class BookController
{
publicString index(Model model)
{
model.addAttribute("tip", "欢迎访问第一个Spring Boot应用");
return"hello";
}
public ResponseEntity restIndex()
{
returnnew ResponseEntity<>("欢迎访问第一个Spring Boot应用",
null, HttpStatus.OK);
}
privateBookService bookService;
publicString addBook(Book book, Model model)
{
bookService.addBook(book);
return"redirect:listBooks";
}
public ResponseEntity<Map<String, String>> restAddBook( book)
{
bookService.addBook(book);
returnnew ResponseEntity<>(Map.of("tip","添加成功"),
null, HttpStatus.OK);
}
publicString list(Model model)
{
model.addAttribute("books", bookService.getAllBooks());
return"list";
}
public ResponseEntity<List<Book>> restList()
{
returnnew ResponseEntity<>(bookService.getAllBooks(),
null, HttpStatus.OK);
}
publicString delete(Integer id)
{
bookService.deleteBook(id);
return"redirect:listBooks";
}
public ResponseEntity<Map<String,String>> restDelete( Integer id)
{
bookService.deleteBook(id);
returnnew ResponseEntity<>(Map.of("tip","删除成功"),
null, HttpStatus.OK);
}
}
<html xmlns:th="http://www.thymeleaf.org">
<head>
<metacharset="UTF-8"/>
<title>第一个Spring Boot应用</title>
<!-- 引用WarJar中的静态资源-->
<link rel="stylesheet"th:href="@{/webjars/bootstrap/4.5.3/css/ bootstrap.min.css}"/>
<scripttype="text/javascript" th:src="@{/webjars/jquery/3.5.1/jquery.js}"> </script>
</head>
<body>
<div>
<!-- 使用th:text将表达式的值绑定到标准HTML元素 -->
<divclass="alert alert-primary"th:text="${tip}"></div>
<h2>添加图书</h2>
<formmethod="post" th:action="@{/addBook}">
<divclass="form-group row">
<labelfor="title" class="col-sm-3 col-form-label">图书名:</label>
<divclass="col-sm-9">
<input type="text" id="title"name="title"
class="form-control"placeholder="输入图书名">
</div>
</div>
<divclass="form-group row">
<labelfor="author" class="col-sm-3 col-form-label">作者:</label>
<divclass="col-sm-9">
<input type="text" id="author"name="author"
class="form-control"placeholder="输入作者">
</div>
</div>
<divclass="form-group row">
<labelfor="price" class="col-sm-3 col-form-label">价格:</label>
<div>
<input type="number" step="0.1"id="price" name="price"
class="form-control"placeholder="输入价格">
</div>
</div>
<divclass="form-group row">
<divclass="col-sm-6 text-right">
<button type="submit" class="btnbtn-primary">添加</button>
</div>
<divclass="col-sm-6">
<button type="reset" class="btnbtn-danger">重设</button>
</div>
</div>
</form>
</div>
</body>
</html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<metacharset="UTF-8"/>
<title>所有图书</title>
<!-- 引用WarJar中的静态资源-->
<link rel="stylesheet"th:href="@{/webjars/bootstrap/4.5.3/css/bootstrap.min.css}"/>
<scripttype="text/javascript" th:src="@{/webjars/jquery/3.5.1/jquery.js}">
</script>
</head>
<body>
<div>
<h2>全部图书</h2>
<tableclass="table table-hover">
<tr>
<th>书名</th>
<th>作者</th>
<th>价格</th>
<th>操作</th>
</tr>
<trth:each="book : ${books}">
<tdth:text="${book.title}">书名</td>
<tdth:text="${book.author}">作者</td>
<tdth:text="${book.price}">0</td>
<td><ath:href="@{/deleteBook?id=}+ ${book.id}">删除</a></td>
</tr>
</table>
<divclass="text-right"><a class="btnbtn-primary"
th:href="@{/}">添加图书</a></div>
</div>
</body>
</html>
开发DAO组件
Spring Boot Data JPA依赖。
mysql数据库驱动依赖。
<!-- Spring Boot Data JPA依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- MySQL数据库驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=32147
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
public class Book
{
privateInteger id;
privateString title;
privateString author;
privatedouble price;
// 省略getter、setter方法
...
}
public interface BookDaoextends CrudRepository<Book, Integer>
{
}
如果希望阅读Spring Boot的后续干货好文 欢迎 在看丨留言丨分享至朋友圈 三连
以上是关于你的第一个Spring Boot应用的主要内容,如果未能解决你的问题,请参考以下文章
一张图,理顺 Spring Boot应用在启动阶段执行代码的几种方式
一张图帮你记忆,Spring Boot 应用在启动阶段执行代码的几种方式
一张图,理顺 Spring Boot应用在启动阶段执行代码的几种方式