Java Web系列:Spring Boot 基础

Java Web系列:Spring Boot 基础,第1张

概述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 com.h2database h2 runtime @H_403_0@如果需要在控制台查看生成SQL语句,可以添加/src/main/resources/application.propertIEs

logging.level.org.hibernate.sql=deBUG@H_403_0@(2)添加实体

@H_403_0@添加User、Role、category和Post实体。

@H_403_0@User:

java.util.* javax.persistence.* @ManyToMany(cascade = List roles = ArrayList .ID = .username = .password = Email = List setRoles(List .roles = Version = }@H_403_0@Role:

java.util.* javax.persistence.* @ManyToMany(cascade = List users = ArrayList .ID = .rolename = List setUsers(List .users = }@H_403_0@category:

java.util.* javax.persistence.* List posts = ArrayList .ID = name = List setposts(List .posts = }@H_403_0@Post:

java.util.* javax.persistence.* .ID = name = HTML = Text = CreateAt = .category = }@H_403_0@(3)添加资源库

@H_403_0@添加UserRepository、RoleRepository、categoryRepository和PostRepository接口,无需实现。

@H_403_0@UserRepository:

org.springframework.data.repository.* simple.domain.* UserRepository CrudRepository }@H_403_0@RoleRepository

org.springframework.data.repository.* simple.domain.* RoleRepository CrudRepository }@H_403_0@categoryRepository

org.springframework.data.repository.* simple.domain.* categoryRepository CrudRepository }@H_403_0@PostRepository

<span >import org.springframework.data.repository.*<span >;

<span >import simple.domain.*<span >;

<span >public <span >interface PostRepository <span >extends CrudRepository<User,Long><span > {

}

@H_403_0@(4)在控制器中注入资源库接口

org.springframework.beans.factory.annotation.* org.springframework.web.bind.annotation.* simple.repository.* .userRepository = .roleRepository = .categoryRepository = .postReppository = @RequestMapPing("/" }@H_403_0@使用事务时在方法上应用注解@Transactional

3.添加验证和授权支持@H_403_0@(1)添加依赖

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 org.springframework.boot spring-boot-starter-security @H_403_0@(2)修改Application.java

org.springframework.boot.* org.springframework.boot.autoconfigure.* org.springframework.security.config.annotation.method.configuration.* simple.controller.* @EnableGlobalMethodSecurity(securedEnabled = ,prePostEnabled = main(String[] args) SpringApplication.run( Object[] { Application.,args); MyWebSecurityConfigurer configure(httpSecurity http) http.authorizeRequests().antMatchers("/account**","/admin**" http.formLogin().usernameParameter("username").passwordParameter("password").loginPage("/login" .loginProcessingUrl("/login").successHandler( .and().logout().logoutUrl("/logout").logoutSuccessUrl("/" http.rememberMe().rememberMeParameter("rememberMe" }@H_403_0@访问http://localhost:8080/account会自动跳转到login登录页。Spring Security的具体使用前文已有所述。

@H_403_0@

参考:@H_403_0@(1)https://github.com/spring-projects/spring-boot

@H_403_0@(2)http://projects.spring.io/spring-boot/

@H_403_0@(3)https://github.com/qibaoguang/Spring-Boot-Reference-GuIDe/blob/master/SUMMARY.md

总结

以上是内存溢出为你收集整理的Java Web系列:Spring Boot 基础全部内容,希望文章能够帮你解决Java Web系列:Spring Boot 基础所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1270036.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存