JPA 手写分页方法2种数据接收方式(实体类接收数据,Map接收数据)

2023-05-16

1 实体类接收数据

package com.dao;
import org.hibernate.query.internal.NativeQueryImpl;
import org.hibernate.transform.Transformers;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Repository
public class BaseDao {

    @PersistenceContext
    private EntityManager entityManager;

    /**
     * 分页方法(通用)
     *
     * @param thisClass xx类.class
     * @param selectSql select语句(不能带参数,paramType=1时参数格式为?,paramType=2时参数格式为:xxx
     * @param otherSql
     * @param params
     * @param paramType
     * @param pageNo
     * @param pageSize
     * @param <T>
     * @return
     */
    private <T> Page<T> getListByPage(Class thisClass, String selectSql, String otherSql, Object params, int paramType, int pageNo, int pageSize) {
        String countSelectSql = "select count(*) ";
        //获取数量
        String countSql = countSelectSql + " " + otherSql;
        Query countQuery = null;
        if (pageNo > 0 && pageSize > 0) {
            countQuery = entityManager.createNativeQuery(countSql);
        }

        int count = 0;
        if (countQuery != null) {//分页
            //填充参数
            setParameter(params,paramType,countQuery)
            Object result = countQuery.getSingleResult();
            if (null != result && !"".equals(result.toString())) {
                count = Integer.parseInt(result.toString());
            }
        }

        //获取数据
        String sql = selectSql + " " + otherSql;
        Query query = this.entityManager.createNativeQuery(sql);
        //填充参数
        setParameter(params,paramType,query);
        if (countQuery != null) {//分页
            query.setFirstResult((pageNo - 1) * pageSize);
            query.setMaxResults(pageSize);
        }
        query.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.aliasToBean(thisClass));
        List<T> list = query.getResultList();
        if (countQuery != null) {//分页
            Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
            return new PageImpl<T>(list, pageable, count);
        }
        return new PageImpl<T>(list);
    }
}

2 Map接收数据

用于需要获取动态表的数据,但是动态表没有对应的实体类的情况。

package com.dao;
import org.hibernate.query.internal.NativeQueryImpl;
import org.hibernate.transform.Transformers;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Repository
public class BaseDao {

    @PersistenceContext
    private EntityManager entityManager;

    /**
     * 分页方法(Map接收数据)
     *
     * @param selectSql select语句(不能带参数,paramType=1时参数格式为?,paramType=2时参数格式为:xxx
     * @param otherSql
     * @param params
     * @param paramType
     * @param pageNo
     * @param pageSize
     * @return
     */
    private Page<Map<String, Object>> getListByMapPage(String selectSql, String otherSql, Object params, int paramType, int pageNo, int pageSize) {
        String countSelectSql = "select count(*) ";
        //获取数量
        String countSql = countSelectSql + " " + otherSql;
        Query countQuery = null;
        if (pageNo > 0 && pageSize > 0) {
            countQuery = entityManager.createNativeQuery(countSql);
        }

        int count = 0;
        if (countQuery != null) {//分页
            //填充参数
            setParameter(params,paramType,countQuery)
            Object result = countQuery.getSingleResult();
            if (null != result && !"".equals(result.toString())) {
                count = Integer.parseInt(result.toString());
            }
        }

        //获取数据
        String sql = selectSql + " " + otherSql;
        Query query = this.entityManager.createNativeQuery(sql);
        //填充参数
        setParameter(params,paramType,query);
        if (countQuery != null) {//分页
            query.setFirstResult((pageNo - 1) * pageSize);
            query.setMaxResults(pageSize);
        }
        //设置接收数据类型为Map
        query.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List<Map<String, Object>> list = query.getResultList();
        if (countQuery != null) {//分页
            Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
            return new PageImpl<Map<String, Object>>(list, pageable, count);
        }
        return new PageImpl<Map<String, Object>>(list);
    }
}

注:setParameter()的源码请查看以下博客。

JPA 实现两种参数注入方式(?和:xxx)

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JPA 手写分页方法2种数据接收方式(实体类接收数据,Map接收数据) 的相关文章

随机推荐