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

InnoDBBufferPool通过缓存数据和索引页来减少磁盘I/O,提升数据库性能。其工作原理包括:1.数据读取:从BufferPool中读取数据;2.数据写入:修改数据后写入BufferPool并定期刷新到磁盘;3.缓存管理:使用LRU算法管理缓存页;4.预读机制:提前加载相邻数据页。通过调整BufferPool大小和使用多个实例,可以优化数据库性能。

MySQL与其他编程语言相比,主要用于存储和管理数据,而其他语言如Python、Java、C 则用于逻辑处理和应用开发。 MySQL以其高性能、可扩展性和跨平台支持着称,适合数据管理需求,而其他语言在各自领域如数据分析、企业应用和系统编程中各有优势。

MySQL值得学习,因为它是强大的开源数据库管理系统,适用于数据存储、管理和分析。1)MySQL是关系型数据库,使用SQL操作数据,适合结构化数据管理。2)SQL语言是与MySQL交互的关键,支持CRUD操作。3)MySQL的工作原理包括客户端/服务器架构、存储引擎和查询优化器。4)基本用法包括创建数据库和表,高级用法涉及使用JOIN连接表。5)常见错误包括语法错误和权限问题,调试技巧包括检查语法和使用EXPLAIN命令。6)性能优化涉及使用索引、优化SQL语句和定期维护数据库。

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


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

SublimeText3 Linux新版
SublimeText3 Linux最新版

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

禅工作室 13.0.1
功能强大的PHP集成开发环境

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。