springBoot和mybaitsplus实现供管理后台使用的用户列表(列表中需要显示余额、做分页)、编辑、删除接口。(必做)
时间: 2024-01-09 14:04:14 浏览: 150
好的,下面是详细的实现步骤:
1. 在Maven项目中添加Spring Boot和Mybatis Plus的依赖,在pom.xml文件中添加以下依赖:
```
<dependencies>
<!-- Spring Boot -->
<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.3.1</version>
</dependency>
<!-- MySQL 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
</dependencies>
```
2. 创建用户表,并使用Mybatis Plus的注解进行表映射。在src/main/java目录下创建一个com.example.entity包,创建User实体类:
```
package com.example.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("user")
public class User {
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@TableField(value = "username")
private String username;
@TableField(value = "password")
private String password;
@TableField(value = "balance")
private Double balance;
}
```
3. 创建UserMapper接口,继承Mybatis Plus提供的BaseMapper接口,用于提供基本的增删改查操作。在src/main/java目录下创建一个com.example.mapper包,创建UserMapper接口:
```
package com.example.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.entity.User;
public interface UserMapper extends BaseMapper<User> {
}
```
4. 创建UserService类,实现用户列表、编辑、删除等业务逻辑。在src/main/java目录下创建一个com.example.service包,创建UserService类:
```
package com.example.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.User;
import com.example.mapper.UserMapper;
import org.springframework.stereotype.Service;
@Service
public class UserService extends ServiceImpl<UserMapper, User> {
public Page<User> getUserList(int pageNum, int pageSize) {
Page<User> page = new Page<>(pageNum, pageSize);
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.orderByAsc("id");
return baseMapper.selectPage(page, wrapper);
}
public User getUserById(Long id) {
return baseMapper.selectById(id);
}
public void saveUser(User user) {
baseMapper.insert(user);
}
public void updateUser(User user) {
baseMapper.updateById(user);
}
public void deleteUser(Long id) {
baseMapper.deleteById(id);
}
}
```
5. 创建UserController类,提供RESTful接口,用于前端页面调用。在src/main/java目录下创建一个com.example.controller包,创建UserController类:
```
package com.example.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("")
public Page<User> getUserList(@RequestParam(defaultValue = "1") int pageNum,
@RequestParam(defaultValue = "10") int pageSize) {
return userService.getUserList(pageNum, pageSize);
}
@GetMapping("/{id}")
public User getUserById(@PathVariable Long id) {
return userService.getUserById(id);
}
@PostMapping("")
public void saveUser(@RequestBody User user) {
userService.saveUser(user);
}
@PutMapping("/{id}")
public void updateUser(@PathVariable Long id, @RequestBody User user) {
user.setId(id);
userService.updateUser(user);
}
@DeleteMapping("/{id}")
public void deleteUser(@PathVariable Long id) {
userService.deleteUser(id);
}
}
```
6. 在src/main/resources/application.properties文件中添加数据库连接配置:
```
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis-plus.mapper-locations=classpath:mapper/*.xml
```
7. 在启动类中添加注解@EnableTransactionManagement,开启事务管理。在src/main/java目录下创建一个com.example包,创建启动类Application:
```
package com.example;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
@SpringBootApplication
@MapperScan("com.example.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
8. 在前端页面中,使用Vue或React等框架,调用后端接口,实现用户列表的展示、编辑、删除等操作。
希望这些步骤能够对您有所帮助,如果还有其他问题,可以随时继续提问。
阅读全文