spring boot 2.0

hibernate 5.2


2018/06/27 - [프로그램 자료/Java & Spring] - [hibernate] create query pageable



이것을 사용하도록 한다

2018/07/24 - [프로그램 자료/Java & Spring] - [hibernate] 프로시저 조회 call procedure List to List>




EntityManager를 사용하지 않고 native query를 사용했더니 추후 사용시 Object[]의 순서로만 접근이 가능하고,

Map<Key, Value> 형식을 지원하지 않는 거 같더라.


가져다가 쓸 때 꽤나 불편한 느낌이라 한 번 변경시켰다.


사용법은 다음과 같다. 

new CustomPage(page, "colname1", "colname2");



import java.io.Serializable;

import java.lang.reflect.Field;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

import org.springframework.data.domain.Page;

 

import lombok.Getter;

import lombok.extern.slf4j.Slf4j;

 

@Slf4j

@Getter

public class CustomPage implements Serializable {

 

    private static final long serialVersionUID = 1L;

 

    private List<Map<String, Object>> content;

    private int page;

    private int pageSize;

    private int totalPages;

    private int begin;

    private int end;

    private long totalElements;

    private boolean first;

    private boolean last;

 

    /**

     * Page<Object[]> 반환시 Contents에 대해 Map 방식으로 변환하여 넘기는 클래스

     *

     * @param pageObject

     *            : Page<Object[]> 형식만 지원

     * @param columnNameArgs

     *            : "colName1", "colName2"

     */

    public CustomPage(Page<Object[]> pageObject, String... columnNameArgs) {

        this.page = pageObject.getNumber() + 1;

        this.pageSize = pageObject.getSize();

        this.totalPages = pageObject.getTotalPages();

        this.totalElements = pageObject.getTotalElements();

        this.first = pageObject.isFirst();

        this.last = pageObject.isLast();

        this.content = ConvertContents(pageObject.getContent(), columnNameArgs);

        this.begin = Math.max(1, pageObject.getNumber() - 4);

        this.end = Math.min(pageObject.getNumber() + 5,

                pageObject.getTotalPages() == 0 ? 1 : pageObject.getTotalPages());

    }

 

    /**

     * Page<T> 반환시 Contents에 대해 Map 방식으로 변환하여 넘기는 클래스

     *

     * @param pageObject

     *            : Page<Object[]> 형식만 지원

     * @param columnNameArgs

     *            : "colName1", "colName2"

     */

    public CustomPage(Page<?> pageObject) throws InstantiationException, IllegalAccessException {

        this.page = pageObject.getNumber() + 1;

        this.pageSize = pageObject.getSize();

        this.totalPages = pageObject.getTotalPages();

        this.totalElements = pageObject.getTotalElements();

        this.first = pageObject.isFirst();

        this.last = pageObject.isLast();

        this.content = ConvertContents(pageObject.getContent());

        this.begin = Math.max(1, pageObject.getNumber() - 4);

        this.end = Math.min(pageObject.getNumber() + 5,

                pageObject.getTotalPages() == 0 ? 1 : pageObject.getTotalPages());

    }

 

    private final static String[] checkFieldNames = { "password", "serialVersionUID", };

 

    public static List<Map<String, Object>> ConvertContents(List<?> contents)

            throws InstantiationException, IllegalAccessException {

        List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();

 

        for (Object item : contents) {

            Map<String, Object> cols = new HashMap<String, Object>();

 

            Field[] allFields = item.getClass().getDeclaredFields();

            for (Field field : allFields) {

                if(!Arrays.stream(checkFieldNames).anyMatch(x -> field.getName().toLowerCase().contains(x.toLowerCase()))) {

                    field.setAccessible(true); // Additional line

                    log.debug("name : {}", field.getName());

                    log.debug("value : {}", field.get(item));

                    cols.put(field.getName(), field.get(item));

                }

            }

            rows.add(cols);

        }

        return rows;

    }

 

    public static List<Map<String, Object>> ConvertContents(List<Object[]> contents, String... columnNameArgs) {

        List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();

 

        for (Object[] obj : contents) {

            Map<String, Object> row = new HashMap<String, Object>();

            for (int i = 0; i < columnNameArgs.length; i++) {

                row.put(columnNameArgs[i], obj[i]);

            }

            rows.add(row);

        }

        return rows;

    }

 

}

 


Posted by motolies
,