MybatisPlus---快速开始
发布日期:2021-05-17 08:42:30 浏览次数:20 分类:精选文章

本文共 4626 字,大约阅读时间需要 15 分钟。

MyBatis-Plus 开发指南:基于 SpringBoot 项目的快速入门

简介

MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,专注于在不改变 MyBatis 基础功能的前提下,提供一系列增强功能,旨在简化开发流程并提高开发效率。

特性

  • 无侵入性:引入 MP 时完全不影响现有项目,安装和配置都非常简单。
  • 性能优越:启动时自动注入基础 CURD 操作,无需手动编写,性能损耗可忽略不计。
  • 强大 CRUD 操作:内置通用 Mapper 和 Service,通过简单配置即可完成单表大部分 CRUD 操作。
  • 灵活的查询条件:支持复杂的查询条件构造,满足多种使用场景。
  • 主键自动生成:支持多种主键生成策略,包括分布式唯一 ID 生成器(Sequence)。
  • ActiveRecord 模式支持:实体类只需继承 Model 类即可执行 CRUD 操作。
  • 代码生成器:支持通过代码或 Maven 插件生成 Mapper、Model、Service、Controller 层代码。
  • 内置分页插件:基于 MyBatis 物理分页,无需手动编写分页逻辑。
  • 多种数据库支持:兼容 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等主流数据库。
  • 性能分析工具:可输出 SQL 语句及其执行时间,帮助快速定位性能问题。
  • 全局拦截插件:提供智能分析和拦截功能,有效防止误操作。
  • 快速开始

    基于 SpringBoot 项目开发时,可以快速搭建起一个 MyBatis-Plus 项目。以下是开发步骤:

    添加依赖

    在项目 pom.xml 中添加以下依赖:

    org.springframework.boot
    spring-boot-starter-web
    org.springframework.boot
    spring-boot-starter-thymeleaf
    mysql
    mysql-connector-java
    runtime
    com.baomidou
    mybatis-plus-boot-starter
    3.3.2
    org.springframework.boot
    spring-boot-starter-test
    test

    配置数据库

    在 application.properties 中添加数据库配置:

    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Drivers
    spring.datasource.url=jdbc:mysql://localhost:3306/wangblog?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
    spring.datasource.username=roots
    spring.datasource.password=123456

    创建实体类

    在模型类中使用 MP 提供的注解:

    package com.example.demo.bean;
    import com.baomidou.mybatisplus.annotation.IdType;
    import com.baomidou.mybatisplus.annotation.TableId;
    import com.baomidou.mybatisplus.annotation.TableName;
    import com.baomidou.mybatisplus.extension.activerecord.Model;
    @TableName("users")
    public class Users extends Model
    {
    @TableId(type = IdType.AUTO)
    private Integer id;
    private String userName;
    private String password;
    private String address;
    // getter 和 setter 方法省略...
    }

    创建 Service 接口

    定义 Service 接口:

    package com.example.demo.service;
    import com.baomidou.mybatisplus.extension.service.IService;
    import com.example.demo.bean.Users;
    import org.springframework.stereotype.Service;
    @Service
    public interface UsersService extends IService
    {
    Users insert(String userName, String password, String address);
    }

    实现 Service 类

    在 ServiceImpl 中实现具体逻辑:

    package com.example.demo.service;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.example.demo.bean.Users;
    import com.example.demo.mapper.UsersMapper;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    @Service
    public class UsersServiceImpl extends ServiceImpl
    {
    @Autowired
    private UsersMapper usersMapper;
    @Override
    public Users insert(String userName, String password, String address) {
    Users users = new Users();
    users.setUserName(userName);
    users.setPassword(password);
    users.setAddress(address);
    return usersMapper.insert(users);
    }
    }

    创建 Mapper 接口

    定义 Mapper 接口:

    package com.example.demo.mapper;
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.example.demo.bean.Users;
    import org.apache.ibatis.annotations.Mapper;
    import org.springframework.stereotype.Repository;
    @Mapper
    @Repository
    public interface UsersMapper extends BaseMapper
    {
    }

    创建 Controller

    定义 RESTful API 端点:

    package com.example.demo.controller;
    import com.example.demo.bean.Users;
    import com.example.demo.mapper.UsersMapper;
    import com.example.demo.service.UsersService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import java.util.List;
    @Controller
    public class UsersController extends RestController {
    @Autowired
    private UsersService usersService;
    @Autowired
    private UsersMapper usersMapper;
    @RequestMapping("/")
    @ResponseBody
    public Users index() {
    return usersService.insert("xiaowang2", "qwerty", "shanghai");
    }
    @RequestMapping("/select")
    @ResponseBody
    public List
    select() {
    return usersMapper.selectList(null);
    }
    }

    运行项目

  • 打开项目终端,运行以下命令:
  • mvn spring-boot:run
    1. 访问浏览器,访问 http://localhost:8080
    2. 通过以上步骤,可以快速搭建并运行一个基于 MyBatis-Plus 的 SpringBoot 项目,实现 CRUD 操作。

    上一篇:需求分析
    下一篇:数据结构(Java)--稀疏数组(实现过程):

    发表评论

    最新留言

    路过,博主的博客真漂亮。。
    [***.116.15.85]2025年04月18日 05时08分47秒