概述Spring Boot 项目(参考1) 提供了一个类似ASP.NET MVC的默认模板一样的标准样板,直接集成了一
系列的组件并使用了默认的配置。使用Spring Boot 不会降低学习成本,甚至增加了学习成本,但显著降低了使用成本并提高了开发效率。如果没有Spring
基础不建议直接上手。1.基础项目这里只关注基于Maven的项目构建,使用Spring Boot CLI命令行工具和Gradle构建方式请参考官网。(1)创建项目:创建类型为quickstart的Maven项目,删除默认生成的.java文件保持默认的Maven目录即可。(2)修改/pom.xml1 <?xml version="1.0" encoding="UTF-8"?>2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">4 <modelVersion>4.0.0</modelVersion>5 <groupId>com.example</groupId>6 <artifactId>myproject</artifactId>7 <version>0.0.1-SNAPSHOT</version>8 <properties>9 <java.version>1.8</java.version>10 </properties>11 <parent>12 <groupId>org.springframework.boot</groupId>13 <artifactId>spring-boot-starter-parent</artifactId>14 <version>1.3.1.RELEASE</version>15 </parent>16 <dependencies>17 <dependency>18 <groupId>org.springframework.boot</groupId>19 <artifactId>spring-boot-starter-web</artifactId>20 </dependency>21 </dependencies>22 </project>(3)添加/src/main/sample/controller/HomeController.java文件:1 package simple.controller;23 import org.springframework.web.bind.annotation.*;45 @RestController6 public class HomeController {78 @RequestMapping("/")9 public String index() {10 return "Hello World!";11 }12 }(4)添加/src/main/sample/Application.java文件:1 package simple;23 import org.springframework.boot.*;4 import org.springframework.boot.autoconfigure.*;5 import simple.controller.*;67 @EnableAutoConfiguration8 public class Application {910 public static void main(String[] args) throws Exception {11 SpringApplication.run(new Object[] { Application.class, HomeController.class }, args);12 }1314 } 在浏览器中输入http://localhost:8080/,即可直接看到"Hello World"运行结果。2. 添加数据访问支持(1)修改pom,添加spring-boot-starter-data-jpa和h2依赖:1 <?xml version="1.0" encoding="UTF-8"?>2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">4 <modelVersion>4.0.0</modelVersion>5 <groupId>com.example</groupId>6 <artifactId>myproject</artifactId>7 <version>0.0.1-SNAPSHOT</version>8 <properties>9 <java.version>1.8</java.version>10 </properties>11 <parent>12 <groupId>org.springframework.boot</groupId>13 <artifactId>spring-boot-starter-parent</artifactId>14 <version>1.3.1.RELEASE</version>15 </parent>16 <dependencies>17 <dependency>18 <groupId>org.springframework.boot</groupId>19 <artifactId>spring-boot-starter-web</artifactId>20 </dependency>21 <dependency>22 <groupId>org.springframework.boot</groupId>23 <artifactId>spring-boot-starter-data-jpa</artifactId>24 </dependency>25 <dependency>26 <groupId>com.h2database</groupId>27 <artifactId>h2</artifactId>28 <scope>runtime</scope>29 </dependency>30 </dependencies>31 </project>如果需要在控制台查看生成SQL语句,可以添加/src/main/resources/application.properties1 spring.h2.console.enabled=true2 logging.level.org.hibernate.SQL=debug(2)添加实体添加User、Role、Category和Post实体。User:1 package simple.domain;23 import java.util.*;45 import javax.persistence.*;67 @Entity8 public class User {9 @Id10 @GeneratedValue11 private Long id;1213 private String userName;1415 private String password;1617 private String Email;1819 @javax.persistence.Version20 private Long Version;2122 @ManyToMany(cascade = CascadeType.ALL)23 private List<Role> roles = new ArrayList<Role>();2425 public Long getId() {26 return id;27 }2829 public void setId(Long id) {30 this.id = id;31 }3233 public String getUserName() {34 return userName;35 }3637 public void setUserName(String userName) {38 this.userName = userName;39 }4041 public String getPassword() {42 return password;43 }4445 public void setPassword(String password) {46 this.password = password;47 }4849 public String getEmail() {50 return Email;51 }5253 public void setEmail(String email) {54 Email = email;55 }5657 public List<Role> getRoles() {58 return roles;59 }6061 public void setRoles(List&
@H_403_0@<span >Spring Boot 项目(参考1) 提供了一个类似ASP.NET MVC的默认模板一样的标准样板,直接集成了一系列的组件并使用了默认的配置。使用Spring Boot 不会降低学习成本,甚至增加了学习成本,但显著降低了使用成本并提高了开发效率。如果没有Spring基础不建议直接上手。
1.基础项目@H_403_0@这里只关注基于Maven的项目构建,使用Spring Boot Cli命令行工具和Gradle构建方式请参考官网。@H_403_0@(1)创建项目:@H_403_0@创建类型为quickstart的Maven项目,删除默认生成的.java文件保持默认的Maven目录即可。@H_403_0@(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 @H_403_0@文件: org.springframework.web.bind.annotation.* @RequestMapPing("/" "Hello World!" }@H_403_0@文件: org.springframework.boot.* org.springframework.boot.autoconfigure.* simple.controller.* main(String[] args) SpringApplication.run( Object[] { Application.,HomeController. }@H_403_0@<span >在浏览器中输入http://localhost:8080/,即可直接看到"Hello World"运行结果
<span >。2. 添加数据访问支持@H_403_0@(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