最近在做基於mongodb的spring項目架構,有個問題跟大家分享一下,也方便自己以後能夠用到
先看一個簡單的項目架構:
在架構方面唯一需要說的是採用的是spring的註解:
下面是部分代碼,部分。
/** * @author jessonlv * 使用者註冊介面 */@Controller@RequestMapping("/user") public class UserInfoController {@Autowiredprivate UserInfoManager userManager;//介面文檔@RequestMapping(method=RequestMethod.GET)public String list(HttpServletRequest request,HttpServletResponse response){ response.setContentType("text/html;charset=utf-8"); return "user";}//檢測使用者資訊-根據帳戶@RequestMapping(value="/check",method=RequestMethod.GET) public String getUser(HttpServletRequest request,HttpServletResponse response) throws Exception{//設定HTTP頭 response.setContentType("text/html;charset=utf-8"); //參數擷取 String account=StringUtil.formatStringParameter(request.getParameter("account"), null); String key=StringUtil.formatStringParameter(request.getParameter("key"), null);//驗證調用方 //參數有效性驗證 if(account==null){ throw new ParameterException(); } //TODO:key驗證 //查詢對象 BasicDBObject o=new BasicDBObject("account",account); try {//取資料庫DBObject doc=userManager.getUserInfo(o);//輸出結果PrintWriter writer=response.getWriter();writer.write(doc.toString());} catch (Exception e) {e.printStackTrace();//輸出結果PrintWriter writer=response.getWriter();writer.write(new BasicDBObject().toString());}//db.find(query).skip(pos).limit(pagesize)分頁return null; }
粗體部分就是spring的註解。我們得到的介面調用是這個樣子的:http://localhost/ucenter/user/check?account=11&pwd=11111 注意是get請求。
採用mongodb的最大好處中的其中一個就是不用寫bean,只需做一些簡單的配置
我們看spring-servlet.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tool="http://www.springframework.org/schema/tool"xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsdhttp://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool-3.1.xsdhttp://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd" default-autowire="byName" default-lazy-init="true"><context:annotation-config /><context:component-scan base-package="com.ishowchina.user" /><!-- 啟動Spring MVC的註解功能,完成請求和註解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".html" /> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8" /> <!-- 支援json --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean> </list> </property> </bean> <!-- 匯入設定檔 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:appconfig.properties</value> </list> </property> </bean> <!-- 資料來源 --> <bean id="dataSource" class="com.ishowchina.user.dao.DataSource"> <property name="ip" value="localhost"/> <property name="port" value="27017"/> </bean> <bean id="userDao" class="com.ishowchina.user.dao.impl.UserInfoDaoImpl"> <property name="dbName" value="prop"/> <property name="tableName" value="userinfo"/> <property name="dataSource" ref="dataSource"/> </bean> <bean id="stationDao" class="com.ishowchina.user.dao.impl.StationInfoDaoImpl"> <property name="dbName" value="prop"/> <property name="tableName" value="stationinfo"/> <property name="dataSource" ref="dataSource"/> </bean></beans>
上面的都是些常規的配置,最重要的就是資料來源部分
<property name="ip" value="localhost"/> //資料來源地址
<property name="port" value="27017"/> //連接埠號碼
<property name="dbName" value="prop"/> //資料庫名
<property name="tableName" value="userinfo"/>//對應的表明
道理其實還是和bean是一樣的,這在項目啟動的前期都已經映射了。每寫一個dao就配置一個<bean>....</bean>,剩了很多的事兒,而且剛開始的有些不習慣。但是效率挺高,結構清晰。
介面的輸出結果也很簡單:DBObject myDocDbObject = userManager.getUserInfo(repeatAccount);
String str = myDocDbObject.toString(); 是一個json格式的字元。
呵呵,做個小總結,方便忘記了。