首页 > 代码库 > dozer 简单用法
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 简单用法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。