简单的说Springboot 是一个用于加速构建基于Spring的应用程序的框架,你能够把他叫作Spring集成框架,Spring敏捷开发框架;html
Spring boot,让建立一个可用于生产级别的Spring应用程序变得很是简单,大多数Springboot应用程序须要仅最少的Spring配置。java
ok,能够看得出Springboot并非提供了某种新功能而是,将原来繁琐的框架整合变得更简单了;这样一来咱们即可以专一于务逻辑的实现;mysql
缘由很简单了,以往在开发一个基于Spring的web项目时咱们须要编写大量的配置文件,很是的耗时和低效,且容易出错,可是咱们也会发现大多数应用程序的配置信息都是同样的,在这个敏捷开发大行其道的时代,Java也须要有一个可以大幅加快开发速度的框架,Spring boot应运而生, 我相信即便没有Spring boot 也会出现这样一个框架;web
回顾一下咱们以往在开发一个ssm项目时咱们要作什么事情:spring
Spring boot:sql
须要注意的是Spring boot 是须要依赖构建工具的,能够是maven和gradle;数据库
起步依赖(starter)后端
起步依赖本质上就是一个pom,(项目对象模型),其中包含了某个框架/功能运行所需的基础依赖信息,当咱们须要某个框架/功能时加入该框架/功能的依赖便可浏览器
自动配置tomcat
Spring Boot会在启动时完成对各个框架的自动配置,考虑了众多因素,最终决定Spring配置应该用哪一个,不应用哪一个, 该过程是Spring自动完成的;
内置tomcat 或jetty等servlet容器
没有代码生成,不须要XML配置
尽量自动配置Spring容器中的bean;
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.4.RELEASE</version> </parent>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
//引导类不容许放在根目录下 @SpringBootApplication public class Application { public static void main(String[] args) { //启动Spring boot 须要指定引导类 SpringApplication.run(Application.class); } }
@RestController public class HelloController { @RequestMapping("/hello/{name}") public String helloTest(@PathVariable String name){ return "hello spring : "+name; } }
注意:引导类不能放在根目录下;
maven能够帮助咱们在运行前检查依赖关系
添加插件
<plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins>
maven命令
spring-boot:run
添加了starter-web起步依赖后,工程就完成了Spring 和SpringMVC相关依赖和整合配置,咱们已经能够处理客户端的请求并返回响应了,可是咱们的数据是存储在数据库的,须要整合mybatis到项目中
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <!--mybatis不能肯定咱们要连接的数据库类型 全部须要手动导入MySQL驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` char(10) DEFAULT NULL, `password` char(12) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; INSERT INTO `user` VALUES (1,'admin','123321'),(2,'jerry','123'),(3,'22','222');
public class User { private Integer id; private String username; private String password; set/get }
package com.yyh.mapper; import com.yyh.pojo.User; import java.util.List; @Mapper //mybatis 能识别带有该注解的mapper public interface UserMapper { public List<User> selectAllUser(); }
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.yyh.mapper.UserMapper"> <select id="selectAllUser" resultType="user"> select * from user </select> </mapper>
@RestController public class UserController { @Autowired private UserMapper userMapper; @GetMapping("/user/list") @ResponseBody public List<User> getAllUsers(){ return userMapper.selectAllUser(); } }
在resources下建立名称为application.properties的文件,内容以下,另外spring还支持yml格式的配置文件
#DB Configuration: spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/boot? serverTimezone=Asia/Shanghai spring.datasource.username=root spring.datasource.password=admin #spring集成Mybatis环境 #pojo别名扫描包 mybatis.type-aliases-package=com.yyh.pojo
若没有使用@Mapper注解须要在启动类中添加扫描包信息
@SpringBootApplication @MapperScan("com.yyh.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
默认状况下,maven在打包时会将java目下的*.java
以及resource下的配置文件诸如(*.xml,*.properties
)等编译到target中,这个两个文件夹都属于classpath,一些人为了方便查找可能会将mapper文件和接口文件一块儿放在java目录下,可是maven并不会将java目录下的xml进行编译的,这时能够在pom中指定要编译的目录信息
<resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> <filtering>false</filtering> </resource> </resources>
在这以前咱们一直在使用jsp来做为视图层,可是jsp必须依赖servlet容器,浏览器没法直接渲染jsp文件每次开发调试想要看到最新的效果都必须从新编译jsp,因此Spring boot 并不推荐咱们使用jsp,视图层可使用模板引擎,目前主流的有thymeleaf,和freemaker,官方推荐thymeleaf;
Spring boot 提供了thymeleaf的默认配置,因此在spring boot 中使用thymeleaf很是方便,Spring boot 还设置了thymeleaf的默认视图解析器,能够像操做jsp同样来操做Thymeleaf。Controller层几乎没有任何区别,就是在模板语法上有区别。
须要强调的是:模板依然是在后端完成渲染(先后端不分离),运行流程和jsp是同样的,关于thymeleaf详细用法不是本章的重点,参见官网。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
注意:模板必须放到src/main/resources/templates目录下。
<!DOCTYPE html> <!--添加名称空间后idea能够提示补全--> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <title>user list</title> </head> <body> 用户列表:<br> <table border="1"> <tr> <th>id</th> <th>username</th> <th>password</th> </tr> <tr th:each="user:${userList}"> <td th:text="${user.id}"></td> <td th:text="${user.username}"></td> <td th:text="${user.password}"></td> </tr> </table> </body> </html>
@Service public class UserServiceImpl implements UserService { @Autowired private UserMapper mapper; public List<User> getAall(){ return mapper.selectAllUser(); } }
@RestController public class UserController { @Autowired private UserService userService; @GetMapping("/user/list") @ResponseBody public List<User> getAllUsers(){ return userService.getAall(); } }
当咱们controller和service开发完毕调试视图时,热部署能够在检测到页面修改后自动从新加载,方便调试
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <!--若不生效能够在build中配置fork 表示插件在独立的进程中运行--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin>
File-Settings-Compiler-Build Project automatically
ctrl + shift + alt + /,选择Registry,勾上 Compiler autoMake allow when app running
在SpringMVC中须要手动配置静态资源的映射关系,而Spring boot 默认以及添加了静态资源的配置,能够将静态资源请求映射到resources下的static目录
咱们来看一下目前的目录结构:
这也是一个Spring boot过程基本的目录结构
当咱们须要单独测试容器中某个bean时,从头至尾的执行一遍业务逻辑是很是耗时的,这时就能够Junit来进行单元测
添加起步依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class)//启动类 public class BootTest { //注入须要测试的bean @Autowired UserService userService; @Test public void test1() { List<User> aall = userService.getAall(); System.out.println(aall); } }
也可使用SpringJUnit4ClassRunner来做为测试引擎。
以上就是小编整理的SpringBoot入门教程,只是小编我的的看法,哪里有不许确的地方,还请各位大佬多多指出,我们共同进步!!!
喜欢文章请多多点赞评论转发,关注小编,后续小编会为你们带来更丰富的学习内容~~~~