这个问题已经困扰了几天了,每次查到的结果都是使用Query query = new Query();query.with(Sort……),但是我的文件里面就是找不到with这个方法,但有其他的方法。查了很久,终于查到了这个:点击打开链接  把源码写上了。也是用于的Spring MongoTemplate里面方法。对比了一下,发现是引用的包不一样,大概就这点差别。

        我的包:

<span >	</span>org.springframework.data.document.mongodb.MongoTemplate


别人引用的是

<span >	</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 >	</span>   <groupId>org.mongodb</groupId>  
       <span >	</span> <artifactId>mongo-java-driver</artifactId>  
       <span >	</span> <version>2.10.1</version>  
      <span >	</span>  <type>jar</type>  
      <span >	</span>  <scope>compile</scope>  
    <span >	</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包,如果包冲突则还是会提示构造参数错误。


然后就是排序的代码,就和其他贴吧上写的一样了。

@Override
	public List<Person> sortByAge() {
		// TODO Auto-generated method stub
		Query 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的其他用法了。