//销售记录表
class Sale{
String id;
String customer;//客户id
}class SaleVO{
String id;
String customerId;//客户id
String customerName;//客户name
}class Customer{
String id;
String name;
}
SaleVO是前台展示页面,Sale是数据库表,Sale保存的时候客户只保存id,那么当我从数据库查数据出来的时候,Sale怎么转换成SaleVO比较好呢?我现在是在DAO层写SQL代码的时候直接
select t.id, t.customer customerId, c.name customerName from sale t, customer c
但是当要转换的字段比较多的时候这么写就很烦人了,也很容易出错。大家在遇到类似这种情况的时候是怎么做的,能不能给我说一下大家的思路。
class Sale{
String id;
String customer;//客户id
}class SaleVO{
String id;
String customerId;//客户id
String customerName;//客户name
}class Customer{
String id;
String name;
}
SaleVO是前台展示页面,Sale是数据库表,Sale保存的时候客户只保存id,那么当我从数据库查数据出来的时候,Sale怎么转换成SaleVO比较好呢?我现在是在DAO层写SQL代码的时候直接
select t.id, t.customer customerId, c.name customerName from sale t, customer c
但是当要转换的字段比较多的时候这么写就很烦人了,也很容易出错。大家在遇到类似这种情况的时候是怎么做的,能不能给我说一下大家的思路。
比如 select new SaveVo(...) from Sale s,Customer c where ...(提前要提供构造方法)。希望对你有帮助
这种做法好处是:自己编码极少。 缺点是可能牺牲一点性能。
我的话,一般不写实体类,直接用Map了...