在对mongodb和springmvc的整合是十分简单的,以maven项目为例。具体分为以下几步:
maven配置
在整合之前需要导入jar包,在这里只需在pom.xml中加入依赖即可。
<!-- mongodb开始 -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
<!-- mongodb结束 -->
集群配置
applicationContext.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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
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.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/schema/util
http://www.springframework.org/schema/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.5.xsd
http://www.springframework.org/schema/data/repository
http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd
">
<!-- 自动扫描 -->
<context:annotation-config />
<!-- 设置Spring使用自动扫描功能,使"base-package"中的对象可以使用Annotation配置 -->
<context:component-scan base-package="com.**.**" />
<mongo:mongo id="mongo" replica-set="${mongodb.replicaSet}" />
<mongo:db-factory id="mongoDbFactory" dbname="${mongodb.database}"
mongo-ref="mongo" />
<!-- mongo模板操作对象 -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<!-- 配置读写分离的模式 -->
<property name="readPreference" ref="secondaryPreferredReadPreference"></property>
</bean>
</beans>
配置文件config.properties
#mongodb配置文件
mongodb.database=bookStore
mongodb.replicaSet = 10.0.11.28:27017,10.0.11.29:27017,10.0.11.30:27017
mongodb实例
mongodb对象模型
Book.java,具体代码如下
package com.book.book.model;
import java.io.Serializable;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "book")
public class Book implements Serializable{
@Id
private String id;
// 书名
private String name;
// 作者
private String author;
// 价格
private float price;
// 图书封面
private String picture;
// 图书存储量
private Integer stock;
// 售出量
private int sale;
// 图书简介
private String message;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
public Integer getStock() {
return stock;
}
public void setStock(Integer stock) {
this.stock = stock;
}
public int getSale() {
return sale;
}
public void setSale(int sale) {
this.sale = sale;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
接口BookService.java
package com.book.book.service;
/*
* 图书管理
*/
public interface BookService {
/*
* 查询所有图书
*/
public String FindAll();
/*
* 根据图书名获取所有图书——模糊查询
*
* @name 图书名
*
* @return
*/
public String getByName(String name);
/*
* 根据作者获取所有图书——模糊查询
*
* @author 作者
*
* @return
*/
public String getByAuthor(String author);
/*
* 根据id获取所有图书
*
* @id 图书id
*
* @return
*/
public String getById(String id);
}
接口实现
package com.book.book.service;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import com.book.book.model.Book;
import net.sf.json.JSONObject;
@Path("/Book")
@Service
public class BookServiceImpl implements BookService {
@Autowired
@Qualifier("mongoTemplate")
MongoTemplate mongoTemplate;
public String FindAll() {
JSONObject jsonObject = new JSONObject();
try {
List<Book> bookList = new ArrayList<Book>();
bookList = mongoTemplate.findAll(Book.class);
jsonObject.put("result", 1);
jsonObject.put("msg", bookList);
} catch (Exception e) {
jsonObject.put("result", 0);
jsonObject.put("msg", "接口异常,请联系管理员!");
}
return jsonObject.toString();
}
public String getByName(String name) {
JSONObject jsonObject = new JSONObject();
try {
Criteria criteria = new Criteria();
criteria.andOperator(Criteria.where("name").regex(".*?\\" + name + ".*"));
List<Book> bookList = new ArrayList<Book>();
bookList = mongoTemplate.find(new Query(criteria), Book.class);
jsonObject.put("result", 1);
jsonObject.put("msg", bookList);
} catch (Exception e) {
jsonObject.put("result", 0);
jsonObject.put("msg", "接口异常,请联系管理员!");
}
return jsonObject.toString();
}
public String getByAuthor(String author) {
JSONObject jsonObject = new JSONObject();
try {
Criteria criteria = new Criteria();
criteria.andOperator(Criteria.where("author").regex(".*?\\" + author + ".*"));
List<Book> bookList = new ArrayList<Book>();
bookList = mongoTemplate.find(new Query(criteria), Book.class);
jsonObject.put("result", 1);
jsonObject.put("msg", bookList);
} catch (Exception e) {
jsonObject.put("result", 0);
jsonObject.put("msg", "接口异常,请联系管理员!");
}
return jsonObject.toString();
}
public String getById(String id) {
JSONObject jsonObject = new JSONObject();
try {
Criteria criteria = new Criteria();
criteria.andOperator(Criteria.where("id").is(id));
List<Book> bookList = new ArrayList<Book>();
bookList = mongoTemplate.find(new Query(criteria), Book.class);
jsonObject.put("result", 1);
jsonObject.put("msg", bookList);
} catch (Exception e) {
jsonObject.put("result", 0);
jsonObject.put("msg", "接口异常,请联系管理员!");
}
return jsonObject.toString();
}
}
控制层
package com.book.controllers;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.client.RestTemplate;
import com.book.book.model.Book;
import com.book.book.service.BookService;
import com.book.demo.service.DemoService;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
@Controller
@Path("/")
public class BookController {
@Autowired
BookService bookService;
@Autowired
MongoTemplate mongoTemplate;
@Autowired
RestTemplate restTemplate;
@Autowired
DemoService demoService;
@Get("findAllBooks")
public String FindAll() {
String bookList = bookService.FindAll();
return "@" + bookList;
}
@Get("getByName")
public String getByName(@Param("name") String name) {
String bookList = bookService.getByName(name);
return "@" + bookList;
}
@Get("getByAuthor")
public String getByAuthor(@Param("author") String author) {
String bookList = bookService.getByAuthor(author);
return "@" + bookList;
}
@Get("getById")
public String getById(String id) {
String bookList = bookService.getById(id);
return "@" + bookList;
}
}