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

MongoDB:mongodb在spring项目中的配置

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

最近在做基于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 までご連絡ください。

ホット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 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

作曲家を使用して、推奨システムのジレンマを解決する:Andres-Montanez/推奨 作曲家を使用して、推奨システムのジレンマを解決する:Andres-Montanez/推奨 Apr 18, 2025 am 11:48 AM

eコマースのWebサイトを開発するとき、私は困難な問題に遭遇しました。ユーザーにパーソナライズされた製品の推奨事項を提供する方法です。当初、私はいくつかの簡単な推奨アルゴリズムを試しましたが、結果は理想的ではなく、ユーザーの満足度も影響を受けました。推奨システムの精度と効率を改善するために、より専門的なソリューションを採用することにしました。最後に、Andres-Montanez/Adcumentations Bundleを介してAndres-Montanez/Bundleをインストールしました。これは、問題を解決しただけでなく、推奨システムのパフォーマンスを大幅に改善しました。次の住所から作曲家を学ぶことができます。

MongoDBデータベースパスワードを表示するNAVICATの方法 MongoDBデータベースパスワードを表示するNAVICATの方法 Apr 08, 2025 pm 09:39 PM

Hash値として保存されているため、Navicatを介してMongoDBパスワードを直接表示することは不可能です。紛失したパスワードを取得する方法:1。パスワードのリセット。 2。構成ファイルを確認します(ハッシュ値が含まれる場合があります)。 3.コードを確認します(パスワードをハードコードできます)。

CentosでGitLabのデータベースを選択する方法 CentosでGitLabのデータベースを選択する方法 Apr 14, 2025 pm 04:48 PM

gitlabデータベース展開ガイドcentosシステム適切なデータベースの選択は、gitlabを正常に展開するための重要なステップです。 GitLabは、MySQL、PostgreSQL、MongoDBなど、さまざまなデータベースと互換性があります。この記事では、これらのデータベースを選択して構成する方法を詳細に説明します。データベース選択の推奨MYSQL:広く使用されているリレーショナルデータベース管理システム(RDBMS)。安定したパフォーマンスを備えており、ほとんどのGitLab展開シナリオに適しています。 POSTGRESQL:強力なオープンソースRDBMSは、大規模なデータセットの処理に適した複雑なクエリと高度な機能をサポートしています。 Mongodb:人気のNoSQLデータベース、海の扱いが上手です

Centos Mongodbバックアップ戦略とは何ですか? Centos Mongodbバックアップ戦略とは何ですか? Apr 14, 2025 pm 04:51 PM

MongoDB効率的なバックアップ戦略の詳細な説明CENTOSシステムでは、この記事では、データセキュリティとビジネスの継続性を確保するために、CENTOSシステムにMongoDBバックアップを実装するためのさまざまな戦略を詳細に紹介します。 Dockerコンテナ環境でのマニュアルバックアップ、タイミング付きバックアップ、自動スクリプトバックアップ、バックアップメソッドをカバーし、バックアップファイル管理のベストプラクティスを提供します。マニュアルバックアップ:MongoDumpコマンドを使用して、マニュアルフルバックアップを実行します。たとえば、Mongodump-Hlocalhost:27017-U Username-P Password-Dデータベース名-O/バックアップディレクトリこのコマンドは、指定されたデータベースのデータとメタデータを指定されたバックアップディレクトリにエクスポートします。

MongoDBおよびリレーショナルデータベース:包括的な比較 MongoDBおよびリレーショナルデータベース:包括的な比較 Apr 08, 2025 pm 06:30 PM

MongoDBおよびリレーショナルデータベース:詳細な比較この記事では、NOSQLデータベースMongoDBと従来のリレーショナルデータベース(MySQLやSQLServerなど)の違いを詳細に調べます。リレーショナルデータベースは、行と列のテーブル構造を使用してデータを整理しますが、MongoDBは柔軟なドキュメント指向モデルを使用して、最新のアプリケーションのニーズをより適切に適しています。主にデータ構造を区別します。リレーショナルデータベースは、事前定義されたスキーマテーブルを使用してデータを保存し、テーブル間の関係は一次キーと外部キーを通じて確立されます。 MongoDBはJSONのようなBSONドキュメントを使用してコレクションに保存します。各ドキュメント構造は、パターンのないデザインを実現するために独立して変更できます。アーキテクチャデザイン:リレーショナルデータベースは、事前に定義された固定スキーマが必要です。 Mongodbサポート

Mongodbでユーザーをセットアップする方法 Mongodbでユーザーをセットアップする方法 Apr 12, 2025 am 08:51 AM

MongoDBユーザーを設定するには、次の手順に従ってください。1。サーバーに接続し、管理者ユーザーを作成します。 2。ユーザーアクセスを許可するデータベースを作成します。 3. CreateUserコマンドを使用してユーザーを作成し、その役割とデータベースアクセス権を指定します。 4. Getusersコマンドを使用して、作成されたユーザーを確認します。 5.オプションで、特定のコレクションに他のアクセス許可または付与ユーザーの権限を設定します。

Debian Mongodbでデータを暗号化する方法 Debian Mongodbでデータを暗号化する方法 Apr 12, 2025 pm 08:03 PM

DebianシステムでMongoDBデータベースを暗号化するには、次の手順に従う必要があります。ステップ1:MongoDBのインストール最初に、DebianシステムがMongoDBをインストールしていることを確認してください。そうでない場合は、インストールについては公式のMongoDBドキュメントを参照してください:https://docs.mongodb.com/manual/tutorial/install-mongodb-onedbian/-step 2:暗号化キーファイルを作成し、暗号化キーを含むファイルを作成し、正しい許可を設定します。

Mongodbに接続するためのツールは何ですか Mongodbに接続するためのツールは何ですか Apr 12, 2025 am 06:51 AM

Mongodbに接続するための主なツールは次のとおりです。1。Mongodbシェル、迅速な表示と簡単な操作の実行に適しています。 2。プログラミング言語ドライバー(Pymongo、Mongodb Javaドライバー、Mongodb node.jsドライバーなど)、アプリケーション開発に適していますが、使用方法をマスターする必要があります。 3。GUIツール(Robo 3T、Compassなど)は、初心者と迅速なデータ表示のためのグラフィカルインターフェイスを提供します。ツールを選択するときは、アプリケーションのシナリオとテクノロジースタックを検討し、接続プールやインデックスの使用などの接続文字列の構成、許可管理、パフォーマンスの最適化に注意する必要があります。

See all articles