IF OBJECT_ID ('dbo.QueryPageList') IS NOT NULL
DROP PROCEDURE dbo.QueryPageList
GO
create procedure dbo.QueryPageList
(
@sqlStr varchar(8000), --sql语句 select id,name from t1
@start int, -- 从那行开始(偏移量)
@limit int -- 每页数量大小
)
as
DECLARE @dt varchar(10)
BEGIN
-- rand() 生成随机数字转换成字符,从3位开始截取,最后保留10位长度的字符
SELECT @dt= substring(convert(varchar, rand()), 3, 10)
-- 在select语句中增加行号
-- 最终效果:select rownum=identity(12),id,name from t1
SELECT @sqlStr = stuff(@sqlStr, 1, 7, 'select rownum=identity(12), ')
-- charindex() 查询 FROM 位置做截取
-- 最终效果:select rownum=identity(12),id,name into tempdb..Lining1234 from t1
-- 特别注意:大数量查询时,一定要给查询条件!!!因为,数据量大时,会直接影响到数据 into 插入临时表中的性能
SELECT @sqlStr = stuff(@sqlStr, charindex(' FROM ', upper(@sqlStr)), 6 ,' into tempdb..Lining' + @dt + ' from ')
commit
execute (@sqlStr)
-- 根据行号在临时表里面取数据
select @sqlStr = 'select * from tempdb..Lining' + @dt + ' where rownum > '+convert(varchar, @start)+' and rownum <= '+convert(varchar, (@start+@limit))
execute (@sqlStr)
-- 删除临时表,释放查询资源
SELECT @sqlStr = 'DROP TABLE tempdb..Lining'+@dt
EXECUTE (@sqlStr)
END
GO
package com.yandype.test;
import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import com.cong.core.jdbc.ConnManager;
public class JdbcRowCountSybase {
public static void main(String[] args) {
ConnManager connManager = new ConnManager();
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
/**
* 特别注意:大数量查询时,一定要给查询条件!!!
* 因为,数据量大时,会直接影响到数据 into 插入临时表中的性能
*/
try {
StringBuffer sb = new StringBuffer("select id,goods_name,goods_price,create_time from tb_test_goods");
sb.append(" where ").append(" goods_price = ").append(new BigDecimal("1.0000"));
con = connManager.getConn(1);
CallableStatement prepareCall = con.prepareCall("{call QueryPageList(?,?,?)}");
prepareCall.setString(1, sb.toString());
prepareCall.setInt(2, 0);
prepareCall.setInt(3, 10);
rs = prepareCall.executeQuery();
while(rs.next()){
System.out.println(rs.getString("id"));
System.out.println(rs.getString("goods_name"));
System.out.println(rs.getString("goods_price"));
// 注意:如果是按下标取值。要从2开始,因为在存储过程分页里面表列字段多加了个rownum行号
System.out.println(rs.getString(1)); // 行号
System.out.println(rs.getString(2)); // id
System.out.println(rs.getString(3)); // goods_name
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
connManager.closeConn(rs, stmt, con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
插件包提取地址:
链接: https://pan.baidu.com/s/1mUx4rdXItOAaIEGs2rf3HQ 提取码: vkaf
下载插件包,解压后。直接拷贝到你项目工程即可:
编写配置类:
@Configuration
@MapperScan("com.yandype.mapper")
public class MybatisPlusConfig {
// Sybase分页插件 (按存储过程分页)
@Bean
public SybasePaginationInterceptor sybasePaginationInterceptor(){
return new SybasePaginationInterceptor(new SybaseDialect());
}
}
测试:
@Test
public void testPage(){
Page page = new Page<>(1,5);
QueryWrapper queryWrapper = new QueryWrapper<>();
//queryWrapper.eq("goods_price",new BigDecimal("1.0000"));
//queryWrapper.eq("goods_name","商品_1");
Page goodsPage = this.testGoodsMapper.selectPage(page,queryWrapper);
System.out.println(goodsPage.getTotal());
List goodsList = goodsPage.getRecords();
for (TestGoods mideaTestGoods : goodsList) {
System.out.println(testGoods);
}
}
插件分页资料参考:
https://gitee.com/zycmgit/mybatis-pagehelp-sybase