目录
  1. 1. 安装插件
  2. 2. build.gralde配置
    1. 2.1. 现有配置build.gradle
  3. 3. application.yml数据连接配置
  4. 4. 操作
    1. 4.1. Entity
    2. 4.2. DTO
  5. 5. entity mapper
  6. 6. user mapper
    1. 6.1. jpa repository
    2. 6.2. service
    3. 6.3. serviceImpl
    4. 6.4. controller
  7. 7. 生成mapstruct实现类
  8. 8. 运行
  9. 9. 更多的使用示例
IDEA中Gralde构建Springboot项目使用Mapstruct

安装插件

File->Settings->Plugins->Browse repositories…->搜索MapStruct Support,点击Install安装
File->Settings->Plugins->Browse repositories…->搜索Lombok,点击Install安装

build.gralde配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
...
plugins {
...
id 'net.ltgt.apt' version '0.15'
}

// You can integrate with your IDEs.
// See more details: https://github.com/tbroyer/gradle-apt-plugin#usage-with-ides
apply plugin: 'net.ltgt.apt-idea'
apply plugin: 'net.ltgt.apt-eclipse'

dependencies {
...
compile "org.mapstruct:mapstruct-jdk8:${mapstructVersion}"

annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}"
// If you are using mapstruct in test code
testAnnotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}"
}
...

以上配置引用官方配置,官方示例

现有配置build.gradle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
buildscript {
ext {
springBootVersion = '1.5.9.RELEASE'
}
repositories {
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("net.ltgt.gradle:gradle-apt-plugin:0.9")
}
}

apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: "net.ltgt.apt"

group = 'com.c'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
}


dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
runtime('mysql:mysql-connector-java')
compile('org.projectlombok:lombok:1.16.18')
compile("org.springframework.boot:spring-boot-starter-web")
testCompile('org.springframework.boot:spring-boot-starter-test')
/**
* mapstruct支持
* 此处注释的为老的写法,在gradle版本高的时候可能会出现不兼容的情况
* compile('org.mapstruct:mapstruct-jdk8:1.2.0.Final')
* apt ("org.mapstruct:mapstruct-processor:1.2.0.Final")
* mapstruct-lombok支持,省去POJO,DTO中的Getter、Setter
* 使用lombok注解@Data
* apt ("org.projectlombok:lombok:1.16.18")
*/

/**
* 新的配置
* 有关mapstruct与lombok
* https://github.com/mapstruct/mapstruct/issues/1581
*/
compile('org.mapstruct:mapstruct-jdk8:1.2.0.Final')
annotationProcessor("org.mapstruct:mapstruct-processor:1.2.0.Final")
compile("org.projectlombok:lombok:1.16.18")
annotationProcessor("org.projectlombok:lombok:1.16.18")//<<=== Lombok after mapstruct
}

/**
* 配置mapstruct任务
*/
tasks.withType(JavaCompile) {
options.compilerArgs = [
'-Amapstruct.suppressGeneratorTimestamp=true',
//默认交由spring管理,在外部可以直接使用@Autowired注入mapper
'-Amapstruct.defaultComponentModel=spring',
'-Amapstruct.suppressGeneratorVersionInfoComment=true',
'-Amapstruct.unmappedTargetPolicy=IGNORE'
]
}

application.yml数据连接配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server:
port: 8080

spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/mpt
username: root
password: 123456
jpa:
hibernate:
ddl-auto: update
properties:
hibernate:
show_sql: true
format_sql: true

操作

Entity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.c.test.entity;

import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "user")
@Data
public class User {

@Id
@GeneratedValue
private Long id;

private String name;

private Integer age;
}

DTO

1
2
3
4
5
6
7
8
9
10
11
package com.c.test.DTO;

import lombok.Data;

@Data
public class UserDTO {

private String name;

private Integer age;
}

entity mapper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.c.test.mapper;

import java.util.List;
import java.util.Set;

/**
* Mapper文件基类
* 更多的用法需自行实现
* @author -c
* @param <D> 目标对象,一般为DTO对象
* @param <E>源对象,一般为需要转换的对象
*/
public interface EntityMapper<D, E> {

/**
* 将源对象转换为目标对象
* @param e
* @return D
*/
D toDTO(E e);

/**
* 将源对象集合转换为目标对象集合
* @param es
* @return List<D>
*/
List<D> toDTO(List<E> es);

/**
* 将源对象集合转换为目标对象集合
* @param es
* @return Set<D>
*/
Set<D> toDTO(Set<E> es);

/**
* 将目标对象转换为源对象
* @param d
* @return E
*/
E toEntity(D d);

/**
* 将目标对象集合转换为源对象集合
* @param ds
* @return List<E>
*/
List<E> toEntity(List<D> ds);

/**
* 将目标对象集合转换为源对象集合
* @param ds
* @return Set<E>
*/
Set<E> Set(List<D> ds);
}

user mapper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.c.test.mapper;

import com.c.test.DTO.UserDTO;
import com.c.test.entity.User;
import org.mapstruct.Mapper;

/**
* userMapper接口类
* 继承EntityMapper
* @author -c
*/
@Mapper
public interface UserMapper extends EntityMapper<UserDTO, User>{
}

user mapper只需继承entity mapper,常用方法已封装在内

jpa repository

1
2
3
4
5
6
7
package com.c.test.repository;

import com.c.test.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User,Long> {
}

service

1
2
3
4
5
6
7
8
9
10
11
package com.c.test.service;

import com.c.test.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface UserService {
User findOne(Long l);

Page<User> findAll(Pageable pageable);
}

serviceImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.c.test.service;

import com.c.test.entity.User;
import com.c.test.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

@Autowired
private UserRepository userRepository;

@Override
public User findOne(Long l) {
return userRepository.findOne(l);
}

@Override
public Page<User> findAll(Pageable pageable) {
return userRepository.findAll(pageable);
}
}

controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.c.test.controller;

import com.c.test.entity.User;
import com.c.test.service.UserService;
import com.c.test.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* user控制器入口
* @author chensubao
*/
@Slf4j
@RestController
@RequestMapping("user")
public class UserController {

@Autowired
private UserService userService;

@Autowired
private UserMapper userMapper;

@GetMapping("/findOne")
public ResponseEntity findOne() {
return ResponseEntity.ok(userMapper.toDTO(userService.findOne(1L)));
}

@GetMapping
public ResponseEntity findAll(@PageableDefault(sort = {"id"}, direction = Sort.Direction.DESC) Pageable pageable){
Page<User> page = userService.findAll(pageable);
return ResponseEntity.ok(page.map(userMapper::toDTO));
}
}

生成mapstruct实现类

1
2
3
gradle build //会打包成jar文件,若有test类,请执行 gradle build -x test 
// 或
gradle compileJava //不打包jar文件

执行完后可以在build目录中找到生成的实现类

mapstruct-3.png

运行

  • 浏览器输入localhost:8088/user/findOne

mapstruct-1.png

  • 浏览器输入localhost:8088/user

mapstruct-2.png

更多的使用示例

官方文档

文章作者: 今晚早睡
文章链接: https://www.chensubao.com/article/35081.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 洛 苏り

评论