项目(参考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.*
figuration
main(String[] args) SpringApplication.run( Object[] { Application.,HomeController.
}
(1)修改pom,添加spring-boot-starter-data-jpa和依赖:
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
coperuntimecope
如果需要在控制台查看生成sql语句,可以添加/src/main/resources/application.properties
logging.level.org.hibernate.sql=debug
(2)添加实体
添加User、Role、Category和Post实体。
User:
display: none;" onclick="cnblogs_code_hide('cf95a9a7-f8b8-4ed3-b917-04ef0f6577e5',event)" src="/res/2019/02-07/23/405b18b4b6584ae338e0f6ecaf736533.gif" alt="">
java.util.*
javax.persistence.*
@ManyToMany(cascade = List roles = ArrayList
.id =
.userName =
getpassword() {
.password =
Email =
List
setRoles(List .roles =
Version = }
Role:
display: none;" onclick="cnblogs_code_hide('18d25569-722e-45bb-ac86-dfa64a549f7a',event)" src="/res/2019/02-07/23/405b18b4b6584ae338e0f6ecaf736533.gif" alt="">
java.util.*
javax.persistence.*
@ManyToMany(cascade = List users = ArrayList
.id =
.roleName =
List
setUsers(List .users = }
Category:
display: none;" onclick="cnblogs_code_hide('18a06c8d-331d-47ea-bcea-19adadc39101',event)" src="/res/2019/02-07/23/405b18b4b6584ae338e0f6ecaf736533.gif" alt="">
java.util.*
javax.persistence.*
etoMany
List posts = ArrayList
.id =
Name =
List
setPosts(List .posts = }
Post:
display: none;" onclick="cnblogs_code_hide('8254ac00-5c28-4ef7-8b00-56075c8d0552',event)" src="/res/2019/02-07/23/405b18b4b6584ae338e0f6ecaf736533.gif" alt="">
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
(1)添加spring-boot-starter-security依赖
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
coperuntimecope
org.springframework.boot
spring-boot-starter-security
(2)修改Application.java
org.springframework.boot.* org.springframework.boot.autoconfigure.* org.springframework.security.config.annotation.method.configuration.* ecurity.config.annotation.web.builders.HttpSecurity;
ecurity.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
ecurity.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
simple.controller.*
figuration
@EnableGlobalMethodSecurity(securedEnabled = ,prePostEnabled =
main(String[] args) SpringApplication.run( Object[] { Application.,args);
ecurityConfigurerAdapter webSecurityConfigurerAdapter() {
ecurityConfigurer();
MyWebSecurityConfigurer ecurityConfigurerAdapter {
configure(HttpSecurity http) disable();
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的具体使用前文已有所述。
bedac7966bcaa697e9921d77849.png" alt="">
参考:
(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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。