[Pit] Mybatis paginates and obtains the total number of records at the same time, reporting an error java.sql.SQLException: Column 'count' not found.

my xml code

<?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>

Since the number of records needs to be obtained while paging the query, there are two select statements, and the two statements have a sequence. Therefore, the order of the resultMap attribute in the <select> tag should also be paid attention to. If the order is reversed (such as commented out Statement) reported this error

Guess you like

Origin blog.csdn.net/weixin_41786574/article/details/104693813