一个项目中我们或多或少会对某些实体进行转换(DTO VO DO等),他们往往具有相同的属性名称,数量少的情况下我们可以直接采取set、get方法进行赋值,可是如果这样的转换在很多地方都会用到,还是靠set来进行操作势必会大大的影响开发效率,并且代码页很不美观。
实体转换
关于实体转换,这里简单讲下,我们把一个实体对应一张表(这可以当成DO)。在业务中与第三方进行数据交互,我们需要把实体的数据传给他们,但不一定是一个DO中的所有属性可能减少或者多个DO中的属性组成,这里我们引入DTO(这个实体中我们可以去除一些隐私信息,比如:银行卡号,身份证,密码)。一个性别我们用1、2表示男女,页面中不能直接显示1或者2,需要显示男、女或者靓仔(男)、靓妹(女),这时候代表这样的一个实体我们可以看作VO。
转换方法
- 手动get/set
- apache 的BeanUtils和PropertyUtils类中拷贝方法
- Spring 的BeanUtils类中拷贝方法
- Cglib BeanCopier
还有很多其他方式,这里不一一列举。为什么我们要用Cglib BeanCopier呢?主要因为这个性能比其他强很多,具体网上可以查询各个性能测试。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| @Slf4j public class CGlibBeanUtils { private static final ConcurrentHashMap<String, BeanCopier> mapCaches = new ConcurrentHashMap<>();
public static <O, T> T copyProperties(O source, T target) { return copy(source, target); }
public static <O, T> T copyProperties(O source, T target, IAction<T> action) { target = copy(source, target); action.run(target); return target; }
private static <O, T> T copy(O source, T target) { String baseKey = generateKey(source.getClass(), target.getClass()); BeanCopier copier; if (!mapCaches.containsKey(baseKey)) { copier = BeanCopier.create(source.getClass(), target.getClass(), false); mapCaches.put(baseKey, copier); } else { copier = mapCaches.get(baseKey); } copier.copy(source, target, null); return target; }
private static String generateKey(Class<?> c1, Class<?> c2) { return c1.toString() + c2.toString(); }
}
|
IAction代码
1 2 3 4
| @FunctionalInterface public interface IAction<T> { void run(T param); }
|