前端之家收集整理的这篇文章主要介绍了
Java Web系列:Spring Boot 基础,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
项目(参考1) 提供了一个类似ASP.NET MVC的默认模板一样的标准样板,直接集成了一系列的组件并使用了默认的配置。使用Spring Boot 不会降低学习成本,甚至增加了学习成本,但显著降低了使用成本并提高了开发效率。如果没有Spring基础不建议直接上手。
1.基础项目
这里只关注基于Maven的项目构建,使用Spring Boot CLI命令行工具和Gradle构建方式请参考官网。
(1)创建项目:
创建类型为quickstart的Maven项目,删除默认生成的.java文件保持默认的Maven目录即可。
(2)修改/
4.0.0
com.example
myproject
0.0.1-SNAPSHOT
1.8
org.springframework.boot
spring-boot-starter-parent
1.3.1.RELEASE
org.springframework.boot
spring-boot-starter-web
添加/src/main/sample/controller/文件:
org.springframework.web.bind.annotation.*
@RequestMapping("/" "Hello World!" }
添加/src/main/sample/文件:
org.springframework.boot.* org.springframework.boot.autoconfigure.* simple.controller.*
main(String[] args) SpringApplication.run( Object[] { Application.,HomeController.
}
(1)修改pom,添加和依赖:
4.0.0
com.example
myproject
0.0.1-SNAPSHOT
1.8
org.springframework.boot
spring-boot-starter-parent
1.3.1.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-jpa
com.h2database
h2
runtime
如果需要在控制台查看生成sql语句,可以添加/src/main/resources/application.properties
logging.level.org.hibernate.sql=debug
(2)添加实体
添加User、Role、Category和Post实体。
User:
java.util.*
javax.persistence.*
@ManyToMany(cascade = List roles = ArrayList
.id =
.userName =
.password =
Email =
List
setRoles(List .roles =
Version = }