1. 程式人生 > >Spring Boot bean轉換Dozer的快速上手

Spring Boot bean轉換Dozer的快速上手

為什麼要bean轉換

看圖

大白話來說就是,我有的不是你想要的,那就只能通過轉換成你想要再給到你.

分這麼多隻有一點差異的實體有必要嗎? 有,這點麻煩會解決太多問題了.

例如:

1.比如添加了@RestController返回json格式資料會把返回的實體進行序列化.會觸發所有懶載入.(懶載入形同虛設)

2.一個持久實體 包含 密碼 欄位,不好頻繁的把實體的密碼去掉再給到 前端. 需要 po -> vo.

3.自己的實體需要改成別人的實體型別(比如接其他系統),但是格式名稱不一樣.需要 ?? -> dto.

還有.......(以下省略1k文字)

但是以上實現都需要各種set set set(一萬個set),怎麼解決?

Dozer

大白話:dozer是一個能把實體和實體之間進行轉換的工具.只要建立好對映關係.就像是ORM的資料庫和實體對映一樣.

// entity -> entityVo
EntityVo entityVo = dozerMapper.map(entity, entityVo);

每個轉換都要寫對映?

不! 

預設是根據屬性名稱來匹配的.

快速開始

<dependency>
   <groupId>net.sf.dozer</groupId>
   <artifactId>dozer</artifactId>
   <version>5.4.0</version>
</dependency>

 測試實體


import lombok.Data;
import org.dozer.Mapping;

import java.util.Date;

/**
 * @author: ZGC
 * @date Created in 2018/9/1 上午 11:49
 */
//@Data
public class TestEntity {


    private Long id;

    /**
     * 屬性
     */
    @Mapping("theirProperty")
    private String myProperty = "";

    /**
     * 標題
     */
    private String title = "";

    /**
     * 密碼
     */
    private String password = "";


    private Date createAt;
    private Date updatedAt;

    // 本案列.省略getting setting方法.......
}

 測試vo實體


import lombok.Data;

/**
 * @author: ZGC
 * @date Created in 2018/9/1 上午 11:35
 */
@Data
public class TestEntityVo {

    /**
     * 屬性 另一邊的 myProperty 通過@Mapping("theirProperty")對映到這個欄位
     *
     * TestEntity.myProperty  -> TestEntityVo.theirProperty
     */
    private String theirProperty = "";

    /**
     * 標題 預設的對映
     *
     * TestEntity.title  -> TestEntityVo.title
     */
    private String title = "";

    /**
     * 密碼(不展示)
     *
     * 直接不寫 就不會對映
     */
    // String password = "";
    // private Long id;
    // private Date createAt;
    // private Date updatedAt;
}

加個bean到spring容器中

import org.dozer.DozerBeanMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author: ZGC
 * @date Created in 2018/8/31 下午 7:39
 */
@Configuration
public class DozerBeanMapperConfigure {
    @Bean
    public DozerBeanMapper mapper() {
        DozerBeanMapper mapper = new DozerBeanMapper();
        return mapper;
    }
}

 注入然後開始使用啦

    @Autowired
    protected Mapper dozerMapper;

    @Test
    public void test() {
        TestEntity testEntity = new TestEntity();
        testEntity.setMyProperty("testEntity.MyProperty");
        testEntity.setTitle("testEntity.title");
        testEntity.setPassword("6asd5f46asd8f746a8df74");

        System.out.println(testEntity);
        
        // convert bean
        TestEntityVo convert = dozerMapper.map(testEntity, TestEntityVo.class);

        System.out.println(TestEntityVo);
    }


TestEntity(id=null, myProperty=testEntity.MyProperty, title=testEntity.title, password=6asd5f46asd8f746a8df74, createAt=null, updatedAt=null)

TestEntityVo(theirProperty=testEntity.MyProperty, title=testEntity.title)

Okay, your demo's done

本案例只是簡易快速上手教程,詳細的內容和複雜對映請看dozer官網和其他帖子

本案例參考的帖子

以下省略一堆一堆的帖子