💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## BeanConvert 可以为POJO定义一个注解,在sql准备参数前,调用此API,得到一个新Bean,用于参数设定。BeanConvert定义如下 ```java @Plugin public interface BeanConvert { /** * 返回入库之前的对象 * @param ctx * @param obj * @param an 注解信息,可以提供额外参数 * @return */ default Object before(ExecuteContext ctx, Object obj, Annotation an){ return obj; } /** * 返回查询结果后的对象 * @param ctx * @param obj * @param an * @return */ default Object after(ExecuteContext ctx, Object obj, Annotation an){ return obj; } } ``` 比如以AttributeConvert的例子作为说明,可以定义如下BeanEncrypt, 其执行类BeanStringConvert, 注解有个attr方法,标识需要加密的字段 ```java @Retention(RetentionPolicy.RUNTIME) @Target(value = {ElementType.TYPE}) @Builder(BeanStringConvert.class) public @interface BeanEncrypt { String attr(); } ``` BeanStringConvert的实现如下(仅仅简单修改了需要加密的字段,添加一个时间戳) ```java public class BeanStringConvert implements BeanConvert{ public Object before(ExecuteContext ctx, Object obj, Annotation an){ BeanEncrypt beanEncrypt = (BeanEncrypt)an; String attrName = beanEncrypt.attr(); String attrValue = (String)BeanKit.getBeanProperty(obj,attrName); String encryptAttrValue = attrValue+"-"+System.currentTimeMillis(); BeanKit.setBeanProperty(obj,encryptAttrValue,attrName); return obj; } public Object after(ExecuteContext ctx, Object obj, Annotation an){ BeanEncrypt beanEncrypt = (BeanEncrypt)an; String attrName = beanEncrypt.attr(); String encryptAttrValue = (String)BeanKit.getBeanProperty(obj,attrName); String attrValue = encryptAttrValue.split("-")[0]; BeanKit.setBeanProperty(obj,attrValue,attrName); return obj; } } ``` ```java @Table(name="sys_user") @Data @BeanEncrypt( attr="name") public static class UserEntity2{ @Auto Long id ; String name; } ```