MongoDB:mongodb在spring項目中的配置

最近在做基於mongodb的spring項目架構,有個問題跟大家分享一下,也方便自己以後能夠用到

先看一個簡單的項目架構:


在架構方面唯一需要說的是採用的是spring的註解:

下面是部分代碼,部分。

/**
 * @author jessonlv
 * 用戶註冊接口
 */
@Controller
@RequestMapping("/user")  
public class UserInfoController {
	@Autowired
	private 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.xsd
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
	http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
	http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
	http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool-3.1.xsd
	http://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格式的字符。

呵呵,做個小總結,方便忘記了。

so....over,下期我將會總結下mongodb的主從複製

原創文章,轉載請註明出處:http://blog.csdn.net/jessonlv


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章