1、配置pom.xml
教程一创建的项目为maven项目,所以搭建一个Spring Boot的Web项目,先导入一下jar包:即在pom.xml以下依赖:
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-web</artifactId> 5 <exclusions> 6 <exclusion> 7 <artifactId>jackson-core</artifactId> 8 <groupId>com.fasterxml.jackson.core</groupId> 9 </exclusion> 10 <exclusion> 11 <artifactId>jackson-datatype-jsr310</artifactId> 12 <groupId>com.fasterxml.jackson.datatype</groupId> 13 </exclusion> 14 </exclusions> 15 </dependency> 16 17 <dependency> 18 <groupId>org.springframework.boot</groupId> 19 <artifactId>spring-boot-starter-test</artifactId> 20 <scope>test</scope> 21 <exclusions> 22 <exclusion> 23 <groupId>org.junit.vintage</groupId> 24 <artifactId>junit-vintage-engine</artifactId> 25 </exclusion> 26 </exclusions> 27 </dependency> 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-devtools</artifactId> 31 <optional>true</optional> <!-- 这个需要为 true 热部署才有效 --> 32 </dependency> 33 <!-- servlet依赖. --> 34 <dependency> 35 <groupId>javax.servlet</groupId> 36 <artifactId>javax.servlet-api</artifactId> 37 <scope>provided</scope> 38 </dependency> 39 <dependency> 40 <groupId>javax.servlet</groupId> 41 <artifactId>jstl</artifactId> 42 </dependency> 43 44 <!-- tomcat的支持.--> 45 <dependency> 46 <groupId>org.apache.tomcat.embed</groupId> 47 <artifactId>tomcat-embed-jasper</artifactId> 48 <scope>provided</scope> 49 </dependency> 50 <dependency> 51 <groupId>org.mybatis.spring.boot</groupId> 52 <artifactId>mybatis-spring-boot-starter</artifactId> 53 <version>1.3.2</version> 54 </dependency> 55 <dependency> 56 <groupId>mysql</groupId> 57 <artifactId>mysql-connector-java</artifactId> 58 <version>8.0.16</version> 59 </dependency> 60 <dependency> 61 <groupId>org.apache.maven.shared</groupId> 62 <artifactId>maven-filtering</artifactId> 63 <version>1.3</version> 64 <exclusions> 65 <exclusion> 66 <artifactId>plexus-interpolation</artifactId> 67 <groupId>org.codehaus.plexus</groupId> 68 </exclusion> 69 <exclusion> 70 <artifactId>plexus-interactivity-api</artifactId> 71 <groupId>org.codehaus.plexus</groupId> 72 </exclusion> 73 <exclusion> 74 <artifactId>classworlds</artifactId> 75 <groupId>classworlds</groupId> 76 </exclusion> 77 <exclusion> 78 <artifactId>plexus-utils</artifactId> 79 <groupId>org.codehaus.plexus</groupId> 80 </exclusion> 81 <exclusion> 82 <artifactId>plexus-container-default</artifactId> 83 <groupId>org.codehaus.plexus</groupId> 84 </exclusion> 85 </exclusions> 86 </dependency> 87 <dependency> 88 <groupId>org.springframework.boot</groupId> 89 <artifactId>spring-boot-starter-thymeleaf</artifactId> 90 </dependency> 91 <dependency> 92 <groupId>com.fasterxml.jackson.module</groupId> 93 <artifactId>jackson-module-parameter-names</artifactId> 94 </dependency> 95 <dependency> 96 <groupId>com.fasterxml.jackson.datatype</groupId> 97 <artifactId>jackson-datatype-jdk8</artifactId> 98 </dependency> 99 <dependency> 100 <groupId>com.fasterxml.jackson.datatype</groupId> 101 <artifactId>jackson-datatype-jsr310</artifactId> 102 <version>2.8.6</version> 103 </dependency> 104 105 </dependencies>
Springboot的默认视图支持是Thymeleaf,但是Thymeleaf我们不熟悉,我们熟悉的还是jsp。 所以下面是讲解如何让Springboot支持 jsp。
修改pom.xml文件,添加对jsp支持
1 <!-- servlet依赖 --> 2 <dependency> 3 <groupId>javax.servlet</groupId> 4 <artifactId>javax.servlet-api</artifactId> 5 </dependency> 6 <dependency> 7 <groupId>javax.servlet</groupId> 8 <artifactId>jstl</artifactId> 9 </dependency> 10 <!-- tomcat的支持--> 11 <dependency> 12 <groupId>org.apache.tomcat.embed</groupId> 13 <artifactId>tomcat-embed-jasper</artifactId> 14 </dependency>
数据库驱动依赖根据数据库版本修改
2、配置webapp
手动在src/main/目录下创建webapp/WEB-INF,然后打开以下界面,设置web目录
结果如下:
接下来配置application.properties,但是application.yml是树状结构,修改查看比较清晰直观,所以推荐使用。
在application.yml添加如下配置:
spring:
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
datasource:
url: jdbc:mysql://localhost:3306/?
username: root
password:
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
show-sql: true
generate-ddl: true
hibernate:
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL55Dialect
database: MYSQL
这样配置了数据库连接和控制web访问路径和格式。
3、配置开发目录和环境配置,在resources文件下创建mapper文件夹,主要放置*mapper.xml,其他的逻辑代码放置在java包下,注意不能放在启动类的外部,应该和启动类同级或启动类所在的包能包含别的代码所在的包,否则会扫描不到代码,启动时或者运行时会报错。
⚠️添加完依赖记得重新导入maven,然后查看是否有依赖冲突,以上项目配置的仅支持jdk1.8版本,别的版本会有不同的错误,但可以解决。