一、基本搭建搭建

1 使用maven工程搭建

1.1 创建一个maven项目

(和以前一样)
image.png
选用骨架

1.2 添加依赖

配置pom.xml文件,添加springboot的依赖
spring4.3版本 —-> 至少JDK1.7
springboot1.x —-> 基于spring4.x 版本 —-> 至少jdk1.7
springboot2.x —-> 基于spring5.x版本 —-> 至少jdk1.8

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.5.12</version>
  5. </parent>
  6. <modelVersion>4.0.0</modelVersion>
  7. <groupId>com.hua</groupId>
  8. <artifactId>springboot01_demo</artifactId>
  9. <version>0.0.1-SNAPSHOT</version>
  10. <name>springboot01_demo</name>
  11. <description>springboot01_demo</description>
  12. <properties>
  13. <java.version>1.8</java.version>
  14. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  15. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  16. </properties>
  17. <dependencies>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-web</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-test</artifactId>
  25. <scope>test</scope>
  26. </dependency>
  27. </dependencies>
  28. <build>
  29. <plugins>
  30. <plugin>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-maven-plugin</artifactId>
  33. </plugin>
  34. </plugins>
  35. </build>

1.3 编写一个controller

(和springMVC一样)

  1. import org.springframework.stereotype.Controller;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.ResponseBody;
  4. @Controller
  5. @RequestMapping("user")
  6. public class UserController {
  7. @RequestMapping("test1")
  8. @ResponseBody
  9. public String test1(){
  10. System.out.println("userController的test1方法被执行了!!!");
  11. return "ok";
  12. }
  13. }

1.4 编写一个启动类

备注:springboot内置tomcat,可以不用外部tomcat而直接运行springboot项目
启动类需要在com.baizhi包下新建(强调)
原因:
启动类的扫描默认是扫描与该启动类同包以及其子包下的类,扫描其他类,需要指定位置。

  1. import org.mybatis.spring.annotation.MapperScan;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. //标注启动类
  6. @MapperScan("com.baizhi.dao")
  7. public class Application { //我们自己的启动类,Application类名随便
  8. public static void main(String[] args) {
  9. //run方法的第一个参数: 自己启动类.class
  10. // 第二个参数:args当前main函数的形参,目前没啥
  11. SpringApplication.run(Application.class,args);
  12. }
  13. }

SpringBoot搭建 - 图2

  1. @SpringBootApplication SpringApplication.run(启动类.class,args)

1.5 新建 application.properties

在resources目录下新建一个application.properties文件(file文件)
application.properties 文件名固定写法,不要写错。

  1. # 配置项目名称,默认没有项目名称
  2. server.servlet.context-path=/springboot-day1
  3. # 配置端口号,默认8080
  4. server.port=8989

1.6 执行启动类

执行Application,即可启动项目,不需要配置外部tomcat
即运行Application启动类的主函数

1.7 访问测试

SpringBoot搭建 - 图3
整个项目的结构如下:
SpringBoot搭建 - 图4

2 Spring Initializr搭建

2.1 选择Spring Initializr

本质上说,Spring Initializr是一个Web应用,它提供了一个基本的项目结构,能够帮助我们快速构建一个基础的Spring Boot项目
image.png

  1. Project SDK”用于设置创建项目使用的JDK版本,这里,使用之前初始化设置好的JDK版本即可;
  2. 在“Choose Initializr Service URL(选择初始化服务地址)”下使用默认的初始化服务地址“https://start.spring.io”进行Spring Boot项目创建(注意使用快速方式创建Spring Boot项目时,所在主机须在联网状态下)
  3. 也可以选择其他初始化服务地址,如https://start.aliyun.com

image.png

  1. 新版本的IDEA取消了Spring Initializr,可以下载插件,进行使用。

image.png

2.2 配置依赖声明

image.png

2.3 添加组件

选择web场景中的web依赖。
image.png

2.4 生成效果

  1. 生成了启动类
  2. 导入父pom工程
  3. resources目录下有static和templates目录。

SpringBoot搭建 - 图10
使用Spring Initializr方式构建的Spring Boot项目会默认生成项目启动类、存放前端静态资源和页面的文件夹、编写项目配置的配置文件以及进行项目单元测试的测试类。

2.5 创建Controller

  1. @RestController // 该注解为组合注解,等同于Spring中@Controller+@ResponseBody注解
  2. public class DemoController {
  3. @RequestMapping("/demo")
  4. public String demo(){
  5. return "hello springBoot";
  6. }
  7. }

