1. 创建 Spring Boot 项目

首先,我们需要创建一个 Spring Boot 项目。可以使用 Spring Initializr 或者 IDEA 创建一个空的 Spring Boot 项目。

  1. 添加依赖

在 pom.xml 文件中添加以下依赖:

<dependencies>
    <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.4.0</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.2.6</version>
    </dependency>
</dependencies>

其中,spring-boot-starter-web 是 Spring Boot Web 框架的核心依赖,mybatis-plus-boot-starter 是 MyBatis-Plus 的 Spring Boot Starter,druid-spring-boot-starter 是阿里巴巴的 Druid 数据库连接池的 Spring Boot Starter。

  1. 配置数据源

在 application.properties 或 application.yml 中配置数据源信息,例如:

spring.datasource.url=jdbc:mysql://localhost:3306/user_db?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  1. 创建实体类

创建一个 User 实体类,用于映射数据库中的 user 表:

@Data
@TableName('user')
public class User {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String username;
    private String password;
    private String email;
    private Integer age;
    private Date createTime;
    private Date updateTime;
}

其中,@Data 是 Lombok 注解,用于自动生成 getter、setter、toString 等方法;@TableName 是 MyBatis-Plus 注解,用于指定实体类对应的数据库表名;@TableId 是 MyBatis-Plus 注解,用于指定主键的生成策略。

  1. 创建 Mapper 接口

创建一个 UserMapper 接口,用于定义操作 user 表的方法:

public interface UserMapper extends BaseMapper<User> {
}

其中,BaseMapper 是 MyBatis-Plus 提供的基础 Mapper 接口,可以直接继承使用。

  1. 创建 Service 接口和实现类

创建一个 UserService 接口,用于定义操作 user 表的方法:

public interface UserService {
    User getById(Long id);
    boolean save(User user);
    boolean updateById(User user);
    boolean removeById(Long id);
    List<User> list();
}

创建一个 UserServiceImpl 实现类,用于实现 UserService 接口中的方法:

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User getById(Long id) {
        return userMapper.selectById(id);
    }

    @Override
    public boolean save(User user) {
        return userMapper.insert(user) == 1;
    }

    @Override
    public boolean updateById(User user) {
        return userMapper.updateById(user) == 1;
    }

    @Override
    public boolean removeById(Long id) {
        return userMapper.deleteById(id) == 1;
    }

    @Override
    public List<User> list() {
        return userMapper.selectList(null);
    }
}

其中,@Service 是 Spring 的注解,用于标注这是一个服务类;@Autowired 是 Spring 的注解,用于自动注入 UserMapper。

  1. 创建 Controller 类

创建一个 UserController 类,用于处理用户管理相关的请求:

@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);
    }

    @GetMapping
    public List<User> list() {
        return userService.list();
    }
}

其中,@RestController 是 Spring 的注解,用于标注这是一个 RESTful 风格的控制器;@RequestMapping 是 Spring 的注解,用于指定请求路径;@GetMapping、@PostMapping、@PutMapping、@DeleteMapping 是 Spring 的注解,用于指定 HTTP 请求方法。

至此,我们已经完成了一套简单的用户管理系统的开发。可以使用 Postman 等工具测试接口的正确性。

使用 Java 开发用户管理系统:SpringBoot + MyBatis-Plus

原文地址: https://www.cveoy.top/t/topic/jCIA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录