前几天使用python的scrapy框架在爬了下腾讯社招网站上的数据存储在mongodb中,于是乎就想使用javaweb将数据查询展示一下,所以就做了一下一个简单的demo,mongoDB我并没有设置验证,所以不需要用户名密码,要的话自己配置,网上找教程很多,以下是
spring-mongo+springMVC的简单demo
构建maven工程,pom.xml如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>mongoDemo</groupId> <artifactId>mongoDemo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>mongoDemo Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.data.mongodb>1.9.2.RELEASE</spring.data.mongodb> <mongodb.java.driver>3.2.2</mongodb.java.driver> <fastjson.version>1.2.3</fastjson.version> <spring.version>4.2.6.RELEASE</spring.version> <jackson.version>2.7.0</jackson.version> <junit>3.8.1</junit> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>${mongodb.java.driver}</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>${spring.data.mongodb}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.2.2</version> </dependency> <!-- json --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> </dependencies> <build> <finalName>mongoDemo</finalName> </build> </project>
配置spring.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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <context:component-scan base-package="com.advance.dao.imp"></context:component-scan> <!-- 导入mongodb的配置文件 --> <import resource="mongodb-context.xml"/> </beans>
然后为了便于管理,抽出一个spring-mongo.xml文件,主要用于集成mongo,如下:
<?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-4.2.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.8.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd"> <!-- 加载mongodb的属性配置文件 --> <context:property-placeholder location="classpath:mongodb.properties" /> <!-- mongo对象 --> <mongo:mongo-client id="mongo" replica-set="${mongo.hostport}"> <mongo:client-options connections-per-host="${mongo.connectionsPerHost}" threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}" connect-timeout="${mongo.connectTimeout}" max-wait-time="${mongo.maxWaitTime}" socket-keep-alive="${mongo.socketKeepAlive}" socket-timeout="${mongo.socketTimeout}" /> </mongo:mongo-client> <mongo:db-factory dbname="${mongo.databaseName}" id="mongoFactory" mongo-ref="mongo" /> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref="mongoFactory" /> </bean> </beans>
不急,上述配置文件中还缺少一个mongo的资源配置文件,如下(其中的数据库相关信息请自行修改):
mongo.hostport=127.0.0.1:27017
mongo.connectionsPerHost=8
mongo.threadsAllowedToBlockForConnectionMultiplier=4
#\\u8FDE\\u63A5\\u8D85\\u65F6\\u65F6\\u95F4
mongo.connectTimeout=1000
#\\u7B49\\u5F85\\u65F6\\u95F4
mongo.maxWaitTime=1500
mongo.socketKeepAlive=true
#Socket\\u8D85\\u65F6\\u65F6\\u95F4
mongo.socketTimeout=15000
mongo.databaseName=tecent
好了,这里已经配置好了,为了方便测试,还需要配置一下springmvc.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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> <!-- 自动扫描controller包 --> <context:component-scan base-package="com.advance.controller" /> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <!-- json转换器 --> <ref bean="mappingJacksonHttpMessageConverter" /> </list> </property> </bean> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 配置多文件上传 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding"> <value>UTF-8</value> </property> <property name="maxUploadSize"> <value>32505856</value> </property> <property name="maxInMemorySize"> <value>4096</value> </property> </bean> <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg> <value>UTF-8</value> </constructor-arg> </bean> </mvc:message-converters> </mvc:annotation-driven> </beans>
最后,在编写代码之前,还有一个web.xml文件,这个是所有web应用的入口文件,配置跟通常的没两样:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>test.do</welcome-file> </welcome-file-list> </web-app>
完成这里就可以开始编写代码了,我的项目结构如下:
,接下来就贴代码啦,很简单,首先肯定是建实体类TecentItem.java:
这里需要注意的是mongoDB的_id会自动加入@Id注解下的String或者BigInteger类型的属性中去,然后@Document(collection="tecent_item")这一句话是必不可少的,必须对应你要查询的cillection
package com.advance.entity; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; @Document(collection="tecent_item") public class TecentItem { @Id private String Id; private String type; private String name; private String address; private String time; private String personNum; private String pageUrl; public String getPageUrl() { return pageUrl; } public void setPageUrl(String pageUrl) { this.pageUrl = pageUrl; } public String getPersonNum() { return personNum; } public void setPersonNum(String personNum) { this.personNum = personNum; } public String getId() { return Id; } public void setId(String id) { Id = id; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } }
接下来就直接使用MongoTemplate进行操作啦,这里提供几个简单的方法,其他的可以查看MongoTemplate的详细用法啦:
package com.advance.dao; import java.util.List; import java.util.Set; import com.advance.entity.TecentItem; public interface TecentItemDao { List<TecentItem> findAll(); Set<String> getCollectionNames(); Long count(); }
package com.advance.dao.imp; import java.util.List; import java.util.Set; import javax.annotation.Resource; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.stereotype.Service; import com.advance.dao.TecentItemDao; import com.advance.entity.TecentItem; @Service public class TecentItemDaoImp implements TecentItemDao { @Resource private MongoTemplate mongoTemplate; public List<TecentItem> findAll() { // TODO Auto-generated method stub return mongoTemplate.findAll(TecentItem.class); } public Set<String> getCollectionNames() { Set<String> collections = mongoTemplate.getCollectionNames(); return collections; } public Long count() { // TODO Auto-generated method stub return mongoTemplate.count(null, TecentItem.class); } }
最后是编写controller类,成不成功在此一举:
package com.advance.controller; import java.util.List; import java.util.Set; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.advance.dao.TecentItemDao; import com.advance.dao.imp.TecentItemDaoImp; import com.advance.entity.TecentItem; @Controller public class MongoController { @Resource private TecentItemDao tDao; @RequestMapping("/test.do") public String index(HttpServletRequest request){ Set<String> collNames = tDao.getCollectionNames(); List<TecentItem> items = tDao.findAll(); Long count = tDao.count(); request.setAttribute("colls", collNames); request.setAttribute("items", items); request.setAttribute("count", count); return "index"; } }
编写界面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>MongoDb-Spring测试 首页</title> <style type="text/css"> .items{width: 95%;border: 1px solid #ddd; border-collapse: collapse;} .items thead tr{background: #e9e9e9;height: 40px;font-weight: 600} .items tr{height: 35px;} .items tr td{text-align: center;font-size: 15px;border: 1px solid #c4c4c4;} </style> </head> <body> <center> <h1>spring-mongo Demo 首页</h1> <br/><hr/> <h3>collections 查询展示</h3> <table> <tbody> <c:forEach items="${colls }" var="coll"> <td>${coll }    </td> </c:forEach> </tbody> </table> <br/><hr/> <h3>社招条目 查询展示</h3>   <em style="color: red"><strong>总条数:${count }条</strong></em><br/> <br/> <table class="items"> <thead> <tr> <td>编号</td> <td>名称</td> <td>类型</td> <td>地址</td> <td>发布时间</td> <td>招聘人数</td> </tr> </thead> <tbody> <c:forEach items="${items }" var="item"> <tr> <td>${item.id }</td> <td><a href="${item.pageUrl }" target="_blank">${item.name }</a></td> <td>${item.type }</td> <td>${item.address }</td> <td>${item.time }</td> <td>${item.personNum }人</td> </tr> </c:forEach> </tbody> </table> </center> </body> </html>
可以的,最后做下对比:
界面展示
数据库数据
我觉得应该是OK了。