关于Spring中MongoTemplate排序问题
来源:互联网 发布:python __ 编辑:程序博客网 时间:2024/05/17 22:51
这个问题已经困扰了几天了,每次查到的结果都是使用Query query = new Query();query.with(Sort……),但是我的文件里面就是找不到with这个方法,但有其他的方法。查了很久,终于查到了这个:点击打开链接 把源码写上了。也是用于的Spring MongoTemplate里面方法。对比了一下,发现是引用的包不一样,大概就这点差别。
我的包:
<span style="white-space:pre"></span>org.springframework.data.document.mongodb.MongoTemplate别人引用的是
<span style="white-space:pre"></span>org.springframework.data.mongodb.core.MongoTemplate后来又搜索发现这篇文档里面说的点击打开链接
注意:官方文档和案例配置都是旧版本的配置案例,spring-data-mongo从1.0.0.M1到1.0.0.M3的版本叫做Spring Data Document。1.0.0.M4开始更名为Spring Data MongoDB 1.0.0 M4,不过官网并没有特别说明,乍一看有点莫名其妙,尤其是MongoTemplate从org.springframework.data.document.mongod移动到org.springframework.data.mongodb.core,官网的HelloWorldExample却还是用org.springframework.data.document.mongodb做配置案例。多少会导致使用时的误导。
发现问题就是包的引用问题。然后就按照他上面的配置。添加pom.xml中的引用,我只改了这几项:
<!-- mongodb --> <dependency> <span style="white-space:pre"></span> <groupId>org.mongodb</groupId> <span style="white-space:pre"></span> <artifactId>mongo-java-driver</artifactId> <span style="white-space:pre"></span> <version>2.10.1</version> <span style="white-space:pre"></span> <type>jar</type> <span style="white-space:pre"></span> <scope>compile</scope> <span style="white-space:pre"></span></dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>1.2.1.RELEASE</version> <type>jar</type> <scope>compile</scope> </dependency>然后把之前的mongo配置的包取消BuildPath。
然后就开始改错了……
开始的时候只是改了spring-mongodb.xml里面的配置mongoTemplate的引入的包,出现了各种错误;
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'HOST_MONITOR_HOST_DaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.data.document.mongodb.MongoTemplate com.nari.dao.host.HOST_MONITOR_HOST_DaoImpl.mongoTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.data.document.mongodb.MongoTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
发现是mongoTemplate中的参数配置错误。
现在贴配置spring-mongodb.xml代码:修改后的
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:mongo="http://www.springframework.org/schema/data/mongo"xsi:schemaLocation=" http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <!-- 搜索需要注解的文档 --> <context:component-scan base-package="com.nari.dao,com.nari.dao.host" /> <!-- 引入mongodb属性文件 --> <mongo:mongo host="localhost" port="27017"/> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <!-- 两个constructor-arg为mongTemplate进行实例化的构造函数,如果不知道添加哪个参数,可点击MongoTemplate查看源代码 --> <constructor-arg name="mongo" ref="mongo"/> <constructor-arg name="databaseName" value="NARI_MONITOR_2015_07"/> </bean> <context:annotation-config /> </beans>
后来又删除了几个spring-data-common.jar包,如果包冲突则还是会提示构造参数错误。
然后就是排序的代码,就和其他贴吧上写的一样了。
@Overridepublic List<Person> sortByAge() {// TODO Auto-generated method stubQuery query = new Query();query.with(new Sort(new Order(Direction.DESC,"age")));List<Person> li = this.mongoTemplate.find(query, Person.class, "person");return li;}
还在继续学习中,积累的经验就是:
看错误日志,主要看caused by 这些,然后对应代码,查看源代码;
配置文件出错首先要检查构造参数是否与要求的一致;
查看拼写是否错误;
查看类型是否匹配。
接下来在继续学习MongoTemplate的其他用法了。
- 关于Spring中MongoTemplate排序问题
- java mongoTemplate mongodb中排序问题
- 关于Spring中MongoTemplate.aggregate的一个奇异bug
- Spring结合mongotemplate遇到的问题
- mongodb和spring集成中MongoTemplate的总结是使用方法
- mongodb和spring集成中MongoTemplate的总结是使用方法
- Spring 的单个MongoTemplate配置中加入用户认证信息
- spring mongoTemplate使用笔记
- Spring Class MongoTemplate详解
- spring mongoTemplate简单使用
- Spring集成MongoTemplate复杂查询
- spring web项目mongoTemplate配置
- mongoTemplate.getCollection方法返回值问题
- oracle中关于null排序的问题
- 关于Oracle中排序后取前几行的问题
- 关于vc中Listbox 无法排序问题
- 关于oracle中排序的问题
- 关于java中Map排序问题
- Apache 更改80端口
- hdu 5299 Circles Game
- 更换服务器后,重新进行域名解析
- iOS OC06_省市区字典数组
- Android 原生 APP
- 关于Spring中MongoTemplate排序问题
- js中onload和$(function(){})的区别
- Android入门:短信和拨打电话
- Scala入门到精通——第六节:类和对象(一)
- linux patch 命令小结
- 用指针数组保存字符串并将字符元素打印输出
- PHP编辑器之Komodo Edit的快捷键
- SpringMVC----初步配置和静态资源无法访问的原因及解决
- 多校1007(最短路+最小割)