前言:本文主要介绍使用IDEA从零开始搭建SpringBoot+Mybatis-Plus项目的过程
Step1: 创建SpringBoot项目
1. File -- New -- Project -- Maven

2. 填写项目信息

3. 设置工程名及存放路径

4. 构建项目基本结构

注意:
1.请留意java、resources这两个文件夹的标记是否正确
2.查看方式:选中文件夹 -- 右键 -- Mark Directory as
3.java文件夹需标记为Sources Root;resources文件夹需标记为Resources Root
Step2:项目初始化设置
1. Maven设置:
1.1. File -- Settings -- 搜索框中输入Maven

说明:
1.settings.xml可以在Maven安装目录的conf文件夹中找到
2.建议将文件中的镜像设置为阿里云仓库,同时设置好本地仓库的存放路径

1.1. 修改工程目录中的pom.xml文件,添加SpringBoot、MyBatis-Plus相关依赖(参考依赖如下)
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.study</groupId>
<artifactId>construct</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<dependencies>
<!--boot核心-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.3.0</version>
</dependency>
<!--测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<!--lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>
2. SpringBoot设置:
2.1. resources文件夹中新建配置文件:application.yml (目录结构如下)

2.2. 编辑yml文件(参考设置如下):
server:
port: 8080 # 服务器端口号设置
mybatis-plus:
mapper-locations: classpath:mapper/*.xml # 配置mapper.xml路径
check-config-location: true # 检查xml文件是否存在
configuration:
map-underscore-to-camel-case: true # 开启驼峰命名法
type-aliases-package: com.study.construct.entity # 实体类存放位置
spring:
datasource: # 数据库配置
url: jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8 # 数据库连接地址
driver-class-name: com.mysql.cj.jdbc.Driver # 数据库驱动
username: root # 用户名
password: 123456 # 密码
说明:mybatis-plus还可以有很多其他的配置,详情请参考官方文档 - 使用配置
2.3. 创建SpringBoot启动类(位置与entity、mapper等包同级)

2.4. 编写启动类代码
package com.study.construct;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 启动类代码
*/
@SpringBootApplication
@MapperScan("com.study.construct.mapper") // 注解方式设置mapper扫描路径
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
2.4. 启动项目,选择Application类 -- 右键 -- Run Application (当出现下图,则表示启动成功)

网友评论