mongodb和springMVC整合
来源:互联网 发布:lastindexof的用法 js 编辑:程序博客网 时间:2024/04/30 18:01
在对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/repositoryhttp://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=bookStoremongodb.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{@Idprivate 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")@Servicepublic 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 {@AutowiredBookService bookService;@AutowiredMongoTemplate mongoTemplate;@AutowiredRestTemplate restTemplate;@AutowiredDemoService 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;}}
阅读全文
0 0
- mongodb和springMVC整合
- springMVC+mongodb整合(morphia)
- springmvc-mongodb-maven整合
- springmvc mongodb maven 整合
- SpringMVC整合MongoDB开发
- SpringMVC+MongoDB+Maven整合
- springmvc + mongodb 整合
- springmvc、jpa、spring、mongodb和ehcache整合框架demo
- Mongodb数据库与springmvc整合
- springmvc整合mongodb 增查改删操作
- SpringMVC+MongoDB整合及注意事项
- SpringMvc整合mongodb maven项目
- MongoDB和Spring整合
- solr和mongodb整合
- mongodb和spring整合
- Mongodb和log4j整合
- SpringMVC整合MongoDB开发 高级操作
- SpringMVC整合MongoDB开发 架构搭建
- java ByteBuffer类的重点
- 单
- js 根据已知的时间,判断是否是今天,明天,昨天,后天
- Web与OC交互---WKWebView
- centos用户和用户组管理
- mongodb和springMVC整合
- 【CSP201312-3】最大的矩形
- 安卓intent详解
- 数据分析书单
- 【poj3320】Jessica's Reading Problem(尺取)
- mac下 maven打包
- JAVA中递归的概念
- 剑指Offer—60—把二叉树打印成多行
- 好文-Class