検索
ホームページデータベースmysql チュートリアルMongoDB:mongodb在spring项目中的配置

MongoDB:mongodb在spring项目中的配置

Jun 07, 2016 pm 03:22 PM
mongodbspringマッチプロジェクト

最近在做基于mongodb的spring项目架构,有个问题跟大家分享一下,也方便自己以后能够用到 先看一个简单的项目架构: 在架构方面唯一需要说的是采用的是spring的注解: 下面是部分代码,部分。 /** * @author jessonlv * 用户注册接口 */ @Controller@Request

最近在做基于mongodb的spring项目架构,有个问题跟大家分享一下,也方便自己以后能够用到

先看一个简单的项目架构:

\

在架构方面唯一需要说的是采用的是spring的注解:

下面是部分代码,部分。

/**
 * @author jessonlv
 * 用户注册接口
 */
<strong>@Controller
@RequestMapping("/user")</strong>  
public class UserInfoController {
	@Autowired
	private UserInfoManager userManager;
	//接口文档
	<strong>@RequestMapping(method=RequestMethod.GET)</strong>
	public String list(HttpServletRequest request,HttpServletResponse response){
		 response.setContentType("text/html;charset=utf-8");  
		return "user";
	}
	//检测用户信息-根据帐户
	<strong>@RequestMapping(value="/check",method=RequestMethod.GET)
</strong>    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>

上面的都是些常规的配置,最重要的就是数据源部分

//数据源地址
//端口号

//数据库名
//对应的表明

道理其实还是和bean是一样的,这在项目启动的前期都已经映射了。每写一个dao就配置一个....,剩了很多的事儿,而且刚开始的有些不习惯。但是效率挺高,结构清晰。

接口的输出结果也很简单:DBObject myDocDbObject = userManager.getUserInfo(repeatAccount);

String str = myDocDbObject.toString(); 是一个json格式的字符。

呵呵,做个小总结,方便忘记了。

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
MySQLのパフォーマンスを監視するために使用できるツールは何ですか?MySQLのパフォーマンスを監視するために使用できるツールは何ですか?Apr 23, 2025 am 12:21 AM

MySQLのパフォーマンスを効果的に監視する方法は? MySqladmin、ShowGlobalStatus、PerconAmonitoring and Management(PMM)、MySQL EnterpriseMonitorなどのツールを使用します。 1. mysqladminを使用して、接続の数を表示します。 2。showglobalstatusを使用して、クエリ番号を表示します。 3.PMMは、詳細なパフォーマンスデータとグラフィカルインターフェイスを提供します。 4.mysqlenterprisemonitorは、豊富な監視機能とアラームメカニズムを提供します。

MySQLはSQL Serverとどのように違いますか?MySQLはSQL Serverとどのように違いますか?Apr 23, 2025 am 12:20 AM

MySQLとSQLServerの違いは次のとおりです。1)MySQLはオープンソースであり、Webおよび埋め込みシステムに適しています。2)SQLServerはMicrosoftの商用製品であり、エンタープライズレベルのアプリケーションに適しています。ストレージエンジン、パフォーマンスの最適化、アプリケーションシナリオの2つには大きな違いがあります。選択するときは、プロジェクトのサイズと将来のスケーラビリティを考慮する必要があります。

どのシナリオでMySQLよりもSQL Serverを選択できますか?どのシナリオでMySQLよりもSQL Serverを選択できますか?Apr 23, 2025 am 12:20 AM

高可用性、高度なセキュリティ、優れた統合を必要とするエンタープライズレベルのアプリケーションシナリオでは、MySQLの代わりにSQLServerを選択する必要があります。 1)SQLServerは、高可用性や高度なセキュリティなどのエンタープライズレベルの機能を提供します。 2)VisualStudioやPowerbiなどのMicrosoftエコシステムと密接に統合されています。 3)SQLSERVERは、パフォーマンスの最適化に優れた機能を果たし、メモリが最適化されたテーブルと列ストレージインデックスをサポートします。

MySQLは文字セットと照合をどのように処理しますか?MySQLは文字セットと照合をどのように処理しますか?Apr 23, 2025 am 12:19 AM

mysqlManagesCharacterSetSetSetsAndCollat​​ions ByUSINGUTF-8ASTHEDEDEFAULT、CONFIGURATIONATDATABASE、TABLE、ANDCOLUMNLEVELS、ANDREQUIRINGCAREACTERSETANDCOLLATIONSFORADABASE.2

mysqlのトリガーとは何ですか?mysqlのトリガーとは何ですか?Apr 23, 2025 am 12:11 AM

MySQLトリガーは、特定のデータ操作が実行されたときに一連の操作を実行するために使用されるテーブルに関連付けられた自動的に実行されたストアドプロシージャです。 1)定義と機能のトリガー:データ検証、ロギングなどに使用。2)動作原則:それは前後に分割され、行レベルのトリガーをサポートします。 3)使用例:給与の変更を記録したり、在庫を更新したりするために使用できます。 4)デバッグスキル:ShowTriggersとShowCreatetriggerコマンドを使用します。 5)パフォーマンスの最適化:複雑な操作を避け、インデックスを使用し、トランザクションを管理します。

MySQLでユーザーアカウントをどのように作成および管理しますか?MySQLでユーザーアカウントをどのように作成および管理しますか?Apr 22, 2025 pm 06:05 PM

MySQLでユーザーアカウントを作成および管理する手順は次のとおりです。1。ユーザーの作成:createUser'newuser '@' localhost'identifidedby'password 'を使用します。 2。許可を割り当てる:grantselect、insert、updateonmydatabase.to'newuser'@'localhost 'を使用します。 3.許可エラーを修正:Revokeallprivilegesonmydatabase.from'newuser'@'localhost 'を使用します。次に、許可を再割り当てします。 4。最適化権限:Showgraを使用します

MySQLはOracleとどのように違いますか?MySQLはOracleとどのように違いますか?Apr 22, 2025 pm 05:57 PM

MySQLは、迅速な開発や中小規模のアプリケーションに適していますが、Oracleは大規模な企業や高可用性のニーズに適しています。 1)MySQLはオープンソースで使いやすく、Webアプリケーションや中小企業に適しています。 2)Oracleは強力で、大企業や政府機関に適しています。 3)MySQLはさまざまなストレージエンジンをサポートし、Oracleは豊富なエンタープライズレベルの機能を提供します。

他のリレーショナルデータベースと比較してMySQLを使用することの欠点は何ですか?他のリレーショナルデータベースと比較してMySQLを使用することの欠点は何ですか?Apr 22, 2025 pm 05:49 PM

他のリレーショナルデータベースと比較したMySQLの欠点には次のものがあります。1。パフォーマンスの問題:大規模なデータを処理する際にボトルネックに遭遇する可能性があり、PostgreSQLは複雑なクエリとビッグデータ処理でより良いパフォーマンスを発揮します。 2。スケーラビリティ:水平スケーリング能力は、Google SpannerやAmazon Auroraほど良くありません。 3。機能的な制限:高度な機能におけるPostgreSQLやOracleほど良くないため、一部の関数では、より多くのカスタムコードとメンテナンスが必要です。

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

MantisBT

MantisBT

Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境

VSCode Windows 64 ビットのダウンロード

VSCode Windows 64 ビットのダウンロード

Microsoft によって発売された無料で強力な IDE エディター

SublimeText3 Linux 新バージョン

SublimeText3 Linux 新バージョン

SublimeText3 Linux 最新バージョン