SpringBoot概要
SpringBoot是由Pivotal团队提供的全新框架,其设计的目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义模板化的配置。
SpringBoot不是编写应用程序的框架,它可以帮助我们以最少的配置或零配置开发和构建,打包和部署应用程序。
SpringBoot不是应用程序服务器。但是它是提供应用程序服务器功能的嵌入式servlet容器,而不是SpringBoot本身。
SpringBoot不实现任何企业Java规范,例如JPA或JMS。例如,SpringBoot不实现JPA,但它通过为JPA实现(例如Hibernate)自动配置适当的bean来支持JPA。
SpringBoot不使用任何形式的代码生成来完成它的功能。它是利用Spring 4的条件配置功能,以及Maven和Grandle提供的传递依赖关系解析,以在Spring应用程序上下文中自动配置bean。
SpringBoot的优点和限制
SpringBoot优点:
1:使用Java或Groovy开发基于Spring的应用程序非常容易。
2:它减少了大量开发时间并提高了生产力。
3:它避免了编写大量的样板代码,注释和XML配置。
4:SpringBoot应用程序与其Spring生态系统(如Spring JDBC,Spring ORM,Spring Data,Spring Security等)集成非常容易。
5:它遵循“自用默认配置”方法,以减少开发工作量。
6:它提供嵌入式HTTP服务器,如Tomcat,Jetty等,使开发和测试Web应用程序非常容易。
7:它提供CLI(命令行界面)工具以命令提示符,非常容易和快速的开发和测试SpringBoot(Java或Groovy)应用程序。
8:她提供了许多插件来开发和测试Spring启动应用程非常容易使用构建工具,如Maven和Gradle。
9:它提供了许多插件使嵌入式和内存数据库工作非常容易。
SpringBoot限制
将现有或者传统的Spring Framework项目转换为Spring Boot应用程序是一个非常困难和耗时的过程。它仅适用于全新Spring项目。
SpringBoot项目构建
Maven项目构建
访问:http://start.spring.io/
选择:Generate a Maven Project with Java and Spring Boot 2.0.2
填入:Group、Artifact
添加:Dependencies
Web(JPA、MyBatis、MySQL、Freemarker、Security、Redis、JMS(ActiveMQ)、Solr )
点击:Generate Project
将下载的压缩文件,解压缩放入IDE的Workspace中
在IDE中导入已存在的Maven项目
POM依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>provided</scope> </dependency> |
项目结构
src/main/java (Java源代码) package groupId.artifactId (SpringbootApplication.java,启动程序、框架配置) package groupId.artifactId.controller (控制器Controller) package groupId.artifactId.service (业务Service) package groupId.artifactId.domain (实体Entity和数据访问Repository)
src/main/resources (配置文件、静态资源、模板)
src/test/java (测试程序) |
编写Controller
@RestController @RequestMapping(value= {"","/index"}) public class IndexController { @RequestMapping(value= {"","/index"}) public String index() { return "Hello SpringBoot"; } } |
Thymeleaf视图解析器
Thymeleaf视图解析器属于第四代视图解析器。SpringBoot目前比较建议使用的视图解析器。提倡在标签内部来写代码。
Thymeleaf的配置
POM添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> |
配置文件:application.properties
spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.mode=HTML5 spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.cache=false spring.resources.chain.strategy.content.enabled=true spring.resources.chain.strategy.content.paths=/** |
修改Controller
@Controller @RequestMapping(value = { "", "/index" }) public class IndexController { @RequestMapping(value = { "", "/index" }) public String index(Model model) { return "/index/index"; } } |
创建视图View
src/main/resources目录下: |
static/ 静态资源 |
templates/ 视图文件 |
Thymeleaf的使用
替换Text: th:text="${varible}" |
替换HTML: th:utext="${varible}" |
循环: th:each="iterator:${Collection}" |
判断: th:text="logic?true:false" th:if="logic" |
路径: th:src="@{/url/}+${varible}" |
Spring Data JPA
实现数据持久层,开发效率更高。
Spring Data JPA配置
添加POM依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> |
配置文件application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/springboot spring.datasource.username=root spring.datasource.password= spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.properties.hibernate.hbm2ddl.auto=update spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.show-sql= true |
ORM
@Entity |
@Id |
@GeneratedValue |
Repository基本使用
创建接口: public interface EntityRepository extends JpaRepository<Entity, ID> { } |
父接口中含有以下方法: |
List<Entity> findAll(); Entity findOne(ID); void save(Entity); void delete(Entity); Long count(); boolean exists(ID); |
Repository扩展使用
//判断值与列相等 List<Product> findByProductName(String productName); |
//条件比较 List<Product> findByProductPriceGreaterThan(Double price); List<Product> findByProductPriceGreaterThanEqual(Double price); List<Product> findByProductPriceLessThan(Double price); List<Product> findByProductPriceLessThanEqual(Double price); List<Product> findByProductPriceBetween(Double min,Double max); |
//多条件判断 List<Product> findByProductPriceAndProductStatus(Double price,Integer status); List<Product> findByProductPriceOrProductStatus(Double price,Integer status); |
//判断列与值匹配 List<Product> findByProductNameLike(String productName); |
//in范围匹配 List<Product> findByProductIdIn(Collection ids); |
//分页排序查询 Page<Product> findAll(Pageable pageable); |
//自定义查询 @Modifying @Query("from Product p where p.productId = 5") List<Product> myQuery(); |