该文章是Java入门系列的第五章:springboot入门
MyBatis-Plus
首先我们打开MyBatis-Plus官网,点击“快速开始”,可以看到简介对MyBatis-Plus做了简单介绍,他是 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变。
引入 Spring Boot Starter 父工程
打开pom.xml文件,输入代码如下:
...
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
<relativePath/>
</parent>
...
</project>
引入依赖
还是在pom.xml文件,引入 spring-boot-starter
、spring-boot-starter-test
、mybatis-plus-boot-starter
、spring-boot-starter-web
、mysql-connector-java
依赖,代码如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
创建一个SpringBoot类
接下来我们开始编写代码,首先在 org 的 example 目录下,新建一个 Java 类,命名为 Application
Application 代码如下:
其中 @SpringBootApplication 这个注解表示这是一个 Spring Boot 启动类,SpringApplication.run(Application.class, args);
是固定写法,说明他是一个 Spring Boot 启动类
package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
添加 Spring Boot 的配置文件
然后我们在resources文件夹下添加 Spring Boot 的配置文件,新建-文件,命名为 application.yaml
编写代码如下,将其中的username和password改成自己数据库的信息,然后新建一个名为 student_manage 的数据库,url中填写的地址就是数据库的地址:
spring:
datasource:
username: root
password: 'root'
url: jdbc:mysql://127.0.0.1:3306/student_manage?characterEncoding=UTF-8&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
此时我们再回到Demo.java,运行后提示我们的项目已经启动在8080端口
在浏览器打开 http://localhost:8080/ ,发现是一个error页面,这是对的,因为我们还没有配置路由
写在最后
以上就是springboot入门的说明