SpringBoot项目搭建
本文主要讲解如何快速搭建一个springboot项目,文章最末尾有代码 github 地址
Intellij创建项目
1、使用intellij创建一个新项目
2、选择springboot项目
3、输入包名称和项目名称
4、项目目录结构如下
功能介绍
- 数据库简单操作
- 使用log4j格式化日志打印
核心maven引入
<dependencies>
<!--引入web相关包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions><!-- 去掉springboot默认配置 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--引入mybatis插件-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!--导入mysql连接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--引入boot测试包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--引入lombok依赖,自动生成set/get等方法-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions><!-- 去掉springboot默认配置 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--引入日志打印模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.70</version>
</dependency>
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
核心功能代码
配置文件
核心配置
spring:
datasource:
#数据URL配置
url: jdbc:mysql://127.0.0.1:3306/educate?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8
#执行SQL语句模式,这里为每次启动都会执行
initialization-mode: always
schema: classpath:db/table.sql
data: classpath:db/data.sql
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
# schema-username: root
# schema-password: yinchong@123
# data-username: root
# data-password: yinchong@123
application:
name: boot-educate
server:
port: 8090
servlet:
context-path: /educate
mybatis:
#mapper文件路径位置
mapper-locations: classpath:mapper/*Mapper.xml
#别名扫描的包路