【org.mapstruct】使用org.mapstruct做bean的映射
针对编译时碰到空指针问题,需要在idea工具中加入如上配置
-Djps.track.ap.dependencies=false
一、在pom中引入依赖。lombok的依赖,要在mapstruct之前【这个还是比较不靠谱的,或者双方都实现sett,gett方法】
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.18</version> </dependency> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-jdk8</artifactId> <version>1.3.1.Final</version> </dependency> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>1.3.1.Final</version> </dependency>
二、entity和 PO 的示意
entity
package com.sxq.study.dao.domain; import lombok.Data; public class User { /** * This field was generated by MyBatis Generator. * This field corresponds to the database column sxq_user.id * * @mbggenerated Sun Aug 06 18:45:18 CST 2023 */ private long id; /** * This field was generated by MyBatis Generator. * This field corresponds to the database column sxq_user.user_name * * @mbggenerated Sun Aug 06 18:45:18 CST 2023 */ private String userName; /** * This field was generated by MyBatis Generator. * This field corresponds to the database column sxq_user.age * * @mbggenerated Sun Aug 06 18:45:18 CST 2023 */ private Integer age; /** * This field was generated by MyBatis Generator. * This field corresponds to the database column sxq_user.address * * @mbggenerated Sun Aug 06 18:45:18 CST 2023 */ private String address; /** * This field was generated by MyBatis Generator. * This field corresponds to the database column sxq_user.password * * @mbggenerated Sun Aug 06 18:45:18 CST 2023 */ private String password; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
po
package com.sxq.study.dao.po; import lombok.Data; /** * @author: shangxiaofei01@shizhuang-inc.com * @description: * @date: 2023/8/9 13:58 */ public class UserDTO { private long id; /** * This field was generated by MyBatis Generator. * This field corresponds to the database column sxq_user.user_name * * @mbggenerated Sun Aug 06 18:45:18 CST 2023 */ private String userName; /** * This field was generated by MyBatis Generator. * This field corresponds to the database column sxq_user.age * * @mbggenerated Sun Aug 06 18:45:18 CST 2023 */ private Integer age; /** * This field was generated by MyBatis Generator. * This field corresponds to the database column sxq_user.address * * @mbggenerated Sun Aug 06 18:45:18 CST 2023 */ private String address; /** * This field was generated by MyBatis Generator. * This field corresponds to the database column sxq_user.password * * @mbggenerated Sun Aug 06 18:45:18 CST 2023 */ private String password; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
三、Mapper接口定义
package com.sxq.study.dao.po.mapper; import com.sxq.study.dao.domain.User; import com.sxq.study.dao.po.UserDTO; import org.mapstruct.Mapper; /** * @author: shangxiaofei01@shizhuang-inc.com * @description: * @date: 2023/8/9 13:59 */ @Mapper() public interface UserMapper { UserDTO entity2UserDTO(User user); }
四、测试
进行代码编译,就会生成如下类 。 路径target/generated-sources/annotations下
package com.sxq.study.dao.po.mapper; import com.sxq.study.dao.domain.User; import com.sxq.study.dao.po.UserDTO; import javax.annotation.Generated; @Generated( value = "org.mapstruct.ap.MappingProcessor", date = "2023-08-09T14:48:06+0800", comments = "version: 1.3.1.Final, compiler: javac, environment: Java 1.8.0_381 (Oracle Corporation)" ) public class UserMapperImpl implements UserMapper { @Override public UserDTO entity2UserDTO(User user) { if ( user == null ) { return null; } UserDTO userDTO = new UserDTO(); userDTO.setId( user.getId() ); userDTO.setUserName( user.getUserName() ); userDTO.setAge( user.getAge() ); userDTO.setAddress( user.getAddress() ); userDTO.setPassword( user.getPassword() ); return userDTO; } }