import com.example.demo.Dao.userandroleandpermissionDao.SessionUserDao;
import com.example.demo.Entity.userandroleandpermission.SessionUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Set;
/**
* 应用程序启动的时候,删除session_user的表(这个方法实在应用启动之后立即执行)
*/
@Component
public class MyApplicationRunner implements ApplicationRunner {
@Resource
private SessionUserDao sessionUserDao;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Override
public void run(ApplicationArguments args) throws Exception {
sessionUserDao.deleteAll();
Set<String> keys = stringRedisTemplate.keys("*");//清空redis数据库中所有的键值对
stringRedisTemplate.delete(keys);
System.out.println("sessionUser全部删除了");
}
}
只要在类继承ApplicationRunner并且在头部注入Component就可以了。
Set<String> keys = stringRedisTemplate.keys("*");//清空redis数据库中所有的键值对
stringRedisTemplate.delete(keys);
上面两句就可以清空redis了
参考:https://blog.csdn.net/weixin_44001753/article/details/103646187
版权声明:本文为qq_35383788原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。