会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132482个问题
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 29911楼

启动common_item错误代码如下,

TxManager启动正常,

防火墙已关,

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.bjsxt.item.CommonItemApplication]; nested exception is java.io.FileNotFoundException: class path resource [com/codingapi/txlcn/txmsg/MessageConfiguration.class] cannot be opened because it does not exist
 at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:596) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:302) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:242) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:199) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:167) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:315) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:705) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
 at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
 at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:311) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
 at com.bjsxt.item.CommonItemApplication.main(CommonItemApplication.java:18) ~[classes/:na]
Caused by: java.io.FileNotFoundException: class path resource [com/codingapi/txlcn/txmsg/MessageConfiguration.class] cannot be opened because it does not exist
 at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180) ~[spring-core-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:51) ~[spring-core-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:103) ~[spring-core-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.createMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:86) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
 at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.getMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:73) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
 at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:81) ~[spring-core-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.context.annotation.ConfigurationClassParser.asSourceClass(ConfigurationClassParser.java:682) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.context.annotation.ConfigurationClassParser.asSourceClasses(ConfigurationClassParser.java:661) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:567) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 ... 17 common frames omitted
image.pngimage.png

JAVA 全系列/(旧的隐藏)第二十一阶段:百战商城项目(Spring Cloud最新架构)/百战商城项目 29912楼
JAVA 全系列/第九阶段:权限控制与安全认证/Shiro(旧) 29913楼
JAVA 全系列/第一阶段:JAVA 快速入门/IDEA的使用和第一个java项目 29917楼
Python 全系列/第一阶段:Python入门/控制语句 29919楼

看到这位同学的提问:

image.png

参考帖子:https://www.cnblogs.com/xiufengd/p/4739402.html

结合上次课top-n的查询,与这次课分页查询做对比,可以发现top-n的rownum使用的是主查询当中的,而分页时使用的是子查询的rownum,有什么区别呢?

先说上次课的top-n: 查挣钱最多的3个人的姓名,工资。

select rownum,last_name,salary

from (select last_name,salary from employees order by salary desc)

where rownum<=3;

执行:

image.png


这里注意:如果我们非得使用子查询的rownum的话,语句是这样的。

select em.r,em,last_name,em.salary

from (select rownum r,last_name,salary from employees order by salary desc) em

where em.r<=3;

执行结果:

image.png

为什么结果会不一样呢? 因为rownum的值是在order by之前被赋上的,所以order by不会影响rownum的值,rownum是记录一条一条读取到内存时候被赋的值,从1开始。并且对于同一个表,每条记录返回的rownum也可能不同。

这里可以看到rownum是原表中的rownum的值:

image.png

所以只有使用主查询的rownum,才是已经排完序的rownum,也就是内层order by生效后的数据的rownum。

这也是为什么top-n使用的是主查询当中的rownum的原因。


那么分页查询,为什么不用主查询当中的rownum呢?

对于视频中的示例:查询雇员表中数据,每次返回10条。

这时候如果我们依旧像top-n一样使用主查询当中的rownum:当然也没有排序的影响,是可以查出来前10条的。

select rownum,em.* from (select * from employees)em
where rownum>=1 and rownum<=10;

image.png

但是,你会发现,这种写法,当你查11-20条的时候。居然一条数据也没有。

select rownum,em.* from (select * from employees)em
where rownum>=11 and rownum<=20;

image.png

为什么呢?是因为rownum的特殊性,

参考帖子:https://www.cnblogs.com/antis/p/6740777.html

关于Oracle 的 rownum 问题,很多资料都说不支持SQL语句中的“>、>=、=、between...and”运算符,只能用如下运算符号“<、<=、!=”,为什么?

对于下面的SQL语句

   SQL>select rownum,id,age,name from loaddata where rownum > 2;

    ROWNUM ID     AGE NAME
    ------- ------ --- ------

    rownum>2,没有查询到任何记录。

    因为rownum总是从1开始的,第一条不满足去掉的话,第二条的rownum 又成了1。依此类推,所以永远没有满足条件的记录。

    可以这样理解:rownum是一个序列,是Oracle数据库从数据文件或缓冲区中读取数据的顺序。

    它取得第一条记录则rownum值为1,第二条为2。依次类推。

    当使用“>、>=、=、between...and”这些条件时,从缓冲区或数据文件中得到的第一条记录的rownum为1,不符合sql语句的条件,会被删除,接着取下条。

    下条的rownum还会是1,又被删除,依次类推,便没有了数据。

 这就是为什么当我们对主查询的rownum进行>=判断的时候,出不来数据,实际上:

select rownum,em.* from (select * from employees)em
where rownum>=1 and rownum<=10;

这样查前10条,与top-n的写法是一样的,效果也是一样的。

top-n:查前10条:select rownum,em.* from (select * from employees)em
where rownum<=10;


那么怎么解决呢? 用子查询中的rownum,将rownum变成内建视图当中的一个列,再使用主查询来对内建视图当中存在的列进行判断,这样的话,内建视图当中的rownum就不是主查询当中的伪列了,自然也就不存在不能使用>,>=.between and 等运算符的问题了。

这就是为什么我们在分页的时候要使用子查询当中的rownum,就是为了可以让主查询能够使用>=、>、between and符号。


再回到同学的问题:

目的是利用rownum进行排序分页,查薪资排名4-5的雇员。

使用:select e.rm,e.last_name, e.salary 

from (select rownum rm, e.last_name,e.salary from employees e order by e.salary desc) e 

where e.rm>3 and e.rm<6;

的话,子查询有order by了,子查询当中的rownum并不是真正排好序的序号,所以外层主查询查的时候,也不能查到真正的信息。查不到。

使用:select rownum,e.last_name, e.salary 

from (


JAVA 全系列/第三阶段:数据库编程/SQL 语言 29920楼
Python 全系列/第二阶段:Python 深入与提高/模块 29923楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637