【问题标题】:marklogic pojo databinding without annotate没有注释的marklogic pojo数据绑定
【发布时间】:2015-07-14 10:49:42
【问题描述】:

是否可以在不使用注释的情况下定义映射?如下所述:

@id 或@PathIndexProperty(scalarType = ScalarType.STRING)

我想在外部文件中定义绑定并告诉 newPojoRepository 使用它来进行映射。

谢谢。

耳廓

【问题讨论】:

    标签: mapping marklogic pojo


    【解决方案1】:

    您始终可以创建一个新类来扩展您的独立 pojo 对象并将您的 pojo 注释附加到新类。这是一个例子。请记住在 ManagedCat/averageHeight 上创建 int 类型的路径范围索引。

    猫.java

    public class Cat {
        private String id;
        private int averageHeight;
    
        public Cat() {}
    
        public Cat(String id, int averageHeight) {
            this.id = id;
            this.averageHeight = averageHeight;
        }
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public int getAverageHeight() {
            return averageHeight;
        }
    
        public void setAverageHeight(int averageHeight) {
            this.averageHeight = averageHeight;
        }
    
        public String toString() {
            return "id=[" + id + "] averageHeight=[" + averageHeight + "]";
        }
    }
    

    ManagedCat.java

    import com.marklogic.client.pojo.annotation.Id;
    import com.marklogic.client.pojo.annotation.PathIndexProperty;
    import com.marklogic.client.pojo.annotation.PathIndexProperty.ScalarType;
    
    public class ManagedCat extends Cat {
        public ManagedCat() {
            super();
        }
    
        public ManagedCat(String id, int averageHeight) {
            super(id, averageHeight);
        }
    
        @Id
        @Override
        public String getId() {
            return super.getId();
        }
    
        @PathIndexProperty(scalarType = ScalarType.STRING)
        @Override
        public int getAverageHeight() {
            return super.getAverageHeight();
        }
    }
    

    CatManager.java

    import com.marklogic.client.DatabaseClient;
    import com.marklogic.client.DatabaseClientFactory;
    import static com.marklogic.client.DatabaseClientFactory.Authentication.DIGEST;
    import com.marklogic.client.pojo.PojoPage;
    import com.marklogic.client.pojo.PojoQueryDefinition;
    import com.marklogic.client.pojo.PojoRepository;
    import static com.marklogic.client.pojo.PojoQueryBuilder.Operator.GT;
    import java.util.ArrayList;
    import java.util.List;
    
    public class CatManager {
        private DatabaseClient client = DatabaseClientFactory.newClient(
            "localhost", 8000, "admin", "admin", DIGEST);
        private PojoRepository<ManagedCat, String> catRepo =
            client.newPojoRepository(ManagedCat.class, String.class);
        private PojoQueryDefinition largeCatQuery =
            catRepo.getQueryBuilder().range("averageHeight", GT, 20);
        private long start = 1;
    
        public void saveCat(ManagedCat cat) {
            catRepo.write(cat);
        }
    
        public Cat getCat(String id) {
            return catRepo.read(id);
        }
    
        public List<Cat> getLargeCats() {
            ArrayList list = new ArrayList<Cat>();
            catRepo.search(largeCatQuery, start)
                .forEach(cat -> list.add(cat));
            return list;
        }
    
        public static void main(String[] args) {
            CatManager catMgr = new CatManager();
            catMgr.saveCat( new ManagedCat("houseCat", 10) );
            catMgr.saveCat( new ManagedCat("lion",     45) );
    
            System.out.println("saved house cat=[" + catMgr.getCat("houseCat")+ "]");
            System.out.println("saved lion=[" + catMgr.getCat("lion")+ "]");
            System.out.println("large cats=[" + catMgr.getLargeCats()  + "]");
        }
    }
    

    【讨论】:

      【解决方案2】:

      在 Java 中,这是一个非常普遍的问题,因此存在许多可用的数据映射工具,它们为您提供了广泛的选择。 首先,大多数数据映射工具很乐意忽略它们没有专门处理的注释。一个常见的用例是将 JAXB 类映射到其他用途(充满注释)。 例如,Jackson DataBinding 库 (https://github.com/FasterXML/jackson-databind) 在带有“外来”注释的类上工作得非常好。我会首先检查您的工具,以确认您需要做任何事情。

      更常见的情况是数据映射工具需要自己的注释,但您想要映射没有源或无法更改的第 3 方类。再一次,Jackson 用许多选项很好地处理了这个问题,包括“视图”和“混合”

      对于未连接到序列化并且也针对相同问题设计的数据绑定库,请考虑纯“数据映射”库 这会将一个类映射/复制到另一个类,而无需自定义任何一个类。

      MapStruct: http://mapstruct.org/ 推土机:https://github.com/DozerMapper/dozer

      请注意,Jackson 的 DataMapper 可用于数据绑定和数据映射。

      这里有一个列表,其中大部分我都没有尝试过, http://www.javacodegeeks.com/2013/10/java-object-to-object-mapper.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多