具体方法:
public R initDBData(@RequestParam(value = "starTime",required = false) LocalDateTime starTime
, @RequestParam(value = "endTime",required = false) LocalDateTime endTime ) throws Exception {
//获取所有用同步自定义注解的类名
String[] beanNameArr = SpringUtils.getApplicationContext().getBeanNamesForAnnotation(SyncData.class);
// Map<String,List<?>> resultMap = new HashMap<>();
Map<String,Integer> resultMap = new HashMap<>();
for (String key : beanNameArr) {
//获取对象实体类
Class<?> clazz = SpringUtils.getBean(key).getClass();
// 根据对象获取注解属性值
SyncData annotation = clazz.getAnnotation(SyncData.class);
//开启的属性值是否是打开
if(annotation.enabled()){
System.out.println(JSON.toJSONString(annotation));
//根据类名调用同步数据方法查询所有需要同步的数据并调用获取返回结果
for (Method method : clazz.getMethods()) {
if(method.getName().equals("queryAllDataByUpdateTime")){
//调用具体方法
List<?> list = (List<?>) method.invoke(SpringUtils.getBean(clazz),starTime,endTime);
resultMap.put(key,list.size());
break;
}
}
}
}
return R.success(resultMap);
}
工具类 springUtils
public final class SpringUtils {
private SpringUtils() {
}
/**
* 单例Holder模式: 优点:将懒加载和线程安全完美结合的一种方式(无锁)。(推荐)
*
* @return 实实例
*/
public static SpringUtils getInstance() {
return SpringUtilsHolder.INSTANCE;
}
private static ApplicationContext applicationContext;
private static ApplicationContext parentApplicationContext;
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static void setApplicationContext(ApplicationContext ctx) {
Assert.notNull(ctx, "SpringUtil injection ApplicationContext is null");
applicationContext = ctx;
parentApplicationContext = ctx.getParent();
}
public static Object getBean(String name) {
Assert.hasText(name, "SpringUtil name is null or empty");
try {
return applicationContext.getBean(name);
} catch (Exception e) {
return parentApplicationContext.getBean(name);
}
}
public static <T> T getBean(String name, Class<T> type) {
Assert.hasText(name, "SpringUtil name is null or empty");
Assert.notNull(type, "SpringUtil type is null");
try {
return applicationContext.getBean(name, type);
} catch (Exception e) {
return parentApplicationContext.getBean(name, type);
}
}
public static <T> T getBean(Class<T> type) {
Assert.notNull(type, "SpringUtil type is null");
try {
return applicationContext.getBean(type);
} catch (Exception e) {
return parentApplicationContext.getBean(type);
}
}
public static <T> Map<String, T> getBeansOfType(Class<T> type) {
Assert.notNull(type, "SpringUtil type is null");
try {
return applicationContext.getBeansOfType(type);
} catch (Exception e) {
return parentApplicationContext.getBeansOfType(type);
}
}
/**
* <p>
* 类级的内部类,也就是静态的成员式内部类,该内部类的实例与外部类的实例
* 没有绑定关系,而且只有被调用到才会装载,从而实现了延迟加载
*/
private static class SpringUtilsHolder {
/**
* 静态初始化器,由JVM来保证线程安全
*/
private static final SpringUtils INSTANCE = new SpringUtils();
}
}
自定义注解类
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface SyncData {
/**
* 是否启用 操作日志
*
* @return 是否启用
*/
boolean enabled() default true;
/**
* 描述
*
* @return {String}
*/
String value() default "";
}
具体查询方法:
/**
* 根据指定的修改时间查询范围内的所有记录
* @param startTime
* @param endTime
* @param column
* @return
*/
public List<T> queryAllDataByUpdateTime(LocalDateTime startTime, LocalDateTime endTime) {
//取开始时间的0点和结束时间的23点59分59秒
if(ObjectUtils.allNotNull(startTime,endTime)){
startTime = DateUtils.getLocalDateStart(startTime.toLocalDate());
endTime = DateUtils.getLocalDateEnd(endTime.toLocalDate());
}
QueryWrapper<T> queryWrapper = new QueryWrapper<T>();
if(ObjectUtils.allNotNull(startTime,endTime)){
queryWrapper.between("updateTime", startTime, endTime);
}
List<T> attributeDefineList = ((SuperMapper) getBaseMapper()).selectList(queryWrapper);
return attributeDefineList;
}
/**
* 获取指定日期的开始时间
* 如:00:00:00
*
* @param value 日期
* @return 解析后的日期
*/
public static LocalDateTime getLocalDateStart(LocalDate value) {
LocalDateTime todayStart = LocalDateTime.of(value, LocalTime.MIN);
return todayStart;
}
/**
* 获取指定日期的结束时间
* 如:23:59:59
*
* @param value 日期
* @return 解析后的日期
*/
public static LocalDateTime getLocalDateEnd(LocalDate value) {
LocalDateTime dateEnd = LocalDateTime.of(value, LocalTime.MAX);
return dateEnd;
}