springboot+mybatis-plus+MySQL前后端增删改查完整的demo
时间: 2023-09-29 07:07:36 浏览: 302
好的,以下是基于Spring Boot和MyBatis Plus框架搭建的MySQL前后端增删改查的完整示例代码:
1. 在pom.xml文件中添加相关依赖:
```xml
<dependencies>
<!-- Spring Boot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MyBatis Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
```
2. 创建数据库表和相关实体类
```sql
CREATE DATABASE IF NOT EXISTS `test`;
USE `test`;
CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`name` varchar(30) NOT NULL COMMENT '姓名',
`age` int(3) NOT NULL COMMENT '年龄',
`email` varchar(50) NOT NULL COMMENT '邮箱',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='用户表';
INSERT INTO `user` (`name`, `age`, `email`) VALUES ('Tom', 18, 'tom@example.com');
INSERT INTO `user` (`name`, `age`, `email`) VALUES ('Jerry', 20, 'jerry@example.com');
```
创建实体类User.java:
```java
@Data
@TableName(value = "user")
public class User {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String name;
private Integer age;
private String email;
}
```
3. 创建Mapper接口和对应的Mapper.xml文件
创建UserMapper.java:
```java
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
```
创建userMapper.xml:
```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>
<select id="selectAll" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from user
</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.Integer">
delete from user where id = #{id}
</delete>
</mapper>
```
4. 创建Service接口和实现类
创建UserService.java:
```java
public interface UserService {
User selectById(int id);
List<User> selectAll();
boolean insert(User user);
boolean updateById(User user);
boolean deleteById(int id);
}
```
创建UserServiceImpl.java:
```java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User selectById(int id) {
return userMapper.selectById(id);
}
@Override
public List<User> selectAll() {
return userMapper.selectList(null);
}
@Override
public boolean insert(User user) {
return userMapper.insert(user) > 0;
}
@Override
public boolean updateById(User user) {
return userMapper.updateById(user) > 0;
}
@Override
public boolean deleteById(int id) {
return userMapper.deleteById(id) > 0;
}
}
```
5. 创建Controller类和接口
创建UserController.java:
```java
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/select")
public User selectById(@RequestParam("id") Integer id) {
return userService.selectById(id);
}
@GetMapping("/list")
public List<User> selectAll() {
return userService.selectAll();
}
@PostMapping("/insert")
public boolean insert(User user) {
return userService.insert(user);
}
@PostMapping("/update")
public boolean updateById(User user) {
return userService.updateById(user);
}
@PostMapping("/delete")
public boolean deleteById(@RequestParam("id") Integer id) {
return userService.deleteById(id);
}
}
```
至此,一个基于Spring Boot和MyBatis Plus框架的MySQL前后端增删改查的完整示例代码就完成了。
阅读全文