Mapstruct 使用方法(mappings)

案例:现在有这么个场景,从数据库查询出来了一个user对象(包含id,用户名,密码,手机号,邮箱,角色这些字段)和一个对应的角色对象role(包含id,角色名,角色描述这些字段),现在在controller需要用到user对象的id,用户名,和角色对象的角色名三个属性。一种方式是直接把两个对象传递到controller层,但是这样会多出很多没用的属性。更通用的方式是需要用到的属性封装成一个类**(DTO**),通过传输这个类的实例来完成数据传输。
导入依赖:

org.mapstruct

mapstruct-jdk8
1.2.0.Final


org.mapstruct
mapstruct-processor
1.2.0.Final

原文链接:https://blog.csdn.net/qq122516902/article/details/87259752
1.建一个user类和role类
@AllArgsConstructor
@Data
public class User {
private Long id;
private String username;
private String password;
private String phoneNum;
private String email;
private Role role;
}

@AllArgsConstructor
@Data
public class Role {
private Long id;
private String roleName;
private String description;
}
2.建一个DTO类(数据传输类)

@Data
public class UserRoleDto {
/**
* 用户id
/
private Long userId;
/
*
* 用户名
/
private String name;
/
*
* 角色名
*/
private String roleName;
}
3.使用MapStruct来封装需要的属性即上面的第二种方式(第一种方式就不说了大家都懂)
这里我们沿用User.java、Role.java、UserRoleDto.java。
新建一个UserRoleMapper.java,这个来用来定义User.java、Role.java和UserRoleDto.java之间属性对应规则:
UserRoleMapper.java
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

/**

  • @Mapper 定义这是一个MapStruct对象属性转换接口,在这个类里面规定转换规则
  •      在项目构建时,会自动生成改接口的实现类,这个实现类将实现对象属性值复制
    

*/
@Mapper
public interface UserRoleMapper {

/**
 * **获取该类自动生成的实现类的实例
 * 接口中的属性都是 public static final 的 方法都是public abstract的**
 */
UserRoleMapper INSTANCES = Mappers.getMapper(UserRoleMapper.class);

/**
 * 这个方法就是用于实现对象属性复制的方法
 *
 * @Mapping 用来定义属性复制规则 source 指定源对象属性 target指定目标对象属性
 *
 * @param user 这个参数就是源对象,也就是需要被复制的对象
 * @return 返回的是目标对象,就是最终的结果对象
 */
@Mappings({
        @Mapping(source = "id", target = "userId"),
        @Mapping(source = "username", target = "name"),
        @Mapping(source = "role.roleName", target = "roleName")
})
UserRoleDto toUserRoleDto(User user);

/**
* 更新对象属性
* @param user
* @param userRoleDto
* @return
*/
@Mappings({
@Mapping(source = “id”, target = “userId”),
@Mapping(source = “username”, target = “name”),
@Mapping(source = “role.roleName”, target = “roleName”)
})
void update(User user, @MappingTarget UserRoleDto userRoleDto);
}
4.测试
package com.lym.demo;

import com.lym.demo.dto.UserRoleDto;
import com.lym.demo.mapper.TestMapper;
import com.lym.demo.mapper.UserRoleMapper;
import com.lym.demo.pojo.Person;
import com.lym.demo.pojo.Role;
import com.lym.demo.pojo.User;
import com.lym.demo.service.TestService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**

  • @Author liyueming

  • @CreateTime 2021/2/22

  • @Description
    /
    public class JunitTest {
    User user = null;
    /
    *

    • 模拟从数据库中查出user对象
      */
      @Before
      public void before() {
      Role role = new Role(2L, “administrator”, “超级管理员”);
      user = new User(1L, “zhangsan”, “12345”, “17677778888”, “123@qq.com”, role);
      }

    @Test
    public void test1() throws InterruptedException {
    UserRoleDto userRoleDto = new UserRoleDto();
    userRoleDto.setUserId(user.getId());
    userRoleDto.setName(user.getUsername());
    userRoleDto.setRoleName(user.getRole().getRoleName());
    System.out.println(userRoleDto);

    }
    @Test
    public void testMapStruct() throws InterruptedException {
    UserRoleDto userRoleDto = UserRoleMapper.INSTANCES.toUserRoleDto(user);
    System.out.println(userRoleDto);

    }
    @Test
    public void testMapStructUpdate() throws InterruptedException {
    UserRoleDto userRoleDto = new UserRoleDto();
    UserRoleMapper.INSTANCES.update(user,userRoleDto);
    System.out.println(userRoleDto);

    }
    }

@InheritInverseConfiguration使用该注解的前提是有了正映射,就可以使用它达到逆映射
@InheritConfiguration可以继承@Mapping,@BeanMapping,@IterableMapping的映射规则。
@InheritConfiguration注解的方法上,有需要映射的字段,它会搜索有相同配置的映射,找到了直接复用此映射;若找到多个方法上都有满足此映射的配置,需要制定@InheritConfiguration#name的值,制定继承方法的映射。
使用Spring依赖注入

// 这里主要是这个***componentModel 属性,它的值就是当前要使用的依赖注入的环境***
@Mapper(componentModel = “spring”)
public interface CustomerMapper {

@Mapping(source = "name", target = "customerName")
CustomerDto toCustomerDto(Customer customer);

}
这样的话就少去了上面的Mappers.getMapper(XXX.class)方法

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值