2.6 运行项目

运行主程序启动类SpringbootDemoApplicat
ion,项目启动成功后,在控制台上会发现Spring Boot项目默认启动的端口号为8080,此时,可以在浏览器上访问“http://localhost:8080/hello”
SpringBoot搭建 - 图11

页面输出的内容是“hello Spring Boot”,至此,构建Spring Boot项目就完成了

二、整合其他框架

3 springboot整合Mybatis

springboot整合mybatis完成DAO层开发

3.1 添加依赖

新增两个依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>junit</groupId>
  4. <artifactId>junit</artifactId>
  5. <version>4.12</version>
  6. <scope>test</scope>
  7. </dependency>
  8. <!-- springboot整合web开发的 -->
  9. <dependency>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-web</artifactId>
  12. <version>2.1.4.RELEASE</version>
  13. </dependency>
  14. <!-- springboot整合junit测试-->
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-starter-test</artifactId>
  18. <version>2.1.4.RELEASE</version>
  19. </dependency>
  20. <!--两个核心依赖-->
  21. <!-- springboot整合mybatis-->
  22. <dependency>
  23. <groupId>org.mybatis.spring.boot</groupId>
  24. <artifactId>mybatis-spring-boot-starter</artifactId>
  25. <version>1.3.2</version>
  26. </dependency>
  27. <!-- mysql驱动-->
  28. <dependency>
  29. <groupId>mysql</groupId>
  30. <artifactId>mysql-connector-java</artifactId>
  31. <version>5.1.34</version>
  32. </dependency>
  33. </dependencies>

建表、实体类、DAO接口、Mapper文件(和以前一致)

3.2 配置application.properties

(用于替换applicationContext.xml)

#配置项目名称,默认没有项目名
server.servlet.context-path=/springboot-day1
#配置端口号,默认是8080
server.port=8989

#数据源 url driver username password
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456


#mybatis相关的配置
#注册mapper文件
mybatis.mapper-locations=classpath:mappers/*Mapper.xml
#实体类起别名
mybatis.type-aliases-package=com.baizhi.entity

#告诉pageHelper你使用的数据库,可以省略不写,因为底层会根据我们的的url判断是哪个数据库
#pagehelper.helper-dialect=mysql

3.3 集成连接池

springboot自动内置hikariCP连接池、jackson(json转换工具)
如果不想用内置连接池,可以集成其他连接池,如druid连接池
SpringBoot搭建 - 图12

SpringBoot搭建 - 图13

4 Springboot访问webapp的jsp

问题:在webapp下新建show.jsp(页面)无法访问

4.1 使用插件启动springboot项目

<build>
  <plugins>
<!-- spring-boot-maven-plugin插件作用:
        通过该插件启动springboot项目,进而可以访问webapp下的资源 -->
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <version>2.1.4.RELEASE</version>
      <!--解决maven插件启动时中文乱码问题
        如果不乱码,可以不配置-->
      <configuration>
        <fork>true</fork>
        <!-- spring-boot:run 中文乱码解决 -->
        <jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
      </configuration>
    </plugin>
  </plugins>
</build>

注意:

  1. 插件版本要和springboot保持一致。
  2. 此时已经可以访问html等页面资源,但仍不可以访问jsp。
  3. 修改java代码与配置后,要先点击控制台处的红色按钮(stop),再双击插件。

最好选择控制台的重启按钮。

4.2 添加依赖

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-tomcat</artifactId>
  <version>2.1.4.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.apache.tomcat.embed</groupId>
  <artifactId>tomcat-embed-jasper</artifactId>
  <version>8.5.34</version>
</dependency>

在添加两个依赖就可以访问jsp资源了。

4.3 正常访问webapp下的页面

SpringBoot搭建 - 图14

4.3 热部署

使webapp目录下jsp文件可以修改即使用。

# jsp (webapp下的)热部署设置
server.servlet.jsp.init-parameters.development=true

5 整合分页插件pageHelper

分页插件 springboot 整合pageHelper

5.1 添加依赖

<dependency>
  <groupId>com.github.pagehelper</groupId>
  <artifactId>pagehelper-spring-boot-starter</artifactId>
  <version>1.2.5</version>
 </dependency>

备注:添加的依赖和以前不同,但是java代码相同,并且不需要做额外配置

5.2 使用pageHelper完成分页

SpringBoot搭建 - 图15