分頁查詢數據類PagingData

分頁查詢數據類

public class PagingData<T> implements Serializable
{
    // 構造初始化大小
    public ModelTest() {
        list = new ArrayList<T>();
        total= 0;
    }

    public ModelTest(List<T> list,Integer total) {
        list = new ArrayList<T>();
        count = 0;
        this.list = list;
        this.total= total;
    }
    private List<T> rows; // 返回數據集合
    private Integer total;  //總條數
    public PagingData(List<T> rows, Integer total)
    {
        this.rows = new ArrayList();
        this.total = Integer.valueOf(0);
        this.rows = rows;
        this.total = total;
    }

    public PagingData()
    {
        rows = new ArrayList();
        total = Integer.valueOf(0);
    }

    public List getRows()
    {
        return rows;
    }

    public void setRows(List rows)
    {
        this.rows = rows;
    }

    public Integer getTotal()
    {
        return total;
    }

    public void setTotal(Integer total)
    {
        this.total = total;
    }

}

用法


@Service
@Transactional(readOnly = true)
public class BlockchainCoinRecordServiceImpl implements BlockchainCoinRecordService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BlockchainCoinRecordServiceImpl.class);

    @Autowired
    BlockchainCoinRecordMapper blockchainCoinRecordMapper;


    /**
     * BlockchainCoinRecord 爲實體類
     */
    @Override
    public PagingData<BlockchainCoinRecord> selectPage(BlockchainCoinRecord entity) {
        PagingData<BlockchainCoinRecord> pagingData = new PagingData<>();

        if (null == entity) {
            LOGGER.warn("select entity page, but entity is null...");
            return pagingData;
        }

        Integer queryCount = blockchainCoinRecordMapper.selectByIndexCount(entity);
        pagingData.setTotal(queryCount);

        if (null != queryCount && queryCount <= 0) {
            LOGGER.info("select entity page , but count {} == 0 ...", queryCount);
            return pagingData;
        }

        List<BlockchainCoinRecord> entitys = selectByIndex(entity);
        pagingData.setRows(entitys);
        return pagingData;
    }

    @Override
    public List<BlockchainCoinRecord> selectByIndex(BlockchainCoinRecord entity) {
        List<BlockchainCoinRecord> entitys = new ArrayList<>();
        if (entity == null) {
            LOGGER.warn("select entity by index, but entity is null ...");
            return entitys;
        }
        return blockchainCoinRecordMapper.selectByIndex(entity);
    }

}

mapper 映射

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hesvit.mapper.BlockchainCoinRecordMapper">
    <sql id="baseColumns">
        a.id, a.user_id, a.coin, a.type, a.remark, a.status,
        a.create_user_id, a.create_time, a.update_user_id, a.update_time
    </sql>

<sql id="whereClause">
        <trim prefix="WHERE" prefixOverrides="AND">
            <if test="id != null">
                AND ${columnPrefix}id = #{id}
            </if>
            <if test="userId != null">
                AND ${columnPrefix}user_id = #{userId}
            </if>
            <if test="coin != null">
                AND ${columnPrefix}coin = #{coin}
            </if>
            <if test="type != null">
                AND ${columnPrefix}type = #{type}
            </if>
            <if test="remark != null">
                AND ${columnPrefix}remark = #{remark}
            </if>
            <if test="status != null and status != -1">
                AND ${columnPrefix}status = #{status}
            </if>
            <if test="createUserId != null"> 
                AND ${columnPrefix}create_user_id = #{createUserId}
            </if>
            <if test="createTime != null">
                AND ${columnPrefix}create_time = #{createTime}
            </if>
            <if test="updateUserId != null">
                AND ${columnPrefix}update_user_id = #{updateUserId}
            </if>
            <if test="updateTime != null">
                AND ${columnPrefix}update_time = #{updateTime}
            </if>
            <if test="startDate != null">
                AND ${columnPrefix}create_time &gt; #{startDate}
            </if>
            <if test="endDate != null">
                AND ${columnPrefix}create_time &lt; #{endDate}
            </if>
        </trim>
    </sql>
<select id="selectByIndexCount" parameterType="BlockchainCoinRecord"
        resultType="int">
    SELECT count(1)
    FROM t_blockchain_coin_record a
    <include refid="whereClause">
        <property name="columnPrefix" value="a." />
    </include>
</select>

<select id="selectByIndex" parameterType="BlockchainCoinRecord"
        resultType="BlockchainCoinRecord">
    SELECT
    <include refid="baseColumns" />
    FROM t_blockchain_coin_record a
    <include refid="whereClause">
        <property name="columnPrefix" value="a." />
    </include>
    group by a.id
    ORDER BY a.id DESC
    <if test="limit != -1">
    LIMIT #{offset} , #{limit}
    </if>
</select>

</mapper>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章