返回

手把手教你使用SpringBoot3 + Thymeleaf + Mybatis-Plus 实现多表分页查询 增删改

后端

SpringBoot + Thymeleaf + Mybatis-Plus:构建强大Web应用的利器

引言

SpringBoot、Thymeleaf 和 Mybatis-Plus 是目前备受青睐的 Java Web 开发技术栈,它们凭借强大的功能和简便的开发体验,助力开发者快速构建功能完备的 Web 应用程序。本文将深入探讨如何使用这三个框架实现多表分页查询、增删改等基本操作,为你的 Web 开发之旅提供实践指南。

环境准备

在着手之前,确保你的开发环境已具备以下组件:

  • Java Development Kit (JDK) 8 或更高版本
  • Maven
  • MySQL 数据库
  • Spring Boot 命令行界面 (CLI)

创建 Spring Boot 项目

使用 Spring Boot CLI 创建一个新的项目:

  1. 打开终端并导航到要存放项目的位置。
  2. 执行命令:
spring init springboot-mybatis-plus
  1. 选择项目构建工具为 Maven。

添加 Mybatis-Plus 依赖

在 pom.xml 文件中添加 Mybatis-Plus 依赖:

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.3</version>
</dependency>

配置数据源

在 application.yml 文件中配置数据源:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/springboot_mybatis_plus
    username: root
    password: 123456

创建实体类

接下来,创建两个实体类:User 和 Role,分别对应于用户表和角色表:

@Data
@TableName("t_user")
public class User {

    private Long id;

    private String username;

    private String password;

    private Integer roleId;

}

@Data
@TableName("t_role")
public class Role {

    private Long id;

    private String roleName;

}

创建 Mapper 接口

为 User 和 Role 实体类创建对应的 Mapper 接口:

@Mapper
public interface UserMapper extends BaseMapper<User> {
}

@Mapper
public interface RoleMapper extends BaseMapper<Role> {
}

创建 Service 接口

定义 Service 接口,声明业务逻辑方法:

public interface UserService {

    List<User> findAll();

    User findById(Long id);

    void save(User user);

    void update(User user);

    void delete(Long id);

}

public interface RoleService {

    List<Role> findAll();

    Role findById(Long id);

    void save(Role role);

    void update(Role role);

    void delete(Long id);

}

创建 ServiceImpl 类

实现 Service 接口,提供具体实现逻辑:

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

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

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

    @Override
    public void save(User user) {
        userMapper.insert(user);
    }

    @Override
    public void update(User user) {
        userMapper.updateById(user);
    }

    @Override
    public void delete(Long id) {
        userMapper.deleteById(id);
    }

}

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> findAll() {
        return roleMapper.selectList(null);
    }

    @Override
    public Role findById(Long id) {
        return roleMapper.selectById(id);
    }

    @Override
    public void save(Role role) {
        roleMapper.insert(role);
    }

    @Override
    public void update(Role role) {
        roleMapper.updateById(role);
    }

    @Override
    public void delete(Long id) {
        roleMapper.deleteById(id);
    }

}

创建 Controller 类

处理用户请求并返回响应:

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/users")
    public String findAll(Model model) {
        List<User> users = userService.findAll();
        model.addAttribute("users", users);
        return "users";
    }

    @GetMapping("/user/{id}")
    public String findById(@PathVariable Long id, Model model) {
        User user = userService.findById(id);
        model.addAttribute("user", user);
        return "user";
    }

    @GetMapping("/user/add")
    public String add(Model model) {
        return "user-add";
    }

    @PostMapping("/user/save")
    public String save(User user) {
        userService.save(user);
        return "redirect:/users";
    }

    @GetMapping("/user/update/{id}")
    public String update(@PathVariable Long id, Model model) {
        User user = userService.findById(id);
        model.addAttribute("user", user);
        return "user-update";
    }

    @PostMapping("/user/update")
    public String update(User user) {
        userService.update(user);
        return "redirect:/users";
    }

    @GetMapping("/user/delete/{id}")
    public String delete(@PathVariable Long id) {
        userService.delete(id);
        return "redirect:/users";
    }

}

@Controller
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/roles")
    public String findAll(Model model) {
        List<Role> roles = roleService.findAll();
        model.addAttribute("roles", roles);
        return "roles";
    }

    @GetMapping("/role/{id}")
    public String findById(@PathVariable Long id, Model model) {
        Role role = roleService.findById(id);
        model.addAttribute("role", role);
        return "role";
    }

    @GetMapping("/role/add")
    public String add(Model model) {
        return "role-add";
    }

    @PostMapping("/role/save")
    public String save(Role role) {
        roleService.save(role);
        return "redirect:/roles";
    }

    @GetMapping("/role/update/{id}")
    public String update(@PathVariable Long id, Model model) {
        Role role = roleService.findById(id);
        model.addAttribute("role", role);
        return "role-update";
    }

    @PostMapping("/role/update")
    public String update(Role role) {
        roleService.update(role);
        return "redirect:/roles";
    }

    @GetMapping("/role/delete/{id}")
    public String delete(@PathVariable Long id) {
        roleService.delete(id);
        return "redirect:/roles";
    }

}

创建模板文件

定义页面布局和内容:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    
</head>
<body>
    <h1>SpringBoot + Thymeleaf + Mybatis-Plus</h1>
    <a href="/users">用户列表</a>
    <a href="/roles">角色列表</a>
    <hr>
    {{#content}}
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    
</head>
<body>
    <h1>用户列表</h1>
    <table border="1">
        <thead>
            <tr>
                <th>ID</th>
                <th>用户名</th>
                <th>密码</th>
                <th>角色ID</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            {{#users}}
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td>
                    <a href="/user/update/{id}">修改</a>
                    <a href="/user/delete/{id}">删除</a>
                </td>
            </tr>
            {{/users}}
        </tbody>
    </table>
</body>
</html>

运行项目

在终端运行以下命令启动项目:

mvn spring-boot:run

功能演示

访问 http://localhost:8080/users 查看用户列表,其中包含所有用户的 ID、用户名、密码和角色 ID。

点击 "修改" 链接编辑某个用户的信息,点击 "保存" 按钮更新数据。

点击 "新增" 链接创建新用户,填写相关信息并点击 "保存" 按钮。