mirror of
https://github.com/yzcheng90/X-SpringBoot
synced 2025-11-04 05:35:45 +08:00
优化分表
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.suke.czx.common.aspect;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.suke.czx.common.utils.HttpContextUtils;
|
||||
@@ -23,7 +24,7 @@ import java.util.Date;
|
||||
|
||||
/**
|
||||
* 系统日志,切面处理类
|
||||
*
|
||||
*
|
||||
* @author czx
|
||||
* @email object_czx@163.com
|
||||
* @date 2017年3月8日 上午11:07:35
|
||||
@@ -32,68 +33,70 @@ import java.util.Date;
|
||||
@Component
|
||||
public class SysLogAspect {
|
||||
|
||||
@Resource
|
||||
private SysLogService sysLogService;
|
||||
|
||||
@Pointcut("@annotation(com.suke.czx.common.annotation.SysLog)")
|
||||
public void logPointCut() {
|
||||
|
||||
}
|
||||
@Resource
|
||||
private SysLogService sysLogService;
|
||||
|
||||
@Around("logPointCut()")
|
||||
public Object around(ProceedingJoinPoint point) throws Throwable {
|
||||
long beginTime = System.currentTimeMillis();
|
||||
//执行方法
|
||||
Object result = point.proceed();
|
||||
//执行时长(毫秒)
|
||||
long time = System.currentTimeMillis() - beginTime;
|
||||
@Pointcut("@annotation(com.suke.czx.common.annotation.SysLog)")
|
||||
public void logPointCut() {
|
||||
|
||||
//保存日志
|
||||
saveSysLog(point, time);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@Around("logPointCut()")
|
||||
public Object around(ProceedingJoinPoint point) throws Throwable {
|
||||
long beginTime = System.currentTimeMillis();
|
||||
//执行方法
|
||||
Object result = point.proceed();
|
||||
//执行时长(毫秒)
|
||||
long time = System.currentTimeMillis() - beginTime;
|
||||
|
||||
@SneakyThrows
|
||||
private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {
|
||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||
Method method = signature.getMethod();
|
||||
//保存日志
|
||||
saveSysLog(point, time);
|
||||
|
||||
SysLog sysLog = new SysLog();
|
||||
com.suke.czx.common.annotation.SysLog syslog = method.getAnnotation(com.suke.czx.common.annotation.SysLog.class);
|
||||
if(syslog != null){
|
||||
//注解上的描述
|
||||
sysLog.setOperation(syslog.value());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//请求的方法名
|
||||
String className = joinPoint.getTarget().getClass().getName();
|
||||
String methodName = signature.getName();
|
||||
sysLog.setMethod(className + "." + methodName + "()");
|
||||
@SneakyThrows
|
||||
private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {
|
||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||
Method method = signature.getMethod();
|
||||
|
||||
//请求的参数
|
||||
Object[] args = joinPoint.getArgs();
|
||||
try{
|
||||
String params = JSONUtil.toJsonStr(args);
|
||||
sysLog.setParams(params);
|
||||
}catch (Exception ignored){
|
||||
SysLog sysLog = new SysLog();
|
||||
com.suke.czx.common.annotation.SysLog syslog = method.getAnnotation(com.suke.czx.common.annotation.SysLog.class);
|
||||
if (syslog != null) {
|
||||
//注解上的描述
|
||||
sysLog.setOperation(syslog.value());
|
||||
}
|
||||
|
||||
}
|
||||
//请求的方法名
|
||||
String className = joinPoint.getTarget().getClass().getName();
|
||||
String methodName = signature.getName();
|
||||
sysLog.setMethod(className + "." + methodName + "()");
|
||||
|
||||
//获取request
|
||||
HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
|
||||
//设置IP地址
|
||||
sysLog.setIp(IPUtils.getIpAddr(request));
|
||||
//请求的参数
|
||||
Object[] args = joinPoint.getArgs();
|
||||
try {
|
||||
String params = JSONUtil.toJsonStr(args);
|
||||
sysLog.setParams(params);
|
||||
} catch (Exception ignored) {
|
||||
|
||||
//用户名
|
||||
String userStr = JSONUtil.toJsonStr(SecurityContextHolder.getContext().getAuthentication().getPrincipal());
|
||||
JSONObject userDetailsUser = JSONUtil.parseObj(userStr);
|
||||
String username = userDetailsUser.getStr("username");
|
||||
sysLog.setUsername(username);
|
||||
}
|
||||
|
||||
sysLog.setTime(time);
|
||||
sysLog.setCreateDate(new Date());
|
||||
//保存系统日志
|
||||
sysLogService.save(sysLog);
|
||||
}
|
||||
//获取request
|
||||
HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
|
||||
//设置IP地址
|
||||
sysLog.setIp(IPUtils.getIpAddr(request));
|
||||
|
||||
//用户名
|
||||
String userStr = JSONUtil.toJsonStr(SecurityContextHolder.getContext().getAuthentication().getPrincipal());
|
||||
if (!StrUtil.isNotEmpty(userStr) && !userStr.equals("anonymousUser")) {
|
||||
JSONObject userDetailsUser = JSONUtil.parseObj(userStr);
|
||||
String username = userDetailsUser.getStr("username");
|
||||
sysLog.setUsername(username);
|
||||
}
|
||||
|
||||
sysLog.setTime(time);
|
||||
sysLog.setCreateDate(new Date());
|
||||
//保存系统日志
|
||||
sysLogService.save(sysLog);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
package com.suke.czx.config;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.db.Db;
|
||||
import cn.hutool.db.DbUtil;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
|
||||
@@ -15,7 +13,6 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
@@ -75,7 +72,8 @@ public class ShardingTableConfig {
|
||||
/**
|
||||
* 每小时执行一次
|
||||
*/
|
||||
@Scheduled(cron = "0 0 0/1 * * ?")
|
||||
// @Scheduled(cron = "0 0 0/1 * * ?")
|
||||
@Scheduled(cron = "1/5 * * * * ?")
|
||||
private void createShardingTable() {
|
||||
// 上锁30秒
|
||||
final boolean success = redissonLock.lock(Constant.SYSTEM_NAME + "createShardingTable", 30, TimeUnit.SECONDS);
|
||||
|
||||
@@ -59,20 +59,18 @@ public class TbBrowseRecordController extends AbstractController {
|
||||
}
|
||||
queryWrapper.lambda().orderByDesc(TbBrowseRecord::getCreateTime);
|
||||
// 设置当前查询哪个表,如果不设置就默认查询最新分表
|
||||
ShardingTableConfig.setTableName("tb_browse_record");
|
||||
//ShardingTableConfig.setTableName("tb_browse_record");
|
||||
IPage<TbBrowseRecord> listPage = tbAdvertiserRecordService.getPage(mpPageConvert.<TbBrowseRecord>pageParamConvert(params), queryWrapper);
|
||||
return R.ok().setData(listPage);
|
||||
}
|
||||
|
||||
|
||||
@AuthIgnore
|
||||
@SysLog("新增浏览记录数据")
|
||||
@PostMapping("/save")
|
||||
@ResourceAuth(value = "新增浏览记录数据", module = "浏览记录")
|
||||
public R save(@RequestBody TbBrowseRecord param, HttpServletRequest request) {
|
||||
if (StrUtil.isEmpty(param.getWatchStatus())) {
|
||||
return R.error("观看状态为空");
|
||||
}
|
||||
|
||||
@GetMapping("/save")
|
||||
//@ResourceAuth(value = "新增浏览记录数据", module = "浏览记录")
|
||||
public R save(HttpServletRequest request) {
|
||||
TbBrowseRecord param = new TbBrowseRecord();
|
||||
param.setCreateTime(new Date());
|
||||
String ipAddr = IPUtils.getIpAddr(request);
|
||||
param.setRequestIp(ipAddr);
|
||||
|
||||
Reference in New Issue
Block a user