zoukankan      html  css  js  c++  java
  • 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


  • 相关阅读:
    pikachu漏洞练习之sql注入
    redis未授权访问漏洞复现
    Springboot导出Excel并下载
    Springboot使用javaMail进行邮件发送
    springboot实现上传并解析Excel
    微信提示“在浏览器打开”效果实现
    docker入门(二):镜像和容器
    centos安装mysql
    centos安装tomcat
    centos7安装jdk
  • 原文地址:https://www.cnblogs.com/jessonlv/p/4388005.html
Copyright © 2011-2022 走看看