首页 > 代码库 > 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 简单用法