黑馬程序員Java品達(dá)通用權(quán)限項(xiàng)目,基于SpringCloud SpringBo

不使用xml進(jìn)行bean的拷貝
坐標(biāo)
<dependency> <groupId>net.sf.dozer</groupId> <artifactId>dozer-spring</artifactId> <version>5.5.1</version> </dependency> <dependency> <groupId>net.sf.dozer</groupId> <artifactId>dozer</artifactId> <version>5.5.1</version> </dependency>
將mapper裝載到spring容器中(啟動(dòng)類中添加就可以)
@Bean public DozerBeanMapperFactoryBean mapper(){ return new DozerBeanMapperFactoryBean(); }
源對(duì)象類和數(shù)據(jù)傳輸類中存在不同名稱的字段時(shí),在源類中的對(duì)應(yīng)字段上加注解
public?class?UserEntity?{ @Mapping("value") private String name; //... }
測(cè)試
@Test public void test(){ User user=new User(); user.setId(11); // set一些屬性 System.out.println(user); UserDTO userDto=mapper.map(user,UserDTO.class); System.out.println(userDto); }
可以創(chuàng)建一個(gè)工具類封裝一下mapper,讓其可以進(jìn)行常用集合的轉(zhuǎn)換
標(biāo)簽: