好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

MapStruct实体间转换的简单用法

摘要: 在实际项目中,我们经常需要将po转dto、dto转po等一些实体间的转换。比较出名的有beanutil 和modelmapper等,它们使用简单,但是在稍显复杂的业务场景下力不从心。mapstruct这个插件可以用来处理domin实体类与model类的属性映射,可配置性强。

建立maven项目

mapstruct需要醒目构建工具(如maven)支持,如果项目结构不标准,可能无法生成对应的转换类。这里我使用maven构建工程。

?

1

2

3

<properties>

<org.mapstruct.version> 1.2 . 0 .cr1</org.mapstruct.version>

</properties>

mapstruct是一个一直在进步的工具,后面的版本不断改进之前版本的不足,修复之前版本的bug,使用的时候最好最新的稳定版。

?

1

2

3

4

5

6

7

<dependencies>

   <dependency>

     <groupid>org.mapstruct</groupid>

     <artifactid>mapstruct-jdk8</artifactid>

     <version>${org.mapstruct.version}</version>

   </dependency>

</dependencies>

需要引入的依赖包,可以看到使用到了java8,最新版甚至已经支持了java9。当然这不以为着你必须使用java8,java版本高于java6都是可以的。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<build>

   <plugins>

     <plugin>

<groupid>org.apache.maven.plugins</groupid>

       <artifactid>maven-compiler-plugin</artifactid>

       <version> 3.5 . 1 </version>

       <configuration>

         <source> 1.8 </source>

         <target> 1.8 </target>

         <annotationprocessorpaths>

           <path>

   <groupid>org.mapstruct</groupid>

             <artifactid>mapstruct-processor</artifactid>

         <version>${org.mapstruct.version}</version>

           </path>

         </annotationprocessorpaths>

       </configuration>

     </plugin>

   </plugins>

</build>

maven插件必不可少,格式也是固定的。

简单示例

有一个实体类-用户user

?

1

2

3

4

5

6

public class user {

   private int id;

   private string name;

   private boolean married;

// setters, getters, tostring

}

有一个实体类-雇员employee,雇员也是用户,只是比用户多一个属性-职位position

?

1

2

3

4

5

6

7

public class employee {

   private int id;

   private string name;

   private string position;

   private boolean married;

// setters, getters, tostring

}

在具体业务场景下,需要user 对象转换为employee对象,有时候需要employee对象转换为user对象。

使用mapstrut的话,需要写一个接口:

?

1

2

3

4

5

6

@mapper

public interface usermapper {

   usermapper instance = mappers.getmapper(usermapper. class );

   employee usertoemployee(user user);

   user employeetouser(employee employee);

}

运行示例

首先需要 mvn compile ,自动生成转换代码。生成的代码放在 target/annotations 下面。大致代码如下:

?

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

import com.training.mapstrut.model.employee;

import com.training.mapstrut.model.user;

import javax.annotation.generated;

@generated (

   value = "org.mapstruct.ap.mappingprocessor" ,

   date = "2017-08-26t17:08:23+0800" ,

   comments = "version: 1.2.0.cr1, compiler: javac, environment: java 1.8.0_92 (oracle corporation)"

)

public class usermapperimpl implements usermapper {

   @override

   public employee usertoemployee(user user) {

     if ( user == null ) {

       return null ;

     }

     employee employee = new employee();

     employee.setid( user.getid() );

     employee.setname( user.getname() );

     employee.setmarried( user.ismarried() );

     return employee;

   }

   @override

   public user employeetouser(employee employee) {

     if ( employee == null ) {

       return null ;

     }

     user user = new user();

     user.setid( employee.getid() );

     user.setname( employee.getname() );

     user.setmarried( employee.ismarried() );

     return user;

   }

}

简单的写个测试类实验一下。

?

1

2

3

4

5

6

7

8

9

10

11

12

public class apptest{

   @test

   public void apptest(){

     user user = new user();

     user.setid( 125 );

     user.setname( "chao" );

     user.setmarried( false );

     employee e = usermapper.instance.usertoemployee(user);

     system.out.println(e);

     assert .assertfalse(e.ismarried());

   }

}

结果输出:

employee [id=125, name=chao, position=null, married=false]

效果不错,需要转换的字段都准确无误,不过有人要吐糟了,这样的用法比beanutil 复杂多了,还只是达到相同的效果。这种简单的转化确实不需要mapstruct,它要做的是更为复杂的业务场景。

mapstruct问题

1.mapstruct转换不准确。

mapstruct一直在更新,一些特性在旧版本中无法识别,在使用的时候最要使用比较新的版本。

2.在eclipse下,需要m2e plug-in的支持,如果eclipse中没有集成,需要去 eclipse marketplace 中下载安装,有时候还需要指定下面的配置在 pom.xml 的properties中,以启用annotation processing
<m2e.apt.activation>jdt_apt</m2e.apt.activation>

3.在eclipse下,maven compile提示 nothing to compile - all classes are up to date ,你可以试一下在golds中填写 clean install compile 。

4.maven 编译的时候,必须使用jdk,不能使用jre,且版本要高于jdk6.

5.在eclipse中,确定已经生成了*mapperimpl.java,但是运行时报错 classnotfoundexception: cannot find implementation for com...*mapper
在项目上右键 – > properties – > java compiler – > annotation processing (enable 所有项) – > factory path (enable) – > add external jars – > 选择 mapstruct-processor-*.jar (大概在计算机的目录 .m2\repository\org\mapstruct\mapstruct-processor ) – > ok

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/jiangchao858/article/details/77512240

查看更多关于MapStruct实体间转换的简单用法的详细内容...

  阅读:17次