小工具知识网

当前位置: 首页 > 行业知识 > SpringBoot与MybatisPlus MybatisPlus

行业知识

SpringBoot与MybatisPlus MybatisPlus

2026-12-07 02:31:17 来源:互联网转载

一、MyBatisPlus概述

MyBatis-Plus(简称 MP)是一个MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发、提高效率而生。

MyBatisPlus的愿景是成为MyBatis最好的搭档,就像魂斗罗中的1P、2P,基友搭配,效率翻倍。

官方网址:MyBatis-Plus 

二、SpringBoot集成MyBatisPlus

1、创建SpringBoot项目,添加MyBatisPlus起步依赖

  org.springframework.boot  spring-boot-starter  org.springframework.boot  spring-boot-starter-test  test  com.baomidou  mybatis-plus-boot-starter  3.5.0  mysql  mysql-connector-java  8.0.26  org.projectlombok  lombok  true

2、在SpringBoot配置文件中配置数据源

# 数据源spring:  datasource:    driver-class-name: com.mysql.cj.jdbc.Driver    url: jdbc:mysql:///mybatis?serverTimezone=UTC    username: root    password: 123456

3、编写实体类

@Datapublic class User {    public Integer id;    public String username;    public String sex;    public String address;    public Integer account;}

 4、编写Mapper接口,继承BaseMapper

public interface UserMapper extends BaseMapper {}

5、在 SpringBoot启动类中添加 @MapperScan 注解,扫描Mapper文件夹

@SpringBootApplication@MapperScan("com.zj.mapper")public class DemoApplication {    public static void main(String[] args) {        SpringApplication.run(DemoApplication.class, args);    }}

6、测试Mapper方法

@SpringBootTestpublic class TestUserMapper {    @Resource    private UserMapper userMapper;    @Test    public void test(){        User user = userMapper.selectById(10);        System.out.println(user);    }}

二、MyBatisPlus  CRUD

2.1 添加

1、配置文件开启SQL日志打印

# 开启SQL日志mybatis-plus:  configuration:   log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

2、测试添加方法

    @Test    public void testAdd(){       User user = new User(null,"梅川内酷","男","临沂市",3000);       userMapper.insert(user);        // MyBatisPlus插入成功后,可直接直接获取主键的值        System.out.println(user.getUserId());    }

2.2 相关注解

我们将user类中的字段修改为和数据库表中的字段不一致。将表名称改为tb_user, 这样表中的字段、表名都和实体类不一致。这样mybatosplus无法自动将字段和属性对应。也就无法生成正确的sql语句。此时需要使用注解来指定映射关系。

@TableName

作用:指定类为哪个表的实体类

位置:类上方

@TableId

作用:指定实体类的属性为主键

位置:属性上方

属性:

  • value:主键字段名

  • type:主键策略

    值描述
    NONE无状态,不设置主键类型
    AUTO自增主键
    INPUT插入前自行设置主键值
    ASSIGN_ID默认策略,使用雪花算法自动生成主键ID,主键类型为整形或字符串。(雪花算法:微博开源的分布式ID生成算法,使用一个64位的Long类型数字作为全局唯一ID。在分布式系统中的应用十分广泛,且ID引入了时间戳,基本上保持自增)
    ASSIGN_UUID自动生成排除中划线的UUID作为主键,主键类型为

    @TableField

    作用:在属性和列名不同的情况下,指定映射关系

    位置:非主键属性上方

    @Data@AllArgsConstructor@TableName("tb_user")public class User {    @TableId(value = "id",type = IdType.AUTO)    public Integer UserId;    @TableField("username")    public String userName;    @TableField("sex")    public String userSex;    @TableField("address")    public String userAddress;    @TableField("account")    public Integer userAccount;}

    2.3 修改

        @Test    public void testUpdate(){        User user = new User(1,"梅川丘酷","男","烟台市",4000);        userMapper.updateById(user);    }

    2.4 删除

     根据id删除

        @Test    public void testDeleteById(){        userMapper.deleteById(8);    }

    批量删除

        @Test    public void testDeleteBatch(){        ArrayList ids = new ArrayList();        ids.add(9);        ids.add(10);        ids.add(11);        userMapper.deleteBatchIds(ids);    }

    根据字段的条件删除

        @Test    public void testDeleteMap(){        Map map = new HashMap();        map.put("sex","男");  ;//删除所有字段(字段是数据库列名不是类的属性名)值是男的数据        userMapper.deleteByMap(map);    }

    2.5 查询

    根据id查询

        @Test    public void testFindById(){        User user = userMapper.selectById(1);        System.out.println(user);    }
    ==>  Preparing: SELECT id AS UserId,username,sex AS userSex,address AS userAddress,account AS userAccount FROM tb_user WHERE id=?==> Parameters: 5(Integer)

上一篇:怎么代理服务器卖?

下一篇:云服务器和ipad区别