-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
daipeng
committed
Sep 7, 2021
1 parent
a5ebb58
commit f21658b
Showing
24 changed files
with
791 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
122-springboot-demo-mapstruct/src/main/java/tech/pdai/springboot/mapstruct/App.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package tech.pdai.springboot.mapstruct; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* @author pdai | ||
*/ | ||
@SpringBootApplication | ||
public class App { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(App.class, args); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...emo-mapstruct/src/main/java/tech/pdai/springboot/mapstruct/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package tech.pdai.springboot.mapstruct.controller; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import tech.pdai.springboot.mapstruct.entity.param.UserQueryParam; | ||
import tech.pdai.springboot.mapstruct.entity.vo.UserVo; | ||
import tech.pdai.springboot.mapstruct.entity.vo.UserWithAddressVo; | ||
import tech.pdai.springboot.mapstruct.service.IUserService; | ||
|
||
/** | ||
* @author pdai | ||
*/ | ||
@RestController | ||
@RequestMapping("/user") | ||
public class UserController { | ||
|
||
@Autowired | ||
private IUserService userService; | ||
|
||
@GetMapping("list") | ||
public List<UserVo> list(UserQueryParam userParam) { | ||
return userService.userList(userParam); | ||
} | ||
|
||
@GetMapping("bind") | ||
public UserWithAddressVo bind(UserQueryParam userParam) { | ||
return userService.bindAddressTest(userParam); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
122-springboot-demo-mapstruct/src/main/java/tech/pdai/springboot/mapstruct/dao/IUserDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package tech.pdai.springboot.mapstruct.dao; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.springframework.stereotype.Component; | ||
import tech.pdai.springboot.mapstruct.entity.User; | ||
import tech.pdai.springboot.mapstruct.entity.param.UserQueryParam; | ||
|
||
/** | ||
* @author pdai | ||
*/ | ||
@Component | ||
public class IUserDao { | ||
|
||
public List<User> list(UserQueryParam userParam) { | ||
return Collections.singletonList(findOne()); | ||
} | ||
|
||
public User findOne() { | ||
return User.builder() | ||
.id(1L).username("pdai") | ||
.birthday(LocalDate.now()) | ||
.createTime(LocalDateTime.now()) | ||
.sex(1) | ||
.config("[{\"field1\":\"xxx\", \"field2\": 22}]") | ||
.description("hello mapstruct") | ||
.password("xdafsfasdfasdf") | ||
.build(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...pringboot-demo-mapstruct/src/main/java/tech/pdai/springboot/mapstruct/entity/Address.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package tech.pdai.springboot.mapstruct.entity; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* @author pdai | ||
*/ | ||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class Address { | ||
private String street; | ||
private Integer zipCode; | ||
private Integer houseNo; | ||
private String description; | ||
} |
28 changes: 28 additions & 0 deletions
28
122-springboot-demo-mapstruct/src/main/java/tech/pdai/springboot/mapstruct/entity/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package tech.pdai.springboot.mapstruct.entity; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* @author pdai | ||
*/ | ||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class User { | ||
private Long id; | ||
private String username; | ||
private String password; // 密码 | ||
private Integer sex; // 性别 | ||
private String description; // 个人描述 | ||
private LocalDate birthday; // 生日 | ||
private LocalDateTime createTime; // 创建时间 | ||
private String config; // 其他扩展信息,以JSON格式存储 | ||
} |
18 changes: 18 additions & 0 deletions
18
...o-mapstruct/src/main/java/tech/pdai/springboot/mapstruct/entity/param/UserQueryParam.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package tech.pdai.springboot.mapstruct.entity.param; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* @author pdai | ||
*/ | ||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class UserQueryParam { | ||
private Long id; | ||
private String username; | ||
} |
32 changes: 32 additions & 0 deletions
32
...ingboot-demo-mapstruct/src/main/java/tech/pdai/springboot/mapstruct/entity/vo/UserVo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package tech.pdai.springboot.mapstruct.entity.vo; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Singular; | ||
|
||
/** | ||
* @author pdai | ||
*/ | ||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class UserVo { | ||
private Long id; | ||
private String username; | ||
private Integer gender; | ||
private LocalDate birthday; | ||
private String createTime; | ||
private List<UserConfig> configs; | ||
|
||
@Data | ||
public static class UserConfig { | ||
private String field1; | ||
private Integer field2; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...o-mapstruct/src/main/java/tech/pdai/springboot/mapstruct/entity/vo/UserWithAddressVo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package tech.pdai.springboot.mapstruct.entity.vo; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* @author pdai | ||
*/ | ||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class UserWithAddressVo { | ||
|
||
private String username; | ||
private Integer sex; | ||
private String street; | ||
private Integer zipCode; | ||
private Integer houseNumber; | ||
private String description; | ||
} |
47 changes: 47 additions & 0 deletions
47
...oot-demo-mapstruct/src/main/java/tech/pdai/springboot/mapstruct/mapper/UserConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package tech.pdai.springboot.mapstruct.mapper; | ||
|
||
|
||
import java.util.List; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
import org.mapstruct.factory.Mappers; | ||
import tech.pdai.springboot.mapstruct.entity.Address; | ||
import tech.pdai.springboot.mapstruct.entity.User; | ||
import tech.pdai.springboot.mapstruct.entity.param.UserQueryParam; | ||
import tech.pdai.springboot.mapstruct.entity.vo.UserVo; | ||
import tech.pdai.springboot.mapstruct.entity.vo.UserWithAddressVo; | ||
|
||
/** | ||
* @author pdai | ||
*/ | ||
@Mapper(componentModel="spring") | ||
public interface UserConverter { | ||
|
||
@Mapping(target = "gender", source = "user.sex") | ||
@Mapping(target = "configs", source = "user.config") | ||
@Mapping(target = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss") | ||
UserVo do2vo(User user); | ||
|
||
@Mapping(target = "sex", source = "gender") | ||
@Mapping(target = "config", source = "configs") | ||
@Mapping(target = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss") | ||
User vo2Do(UserVo userVo); | ||
|
||
UserQueryParam vo2QueryParam(User var1); | ||
|
||
List<UserVo> do2voList(List<User> userList); | ||
|
||
@Mapping(target = "description", source = "user.description") | ||
@Mapping(target = "houseNumber", source = "address.houseNo") | ||
UserWithAddressVo userAndAddress2Vo(User user, Address address); | ||
|
||
default List<UserVo.UserConfig> mapConfigs(String config) { | ||
return JSON.parseArray(config, UserVo.UserConfig.class); | ||
} | ||
|
||
default String mapConfig(List<UserVo.UserConfig> configs) { | ||
return JSON.toJSONString(configs); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...oot-demo-mapstruct/src/main/java/tech/pdai/springboot/mapstruct/service/IUserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package tech.pdai.springboot.mapstruct.service; | ||
|
||
import java.util.List; | ||
|
||
import tech.pdai.springboot.mapstruct.entity.param.UserQueryParam; | ||
import tech.pdai.springboot.mapstruct.entity.vo.UserVo; | ||
import tech.pdai.springboot.mapstruct.entity.vo.UserWithAddressVo; | ||
|
||
/** | ||
* @author pdai | ||
*/ | ||
public interface IUserService { | ||
List<UserVo> userList(UserQueryParam userParam); | ||
|
||
UserWithAddressVo bindAddressTest(UserQueryParam userParam); | ||
} |
Oops, something went wrong.