文章目录
问题:查询订单方法需要获取用户姓名。现有用户表和订单表,订单表里存储了客户id。
分析:不能关联查用户的姓名,但用户的姓名又需要,总是需要获取用户的。
一般做法:查询订单信息,循环拿出客户id,查用户表,取出用户姓名,在赋值给订单类中的客户姓名。
以上做法存在哪些问题呢?
单一原则,查询订单信息 需要在去查用户信息。
开放 关闭原则,开扩展开放,对修改关闭。假如别的地方也需要用到查询订单信息,那么这个类里面冗余了其他业务代码。整个方法无法使用。
如果只是对方法进行动态扩展。有的时候需要增强方法,有的时候用原来方法就能解决问题。可以采用注解加aop方式解决
先来自定义两个注解
1.作用于哪个类和哪个方法
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface NeedSetValue {
Class<?> beanClass();
String param();
String method();
String targetFiled();
}
2.需要对哪个属性赋值
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface NeedSetValueFeild {
}
3.定义aop切面
@Component
@Aspect
public class SetFeildValueAspect {
@Autowired
BeansUtil beansUtil;
@Around("@annotation(com.st.user.annotation.NeedSetValueFeild)")
public Object dosetFeildValue(ProceedingJoinPoint pjp)throws Throwable{
Object ret=pjp.proceed();
//操作结果集
//获取到注解 然后通过反射执行 方法
beansUtil.setFeildValueForCol((Collection) ret);
return ret;
}
}
4.bean的工具类
public class BeansUtil implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public void setFeildValueForCol(Collection col) throws Exception {
Class<?> clazz = col.iterator().next().getClass();
Field[] fields = clazz.getDeclaredFields();
HashMap<String, Object> cache = new HashMap<>();
for (Field needField : fields) {
NeedSetValue sv = needField.getAnnotation(NeedSetValue.class);
if (sv == null)
continue;
needField.setAccessible(true);
Object bean = this.applicationContext.getBean(sv.beanClass());
Method method = sv.beanClass().getMethod(sv.method(), clazz.getDeclaredField(sv.param()).getType());
Field paramFiled = clazz.getDeclaredField(sv.param());
paramFiled.setAccessible(true);
Field targetFiled = null;
Boolean needInnerFile = !StringUtil.isEmpty(sv.targetFiled());
String keyPrefix = sv.beanClass() + "-" + sv.method() + "-" + sv.targetFiled() + "-";
for (Object obj : col) {
Object paramValue = paramFiled.get(obj);
if (paramValue == null)
continue;
Object value = null;
String key = keyPrefix + paramValue;
if (cache.containsKey(key)) {
value = cache.get(key);
} else {
value = method.invoke(bean, paramValue);
if (needInnerFile) {
if (value != null) {
if (targetFiled == null) {
targetFiled = value.getClass().getDeclaredField(sv.targetFiled());
targetFiled.setAccessible(true);
}
value = targetFiled.get(value);
}
}
cache.put(key, value);
}
needField.set(obj, value);
}
}
}
}
5.来看以下如何设置调用:
@NeedSetValueFeild
public Page<Order> pageQuery(String customerId, int pageNum, int pageSize){
Page<Object> page = PageHelper.startPage(pageNum, pageSize);
this.orderDao.query(customerId);
return page;
}
6.在哪里需要赋值就在那个属性上添加,这里我们在Order类上使用注解
@NeedSetValue(beanClass = UserDao.class,param = "customerId",method = "find",targetFiled = "name")
private String customerName;
//UserDao中的find方法: select * from user where id="xxx" 返回user对象