加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Java Web系列:Spring Boot 基础

发布时间:2020-12-15 01:52:15 所属栏目:大数据 来源:网络整理
导读:项目(参考1) 提供了一个类似ASP.NET MVC的默认模板一样的标准样板,直接集成了一系列的组件并使用了默认的配置。使用Spring Boot 不会降低学习成本,甚至增加了学习成本,但显著降低了使用成本并提高了开发效率。如果没有Spring基础不建议直接上手。 1.基

项目(参考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

文件:

org.springframework.web.bind.annotation.* @RequestMapping("/" "Hello World!" }

文件:

org.springframework.boot.* org.springframework.boot.autoconfigure.* simple.controller.* main(String[] args) SpringApplication.run( Object[] { Application.,HomeController. }

2. 添加数据访问支持

(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 = }

Role:

java.util.* javax.persistence.* @ManyToMany(cascade = List users = ArrayList .id = .roleName = List setUsers(List .users = }

Category:

java.util.* javax.persistence.* List posts = ArrayList .id = Name = List setPosts(List .posts = }

Post:

java.util.* javax.persistence.* .id = Name = Html = Text = CreateAt = .category = }

(3)添加资源库

添加UserRepository、RoleRepository、CategoryRepository和PostRepository接口,无需实现。

UserRepository:

org.springframework.data.repository.* simple.domain.* UserRepository CrudRepository }

RoleRepository

org.springframework.data.repository.* simple.domain.* RoleRepository CrudRepository }

CategoryRepository

org.springframework.data.repository.* simple.domain.* CategoryRepository CrudRepository }

PostRepository

<span style="color: #0000ff;">import org.springframework.data.repository.*<span style="color: #000000;">;

<span style="color: #0000ff;">import simple.domain.*<span style="color: #000000;">;

<span style="color: #0000ff;">public <span style="color: #0000ff;">interface PostRepository <span style="color: #0000ff;">extends CrudRepository<User,Long><span style="color: #000000;"> {

}

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

org.springframework.beans.factory.annotation.* org.springframework.web.bind.annotation.* simple.repository.* .userRepository = .roleRepository = .categoryRepository = .postReppository = @RequestMapping("/" }

使用事务时在方法上应用注解@Transactional

3.添加验证和授权支持

(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

(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" }

访问http://localhost:8080/account会自动跳转到login登录页。Spring Security的具体使用前文已有所述。

参考:

(1)https://github.com/spring-projects/spring-boot

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读