一、创建Module项目
选择jdk版本
确定项目名和项目存放路径
二、修改 pom文件
添加阿里镜像
<!--阿里镜像-->
<repositories>
<repository>
<id>central</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<layout>default</layout>
<!-- 是否开启发布版构件下载 -->
<releases>
<enabled>true</enabled>
</releases>
<!-- 是否开启快照版构件下载 -->
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
使用父级配置可以使用默认mevan的版本省去version标签
也可以添加version覆盖父级默认
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/>
</parent>
下面依赖需要在<dependencies></dependencies>下,没有就自己添加
启动依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.5.6</version>
</dependency>
测试依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.5.6</version>
<scope>test</scope>
</dependency>
关于数据库
一般使用mybatis或mybatis-plus
这里使用mybatis-plus
<!--mybaits-plus包-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
关于一些其他相关的依赖自由添加
hutool工具
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.12</version>
</dependency>
文档类注释
swagger或knife4j
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-bean-validators</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-springdoc-ui</artifactId>
<version>3.0.3</version>
</dependency>
如果不想使用tomcat也可以用jetty
<!--Jetty是一个提供HHTP服务器、HTTP客户端和javax.servlet容器的开源项目。-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
lombok自动get、set
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
三、添加application.yml文件
在resource目录下添加yml
配置运行端口号和数据库
server:
port: 8888
spring:
datasource:
#数据库链接url
url: jdbc:mysql://ip:端口/数据库名?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
#数据库账号密码
username: root
password:
#数据库驱动类
driver-class-name: com.mysql.cj.jdbc.Driver
devtools:
restart:
poll-interval: 2s
quiet-period: 1999ms
四、编写启动类
src层级
controller->舍去service层,负责接收http请求、处理、返回数据
mapper-->对应数据库表的接口,加@mapper注解
model-->表的实体对象
启动类
package com.test1;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.test1.mapper")
@SpringBootApplication
public class SpringApplicationStarter {
public static void main(String[] args) {
SpringApplication.run(SpringApplicationStarter.class);
}
}
mapper 接口
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
controller 层
@RequestMapping("user")
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapper("test")
public String test(){return "Hello World";}
@PostMapping("insert")
public String insert(@RequestBody User user){
userMapper.insert(user);
return "ok";
}
}
浏览器访问:http://localhost:8888/user/test
返回 Hello World
数据库连接使用具体请查看
mybatis官方文档:mybatis – MyBatis 3 | 简介
mybatis-plus: MyBatis-Plus