`
yanghuidang
  • 浏览: 913189 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

上传一个工具包dto传值

阅读更多

好好看了下dto传值的问题,觉得在从数据库传出到页面的时候使用dto还是不错的,传进去我觉得无所谓。因此写了一个支持从数据库PO中值自动匹配并填 充到dto中的工具类,其实不能算是工具类,因为我直接实现的DAOSupport和ServiceSupport的接口,只要你继承
DAOSupportImpl并且实现DAOSupport(ServiceDAOSupport类似)接口,那么自动转换将不再需要你担心,这个包的限 制是,dto和PO的属性名称相同,并且ORM映射必须使用JPA Annotation,谁有兴趣可以再增加对xml的支持,发一个给我.
这个的好处还是有的,可以减少装配的代码,对于喜欢用DTO的人来说。而且在页面需要再多显示一个属性的时候,你只需要在DTO中添加相应的属性,然后就可以直接在页面代码加入就可以,而不用改Service层的代码。
下面贴出使用代码:
以下的DAO实现用spring管理,你需要提供一个hibernateTemplate,当然它也是不需要你装配的,只需你在spring容器中

含有这个实例。

package test.main;

import com.ccuywsnjgh.support.dao.DAOSupport;
import com.ccuywsnjgh.support.dao.DAOSupportImpl;
import com.ccuywsnjgh.support.model.Employee;


public class EmployeeDAO extends DAOSupportImpl<Employee> implements
  DAOSupport<Employee> {

}


package test.main;

import com.ccuywsnjgh.support.dao.DAOSupport;
import com.ccuywsnjgh.support.dao.DAOSupportImpl;
import com.ccuywsnjgh.support.model.IdCard;

public class IdCardDAO extends DAOSupportImpl<IdCard> implements
  DAOSupport<IdCard> {

}


测试代码:

package test.main;

import com.ccuywsnjgh.support.dao.DAOSupport;
import com.ccuywsnjgh.support.dao.DAOSupportImpl;
import com.ccuywsnjgh.support.model.Role;

public class RoleDAO extends DAOSupportImpl<Role> implements DAOSupport<Role> {

}

 

new IdCardDAO();
  new EmployeeDAO();
  new RoleDAO();
  this.printEndTime();
  Role role = null;
  Set<Role> roles = new HashSet<Role>();
  for(int i = 0;i < 1000;i++) {
   role = new Role();
   role.setId(i);
   role.setName("name" + i);
   roles.add(role);
  }
  
  Employee em = new Employee();
  IdCard id = new IdCard();
  id.setAddress("北京");
  id.setCardNo("433124");
  em.setIdCard(id);
  em.setCanUse(true);
  em.setCreateTime(new Date());
  em.setE_mail("email");
  em.setGender(Gender.FEMALE);
  em.setPassword("password");
  em.setRealName("龙安");
  em.setUserName("longan");
  em.setRoles(roles);
  List<Employee> ems = new ArrayList<Employee>();
  for (int i = 0; i < 100; ++i)
   ems.add(em);

  LogUtils.openLog();
  this.printStartTime();
  EmployeeDTO dto = ServiceSupportImpl.ServiceSupportUtils.divert(EmployeeDTO.class, ems, null).get(4);
  this.printEndTime();
  System.out.println("userName : " + dto.getUserName()
    + "\npassword : " + dto.getPassword()
    + "\nrealName : " + dto.getRealName()
    + "\nemail : " + dto.getE_mail()
    + "\ncanUse : " + dto.getCanUse()
    + "\ngender : " + dto.getGender()
    + "\ncreateTime : " + dto.getCreateTime()
    + "\naddress : " + dto.getAddress()
    + "\ncardNo : " + dto.getCardNo()
    + "\nroles.size() : " + dto.getRoles().size());
  RoleDTO[] infos = new RoleDTO[1000];
  dto.getRoles().toArray(infos);
  RoleDTO info = infos[(int) (Math.random() * 1000)];
  System.out.println("id : " + info.getId()
    + "\nname : " + info.getName());

 

 

 输出:
end : 203 - 1301032937140   实例化耗时
start : 0 - 1301032937140   开始记录执行所需时间
end : 219 - 1301032937359  执行100个Employee到EmployeeDTO的转换所耗时间,这其中包含每个Employee到EmployeeDTO中均有一个 List<RoleDTO>,长度为1000。假如不包含List,Set(现在不支持Map)的话,10000个转换需要120左右ms, 在我机子上测试,我的机子是Dell 1400 T7250 2GHZ 2G内存
userName : longan       //Employee下的属性
password : password    //Employee下的属性
realName : 龙安    //Employee下的属性
email : email    //Employee下的属性
canUse : true   //Employee下的属性
gender : FEMALE    //Employee下的属性
createTime : Fri Mar 25 14:02:17 CST 2011    //Employee下的属性
address : 北京    //Idcard下的属性,Employee中含有IdCard。
cardNo : 433124   //Idcard下的属性,Employee中含有IdCard。
roles.size() : 1000  //List<RoleDTO>
id : 961    //List<RoleDTO>中的随机的一个RoleDTO下的属性
name : name961  //同上


实现的这个东西对于DAO层有依赖,因此,假使不使用默认的DAO实现的话,将会出问题。
其实从另一方面讲:我实现的DAO和Service层的基本功能足以使用,当然,像一些复杂的东西,你得自己实现,Service层的功能足以让你做大部 分事情,但是Service层返回的都是dto,因此,假如Service层有些地方你可能要用到从数据库返回的PO,那么,DAO层能够满足你的要求

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics