用Mybatis框架执行的结果和自己执行的结果不一致。
用Mybatis框架执行的结果和自己执行的结果不一致。
下面是Mybatis的配置
<select id="selectNoteByPage" parameterType="com.bean.Page" resultMap="NoteResultMap">
(select *
from (select * from bbs.bbsnote order by responsedate desc)
where rownum <![CDATA[<=]]> #{maxnum})
minus
(select *
from (select * from bbs.bbsnote order by responsedate desc)
where rownum <![CDATA[<=]]> #{minnum})
</select>
执行结果:
DEBUG [http-bio-8888-exec-1] - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4e69f6b0]
DEBUG [http-bio-8888-exec-1] - JDBC Connection [oracle.jdbc.driver.T4CConnection@19de40b4] will be managed by Spring
DEBUG [http-bio-8888-exec-1] - ==> Preparing: (select * from (select * from bbs.bbsnote order by responsedate desc) where rownum <= ?) minus (select * from (select * from bbs.bbsnote order by responsedate desc) where rownum <= ?)
DEBUG [http-bio-8888-exec-1] - ==> Parameters: 15(Integer), 10(Integer)
DEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=?
DEBUG [http-bio-8888-exec-1] - ====> Parameters: 51(Long)
DEBUG [http-bio-8888-exec-1] - <==== Total: 22
DEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=?
DEBUG [http-bio-8888-exec-1] - ====> Parameters: 71(Long)
DEBUG [http-bio-8888-exec-1] - <==== Total: 1
DEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=?
DEBUG [http-bio-8888-exec-1] - ====> Parameters: 91(Long)
DEBUG [http-bio-8888-exec-1] - <==== Total: 2
DEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=?
DEBUG [http-bio-8888-exec-1] - ====> Parameters: 223(Long)
DEBUG [http-bio-8888-exec-1] - <==== Total: 25
DEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=?
DEBUG [http-bio-8888-exec-1] - ====> Parameters: 303(Long)
DEBUG [http-bio-8888-exec-1] - <==== Total: 1
DEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=?
DEBUG [http-bio-8888-exec-1] - ====> Parameters: 338(Long)
DEBUG [http-bio-8888-exec-1] - <==== Total: 5
DEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=?
DEBUG [http-bio-8888-exec-1] - ====> Parameters: 351(Long)
DEBUG [http-bio-8888-exec-1] - <==== Total: 12
DEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=?
DEBUG [http-bio-8888-exec-1] - ====> Parameters: 353(Long)
DEBUG [http-bio-8888-exec-1] - <==== Total: 0
DEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=?
DEBUG [http-bio-8888-exec-1] - ====> Parameters: 354(Long)
DEBUG [http-bio-8888-exec-1] - <==== Total: 1
DEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=?
DEBUG [http-bio-8888-exec-1] - ====> Parameters: 355(Long)
DEBUG [http-bio-8888-exec-1] - <==== Total: 2
DEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=?
DEBUG [http-bio-8888-exec-1] - ====> Parameters: 484(Long)
DEBUG [http-bio-8888-exec-1] - <==== Total: 0
DEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=?
DEBUG [http-bio-8888-exec-1] - ====> Parameters: 522(Long)
DEBUG [http-bio-8888-exec-1] - <==== Total: 0
DEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=?
DEBUG [http-bio-8888-exec-1] - ====> Parameters: 540(Long)
DEBUG [http-bio-8888-exec-1] - <==== Total: 0
DEBUG [http-bio-8888-exec-1] - <== Total: 13
上面得到的结果不是我想要的,居然有13条数据,搞不懂这时为什么,但是我自己来执行sql,结果却是正确的,我真的找不出问题点了:
(select *
from (select * from bbs.bbsnote order by responsedate desc)
where rownum <=15)
minus
(select *
from (select * from bbs.bbsnote order by responsedate desc)
where rownum <=10);

这里执行结果是正确的,5条数据。
下面是Mybatis的配置
<select id="selectNoteByPage" parameterType="com.bean.Page" resultMap="NoteResultMap">
(select *
from (select * from bbs.bbsnote order by responsedate desc)
where rownum <![CDATA[<=]]> #{maxnum})
minus
(select *
from (select * from bbs.bbsnote order by responsedate desc)
where rownum <![CDATA[<=]]> #{minnum})
</select>
执行结果:
DEBUG [http-bio-8888-exec-1] - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4e69f6b0]
DEBUG [http-bio-8888-exec-1] - JDBC Connection [oracle.jdbc.driver.T4CConnection@19de40b4] will be managed by Spring
DEBUG [http-bio-8888-exec-1] - ==> Preparing: (select * from (select * from bbs.bbsnote order by responsedate desc) where rownum <= ?) minus (select * from (select * from bbs.bbsnote order by responsedate desc) where rownum <= ?)
DEBUG [http-bio-8888-exec-1] - ==> Parameters: 15(Integer), 10(Integer)
DEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=?
DEBUG [http-bio-8888-exec-1] - ====> Parameters: 51(Long)
DEBUG [http-bio-8888-exec-1] - <==== Total: 22
DEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=?
DEBUG [http-bio-8888-exec-1] - ====> Parameters: 71(Long)
DEBUG [http-bio-8888-exec-1] - <==== Total: 1
DEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=?
DEBUG [http-bio-8888-exec-1] - ====> Parameters: 91(Long)
DEBUG [http-bio-8888-exec-1] - <==== Total: 2
DEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=?
DEBUG [http-bio-8888-exec-1] - ====> Parameters: 223(Long)
DEBUG [http-bio-8888-exec-1] - <==== Total: 25
DEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=?
DEBUG [http-bio-8888-exec-1] - ====> Parameters: 303(Long)
DEBUG [http-bio-8888-exec-1] - <==== Total: 1
DEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=?
DEBUG [http-bio-8888-exec-1] - ====> Parameters: 338(Long)
DEBUG [http-bio-8888-exec-1] - <==== Total: 5
DEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=?
DEBUG [http-bio-8888-exec-1] - ====> Parameters: 351(Long)
DEBUG [http-bio-8888-exec-1] - <==== Total: 12
DEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=?
DEBUG [http-bio-8888-exec-1] - ====> Parameters: 353(Long)
DEBUG [http-bio-8888-exec-1] - <==== Total: 0
DEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=?
DEBUG [http-bio-8888-exec-1] - ====> Parameters: 354(Long)
DEBUG [http-bio-8888-exec-1] - <==== Total: 1
DEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=?
DEBUG [http-bio-8888-exec-1] - ====> Parameters: 355(Long)
DEBUG [http-bio-8888-exec-1] - <==== Total: 2
DEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=?
DEBUG [http-bio-8888-exec-1] - ====> Parameters: 484(Long)
DEBUG [http-bio-8888-exec-1] - <==== Total: 0
DEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=?
DEBUG [http-bio-8888-exec-1] - ====> Parameters: 522(Long)
DEBUG [http-bio-8888-exec-1] - <==== Total: 0
DEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=?
DEBUG [http-bio-8888-exec-1] - ====> Parameters: 540(Long)
DEBUG [http-bio-8888-exec-1] - <==== Total: 0
DEBUG [http-bio-8888-exec-1] - <== Total: 13
上面得到的结果不是我想要的,居然有13条数据,搞不懂这时为什么,但是我自己来执行sql,结果却是正确的,我真的找不出问题点了:
(select *
from (select * from bbs.bbsnote order by responsedate desc)
where rownum <=15)
minus
(select *
from (select * from bbs.bbsnote order by responsedate desc)
where rownum <=10);

这里执行结果是正确的,5条数据。
...全文
相关推荐
解决mybatis执行SQL语句部分参数返回NULL问题
使用resultType时,MyBatis会自动将查询结果映射到Bean对象上,但是如果Bean对象的属性名和数据库表的列名不一致时,可能会导致部分属性为空。
ORM框架之Mybatis
- 注意缓存管理,防止数据不一致。- 使用事务管理,保证数据的一致性。
hibernate和mybatis对比分析
Hibernate提供了较为完整的封装对数据库结构,实现了POJO和数据库表之间的映射,以及SQL的自动生成和执行。
MyBatis的27道面试题
MyBatis将SQL执行结果封装为目标对象并返回,支持多种映射方式,例如使用resultMap进行复杂映射。执行批量插入时,可以使用foreach标签来实现。
【MyBatis源码全面解析】MyBatis一二级缓存介绍
但是,需要合理地配置缓存机制,避免缓存溢出和数据不一致的问题。


基础和管理
