MyBatisPlus之相关映射、数据操作、逻辑删除、乐观锁相关内容

一、字段映射与表名映射

1、@TableField

类型:属性注解
位置:模型类属性定义上方
作用:设置当前属性对应的数据库表中的字段关系
相关属性value(默认):设置数据库表字段名称

public class User{
    @TableField(value="pwd")
    //类中的password字段对应的是数据库表中的pwd字段
    private String password;
}

1.1 属性exist

设置属性在数据库表字段中是否存在,默认为true,此属性无法与value合并使用

public class User{
    @TableField(exist = false)
    //数据库表中该字段不存在
    private Integer online;
}

1.2 属性select

设置属性是否参与查询,此属性与select( )映射配置不冲突

public class User{
    @TableField(value="pwd", select = false)
    //密码不参与查询
    private String password;
}

2、@TableName

类型:类注解
位置:模型类定义上方
作用:设置当前类对应与数据库表关系
相关属性value: 设置数据库表名称

@TableName("tbl_user")
public class User{
    private Long id;   
}

3、id生成策略控制

3.1 @TableId

类型:属性注解
位置:模型类中用于表示主键的属性定义上方
作用:设置当前类中主键属性的生成策略
value: 设置主键属性的生成策略,值参照IdType枚举值
type: 设置主键属性的生成策略,值参照IdType枚举值

public class User{
    @TableId(type = IdType.AUTO)
    private Long id;
}

AUTO:使用数据库id自增策略控制id生成
NONE:不设置id生成策略
INPUT:用户手工输入id
ASSIGN_ID:雪花算法生成id(可兼容数值型与字符串型)
ASSIGN_UUID:以UUID生成算法作为id生成策略

4、全局生成策略

mybatis-plus:
  global-config:
    db-config:
      //指定主键的全局生成策略
      id-type: assign_id
      //主动在表名前加前缀
      table-prefix: tbl_

二、多数据操作

1、定义

当需要删除多个数据,或查询多个数据时,便可以通过deleteBatchIds(list)和selectBatchIds(list)进行操作。

2、示例

2.1按照主键删除多条记录

 @Test
    public void testDeleteBatchIds() {
        List<Long> list = new ArrayList<>();
        list.add(1552826143332147202L);
        list.add(1552828103791767553L);
        userDao.deleteBatchIds(list);
}

2.2 根据主键查询多条记录

 @Test
    public void testselectBatchIds() {
        List<Long> list = new ArrayList<>();
        list.add(1552826143332147202L);
        list.add(1552828103791767553L);
        userDao.selectBatchIds(list);
}

三、逻辑删除

1、定义

有时候需要删除某些级联的记录,为了防止垃圾数据的产生,于是采用逻辑删除的方式。

2、删除步骤

2.1 在数据库添加相应字段

![image.png](https://img-blog.csdnimg.cn/img_convert/bfac58983d32a4b6bdaee95e59c202a3.png#clientId=u2391010a-3c02-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=48&id=u253fd012&margin=[object Object]&name=image.png&originHeight=48&originWidth=762&originalType=binary&ratio=1&rotation=0&showTitle=false&size=1573&status=done&style=none&taskId=u74f9c928-4191-4550-948c-abc39767f76&title=&width=762)

2.2 实体类中添加相应字段

private Integer deleted;

2.3 在xxx.yml文件中配置逻辑删除字面值

mybatis-plus:
  global-config:
    db-config:
      logic-delete-field: deleted
      logic-not-delete-value: 0
      logic-delete-value: 1

3、注意

一旦开启逻辑删除功能以后,删除语句就变成更新语句。
同时原有记录deleted字段值必须默认值是0,经过删除后才为1。如果为null,则什么也搜不到。

四、乐观锁

1、定义

有时候需要对用户操作进行限制,尤其是只有一件商品的时候,便需要进行控制。

2、步骤

2.1 在数据库增加相应字段

在这里插入图片描述

2.2 实体类中添加相应字段

 @Version
 private Integer version;

2.3 创建配置,添加拦截器

package com.example.maybatisplus_01_quickstart.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MpConfig {
    @Bean
    public MybatisPlusInterceptor mpInterceptor(){
        //1.定义Mp拦截器
        MybatisPlusInterceptor mpInterceptor = new MybatisPlusInterceptor();
        //2.添加乐观锁拦截器
        mpInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return mpInterceptor;
    }
}

2.4 相关示例

package com.example.maybatisplus_01_quickstart;

import com.example.maybatisplus_01_quickstart.dao.UserDao;
import com.example.maybatisplus_01_quickstart.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class OptimisticLockTest {
    @Autowired
    private UserDao userDao;

    @Test
    public void OptimisticLockTest() {
        //1.首先通过要修改的数据id将当前数据查询出来(执行修改前先执行查询语句)
        User user1 = userDao.selectById(3L);

        User user2 = userDao.selectById(3L);

        //2.将要修改的属性逐一设置进去(执行修改时使用version字段作为乐观锁检查依据)
        user1.setName("John1");
        userDao.updateById(user1);

        user2.setName("John2");
        userDao.updateById(user2);
    }
}

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

熊凯瑞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值