spring+mongo

一、程序结构

  spring+mongo

   说明:以上两个包的版本要相匹配,否则可能会出现Class找不到的情况(不同版本路径有改变)

   几个主要jar包下载地址:

     http://yangjizhong.iteye.com/blog/1206901

     spring-data-common-core-1.0.0.RELEASE.jar

   现在很多例子程序提供的都是之前spring官网上的链接地址,但是spring官网改版

  后那些下载地址都已经失效。  

二、源码

  1、Url类

spring+mongo
package com.wiscom.data;

import java.io.Serializable;

public class Url implements Serializable {
    
    private static final long serialVersionUID = -6422640281609234112L;
    
    private String url;
    private String url_md5;
    private int isParsed;
    
    public Url() {  
        super();  
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUrl_md5() {
        return url_md5;
    }

    public void setUrl_md5(String url_md5) {
        this.url_md5 = url_md5;
    }

    public int getIsParsed() {
        return isParsed;
    }

    public void setIsParsed(int isParsed) {
        this.isParsed = isParsed;
    }
    
    public String displayInfo(){
        return this.url+", "+this.url_md5+", "+this.isParsed;
    }

}
spring+mongo

  2、IUrlRepository接口

spring+mongo
package com.wiscom.data;

public interface IUrlRepository {
    
    public Url findOne();
}
spring+mongo

  3、UrlRepositoryImpl类:IUrlRepository接口的实现

spring+mongo
package com.wiscom.data;

import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.query.Criteria;
import org.springframework.data.document.mongodb.query.Query;



public class UrlRepositoryImpl implements IUrlRepository {
    
    private MongoTemplate mongoTemplate;
    

    public MongoTemplate getMongoTemplate() {
        return mongoTemplate;
    }


    public void setMongoTemplate(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }


    @Override
    public Url findOne() {
        // TODO Auto-generated method stub
        String url_md5_Str="E5C0818FC19480BBF2CC7BF71241A145";
        return getMongoTemplate().findOne(new Query(Criteria.where("url_md5").is(url_md5_Str)),Url.class);
    }

}
spring+mongo

  4、applicationContext.xml

spring+mongo
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
           http://www.springframework.org/schema/context  
           http://www.springframework.org/schema/context/spring-context-3.1.xsd  
           http://www.springframework.org/schema/aop  
           http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
           http://www.springframework.org/schema/tx   
           http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
           
    <bean id="mongo" class="org.springframework.data.document.mongodb.MongoFactoryBean">
        <property name="host" value="10.0.0.187"/>
        <property name="port" value="25017"/>
    </bean> 
    <bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">    
        <constructor-arg ref="mongo"/>     
        <constructor-arg name="databaseName" value="crawler_data"/>
        <constructor-arg name="defaultCollectionName" value="url"/>
    </bean>
    
    <bean id="urlRepositoryImpl" class="com.wiscom.data.UrlRepositoryImpl">
        <property name="mongoTemplate" ref="mongoTemplate"/>
    </bean> 
    
    <context:annotation-config />
</beans> 
spring+mongo

  PS:xsi:schemaLocation要使用spring3.X以上版本,如果使用2.X会导致constructor-arg无法使用。

三、运行测试

  主函数:

spring+mongo
package com.wiscom.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.wiscom.data.IUrlRepository;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
         System.out.println("mongo test");
        
         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");  
         IUrlRepository urlObj= (IUrlRepository)ctx.getBean("urlRepositoryImpl");
         
         System.out.println(urlObj.findOne().displayInfo());
    }
    
}
spring+mongo

  运行结果:

  spring+mongo

  参考:http://blog.csdn.net/cuiran/article/details/8287204

spring+mongo,布布扣,bubuko.com

spring+mongo

上一篇:Java动态代码模式


下一篇:AES加密的C语言实现