mapstruct的使用

1. mapstruct的优点: 在编译期生成接口 的实现类,实现类中就是重写了接口方法,方法 中使用的是get,set操作,相比BeanUtil,PropertiesUtil,Cglib的BeanCopier的优点也就在于不用再赋值期间使用反射来赋值。记住一句话: set,get赋值是最快的。mapstruct 就可以帮助我们生成繁琐的get,set操作的代码,而且 还能判断是否为null,为null,则不赋值。

2. 添加依赖

<properties>
        <java.version>1.8</java.version>
        <org.mapstruct.version>1.3.0.Final</org.mapstruct.version>
</properties>
<dependencies>
      <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-jdk8</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>

 </dependencies>

3.创建接口类

package com.ws;

import com.ws.mybatis.model.UserInfo;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
import org.mapstruct.NullValuePropertyMappingStrategy;

/**
 * @author :wangsun
 * @date :Created in 2020/10/20 17:25
 * @project : mybatis-test
 * @package : com.ws
 * @description:  NullValuePropertyMappingStrategy 是增加null值的判断策略
 */
@Mapper(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
public interface  UserCopier {

      // 创建一个新的对象,并将参数中的对象的值赋值给新对象
    UserInfo cp2UserInfo(UserInfo userInfo);
    // 将 对象b赋值给对象a
    void updateAFromB(UserInfo b, @MappingTarget UserInfo a);
}

4. 测试类

@Test
    public void testBeanCopyByMapWithoutNull(){
        UserInfo userInfo = new UserInfo();
        userInfo.setId(1);
        userInfo.setAddress("单独");
        UserInfo newUserInfo = new UserInfo();
        newUserInfo.setAge(1);
        newUserInfo.setPhone("2156345");
        UserCopier mapper = Mappers.getMapper(UserCopier.class);
        mapper.updateAFromB(userInfo,newUserInfo);
        System.out.println("newUser is "+newUserInfo.toString());

        System.out.println("userInfo is "+userInfo.toString());
    }

5. 编译工具生成的实现类,不用自己手写,这步才是真的减轻我们的工作量

注意: a.setId(b.getId()); id 会直接赋值,,在真实业务场景中要注意id覆盖的问题。

package com.ws;

import com.ws.mybatis.model.UserInfo;

public class UserCopierImpl implements UserCopier {
    public UserCopierImpl() {
    }

    public UserInfo cp2UserInfo(UserInfo userInfo) {
        if (userInfo == null) {
            return null;
        } else {
            UserInfo userInfo1 = new UserInfo();
            userInfo1.setId(userInfo.getId());
            userInfo1.setNameStr(userInfo.getNameStr());
            userInfo1.setAge(userInfo.getAge());
            userInfo1.setSalary(userInfo.getSalary());
            userInfo1.setAddress(userInfo.getAddress());
            userInfo1.setPhone(userInfo.getPhone());
            userInfo1.setUpdateTime(userInfo.getUpdateTime());
            userInfo1.setUpdateLongTime(userInfo.getUpdateLongTime());
            return userInfo1;
        }
    }

    public void updateAFromB(UserInfo b, UserInfo a) {
        if (b != null) {
            a.setId(b.getId());
            if (b.getNameStr() != null) {
                a.setNameStr(b.getNameStr());
            }

            if (b.getAge() != null) {
                a.setAge(b.getAge());
            }

            if (b.getSalary() != null) {
                a.setSalary(b.getSalary());
            }

            if (b.getAddress() != null) {
                a.setAddress(b.getAddress());
            }

            if (b.getPhone() != null) {
                a.setPhone(b.getPhone());
            }

            if (b.getUpdateTime() != null) {
                a.setUpdateTime(b.getUpdateTime());
            }

            if (b.getUpdateLongTime() != null) {
                a.setUpdateLongTime(b.getUpdateLongTime());
            }

        }
    }
}

posted @ 2020-10-22 15:48  尘世的流浪人  阅读(1777)  评论(0编辑  收藏  举报