- 一、字段映射与表名映射
- 1、@TableField
- 1.1 属性exist
- 1.2 属性select
- 2、@TableName
- 3、id生成策略控制
- 3.1 @TableId
- 4、全局生成策略
- 二、多数据操作
- 1、定义
- 2、示例
- 2.1按照主键删除多条记录
- 2.2 根据主键查询多条记录
- 三、逻辑删除
- 1、定义
- 2、删除步骤
- 2.1 在数据库添加相应字段
- 2.2 实体类中添加相应字段
- 2.3 在xxx.yml文件中配置逻辑删除字面值
- 3、注意
- 四、乐观锁
- 1、定义
- 2、步骤
- 2.1 在数据库增加相应字段
- 2.2 实体类中添加相应字段
- 2.3 创建配置,添加拦截器
- 2.4 相关示例
类型:属性注解
位置:模型类属性定义上方
作用:设置当前属性对应的数据库表中的字段关系
相关属性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生成策略
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 list = new ArrayList<>();
list.add(1552826143332147202L);
list.add(1552828103791767553L);
userDao.deleteBatchIds(list);
}
2.2 根据主键查询多条记录
@Test
public void testselectBatchIds() {
List list = new ArrayList<>();
list.add(1552826143332147202L);
list.add(1552828103791767553L);
userDao.selectBatchIds(list);
}
三、逻辑删除
1、定义
有时候需要删除某些级联的记录,为了防止垃圾数据的产生,于是采用逻辑删除的方式。
2、删除步骤 2.1 在数据库添加相应字段
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,则什么也搜不到。
有时候需要对用户操作进行限制,尤其是只有一件商品的时候,便需要进行控制。
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);
}
}



