前言:本文主要介紹使用IDEA從零開始搭建SpringBoot+Mybatis-Plus項(xiàng)目的過程
Step1: 創(chuàng)建SpringBoot項(xiàng)目
1. File -- New -- Project -- Maven
2. 填寫項(xiàng)目信息
3. 設(shè)置工程名及存放路徑
4. 構(gòu)建項(xiàng)目基本結(jié)構(gòu)
注意:
1.請(qǐng)留意java听哭、resources這兩個(gè)文件夾的標(biāo)記是否正確
2.查看方式:選中文件夾 -- 右鍵 -- Mark Directory as
3.java文件夾需標(biāo)記為Sources Root骂铁;resources文件夾需標(biāo)記為Resources Root
Step2:項(xiàng)目初始化設(shè)置
1. Maven設(shè)置:
1.1. File -- Settings -- 搜索框中輸入Maven
說明:
1.settings.xml可以在Maven安裝目錄的conf文件夾中找到
2.建議將文件中的鏡像設(shè)置為阿里云倉庫览濒,同時(shí)設(shè)置好本地倉庫的存放路徑
1.1. 修改工程目錄中的pom.xml文件拆内,添加SpringBoot夫偶、MyBatis-Plus相關(guān)依賴(參考依賴如下)
<?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設(shè)置:
2.1. resources文件夾中新建配置文件:application.yml (目錄結(jié)構(gòu)如下)
2.2. 編輯yml文件(參考設(shè)置如下):
server:
port: 8080 # 服務(wù)器端口號(hào)設(shè)置
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 # 實(shí)體類存放位置
spring:
datasource: # 數(shù)據(jù)庫配置
url: jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8 # 數(shù)據(jù)庫連接地址
driver-class-name: com.mysql.cj.jdbc.Driver # 數(shù)據(jù)庫驅(qū)動(dòng)
username: root # 用戶名
password: 123456 # 密碼
說明:mybatis-plus還可以有很多其他的配置冤灾,詳情請(qǐng)參考官方文檔 - 使用配置
2.3. 創(chuàng)建SpringBoot啟動(dòng)類(位置與entity球昨、mapper等包同級(jí))
2.4. 編寫啟動(dòng)類代碼
package com.study.construct;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 啟動(dòng)類代碼
*/
@SpringBootApplication
@MapperScan("com.study.construct.mapper") // 注解方式設(shè)置mapper掃描路徑
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}