Spring data mongoDB学习(2)

来源:互联网 发布:足球数据分析师 编辑:程序博客网 时间:2024/06/06 23:16

Spring链接MongoDB的两种方法
在用MongoDB和Spring时,第一件事就是用IoC容器创建一个com.mongodb.mongoDB对象。
1、利用基于bean 元数据的Java创建MongoDB的实例
2、利用基于bean 元数据的XML创建MongoDB的实例

首先,利用第一种方法:
下面利用Java创建Mongo实例。
1、利用com.mongodb.mongo创建Mongo实例 AppConfig类内容如下:

import java.net.UnknownHostException;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import com.mongodb.Mongo;@Configurationpublic class AppConfig {    public @Bean Mongo mongo() throws UnknownHostException{        return new Mongo("localhost");    }
2、利用org.springframework.data.mongodb.core.MongoFactoryBean创建mongo   
    @Bean    public MongoFactoryBean mongo(){        MongoFactoryBean mongo = new MongoFactoryBean();        mongo.setHost("localhost");        return mongo;        }

2、利用XML创建Mongo实例。
首先,找到app-context.xml文件,进行mongodb的一些配置

    <?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:context="http://www.springframework.org/schema/context"          xmlns:mongo="http://www.springframework.org/schema/data/mongo"          xsi:schemaLocation=          "http://www.springframework.org/schema/context          http://www.springframework.org/schema/context/spring-context-3.0.xsd          *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.0.xsd">    <!-- Default bean name is 'mongo' -->    <mongo:mongo host="localhost" port="27017"/></beans>

复制集的配置:

<mongo:mongo id="replicaSetMongo" replica-set="127.0.0.1:27017,localhost:27018"/>

建立新Spring项目,进行验证:
3、利用XML配置文件,如方法2.
在测试类里添加main方法如下:

public static void main(String[] args) throws Exception {        MongoOperations mongoOps = new MongoTemplate(new SimpleMongoDbFactory(new Mongo(), "mongodb"));        mongoOps.insert(new Company("zkwpr", 23));        log.info(mongoOps.findOne(new Query(where("name").is("zkwpr")), Company.class));    }

运行程序,发现插入成功!
这里写图片描述

总结:
1、Spring 和Mongo连接的方法,加上第一天学习的方法,总共有三种。
2、在不同的环境下,利用XML配置mongo的信息,需要注意xml文件的不同。
如:在Spring MVC中,就需要在root-context.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:context="http://www.springframework.org/schema/context"    xmlns:mongo="http://www.springframework.org/schema/data/mongo"    xsi:schemaLocation=    "http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context-3.0.xsd     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.xsd">   <context:annotation-config />    <!-- Root Context: defines shared resources visible to all other web components -->        <mongo:mongo host="localhost" port="27017"/>            </beans>
在Spring MVC结合Maven项目中,就需要在applicationContext-data.xml中进行配置。配置内容如下:![这里写图片描述](http://img.blog.csdn.net/20150318154802411)
<?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:context="http://www.springframework.org/schema/context"    xmlns:jpa="http://www.springframework.org/schema/data/jpa"    xmlns:mongo="http://www.springframework.org/schema/data/mongo"    xsi:schemaLocation=    "http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd    http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context-4.0.xsd    http://www.springframework.org/schema/data/jpa     http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd    http://www.springframework.org/schema/data/mongo     http://www.springframework.org/schema/data/mongo/spring-mongo-1.5.xsd">   <context:annotation-config />    <context:component-scan base-package="xxx.xxxxxx.xxxxxx" />    <mongo:repositories base-package="xxx.xxxxxx.xxxxxx" />    <!-- Default bean name is 'mongo' -->    <mongo:mongo host="localhost" port="27017"/>    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">        <constructor-arg ref="mongo"/>        <constructor-arg name="databaseName" value="xxxxx"/>    </bean></beans>
0 0