一.创建SpringBoot项目
1.IDEA中创建SpringBoot项目,填写相关配置
2.添加所需依赖
二.在pom.xml文件中添加相关依赖
1.添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>RELEASE</version>
</dependency>
<!--mybatis-plus依赖包-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.5</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
</dependencies>
2.添加文件扫描
目的是在springboot项目启动时,加载这些类型的文件
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
三.创建application.yml文件
application.yml文件是SpringBoot项目的配置文件
spring:
datasource: #配置数据库信息
url: jdbc:mysql://localhost:3306/spring?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
username: ******填写你的数据库用户名
password: ******填写你的数据库密码
#mysql5对应为com.mysql.jdbc.Driver
#mysql8对应为com.mysql.cj.jdbc.Driver
driver-class-name: com.mysql.cj.jdbc.Driver
#配置mybatis-plus
mybatis-plus:
#告诉mybatis框架映射文件的位置
mapper-locations: classpath:com/polaris/polarisbootdemo1/dao/mapper/*.xml
#设置类型别名,在映射文件中可以直接写首字母小写的实体类名称即可
type-aliases-package: com.polaris.polarisbootdemo1.pojo
#驼峰映射:例如数据库中的字段名emp_name能对应实体类中的属性名empName
configuration:
map-underscore-to-camel-case: true
#在控制台查看sql语句
logging:
level:
com.oneRound.demo01: debug
四.在启动类添加注解
目的是在启动时扫描mapper接口类
@SpringBootApplication
@MapperScan(basePackages = "com.polaris.polarisbootdemo01.dao")
public class PolarisBootDemo01Application {
public static void main(String[] args) {
SpringApplication.run(PolarisBootDemo01Application.class, args);
}
}
对应项目结构:
其余Service,Mapper,Controller层编写略