好得很程序员自学网

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

MapStruct处理Java中实体与模型间不匹配属性转换的方法

摘要: 前面介绍了 mapstrut简单用法 ,mapstrut的最重要的特点就是处理java中实体与模型间不匹配属性的转换。

实体模型

有一个user对象:

?

1

2

3

4

5

6

7

public class user {

   private integer id;

   private string name;

   private double account;

   private boolean married;

// setters, getters, tostring()

}

有一个employee 对象:

?

1

2

3

4

5

6

7

public class employee {

   private int id;

   private string ename;

   private string position;

   private string married;

// setters, getters, tostring()

}

业务场景

需要user 与employee 对象之间转换。 user 的name属性对应employee 的ename属性,其取值相同,类型相同,名称不同 user 的married属性(取值true和false)对应employee 的married属性(取值y和n),其取值不同,类型不同,名称相同。

分析与实现

最愚蠢的方式是自己写一堆的setter方法与getter方法,大量get/set代码堆积,增加了代码长度和阅读代码的难度。利用工具beanutils是可以处理第一个需求的,但第三种需求就无能为力了。这时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);

}

对于第二个需求,可以通过下面方式实现,注解 @mapping 可以指定需要把哪个字段 source 转换为哪个字段 target 。

?

1

2

3

4

5

6

7

8

9

10

11

12

@mapper

public interface usermapper {

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

   @mappings ({

     @mapping (source= "name" , target= "ename" )

   })

   employee usertoemployee(user user);

   @mappings ({

     @mapping (source= "ename" , target= "name" )

   })

   user employeetouser(employee employee);

}

第三个需求有点变态,但是真实发生在我们的项目中,实现起来确实繁琐一些:

首先,自定义转化逻辑,布尔值到字符串,布尔的true对应字符串的y,布尔的false对应字符串的n:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

public class usertransform {

   public string booleantostring( boolean value){

     if (value){

       return "y" ;

     }

     return "n" ;

   }

   public boolean strtoboolean(string str){

     if ( "y" .equals(str)) {

       return true ;

     }

     return false ;

   }

}

使用很简单,在接口的注解mapper添加 uses 参数,值就是需要刚才的转换逻辑类。

?

1

2

@mapper (uses = usertransform. class )

public interface usermapper {...}

结果与分析

用junit test写两个测试方法,分别测试user 对象转换employee ,employee 对象转换user。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

public class midtest {

   @test

   public void midtest(){

     user user = new user();

     user.setid( 125 );

     user.setname( "lee" );

     user.setmarried( true );

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

     system.out.println(e);

   }

   @test

   public void midtest2(){

     employee e = new employee();

     e.setid( 222 );

     e.setename( "chao" );

     e.setmarried( "n" );

     user u = usermapper.instance.employeetouser(e);

     system.out.println(u);

   }

}

结果如下:

user [id=222, name=chao, account=0.0, married=false]
employee [id=125, ename=lee, position=null, married=y]

转换结果符合预期,转化期间不存在的属性,有了默认值(account和position),包装类也能识别(int和integer),从自动生成的 usermapperimpl.java 中,可以看到,

employee.setmarried( usertransform.booleantostring( user.ismarried() ) ); ,用到了刚才自定义的转换逻辑。第三种需求是很少的,但是遇到了也是很难解决的,mapstruct的自定义函数确实方便不少,不过与其他的转换工具相比,上手难度确实大,配置也稍显繁琐。

项目代码托管在码云。

总结

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

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

查看更多关于MapStruct处理Java中实体与模型间不匹配属性转换的方法的详细内容...

  阅读:24次