来个Mybatis Plus springboot教程
时间: 2024-01-11 09:14:58 浏览: 43
好的,下面是一个简单的 Mybatis Plus + Spring Boot 教程。
1. 创建 Spring Boot 项目
首先,我们需要创建一个 Spring Boot 项目,可以使用 Spring Initializr 快速生成。
2. 添加依赖
在 pom.xml 文件中添加以下依赖:
```xml
<!-- Mybatis Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.1</version>
</dependency>
<!-- MySQL 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
```
3. 配置数据库连接
在 application.properties 文件中添加以下配置:
```
# 数据库连接配置
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# Mybatis Plus 配置
mybatis-plus.mapper-locations=classpath:mapper/*.xml
```
这里使用的是 MySQL 数据库,需要根据自己的实际情况修改数据库连接信息。
4. 创建实体类
在 src/main/java 目录下创建实体类 User:
```java
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
```
这里使用了 Lombok 注解 @Data,简化了实体类的代码。
5. 创建 Mapper
在 src/main/java 目录下创建 Mapper 接口 UserMapper:
```java
public interface UserMapper extends BaseMapper<User> {
}
```
这里使用了 Mybatis Plus 提供的 BaseMapper 接口,无需编写 SQL 语句即可完成基本的 CRUD 操作。
6. 创建 Service
在 src/main/java 目录下创建 Service 接口 UserService:
```java
public interface UserService extends IService<User> {
}
```
这里使用了 Mybatis Plus 提供的 IService 接口,提供了一些常用的方法,如 save、remove、update 等。
7. 实现 Service
在 src/main/java 目录下创建 Service 实现类 UserServiceImpl:
```java
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
```
这里使用了 Mybatis Plus 提供的 ServiceImpl 类,实现了 UserService 接口。
8. 创建 Controller
在 src/main/java 目录下创建 Controller 类 UserController:
```java
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/{id}")
public User getById(@PathVariable Long id) {
return userService.getById(id);
}
@PostMapping
public boolean save(@RequestBody User user) {
return userService.save(user);
}
@PutMapping
public boolean updateById(@RequestBody User user) {
return userService.updateById(user);
}
@DeleteMapping("/{id}")
public boolean removeById(@PathVariable Long id) {
return userService.removeById(id);
}
}
```
这里使用了 Spring MVC 注解 @RestController、@RequestMapping、@GetMapping、@PostMapping、@PutMapping、@DeleteMapping,实现了对 User 实体类的 CRUD 操作。
9. 编写 SQL
在 src/main/resources/mapper 目录下创建 UserMapper.xml 文件,编写 SQL 语句:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.example.demo.entity.User">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="age" property="age" />
<result column="email" property="email" />
</resultMap>
<sql id="Base_Column_List">
id, name, age, email
</sql>
<select id="selectById" resultMap="BaseResultMap">
select <include refid="Base_Column_List" />
from user
where id = #{id}
</select>
<insert id="insert" parameterType="com.example.demo.entity.User">
insert into user(name, age, email)
values(#{name}, #{age}, #{email})
</insert>
<update id="updateById" parameterType="com.example.demo.entity.User">
update user
set name = #{name},
age = #{age},
email = #{email}
where id = #{id}
</update>
<delete id="deleteById" parameterType="java.lang.Long">
delete from user
where id = #{id}
</delete>
</mapper>
```
这里使用了 Mybatis 的 XML 配置方式,编写了 selectById、insert、updateById、deleteById 四个 SQL 语句。
10. 启动项目
在终端中进入项目目录,执行以下命令启动项目:
```
mvn spring-boot:run
```
11. 测试接口
使用 Postman 或其他工具测试接口,可以执行以下操作:
- GET http://localhost:8080/user/1,查询 id 为 1 的用户信息。
- POST http://localhost:8080/user,新增一条用户信息。
```
{
"name": "Tom",
"age": 20,
"email": "tom@example.com"
}
```
- PUT http://localhost:8080/user,修改一条用户信息。
```
{
"id": 1,
"name": "Tom",
"age": 21,
"email": "tom@example.com"
}
```
- DELETE http://localhost:8080/user/1,删除 id 为 1 的用户信息。
以上就是一个简单的 Mybatis Plus + Spring Boot 教程,希望能对你有所帮助。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)