Spring data mongoDB学习(2)

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;

@Configuration
public 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中进行配置。配置内容如下:

![这里写图片描述](https://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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值