Mybatis 分页查询时的总数优化,采用一次查询方式

---------------------------------------------------------------------------------------
1. mysql的SQL_CALC_FOUND_ROWS
2. mybatis的@ResultMap
两个关键点
---------------------------------------------------------------------------------------

/**
 * 数据库
 * @param schema
 * @return
 */
@Results(id = "_schema", value = {@Result(column = "table_schema", property = "table_schema", jdbcType = JdbcType.VARCHAR),
        @Result(column = "table_name", property = "table_name", jdbcType = JdbcType.VARCHAR),
        @Result(column = "table_rows", property = "table_rows", jdbcType = JdbcType.INTEGER)})
@Select({"SELECT t1.* FROM information_schema.tables t1 WHERE t1.table_schema = #{schema}"})
Map schemaForTables(@Param("schema") String schema);

******上面红色粗体部分很重要,返回类型决定了mybatis如何取封装@Results******

/**
 * 数量
 * @return
 */
@Results(id = "_tables", value = {@Result(column = "total", property = "total", jdbcType = JdbcType.INTEGER)})
@Select({"SELECT COUNT(*) AS total FROM information_schema.tables"})
Integer countAll();

/**
 * 获取分页指定数量的数据表
 *
 * @param page
 * @return
 */
@Select({"select SQL_CALC_FOUND_ROWS t1.* from information_schema.tables t1 where t1.table_schema=#{params}",
        " limit ${(page-1) * limit},${limit};"
        ,"SELECT FOUND_ROWS() total;"
        })
@ResultMap({"_schema", "_tables"})
public List> querySchemaTables(PageBean,String> page);

你可能感兴趣的:(mybatis)