1.创建项目
2.修改编码格式为UTF-8
3.检查或配置代码⾃动补全功能
4.检查或配置⾃动导包
5.检查或配置Maven,可以⽤国内仓库镜像
6.idea识别Maven项⽬:在Notifications视图中会提⽰Load Maven Project,点击即可。
7.开启热部署
a.确认 pom.xml中已经引⽤依赖
!-- dependencies 标签下检查是否引⼊spring-boot-devtools依赖,如果没有加⼊如下引⽤ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
b.在Settings对应栏⽬中勾选如下选项
8.选择YAML或Properties文件
项⽬中可以选择YAML和Properties⽂件做为配置⽂件(可以同时存在),这里使用application.yml文件
# application.yml
# Spring 全局配置
spring:
application:
name: java_forum # 项目名称
output:
ansi:
enabled: ALWAYS # 控制台输出彩色日志
datasource:
url: jdbc:mysql://127.0.0.1:3306/java_forum?characterEncoding=utf8&useSSL=false # 数据库连接串
username: root # 数据库用户名
password: # 数据库密码
driver-class-name: com.mysql.jdbc.Driver # 数据库驱动类
mvc:
pathmatch:
matching-strategy: ANT_PATH_MATCHER #Springfox-Swagger兼容性配置
# JSON序列化配置
jackson:
date-format: yyyy-MM-dd HH:mm:ss # 日期格式
default-property-inclusion: NON_NULL # 不为null时序列化
server:
port: 58080 # 修改Tomcat的默认端口号
# 日志配置
logging:
pattern:
date format: yyyy-MM-dd HH:mm:ss
level:
root: info # 默认日志级别
com.example.demo: debug # 指定包的日志级别
file:
path: D:\git_file\java_forum # 日志保存目录
# mybatis 相关配置,单独配置,顶格写
mybatis:
mapper-locations: classpath:mapper/**/*.xml # 指定 xxxMapper.xml的扫描路径
# 项目自定义相关配置
java-forum:
login:
url: sign-in.html # 未登录状况下强制跳转页面
index: # 首页配置节点
board-num: 9 # 首页中显示的版块个数
9.环境测试
在JavaForumApplication.java同级创建controller包,controller包下创建TestController.java
// Controller注解,返回的对象⽤JSON形式表⽰
@RestController
// 指定测试根路径映射地址前缀
@RequestMapping("/test")
public class TestController {
@ApiOperation(("测试打印"))
@GetMapping("/hello")
public String hello(){
return "hello";
}
}
10.测试Maven
在pom.xml⽂件的properties标签下加⼊如下配置:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>java_forum</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>java_forum</name>
<description>基于 Spring 前后端分离版本的论坛系统</description>
<properties>
<!-- 编译环境JDK版本 -->
<maven.compiler.source>${java.version}</maven.compiler.source>
<!-- 运⾏环境JVM版本 -->
<maven.compiler.target>${java.version}</maven.compiler.target>
<!-- 构建项⽬指定编码集 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 管理依赖版块号-->
<!-- mysql-connector 数据库链接驱动包 -->
<mysql-connector.version>5.1.49</mysql-connector.version>
<!-- mybatis -->
<mybatis-starter.version>2.3.0</mybatis-starter.version>
<!-- 数据源,阿里出的数据源工具 -->
<druid-starter.version>1.2.16</druid-starter.version>
<!-- mybatis生成器 -->
<mybatis-generator-plugin-version>1.4.1</mybatis-generator-plugin-version>
<!-- springfox - Swagger -->
<springfox-boot-starter.version>3.0.0</springfox-boot-starter.version>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--热部署插件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector.version}</version>
</dependency>
<!-- mybatis 依赖
其中已经包含了spring-jdbc不再重复引⽤,
此项⽬中使⽤spring-jdbc提供的HikariCP做为数据源, 相关配置在yml⽂件中
-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-starter.version}</version>
</dependency>
<!-- 阿⾥巴巴druid数据源,如果使⽤SpringBoot默认的数据源,删除或注释这个依赖即可 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid-starter.version}</version>
</dependency>
<!-- API文档生成,基于swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>${springfox-boot-starter.version}</version>
</dependency>
<!-- SpringBoot健康监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 编码解码加密工具包-->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- mybatis ⽣成器插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>${mybatis-generator-plugin-version}</version>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<!--指定Maven中的执行阶段 -->
<phase>deploy</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<!-- 相关配置 -->
<configuration>
<!-- 打开⽇志 -->
<verbose>true</verbose>
<!-- 允许覆盖 -->
<overwrite>true</overwrite>
<!-- 配置⽂件路径 -->
<configurationFile>
src/main/resources/mybatis/generatorConfig.xml
</configurationFile>
</configuration>
</plugin>
</plugins>
</build>
</project>
11.在Maven栏⽬,双击package,运⾏命令
使⽤⽤浏览器访问:http://localhost:58080/test/hello,看到如下结果说明运⾏成功。
12.通过Git推送⾄远程仓库
查看当前状态,列出未修改后添加的⽂件:git status
添加修改后的⽂件到暂存区,再次运⾏git status,上⾯的⽂件会变为绿⾊显⽰:git add .
提交到本地仓库:git commit -m '备注'
推送到远程仓库:git push origin master