最近在做基于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格式的字符。
呵呵,做个小总结,方便忘记了。

MySQL適合初學者學習數據庫技能。 1.安裝MySQL服務器和客戶端工具。 2.理解基本SQL查詢,如SELECT。 3.掌握數據操作:創建表、插入、更新、刪除數據。 4.學習高級技巧:子查詢和窗口函數。 5.調試和優化:檢查語法、使用索引、避免SELECT*,並使用LIMIT。

MySQL通過表結構和SQL查詢高效管理結構化數據,並通過外鍵實現表間關係。 1.創建表時定義數據格式和類型。 2.使用外鍵建立表間關係。 3.通過索引和查詢優化提高性能。 4.定期備份和監控數據庫確保數據安全和性能優化。

MySQL是一個開源的關係型數據庫管理系統,廣泛應用於Web開發。它的關鍵特性包括:1.支持多種存儲引擎,如InnoDB和MyISAM,適用於不同場景;2.提供主從復制功能,利於負載均衡和數據備份;3.通過查詢優化和索引使用提高查詢效率。

SQL用於與MySQL數據庫交互,實現數據的增、刪、改、查及數據庫設計。 1)SQL通過SELECT、INSERT、UPDATE、DELETE語句進行數據操作;2)使用CREATE、ALTER、DROP語句進行數據庫設計和管理;3)複雜查詢和數據分析通過SQL實現,提升業務決策效率。

MySQL的基本操作包括創建數據庫、表格,及使用SQL進行數據的CRUD操作。 1.創建數據庫:CREATEDATABASEmy_first_db;2.創建表格:CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY,titleVARCHAR(100)NOTNULL,authorVARCHAR(100)NOTNULL,published_yearINT);3.插入數據:INSERTINTObooks(title,author,published_year)VA

MySQL在Web應用中的主要作用是存儲和管理數據。 1.MySQL高效處理用戶信息、產品目錄和交易記錄等數據。 2.通過SQL查詢,開發者能從數據庫提取信息生成動態內容。 3.MySQL基於客戶端-服務器模型工作,確保查詢速度可接受。

構建MySQL數據庫的步驟包括:1.創建數據庫和表,2.插入數據,3.進行查詢。首先,使用CREATEDATABASE和CREATETABLE語句創建數據庫和表,然後用INSERTINTO語句插入數據,最後用SELECT語句查詢數據。

MySQL適合初學者,因為它易用且功能強大。 1.MySQL是關係型數據庫,使用SQL進行CRUD操作。 2.安裝簡單,需配置root用戶密碼。 3.使用INSERT、UPDATE、DELETE、SELECT進行數據操作。 4.複雜查詢可使用ORDERBY、WHERE和JOIN。 5.調試需檢查語法,使用EXPLAIN分析查詢。 6.優化建議包括使用索引、選擇合適數據類型和良好編程習慣。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

禪工作室 13.0.1
強大的PHP整合開發環境

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Dreamweaver CS6
視覺化網頁開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境