教程 > mapstruct 教程 > 阅读:1279

mapstruct 自定义映射——迹忆客-ag捕鱼王app官网

我们也可以向使用 org.mapstruct.mapper 注解创建的 mapper 添加自定义方法。 我们也可以创建抽象类而不是接口。 mapstruct 会自动创建相应的映射器类。

现在在接口中创建一个默认的转换方法。

@mapper
public interface studentmapper {
   default student getmodelfromentity(studententity studententity){
      student student = new student();
      student.setid(studententity.getid());
      student.setname(studententity.getname());
      student.setclassname(studententity.getclassval());
      return student;
   }
}

以类似的方式,我们可以创建一个抽象类以及一个映射器。

@mapper
public absgract class studentmapper {
   student getmodelfromentity(studententity studententity){
      student student = new student();
      student.setid(studententity.getid());
      student.setname(studententity.getname());
      student.setclassname(studententity.getclassval());
      return student;
   }
}

示例

打开 mapstruct 基本映射 章节中更新的项目映射。

student.java

package com.jiyik.model;
public class student {
    private int id;
    private string name;
    private string classname;
    public int getid() {
        return id;
    }
    public void setid(int id) {
        this.id = id;
    }
    public string getname() {
        return name;
    }
    public void setname(string name) {
        this.name = name;
    }
    public string getclassname() {
        return classname;
    }
    public void setclassname(string classname) {
        this.classname = classname;
    }
}

studententity.java

package com.jiyik.entity;
public class studententity {
    private int id;
    private string name;
    private string classval;
    public int getid() {
        return id;
    }
    public void setid(int id) {
        this.id = id;
    }
    public string getname() {
        return name;
    }
    public void setname(string name) {
        this.name = name;
    }
    public string getclassval() {
        return classval;
    }
    public void setclassval(string classval) {
        this.classval = classval;
    }
}

studentmapper.java

package com.jiyik.mapper;
import com.jiyik.entity.studententity;
import com.jiyik.model.student;
import org.mapstruct.mapper;
import org.mapstruct.mapping;
@mapper
public interface studentmapper {
    default student getmodelfromentity(studententity studententity){
        student student = new student();
        student.setid(studententity.getid());
        student.setname(studententity.getname());
        student.setclassname(studententity.getclassval());
        return student;
    }
    @mapping(target="classval", source="classname")
    studententity getentityfrommodel(student student);
}

studentmappertest.java

import com.jiyik.entity.studententity;
import com.jiyik.mapper.studentmapper;
import com.jiyik.model.student;
import org.junit.test;
import org.mapstruct.factory.mappers;
import static org.junit.assert.assertequals;
public class studentmappertest {
    private studentmapper studentmapper = mappers.getmapper(studentmapper.class);
    @test
    public void testentitytomodel() {
        studententity entity = new studententity();
        entity.setclassval("x");
        entity.setname("john");
        entity.setid(1);
        student model = studentmapper.getmodelfromentity(entity);
        assertequals(entity.getclassval(), model.getclassname());
        assertequals(entity.getname(), model.getname());
        assertequals(entity.getid(), model.getid());
    }
    @test
    public void testmodeltoentity() {
        student model = new student();
        model.setid(1);
        model.setname("john");
        model.setclassname("x");
        studententity entity = studentmapper.getentityfrommodel(model);
        assertequals(entity.getclassval(), model.getclassname());
        assertequals(entity.getname(), model.getname());
        assertequals(entity.getid(), model.getid());
    }
}

运行以下命令来测试映射。

$ mvn clean test

执行结果如下

mapstruct 基础映射

查看笔记

扫码一下
查看教程更方便
网站地图