Spring Data with MongoDB (一)

Spring Data with MongoDB

节选自《Netkiller Spring Cloud 手札》

Example Spring Data MongoDB

pom.xml

注意Spring4 与 1.9.1.RELEASE有兼容性问题,日志提示 Error creating bean with name 'mongoTemplate' defined in ServletContext resource

 <dependency>
 <groupId>org.springframework.data</groupId>
 <artifactId>spring-data-mongodb</artifactId>
 <version>1.8.1.RELEASE</version>
 </dependency> 

springframework-servlet.xml

 <mongo:db-factory id="mongoDbFactory" host="${mongo.host}" port="${mongo.port}" dbname="${mongo.database}" />
 <!-- username="${mongo.username}" password="${mongo.password}" -->

 <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
 <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
 </bean>
 <mongo:mapping-converter id="converter" db-factory-ref="mongoDbFactory"/>
 <bean id="gridFsTemplate" class="org.springframework.data.mongodb.gridfs.GridFsTemplate">
 <constructor-arg ref="mongoDbFactory"/>
 <constructor-arg ref="converter"/>
 </bean>

例 4.2. Spring Data MongoDB - springframework-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:mvc="http://www.springframework.org/schema/mvc" 
 xmlns:context="http://www.springframework.org/schema/context" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:mongo="http://www.springframework.org/schema/data/mongo"
 xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="
 http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/mvc 
 http://www.springframework.org/schema/mvc/spring-mvc.xsd
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/data/mongo
 http://www.springframework.org/schema/data/mongo/spring-mongo-1.5.xsd  
 ">

 <mvc:resources location="/images/" mapping="/images/**" />
 <mvc:resources location="/css/" mapping="/css/**" />
 <mvc:resources location="/js/" mapping="/js/**" />
 <mvc:resources location="/zt/" mapping="/zt/**" />
 <mvc:resources location="/sm/" mapping="/sm/**" />
 <mvc:resources location="/module/" mapping="/module/**" />

 <context:component-scan base-package="cn.netkiller.controller" />
 <!-- <context:property-placeholder location="classpath:resources/development.properties" /> -->
 <mvc:annotation-driven />

 <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
 <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
 <property name="prefix" value="/WEB-INF/jsp/" />
 <property name="suffix" value=".jsp" />
 </bean>

 <bean id="configuracion" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 <property name="location" value="classpath:resources/development.properties" />
 </bean>
 <!-- MongoDB Connection Factory -->
 <mongo:db-factory id="mongoDbFactory" host="${mongo.host}" port="${mongo.port}" dbname="${mongo.database}" />
 <!-- username="${mongo.username}" password="${mongo.password}" -->
 <!-- MongoDB template definition -->
 <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
 <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
 </bean>
 <!-- MongoDB GridFS template definition -->
 <mongo:mapping-converter id="converter" db-factory-ref="mongoDbFactory"/>
 <bean id="gridFsTemplate" class="org.springframework.data.mongodb.gridfs.GridFsTemplate">
 <constructor-arg ref="mongoDbFactory"/>
 <constructor-arg ref="converter"/>
 </bean>
 <!-- Redis Connection Factory -->
 <bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="192.168.2.1" p:port="6379" p:use-pool="true" />

 <!-- redis template definition -->
 <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnFactory" />
</beans>

development.properties 配置内容

mongo.host=192.168.4.1
mongo.port=27017
mongo.username=test
mongo.password=passw0rd
mongo.database=website

POJO

package cn.netkiller.pojo;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "tracker")
public class Tracker {
 @Id
 private String id;
 private String name;
 private String unique;
 private String hostname;
 private String referrer;
 private String href;

 public Tracker() {
 // TODO Auto-generated constructor stub
 }

 public String getName() {
 return name;
 }

 public void setName(String name) {
 this.name = name;
 }

 public String getUnique() {
 return unique;
 }

 public void setUnique(String unique) {
 this.unique = unique;
 }

 public String getHostname() {
 return hostname;
 }

 public void setHostname(String hostname) {
 this.hostname = hostname;
 }

 public String getReferrer() {
 return referrer;
 }

 public void setReferrer(String referrer) {
 this.referrer = referrer;
 }

 public String getHref() {
 return href;
 }

 public void setHref(String href) {
 this.href = href;
 }

 @Override
 public String toString() {
 return "Tracker [id=" + id + ", name=" + name + ", unique=" + unique + ", hostname=" + hostname + ", referrer=" + referrer + ", href=" + href + "]";
 }

} 

Controller

package cn.netkiller.controller;

import cn.netkiller.pojo.Tracker;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
public class TrackerController {

 @Autowired
 private MongoTemplate mongoTemplate;

 public TrackerController() {

 }

 @RequestMapping("/tracker/test")
 @ResponseBody
 String hello() {
 return "Hello World!";
 }

 @RequestMapping("/tracker")
 @ResponseBody
 String execute() {
 Tracker tracker = new Tracker();
 tracker.setName("test");
 tracker.setUnique("111223456");
 tracker.setHostname("www.example.com");
 tracker.setHref("http://example.com/test.html");
 tracker.setReferrer("http://example.com/");
 this.mongoTemplate.insert(tracker);
 return tracker.toString();
 }

}

查看测试结果

> db.tracker.find();
{ "_id" : ObjectId("5757c0b92c526a6bda5eea3a"), "_class" : "cn.netkiller.repositories.Tracker", "name" : "test", "unique" : "111223456", "hostname" : "www.example.com", "referrer" : "http://example.com/", "href" : "http://example.com/test.html" }

条件查询

 @RequestMapping("/read/name/{name}")
 public ArrayList<Tracker> sort(@PathVariable String name) {
 Query query = new Query(Criteria.where("name").is(name)); 
 ArrayList<Tracker> trackers = (ArrayList<Tracker>) mongoTemplate.find(query, Tracker.class);
 return trackers;
 } 
  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

netkiller-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值