关于mybatis+sqlserver 的多表关联的分页(注意要先分页在多表关联)

Posted ForYouForMe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于mybatis+sqlserver 的多表关联的分页(注意要先分页在多表关联)相关的知识,希望对你有一定的参考价值。

 在这里浅谈下mybatis+sqlserver 遇到的小坑,当我们在mapper.xml 中写sql 语句 取参的时候注意不能用#{}去取值
top 关键字是不支持的 然后会报java.sql.SQLException: ‘@P0‘ 附近有语法错误 只能用${}去取值
SELECT * FROM(
    SELECT TOP ${pageSize}* FROM st_student s
    WHERE s.stu_id NOT IN  (
    SELECT TOP ${pageId} s.stu_id FROM st_student s ORDER BY s.stu_id ASC
    )ORDER BY s.stu_id ASC
    )as a
    left join st_stucourse sc
    on a.stu_id=sc.stu_id
    left join st_course c
    on sc.co_id=c.co_id 


--
准备数据 -- 创建数据库 create database EASTETWO; use EASTETWO go create table st_student( stu_id int not null primary key IDENTITY(1,1), stu_name varchar(30), stu_s int not null, stu_birth datetime, stu_birthplace varchar(30), stu_email varchar(20) ) go select * from st_student; go set identity_insert st_student ON; go insert into st_student (stu_id,stu_name,stu_s,stu_birth,stu_birthplace,stu_email) values(1,小张,1,2016-12-28,上海,[email protected]); insert into st_student (stu_id,stu_name,stu_s,stu_birth,stu_birthplace,stu_email) values(2,小华,1,2016-12-27,上海,[email protected]); insert into st_student (stu_id,stu_name,stu_s,stu_birth,stu_birthplace,stu_email) values(3,小明,1,2016-12-26,上海,[email protected]); insert into st_student (stu_id,stu_name,stu_s,stu_birth,stu_birthplace,stu_email) values(4,小文,1,2016-11-28,上海,[email protected]); insert into st_student (stu_id,stu_name,stu_s,stu_birth,stu_birthplace,stu_email) values(5,小郭,1,2016-11-26,上海,[email protected]); insert into st_student (stu_id,stu_name,stu_s,stu_birth,stu_birthplace,stu_email) values(6,小刘,1,2016-10-28,上海,[email protected]); insert into st_student (stu_id,stu_name,stu_s,stu_birth,stu_birthplace,stu_email) values(7,小雯,1,2016-10-27,上海,[email protected]); go set identity_insert st_student off; go --创建课程表 create table st_course( co_id int not null primary key IDENTITY(1,1), co_name varchar(30) ) go use EASTETWO; select * from st_student; create table st_stucourse( id int not null primary key IDENTITY(1,1), stu_id int not null, co_id int not null, co_score int not null ) go -- 在这里注意当插入数据的时候 一定要将 identity_insert 开启 set identity_insert st_stucourse ON go insert into st_stucourse (id,stu_id,co_id,co_score) values(10,1,1,90); insert into st_stucourse (id,stu_id,co_id,co_score) values(11,1,2,70); insert into st_stucourse (id,stu_id,co_id,co_score) values(12,1,3,88); insert into st_stucourse (id,stu_id,co_id,co_score) values(13,1,4,88); insert into st_stucourse (id,stu_id,co_id,co_score) values(14,1,5,98); insert into st_stucourse (id,stu_id,co_id,co_score) values(6,2,2,88); insert into st_stucourse (id,stu_id,co_id,co_score) values(7,2,3,90); insert into st_stucourse (id,stu_id,co_id,co_score) values(8,2,4,99); insert into st_stucourse (id,stu_id,co_id,co_score) values(9,2,6,88); -- 执行完一定要关闭 否则在另一张表会报错 set identity_insert st_stucourse OFF go select * from st_stucourse; set identity_insert st_course ON go insert into st_course (co_id,co_name) values(1,历史); go insert into st_course (co_id,co_name) values(2,数学); insert into st_course (co_id,co_name) values(3,语文); insert into st_course (co_id,co_name) values(4,英语); insert into st_course (co_id,co_name) values(5,地理); insert into st_course (co_id,co_name) values(6,政治); go set identity_insert st_course OFF go go -- 在实际开发中最好不要用* SELECT * FROM( SELECT TOP 10* FROM st_student s WHERE s.stu_id NOT IN ( SELECT TOP 0 s.stu_id FROM st_student s ORDER BY s.stu_id DESC ) ORDER BY s.stu_id DESC )as a left join st_stucourse sc on a.stu_id=sc.stu_id left join st_course c on sc.co_id=c.co_id go -- 遇到的坑就是先分页还是先关联 如果你先关联表(如果有一 对异常多的) 那么你在前台页面会遇到 -- 个问题就是在显示第一页的数据时会出现显示的记录数会少于我们的要求显示的记录数 go SELECT TOP 10 * FROM st_student s, st_stucourse sc, st_course c where s.stu_id=sc.stu_id and sc.co_id=c.co_id and s.stu_id NOT IN ( SELECT TOP 0 s.stu_id FROM st_student s ORDER BY s.stu_id DESC ) ORDER BY s.stu_id DESC go -- row_number() 会效率更高些 use EASTETWO; select * from ( select top 10 * from ( select row_number() over(order by s.stu_id DESC) as rownumber,* from st_student s ) A where rownumber > 0 )as a left join st_stucourse sc on a.stu_id=sc.stu_id left join st_course c on sc.co_id=c.co_id

 















以上是关于关于mybatis+sqlserver 的多表关联的分页(注意要先分页在多表关联)的主要内容,如果未能解决你的问题,请参考以下文章

mybatis中的多表查询

MyBatis的多表联合查询

MyBatis关于多表联查 关联关系之一--------一对多(单条sql语句查询)

mybatis怎么样使用mapper3实现多表关联查询

oracle多表关联update

MyBatis使用MyBatis的分页组件PageHelper时,多表关联下使用别名查询时,前台传参过来,根据参数排序的解决方案