zoukankan      html  css  js  c++  java
  • Dozer使用

    转载于:https://blog.csdn.net/baidu_37107022/article/details/89082576?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-2.control&dist_request_id=&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-2.control

    Dozer依赖:

    <!--类转换依赖-->
    <dependency>
    <groupId>com.github.dozermapper</groupId>
    <artifactId>dozer-spring-boot-starter</artifactId>
    <version>6.2.0</version>
    </dependency>

     实体类:

    VO:

    package com.baise.model;
    
    import com.baise.bean.ArticleCategory;
    import com.baise.bean.BaseBean;
    import com.fasterxml.jackson.annotation.JsonInclude;
    import lombok.AllArgsConstructor;
    import lombok.Getter;
    import lombok.NoArgsConstructor;
    import lombok.Setter;
    import org.dozer.Mapping;
    
    import java.util.Date;
    
    /**
     * @author wjn
     * @version 1.0
     * @date 2021/4/15 16:24
     */
    @Setter
    @Getter
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @AllArgsConstructor
    @NoArgsConstructor
    public class ArticlePageViewModel{
    
        private Long id;//文章id
    
        private String title;//文章标题
    
        private String keyword;//关键词
    
        private String describe;//描述
    
        private String content;//文章内容
    
        private String imgUrl;//缩略图
    
        private Integer type;//文章类别:1表示文章列表、2表示专题列表、3表示banana图
    
        private String author;//作者
    
        private Integer categoryId;//分类id
    
        @Mapping("articleCategory.articleCategoryName")
        private String articleCategoryName;//分类名称
    
        private Date createTime;//创建时间
    
    
    }
    

    Entity:

    package com.baise.bean;
    
    import com.fasterxml.jackson.annotation.JsonInclude;
    import lombok.*;
    import org.dozer.Mapping;
    
    /**
     * @author wjn
     * @version 1.0
     * @date 2021/4/15 9:56
     */
    @Setter
    @Getter
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @AllArgsConstructor
    @NoArgsConstructor
    public class Article extends BaseBean{
    
        private String title;//文章标题
    
        private String keyword;//关键词
    
        private String describe;//描述
    
        private Integer categoryId;//文章分类id
    
        private Integer state;//是否发布:true表示发布、false表示不发布
    
        private String content;//文章内容
    
        private String imgUrl;//缩略图
    
        private Integer type;//文章类别:1表示文章列表、2表示专题列表、3表示banana图
    
        private String author;//作者
    
        private ArticleCategory articleCategory;//文章分类id
    
        private Integer timeState;//1表示一天内、2表示一周内、3表示一月内、4表示一年内
    
    }
    

    mybatis查询语句:

    <select id="selectArticleListPageView" resultType="com.baise.bean.Article">
            SELECT
                a.id AS "id",
                a.title AS "title",
                a.author AS "author",
                b.NAME AS "articleCategory.articleCategoryName",
                a.update_time AS "createTime",
                a.img_url AS "imgUrl",
                a.keyword AS "keyword",
                a.DESCRIBE AS "describe",
                a.type AS "type",
                a.content AS "content",
                b.id AS "categoryId"
            FROM
                article a
                INNER JOIN article_category b ON a.category_id = b.id
            WHERE
                a.not_delete = 1
                AND a.id = #{id}
                AND a.state =1
        </select>
    

    Service层类转换代码:

    @Override
        public PageModel selectAdminArticleList(Integer pageNum, Integer pageSize, Article article) {
            Page<AdminArticleListModel> pageInfo = PageHelper.startPage(pageNum, pageSize, true)
                    .doSelectPage(() -> Dozer.convert(articleMapper.selectAdminArticleList(article),AdminArticleListModel.class));
            return new PageModel(pageInfo.getPages(), pageSize, pageInfo.getTotal(), pageInfo);
        }
    

     Dozer工具类:

    package com.baise.utils;
    
    /*import com.github.dozermapper.core.Mapper;*/
    import org.dozer.Mapper;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.lang.NonNull;
    import org.springframework.lang.Nullable;
    import org.springframework.stereotype.Component;
    
    import javax.annotation.PostConstruct;
    import java.util.Collections;
    import java.util.List;
    import java.util.Optional;
    import java.util.stream.Collectors;
    
    /**
     *
     * dozer转换类
     *
     *
    
     */
    @Component
    public class Dozer {
    
        /**
         * dozer转换的核心mapper对象
         */
        private static Mapper dozerMapper;
    
        @Autowired
        private Mapper mapper;
    
        @PostConstruct
        private void construct() {
            Dozer.setDozerMapper(mapper);
        }
    
        private static void setDozerMapper(Mapper dozerMapper) {
            Dozer.dozerMapper = dozerMapper;
        }
    
        /**
         * 转换实体为另一个指定的实体
         * 任意一个参数为NULL时 会抛出NPE
         * {@link }//com.github.dozermapper.core.util.MappingValidator#validateMappingRequest
         *
         * @param source 源实体 不能为NULL
         * @param clazz 目标实体 不能为NULL
         * @param <T> 泛型
         * @return 转换后的结果
         */
        @NonNull
        public static <T> T convert(@NonNull Object source, @NonNull Class<T> clazz) {
            return  dozerMapper.map(source, clazz);
        }
    
        /**
         * 转换List实体为另一个指定的实体
         * source如果为NULL 会使用空集合
         * 在目标实体为NULL时 会抛出NPE
         * {@link }//com.github.dozermapper.core.util.MappingValidator#validateMappingRequest
         *
         * @param source 源集合 可以为NULL
         * @param clazz 目标实体 不能为NULL
         * @param <T> 泛型
         * @return 转换后的结果
         */
        @Nullable
        public static <T> List<T> convert(@Nullable List<?> source, @NonNull Class<T> clazz) {
            return Optional.ofNullable(source)
                    .orElse(Collections.emptyList())
                    .stream()
                    .map(bean -> dozerMapper.map(bean, clazz))
                    .collect(Collectors.toList());
        }
    }
    
  • 相关阅读:
    python——socket,IO多路复用(select),socket server实现多并发
    python——多线程,多进程,协程
    python——装饰器,迭代器,生成器
    time模块,datetime模块
    re模块,paramiko模块
    Freemaker中使用中括号来包含标签
    Freemaker中使用中括号来包含标签
    freemarker Velocity获取request,session
    freemarker Velocity获取request,session
    freemarker Velocity获取request,session
  • 原文地址:https://www.cnblogs.com/Hello-TomCat/p/14665496.html
Copyright © 2011-2022 走看看