07. 开发管理员后台接口

3881 字约 8 分钟读完873 次阅读更新于 2026/5/3

新增

image.png

<insert id="insert">
    insert into admin (username, password, name, avatar, role)
    values (#{username}, #{password}, #{name}, #{avatar}, #{role})
</insert>

image.png

删除

delete from admin where id = #{id}

image.png

批量删除

批量删除就是循环调用了单个删除
image.png
image.png

修改

数据库数据在更新的时候被覆盖了 怎么办?
image.png
修改数据需要使用动态SQL

<update id="updateById">
    update admin
    <set>
        <if test="username != null"> username = #{username}, </if>
        <if test="password != null"> password = #{password}, </if>
        <if test="name != null"> name = #{name}, </if>
        <if test="avatar != null"> avatar = #{avatar}, </if>
        <if test="role != null"> role = #{role} </if>
    </set>
    where id = #{id}
</update>

image.png

查询单个

/**
 * 查询单个
 */
@GetMapping("/selectById/{id}")
public Result selectById(@PathVariable Integer id) {
    Admin admin = adminService.selectById(id);
    return Result.success(admin);
}

image.png

查询所有

@GetMapping("/selectAll")
public Result selectAll() {
    List<Admin> list = adminService.selectAll();
    return Result.success(list);
}

image.png

分页查询

/**
 * 查询所有
 */
@GetMapping("/selectPage")
public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
    PageInfo pageInfo = adminService.selectPage(pageNum, pageSize);
    return Result.success(pageInfo);
}
public PageInfo selectPage(Integer pageNum, Integer pageSize) {
    PageHelper.startPage(pageNum, pageSize);
    List<Admin> list = this.selectAll();
    return PageInfo.of(list);
}

image.png

下载Postman测试

https://www.postman.com/downloads/?utm_source=postman-home