mybatis一对多关联查询分页解决方案

1、sql:分页查询

先分页查询获取id,再根据这些id 查询数据

 select a.id, a.xm, a.xb, a.ksno, a.zkzh, a.bmxh, a.zjlx, a.zjno, a.xp, a.status, a.dizhi, a.xuexiao, a.banji, a.cscode, a.
    fenpei_cscode, a.fenzu_code, a.fenzu_num, a.fenzu_datetime, a.tijiandian_id, a.kaoshi_code, a.
    updatetime, a.bak1, a.bak2, a.csrq, a.mzdm, a.wyyzdm, a.kslbdm, a.zzmmdm, a.bylbdm, a.xxdm, a.hkszddm,a.print_status, a.print_datetime, a.zhiwen_status, a.zhiwen_datetime, 
		a.querenbiao_status, a.querenbiao_datetime, a.zhujian_status, a.zhujian_datetime, a.zhiwenqueren_way,b.zong_status,d.id zdyj_id,d.keyNum,d.valueStr
 from t_bmk a,tj_huizong b,t_bmk_zdyj c,t_zdyj d where a.id=b.ks_id and a.id=c.ks_id and c.zdyj_id=d.id and a.id in(
select
 id
 from
(
    select a.id,row_number() over
    (
	 order by ksno
    ) num
 from t_bmk a,tj_huizong b where a.id=b.ks_id and exists(select 1 from t_bmk_zdyj where ks_id=a.id)) as ta where ta.num >1 and ta.num<30);

sqlserver 存储过程


ALTER PROCEDURE [dbo].[tiaoxingma_getAllByPage]
(
  @txm_ksno AS varchar(50) ,
	@txm_zjno AS varchar(50) ,
	@txm_bmxh AS varchar(50) ,
	@txm_xm AS varchar(50) ,
	@txm_xuexiao AS varchar(200) ,
	@txm_xb AS varchar(1) ,
	@txm_printStatus AS varchar(10) ,
	@txm_qsbjbh AS varchar(50) ,
	@txm_zzbjbh AS varchar(50) ,
	@txm_qsbmxh AS varchar(50) ,
	@txm_zzbmxh AS varchar(50) ,
  @startnum AS int ,
  @endnum AS int 
	)
