jeecgboot在线开发配置逻辑删除继续详细描述下
时间: 2023-10-02 13:06:06 浏览: 315
好的,继续详细描述JeecgBoot在线开发配置逻辑删除的步骤:
1. 在实体类中添加逻辑删除字段
在实体类中添加一个逻辑删除字段,通常命名为“delFlag”,用于表示该条数据是否被删除。例如:
```
public class User {
private Long id;
private String username;
private String password;
@TableLogic
private Integer delFlag;
// getter/setter方法省略
}
```
2. 添加@TableLogic注解
在实体类中添加@TableLogic注解,该注解的value属性默认值为“1”,表示逻辑删除后该字段的值为1。例如:
```
public class User {
private Long id;
private String username;
private String password;
@TableLogic(value = "0", delval = "1")
private Integer delFlag;
// getter/setter方法省略
}
```
可以通过@TableLogic注解的value属性设置逻辑删除后该字段的值,通过delval属性设置逻辑删除前该字段的值。
3. 添加全局逻辑删除注解@TableLogic
在Mybatis配置文件中添加全局逻辑删除注解@TableLogic,该注解的value属性默认值为“1”,表示逻辑删除后该字段的值为1。例如:
```
<bean id="globalConfiguration" class="com.baomidou.mybatisplus.core.config.GlobalConfig">
<property name="dbConfig" ref="dbConfig" />
<property name="sqlInjector" ref="logicSqlInjector" />
<property name="logicDeleteValue" value="0" />
<property name="logicNotDeleteValue" value="1" />
<property name="sqlParser" ref="sqlParser" />
<property name="metaObjectHandler" ref="metaObjectHandler" />
<property name="refreshMapper" value="true" />
<property name="sqlRunner" ref="sqlRunner" />
<property name="formatter" ref="formatter" />
<property name="log" ref="log" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<property name="globalConfigUtils" ref="globalConfigUtils" />
<property name="dbType" value="mysql" />
<property name="tableInfoCache" value="concurrenthashmap" />
<property name="sqlSessionBatch" value="true" />
<property name="tableInfoFill" value="true" />
<property name="keyGenerator" ref="keyGenerator" />
<property name="identifierGenerator" ref="identifierGenerator" />
<property name="tablePrefix" value="jeecg_" />
<property name="tableFieldStyle" value="underline_to_camel" />
<property name="useDeprecatedExecutor" value="false" />
<property name="mapperRegistry" ref="mapperRegistry" />
<property name="mapperScannerConfigurer" ref="mapperScannerConfigurer" />
<property name="autoSqlInjector" ref="autoSqlInjector" />
<property name="tableFillList" ref="tableFillList" />
<property name="tableIdType" value="AUTO" />
<property name="idType" value="AUTO" />
<property name="fieldStrategy" value="NOT_NULL" />
<property name="dbColumnUnderline" value="true" />
<property name="insertStrategy" value="IGNORE" />
<property name="updateStrategy" value="IGNORED" />
<property name="deleteStrategy" value="IGNORED" />
<property name="logicDeleteValue" value="0" />
<property name="logicNotDeleteValue" value="1" />
</bean>
```
4. 添加逻辑删除的SQL拦截器
在Mybatis配置文件中添加逻辑删除的SQL拦截器,用于在执行删除操作时将SQL语句进行拦截并添加逻辑删除条件。例如:
```
<bean id="mybatisSqlInjector" class="com.baomidou.mybatisplus.core.injector.methods.LogicDeleteByIdWithFill">
<property name="additionalParameters" value="false" />
<property name="sqlParser" ref="sqlParser" />
<property name="metaObjectHandler" ref="metaObjectHandler" />
<property name="parameterHandler" ref="parameterHandler" />
<property name="dbConfig" ref="dbConfig" />
<property name="tableInfo" ref="tableInfo" />
<property name="sqlSession" ref="sqlSession" />
<property name="sqlStatementHandler" ref="sqlStatementHandler" />
</bean>
```
5. 重写删除操作的Service层方法
在Service层中对删除操作进行重写,实现逻辑删除的功能。例如:
```
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
@Override
public boolean removeById(Serializable id) {
User user = new User();
user.setDelFlag(1);
user.setId((Long) id);
return super.updateById(user);
}
}
```
这里重写了Service层的removeById方法,在删除前将逻辑删除字段设置为1,然后调用updateById方法进行更新操作。
以上就是JeecgBoot在线开发配置逻辑删除的详细步骤,希望对你有所帮助。
阅读全文