Java商城秒杀系统有哪些关键总结点?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1311个文字,预计阅读时间需要6分钟。
原文:本文字写的较为零散,对没有基础的同行不太友好。
一、秒杀系统项目总结(基础版)
classpath 在.properties中时常需要读取资源,定位文件地址时经常用到,classpath 类路径指的的是src/main/jav文本结构较为松散,对基础薄弱者可能不太友好。
一、秒杀系统项目概述(基础版)
在.properties文件中,classpath常用于读取资源,定位文件地址。classpath即类路径,指的是src/main/jav目录。一、秒杀系统项目总结(基础版) classpath本文写的较为零散,对没有基础的同学不太友好。
在.properties中时常需要读取资源,定位文件地址时经常用到classpath
类路径指的是src/main/java,或者是src/main/resource下的路径。例如:resource 下的 classpath:mapping/*.xml,经常用于Mybatis中配置mapping文件地址。
Mybatis-generator在写项目中可以利用mybatis-generator进行一些机械性工作(在pom中引入),这里将配置文件中的一部分进行展示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="DB2Tables" targetRuntime="MyBatis3">
<!--数据库链接地址账号密码-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/库名" userId="sql_id" password="sql_password">
</jdbcConnection>
<!--生成DataObject类存放位置-->
<javaModelGenerator targetPackage="com.imooc.miaoshaproject.dataobject" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成映射文件存放位置-->
<sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--生成Dao类存放位置-->
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.imooc.miaoshaproject.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--生成对应表及类名-->
<!--
<table tableName="user_info" domainObjectName="UserDO" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="user_password" domainObjectName="UserPasswordDO" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"></table>
-->
<table tableName="promo" domainObjectName="PromoDO" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
在使用mybatis-generator之后要注意检查mapping中的文件,进行适当修改,比如Insert操作中声明自增和主键。
Spring异常拦截:- 如果对Spring程序没有进行异常处理,则遇到特定的异常会自动映射为指定的HTTP状态码,部分如下:
表中的异常一般会由Spring自身抛出,作为DispatcherServlet处理过程中或执行校验时出现问题的结果。如果DispatcherServlet无法找到适合处理请求的控制器方法,那么将会抛出NoSuchRequestHandlingMethodException异常,最终的结果就是产生404状态码的响应(Not Found)。
- 通过使用@ResponseStatus注解能将异常映射为特定的状态码:
//定义exceptionhandler解决未被controller层吸收的exception
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Object handlerException(HttpServletRequest request, Exception ex){
Map<String,Object> responseData = new HashMap<>();
if( ex instanceof BusinessException){
BusinessException businessException = (BusinessException)ex;
responseData.put("errCode",businessException.getErrCode());
responseData.put("errMsg",businessException.getErrMsg());
}else{
responseData.put("errCode", EmBusinessError.UNKNOWN_ERROR.getErrCode());
responseData.put("errMsg",EmBusinessError.UNKNOWN_ERROR.getErrMsg());
}
return CommonReturnType.create(responseData,"fail");
}
这里将响应200(OK)状态码,但是大多数时候,我们需要知道这个异常的具体信息,这就需要如上代码所示,加上 @ExceptionHandler(Exception.class),一旦捕捉到异常,则按handler流程运行。 如果需要一个contrller具有该异常处理,可以建立一个基类进行继承,不然需要每个controller都写一遍,这种方式较为麻烦。
一个Controller下多个@ExceptionHandler上的异常类型不能出现一样的,否则运行时抛异常.
-
@ControllerAdvice+@ExceptionHandler拦截异常并统一处理
@ExceptionHandler的作用主要在于声明一个或多个类型的异常,当符合条件的Controller抛出这些异常之后将会对这些异常进行捕获,然后按照其标注的方法的逻辑进行处理,从而改变返回的视图信息。
@ControllerAdvice public class GlobalExceptionHandler{ @ExceptionHandler(Exception.class) @ResponseBody public CommonReturnType doError(HttpServletRequest localhost:8090/order/createorder", data:{ "itemId":g_itemVO.id, "amount":1, "promoId":g_itemVO.promoId }, xhrFields:{withCredentials:true}, success:function(data){ if(data.status == "success"){ alert("下单成功"); window.location.reload(); }else{ alert("下单失败,原因为"+data.data.errMsg); if(data.data.errCode == 20003){ window.location.href="login.html"; } } }, error:function(data){ alert("下单失败,原因为"+data.responseText); } }); });后台下单:
nginx//封装下单请求 @RequestMapping(value = "/createorder",method = {RequestMethod.POST},consumes={CONTENT_TYPE_FORMED}) @ResponseBody public CommonReturnType createOrder(@RequestParam(name="itemId")Integer itemId, @RequestParam(name="amount")Integer amount, @RequestParam(name="promoId",required = false)Integer promoId) throws BusinessException { Boolean isLogin = (Boolean) blog.csdn.net/zhazhagu/article/details/81064406作为web服务器Nginx架构,通过修改nginx.conf来实现这个架构
location /resources/ { alias /usr/local/openresty/nginx/html/resources/; index index.html index.htm; }表明当访问路径命中了/resources之后,就把/resources/替换成
/usr/local/openresty/nginx/html/resources/
并将 /resources/后面的html资源拼接在后面
将所有的前端文件和static文件都移动到resources文件夹中
因为修改了配置文件,所以要重启nginx,nginx提供了无缝平滑重启(用户不会感知):
动静分离服务器将conf/nginx.conf进行配置:
upstream backend_server{ server 172.27.65.183 weight=1; server 172.16.162.179 weight=1; #两个应用服务器,权重均为1,则为轮询方式进行访问 } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location /resources/ { alias /usr/local/openresty/nginx/html/resources/; index index.html index.htm; } #新增 location / { proxy_pass backend_server; #当访问/路径时,将反向代理到backend_server上 proxy_set_header Host $backend_server; proxy_set_header Host $www.558idc.com/stgf.html转载请说明出处】
本文共计1311个文字,预计阅读时间需要6分钟。
原文:本文字写的较为零散,对没有基础的同行不太友好。
一、秒杀系统项目总结(基础版)
classpath 在.properties中时常需要读取资源,定位文件地址时经常用到,classpath 类路径指的的是src/main/jav文本结构较为松散,对基础薄弱者可能不太友好。
一、秒杀系统项目概述(基础版)
在.properties文件中,classpath常用于读取资源,定位文件地址。classpath即类路径,指的是src/main/jav目录。一、秒杀系统项目总结(基础版) classpath本文写的较为零散,对没有基础的同学不太友好。
在.properties中时常需要读取资源,定位文件地址时经常用到classpath
类路径指的是src/main/java,或者是src/main/resource下的路径。例如:resource 下的 classpath:mapping/*.xml,经常用于Mybatis中配置mapping文件地址。
Mybatis-generator在写项目中可以利用mybatis-generator进行一些机械性工作(在pom中引入),这里将配置文件中的一部分进行展示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="DB2Tables" targetRuntime="MyBatis3">
<!--数据库链接地址账号密码-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/库名" userId="sql_id" password="sql_password">
</jdbcConnection>
<!--生成DataObject类存放位置-->
<javaModelGenerator targetPackage="com.imooc.miaoshaproject.dataobject" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成映射文件存放位置-->
<sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--生成Dao类存放位置-->
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.imooc.miaoshaproject.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--生成对应表及类名-->
<!--
<table tableName="user_info" domainObjectName="UserDO" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="user_password" domainObjectName="UserPasswordDO" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"></table>
-->
<table tableName="promo" domainObjectName="PromoDO" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
在使用mybatis-generator之后要注意检查mapping中的文件,进行适当修改,比如Insert操作中声明自增和主键。
Spring异常拦截:- 如果对Spring程序没有进行异常处理,则遇到特定的异常会自动映射为指定的HTTP状态码,部分如下:
表中的异常一般会由Spring自身抛出,作为DispatcherServlet处理过程中或执行校验时出现问题的结果。如果DispatcherServlet无法找到适合处理请求的控制器方法,那么将会抛出NoSuchRequestHandlingMethodException异常,最终的结果就是产生404状态码的响应(Not Found)。
- 通过使用@ResponseStatus注解能将异常映射为特定的状态码:
//定义exceptionhandler解决未被controller层吸收的exception
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Object handlerException(HttpServletRequest request, Exception ex){
Map<String,Object> responseData = new HashMap<>();
if( ex instanceof BusinessException){
BusinessException businessException = (BusinessException)ex;
responseData.put("errCode",businessException.getErrCode());
responseData.put("errMsg",businessException.getErrMsg());
}else{
responseData.put("errCode", EmBusinessError.UNKNOWN_ERROR.getErrCode());
responseData.put("errMsg",EmBusinessError.UNKNOWN_ERROR.getErrMsg());
}
return CommonReturnType.create(responseData,"fail");
}
这里将响应200(OK)状态码,但是大多数时候,我们需要知道这个异常的具体信息,这就需要如上代码所示,加上 @ExceptionHandler(Exception.class),一旦捕捉到异常,则按handler流程运行。 如果需要一个contrller具有该异常处理,可以建立一个基类进行继承,不然需要每个controller都写一遍,这种方式较为麻烦。
一个Controller下多个@ExceptionHandler上的异常类型不能出现一样的,否则运行时抛异常.
-
@ControllerAdvice+@ExceptionHandler拦截异常并统一处理
@ExceptionHandler的作用主要在于声明一个或多个类型的异常,当符合条件的Controller抛出这些异常之后将会对这些异常进行捕获,然后按照其标注的方法的逻辑进行处理,从而改变返回的视图信息。
@ControllerAdvice public class GlobalExceptionHandler{ @ExceptionHandler(Exception.class) @ResponseBody public CommonReturnType doError(HttpServletRequest localhost:8090/order/createorder", data:{ "itemId":g_itemVO.id, "amount":1, "promoId":g_itemVO.promoId }, xhrFields:{withCredentials:true}, success:function(data){ if(data.status == "success"){ alert("下单成功"); window.location.reload(); }else{ alert("下单失败,原因为"+data.data.errMsg); if(data.data.errCode == 20003){ window.location.href="login.html"; } } }, error:function(data){ alert("下单失败,原因为"+data.responseText); } }); });后台下单:
nginx//封装下单请求 @RequestMapping(value = "/createorder",method = {RequestMethod.POST},consumes={CONTENT_TYPE_FORMED}) @ResponseBody public CommonReturnType createOrder(@RequestParam(name="itemId")Integer itemId, @RequestParam(name="amount")Integer amount, @RequestParam(name="promoId",required = false)Integer promoId) throws BusinessException { Boolean isLogin = (Boolean) blog.csdn.net/zhazhagu/article/details/81064406作为web服务器Nginx架构,通过修改nginx.conf来实现这个架构
location /resources/ { alias /usr/local/openresty/nginx/html/resources/; index index.html index.htm; }表明当访问路径命中了/resources之后,就把/resources/替换成
/usr/local/openresty/nginx/html/resources/
并将 /resources/后面的html资源拼接在后面
将所有的前端文件和static文件都移动到resources文件夹中
因为修改了配置文件,所以要重启nginx,nginx提供了无缝平滑重启(用户不会感知):
动静分离服务器将conf/nginx.conf进行配置:
upstream backend_server{ server 172.27.65.183 weight=1; server 172.16.162.179 weight=1; #两个应用服务器,权重均为1,则为轮询方式进行访问 } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location /resources/ { alias /usr/local/openresty/nginx/html/resources/; index index.html index.htm; } #新增 location / { proxy_pass backend_server; #当访问/路径时,将反向代理到backend_server上 proxy_set_header Host $backend_server; proxy_set_header Host $www.558idc.com/stgf.html转载请说明出处】

