• spring+mongo


    一、程序结构

      

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

       几个主要jar包下载地址:

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

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

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

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

    二、源码

      1、Url类

    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;
        }
    
    }

      2、IUrlRepository接口

    package com.wiscom.data;
    
    public interface IUrlRepository {
        
        public Url findOne();
    }

      3、UrlRepositoryImpl类:IUrlRepository接口的实现

    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);
        }
    
    }

      4、applicationContext.xml

    <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> 

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

    三、运行测试

      主函数:

    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());
        }
        
    }

      运行结果:

      

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

  • 相关阅读:
    Spring Cloud Feign 在调用接口类上,配置熔断 fallback后,输出异常
    Spring Cloud Feign 输出日志
    RestTemplate OR Spring Cloud Feign 上传文件
    Springboot swagger2 导出api文档
    springboot + swagger2 生成api文档
    网页输出日志文件
    Mybatis Common Mapper文件
    Java 序列化工具类
    rabbitMQ的三种路由模式
    .net mvc中session的锁机制
  • 原文地址:https://www.cnblogs.com/edisonfeng/p/3658300.html
Copyright © 2020-2023  润新知