MongoDB: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; }
采用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格式的字符。
呵呵,做个小总结,方便忘记了。

ホットAIツール

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

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

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

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

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

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

ホットトピック









.NET 4.0 はさまざまなアプリケーションの作成に使用され、オブジェクト指向プログラミング、柔軟性、強力なアーキテクチャ、クラウド コンピューティングの統合、パフォーマンスの最適化、広範なライブラリ、セキュリティ、スケーラビリティ、データ アクセス、モバイルなどの豊富な機能をアプリケーション開発者に提供します。開発サポート。

この記事では、自動拡張を実現するためにDebianシステムでMongodbを構成する方法を紹介します。主な手順には、Mongodbレプリカセットとディスクスペース監視のセットアップが含まれます。 1。MongoDBのインストール最初に、MongoDBがDebianシステムにインストールされていることを確認してください。次のコマンドを使用してインストールします。sudoaptupdatesudoaptinstinstall-yymongodb-org2。mongodbレプリカセットMongodbレプリカセットの構成により、自動容量拡張を達成するための基礎となる高可用性とデータ冗長性が保証されます。 Mongodbサービスを開始:Sudosystemctlstartmongodsudosys

この記事では、Debianシステムで非常に利用可能なMongoDBデータベースを構築する方法について説明します。データのセキュリティとサービスが引き続き動作し続けるようにするための複数の方法を探ります。キー戦略:レプリカセット:レプリカセット:レプリカセットを使用して、データの冗長性と自動フェールオーバーを実現します。マスターノードが失敗すると、レプリカセットが自動的に新しいマスターノードを選択して、サービスの継続的な可用性を確保します。データのバックアップと回復:MongoDumpコマンドを定期的に使用してデータベースをバックアップし、データ損失のリスクに対処するために効果的な回復戦略を策定します。監視とアラーム:監視ツール(プロメテウス、グラファナなど)を展開して、MongoDBの実行ステータスをリアルタイムで監視し、

プロジェクト要件に基づいて最適な Java フレームワークを選択するには、次のことが必要です。 プロジェクト要件の決定 (アプリケーションの種類、ユーザー数、必要な機能、パフォーマンス要件) 利用可能なフレームワークの調査 (Spring、SpringBoot、Hibernate、JakartaEE など) フレームワークの評価 (機能セット、ユーザー コミュニティ、パフォーマンス、継続的なサポート)

Spring Framework 5.0 には、次の新機能が導入されています。 WebFlux サポート: 高性能 Web アプリケーションを構築するためのリアクティブ プログラミングのサポートを提供します。アノテーション主導の構成: アノテーションを使用して、Bean とコンポーネントの構成を簡素化します。 SpringDataReactive サポート: リアクティブ データベース アクセスを拡張して、データベースとの対話を簡素化します。 Java 構成でのプロセス式: 構成で動的式を使用できるようにします。例外処理の改善: 未処理の例外を適切に処理するための共通メカニズムを提供します。

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

Pinetworkは、革新的なモバイルバンキングプラットフォームであるPibankを立ち上げようとしています! Pinetworkは本日、Pibankと呼ばれるElmahrosa(Face)Pimisrbankのメジャーアップデートをリリースしました。これは、従来の銀行サービスと、フィアット通貨の原子交換と暗号通貨の原子交換を実現します(resuptocursisを使用するなど、聖職者のような聖職者など、 DC)。ピバンクの魅力は何ですか?見つけましょう!ピバンクの主な機能:銀行口座と暗号通貨資産のワンストップ管理。リアルタイムトランザクションをサポートし、生物種を採用します

Spring フレームワークに関する一般的な問題: Bean を注入できない: Bean が登録され、正しく定義され、依存関係が登録され、スコープが正しいことを確認してください。循環依存関係: 遅延初期化には @Lazy アノテーションを使用し、factorybean メソッドを使用し、AspectJ 自動プロキシの使用を検討してください。構成エラー: コンテキスト ファイル、Bean 登録、Bean プロパティ設定を確認してください。パフォーマンスの問題: パフォーマンス分析ツールを使用してボトルネックを特定し、不要な Bean の作成を回避し、キャッシュを使用し、メモリ使用量を監視します。トランザクション管理の問題: トランザクション マネージャーを正しく構成し、トランザクション伝播動作を設定し、トランザクション例外を処理し、ロールバック メカニズムを提供します。
