Skip to content

Commit

Permalink
aa
Browse files Browse the repository at this point in the history
  • Loading branch information
daipeng committed Sep 7, 2021
1 parent a5ebb58 commit f21658b
Show file tree
Hide file tree
Showing 24 changed files with 791 additions and 72 deletions.
86 changes: 82 additions & 4 deletions 122-springboot-demo-mapstruct/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,95 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>tech-pdai-spring-demos</artifactId>
<groupId>tech.pdai</groupId>
<version>1.0-SNAPSHOT</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>

<modelVersion>4.0.0</modelVersion>
<groupId>tech.pdai</groupId>
<artifactId>122-springboot-demo-mapstruct</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<org.mapstruct.version>1.4.2.Final</org.mapstruct.version>
<org.projectlombok.version>1.18.20</org.projectlombok.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- lombok dependencies should not end up on classpath -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.77</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<!-- See https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html -->
<!-- Classpath elements to supply as annotation processor path. If specified, the compiler -->
<!-- will detect annotation processors only in those classpath elements. If omitted, the -->
<!-- default classpath is used to detect annotation processors. The detection itself depends -->
<!-- on the configuration of annotationProcessors. -->
<!-- -->
<!-- According to this documentation, the provided dependency processor is not considered! -->
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

</project>
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);
}
}
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);
}

}
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();
}
}
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;
}
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格式存储
}
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;
}
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;
}
}
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;
}
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);
}
}
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);
}
Loading

0 comments on commit f21658b

Please sign in to comment.