【採坑】mybatis分頁同時獲取總記錄數,報錯java.sql.SQLException: Column 'count' not found.

我的xml代碼

<?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.homework.dao.ClassMapper">
    <resultMap id="classList" type="com.homework.entity.ClassEntity">
        <id property="id" column="id"/>
        <result column="class_title" property="title"/>
        <result column="tch_id" property="tchId"/>
        <result column="tch_name" property="tchName"/>
    </resultMap>
    <resultMap id="classCount" type="java.lang.Integer">
        <result column="count"/>
    </resultMap>
    <!-- 注意此處resultMap的順序 -->
    <!--<select id="getList" resultMap="classCount,classList">-->
    <select id="getList" resultMap="classList,classCount">
        SELECT SQL_CALC_FOUND_ROWS id,class_title,tch_id,tch_name FROM class WHERE class_title LIKE
        CONCAT('%',#{keyword},'%') LIMIT #{pagesize} OFFSET #{pagenum};
        SELECT FOUND_ROWS() AS count;
    </select>
</mapper>

由於要實現分頁查詢的同時獲取記錄條數,因此有兩個select語句,兩個語句有先後順序,因此<select>標籤中的resultMap屬性的順序也要注意,如果順序顛倒了(例如註釋掉的語句)就報了這個錯誤

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