AS
BEGIN
declare @sql nvarchar(2000)	
set @sql='
   select a.id, a.xm, a.xb, a.ksno, a.zkzh, a.bmxh, a.zjlx, a.zjno, a.xp, a.status, a.dizhi, a.xuexiao, a.banji, a.cscode, a.
    fenpei_cscode, a.fenzu_code, a.fenzu_num, a.fenzu_datetime, a.tijiandian_id, a.kaoshi_code, a.
    updatetime, a.bak1, a.bak2, a.csrq, a.mzdm, a.wyyzdm, a.kslbdm, a.zzmmdm, a.bylbdm, a.xxdm, a.hkszddm,a.print_status, a.print_datetime, a.zhiwen_status, a.zhiwen_datetime, 
		a.querenbiao_status, a.querenbiao_datetime, a.zhujian_status, a.zhujian_datetime, a.zhiwenqueren_way,b.zong_status,d.id zdyj_id,d.keyNum,d.valueStr
 from t_bmk a,tj_huizong b,t_bmk_zdyj c,t_zdyj d where a.id=b.ks_id and a.id=c.ks_id and c.zdyj_id=d.id and a.id in(
select
 id
 from
(
    select a.id,row_number() over
    (
	 order by ksno
    ) num
 from t_bmk a,tj_huizong b where a.id=b.ks_id and exists(select 1 from t_bmk_zdyj where ks_id=a.id) ';
IF(@txm_ksno is not null and @txm_ksno!='')
	BEGIN
	set @sql=@sql+' and a.ksno like ''%'+@txm_ksno+'%'' '
	END
IF(@txm_zjno is not null and @txm_zjno!='')
	BEGIN
	set @sql=@sql+' and a.zjno like ''%'+@txm_zjno+'%'' '
	END
IF(@txm_bmxh is not null and @txm_bmxh!='')
	BEGIN
	set @sql=@sql+' and a.bmxh like ''%'+@txm_bmxh+'%'' '
	END
IF(@txm_xm is not null and @txm_xm!='')
	BEGIN
	set @sql=@sql+' and a.xm like ''%'+@txm_xm+'%'' '
	END
IF(@txm_xuexiao is not null and @txm_xuexiao!='')
	BEGIN
	set @sql=@sql+' and a.xuexiao like ''%'+@txm_xuexiao+'%'' '
	END
IF(@txm_xb is not null and @txm_xb!='')
	BEGIN
	set @sql=@sql+' and a.xb ='+@txm_xb
	END
IF(@txm_printStatus is not null and @txm_printStatus!='')
	BEGIN
	set @sql=@sql+' and a.print_status ='''+@txm_printStatus+''''
	END
IF(@txm_qsbjbh is not null and @txm_qsbjbh!='')
	BEGIN
	set @sql=@sql+' and a.banji >= '+@txm_qsbjbh
	END
IF(@txm_zzbjbh is not null and @txm_zzbjbh!='')
	BEGIN
	set @sql=@sql+' and a.banji <='+@txm_zzbjbh
	END
IF(@txm_qsbmxh is not null and @txm_qsbmxh!='')
	BEGIN
	set @sql=@sql+' and a.bmxh >= '''+@txm_qsbmxh+''''
	END
IF(@txm_zzbmxh is not null and @txm_zzbmxh!='')
	BEGIN
	set @sql=@sql+' and a.bmxh <= '''+@txm_zzbmxh+''''
	END
set @sql=@sql+') as ta where ta.num > '+CONVERT(varchar(50),@startnum) + ' and ta.num <='+CONVERT(varchar(50), @endnum) +')'
exec sp_executesql @sql
END


2、查询数据总数:
sql

  select count(1) 
 from t_bmk a,tj_huizong b where a.id=b.ks_id and exists(select 1 from t_bmk_zdyj where ks_id=a.id)

sqlserver存储过程:






ALTER PROCEDURE [dbo].[tiaoxingma_getAllByPage_count]
(
  @txm_ksno AS varchar(50) ,
	@txm_zjno AS varchar(50) ,
	@txm_bmxh AS varchar(50) ,
	@txm_xm AS varchar(50) ,
	@txm_xuexiao AS varchar(200) ,
	@txm_xb AS varchar(10) ,
	@txm_printStatus AS varchar(10) ,
	@txm_qsbjbh AS varchar(50) ,
	@txm_zzbjbh AS varchar(50) ,
	@txm_qsbmxh AS varchar(50) ,
	@txm_zzbmxh AS varchar(50) 
	)
AS
BEGIN
declare @sql nvarchar(2000)	
set @sql='
   select count(1) 
 from t_bmk a,tj_huizong b where a.id=b.ks_id and exists(select 1 from t_bmk_zdyj where ks_id=a.id) ';
IF(@txm_ksno is not null and @txm_ksno!='')
	BEGIN
	set @sql=@sql+' and a.ksno like ''%'+@txm_ksno+'%'' '
	END
IF(@txm_zjno is not null and @txm_zjno!='')
	BEGIN
	set @sql=@sql+' and a.zjno like ''%'+@txm_zjno+'%'' '
	END
IF(@txm_bmxh is not null and @txm_bmxh!='')
	BEGIN
	set @sql=@sql+' and a.bmxh like ''%'+@txm_bmxh+'%'' '
	END
IF(@txm_xm is not null and @txm_xm!='')
	BEGIN
	set @sql=@sql+' and a.xm like ''%'+@txm_xm+'%'' '
	END
IF(@txm_xuexiao is not null and @txm_xuexiao!='')
	BEGIN
	set @sql=@sql+' and a.xuexiao like ''%'+@txm_xuexiao+'%'' '
	END
IF(@txm_xb is not null and @txm_xb!='')
	BEGIN
	set @sql=@sql+' and a.xb ='+@txm_xb
	END
IF(@txm_printStatus is not null and @txm_printStatus!='')
	BEGIN
	set @sql=@sql+' and a.print_status ='+@txm_printStatus
	END
IF(@txm_qsbjbh is not null and @txm_qsbjbh!='')
	BEGIN
	set @sql=@sql+' and a.banji >= '+@txm_qsbjbh
	END
IF(@txm_zzbjbh is not null and @txm_zzbjbh!='')
	BEGIN
	set @sql=@sql+' and a.banji <='+@txm_zzbjbh
	END
IF(@txm_qsbmxh is not null and @txm_qsbmxh!='')
	BEGIN
	set @sql=@sql+' and a.bmxh >='''+@txm_qsbmxh+''''
	END
IF(@txm_zzbmxh is not null and @txm_zzbmxh!='')
	BEGIN
	set @sql=@sql+' and a.bmxh <= '''+@txm_zzbmxh+''''
	END
exec sp_executesql @sql
END




3、mybatis xml




    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
            
            
            
            
        
    
    
    
 


 

你可能感兴趣的:(java)