集合映射與普通的實體類映射都是一樣的。
**1. 兩個實體類**
```java
@Data
public class Cow {
private Integer id;
private String name;
public Cow(Integer id, String name) {
this.id = id;
this.name = name;
}
}
@Data
public class CowDto {
private Integer id;
private String name;
}
```
**2. 映射接口**
```java
@Mapper
public interface CowMapper {
CowMapper INSTANCE = Mappers.getMapper(CowMapper.class);
List<CowDto> toDtoList(List<Cow> source);
Set<CowDto> toDtoSet(Set<Cow> source);
Map<Integer, CowDto> toDtoMap(Map<Integer, Cow> source);
}
```
**3. 測試**
```java
@Test
public void testCowMapper() {
List<Cow> cowList = new ArrayList<>();
Set<Cow> cowSet = new HashSet<>();
Map<Integer, Cow> cowMap = new HashMap<>();
cowList.add(new Cow(1001, "張三"));
cowSet.add(new Cow(1002, "李四"));
cowMap.put(1003, new Cow(1003, "王五"));
List<CowDto> cowDtoList = CowMapper.INSTANCE.toDtoList(cowList);
Set<CowDto> cowDtoSet = CowMapper.INSTANCE.toDtoSet(cowSet);
Map<Integer, CowDto> cowDtoMap = CowMapper.INSTANCE.toDtoMap(cowMap);
//CowList:[Cow(id=1001, name=張三)]
System.out.println("CowList:" + cowList);
//CowDtoList:[CowDto(id=1001, name=張三)]
System.out.println("CowDtoList:" + cowDtoList);
//CowSet:[Cow(id=1002, name=李四)]
System.out.println("CowSet:" + cowSet);
//CowDtoSet:[CowDto(id=1002, name=李四)]
System.out.println("CowDtoSet:" + cowDtoSet);
//CowMap:{1003=Cow(id=1003, name=王五)}
System.out.println("CowMap:" + cowMap);
//CowDtoMap:{1003=CowDto(id=1003, name=王五)}
System.out.println("CowDtoMap:" + cowDtoMap);
}
```
**4. 查看映射接口被Mapstruct編譯后的代碼**
```java
public class CowMapperImpl implements CowMapper {
public CowMapperImpl() {
}
public List<CowDto> toDtoList(List<Cow> source) {
if (source == null) {
return null;
} else {
List<CowDto> list = new ArrayList(source.size());
Iterator var3 = source.iterator();
while(var3.hasNext()) {
Cow cow = (Cow)var3.next();
list.add(this.cowToCowDto(cow));
}
return list;
}
}
public Set<CowDto> toDtoSet(Set<Cow> source) {
if (source == null) {
return null;
} else {
Set<CowDto> set = new HashSet(Math.max((int)((float)source.size() / 0.75F) + 1, 16));
Iterator var3 = source.iterator();
while(var3.hasNext()) {
Cow cow = (Cow)var3.next();
set.add(this.cowToCowDto(cow));
}
return set;
}
}
public Map<Integer, CowDto> toDtoMap(Map<Integer, Cow> source) {
if (source == null) {
return null;
} else {
Map<Integer, CowDto> map = new HashMap(Math.max((int)((float)source.size() / 0.75F) + 1, 16));
Iterator var3 = source.entrySet().iterator();
while(var3.hasNext()) {
Entry<Integer, Cow> entry = (Entry)var3.next();
Integer key = (Integer)entry.getKey();
CowDto value = this.cowToCowDto((Cow)entry.getValue());
map.put(key, value);
}
return map;
}
}
protected CowDto cowToCowDto(Cow cow) {
if (cow == null) {
return null;
} else {
CowDto cowDto = new CowDto();
cowDto.setId(cow.getId());
cowDto.setName(cow.getName());
return cowDto;
}
}
}
```
- MapStruct屬性映射
- MapStruct是什么
- maven依賴
- 基本映射
- 字段名不一致的映射
- 字段類型不一致的映射
- 基本數據類型轉換
- 日期格式轉換
- 使用表達式轉換
- 枚舉映射
- 多個源類的映射
- 集合的映射
- 添加自定義映射方法
- 映射前后
- 添加默認值
- 映射異常處理
- SpringDataJPA
- SpringDataJPA是什么
- 與JPA、Hibernate的關系
- 環境搭建
- 簡單CURD操作
- 內部原理
- 主鍵生成策略
- 聯合主鍵
- 查詢方式
- 方法命名規則查詢
- 限制查詢結果查詢
- 注解@Query查詢
- 命名參數查詢
- SpEL表達式查詢
- 原生查詢
- 更新與刪除
- Specification動態查詢
- 核心接口
- 查詢例子
- 分頁查詢與排序
- 多表查詢
- 一對一查詢
- 一對多查詢
- 多對多查詢
- 注意事項
- Specification多表查詢
- @Query多表查詢
- 只查詢指定字段
- 級聯操作
- 加載規則