dozer 简单用法
Posted jonney
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dozer 简单用法相关的知识,希望对你有一定的参考价值。
maven添加必要的库:
1 <!-- https://mvnrepository.com/artifact/net.sf.dozer/dozer --> 2 <dependency> 3 <groupId>net.sf.dozer</groupId> 4 <artifactId>dozer</artifactId> 5 <version>5.5.1</version> 6 </dependency> 7 <!-- https://mvnrepository.com/artifact/net.sf.dozer/dozer-spring --> 8 <dependency> 9 <groupId>net.sf.dozer</groupId> 10 <artifactId>dozer-spring</artifactId> 11 <version>5.5.1</version> 12 </dependency>
spring添加bean:
1 <bean id="mapper" class="org.dozer.spring.DozerBeanMapperFactoryBean"> 2 <property name="mappingFiles"> 3 <list> 4 <!--<value>classpath*:dozer/dozer-mapping.xml</value>--> 5 </list> 6 </property> 7 </bean>
然后就可以在Java必要的地方注入mapper,例如:
1 @Autowired 2 private Mapper mapper;
mapper的用法:
1 @Override 2 public UserDto getUser(long id) { 3 List<UserEnty> userEnties = getSession() 4 .createQuery("from " + UserEnty.class.getSimpleName() + " where id=:id", UserEnty.class) 5 .setParameter("id", id) 6 .list(); 7 if (userEnties.size() > 0){ 8 return mapper.map(userEnties.get(0), UserDto.class); 9 } 10 return null; 11 }
以上是关于dozer 简单用法的主要内容,如果未能解决你的问题,请参考以下文章