Ajax请求之后台处理

Dazzlingwinter Dazzlingwinter 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
项目中需要用到一些Ajax来做一些异步的请求处理,主要是多级联动,后台的代码怎么写效率更高一些呢?
后台框架SpringMVC+Hibernate

//web:
//AjaxSelectRequestController
public class AjaxSelectRequestControllerimplements Controller{
private AjasSelectRequestService ajaxselectrequestservice;
public void setAjaxselectrequestservicee(AjasSelectRequestService ajaxselectrequestservice) {
this.ajaxselectrequestservice = ajaxselectrequestservice;
}
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("UTF-8");
String type = request.getParameter("type");
String id = request.getParameter("id");
String xml_start = "<selects>";
String xml = ajaxselectrequestservice.getXmlByTypeAndIDService(type, id);
String xml_end = "</selects>";

String last_xml = xml_start + xml + xml_end;
response.getWriter().write(last_xml);
return null;
}
}
//service
//方案一:
//AjasSelectRequestServiceImpl
public AjasSelectRequestServiceImpl implements AjasSelectRequestService {
private AjaxSelectRequestDAO ajaxrequestdao;
public String getXmlByTypeAndIDService(String type, String id){
if(Strutils.isStringNotNull(type) && Strutils.isStringNotNull(id)) {
if("province".equals(type)) {
AjasSelectRequestServiceImpl
public AjasSelectRequestServiceImpl implements AjasSelectRequestService {
private AjaxSelectRequestDAO ajaxrequestdao;
public String getXmlByTypeAndIDService(String type, String id){
if(Strutils.isStringNotNull(type) && Strutils.isStringNotNull(id)) {
if("province".equals(type)) {
ajaxrequestdao...
}
if("city".equals(type)) {
ajaxrequestdao ...
}
if("area".equals(type)) {
ajaxrequestdao...
}
if("school".equals(type)) {
ajaxrequestdao ...
}
if("college".equals(type)) {
ajaxrequestdao...
}
if("major".equals(type)) {
ajaxrequestdao...
}
if("class".equals(type)) {
ajaxrequestdao...
}
...
}
}
}
//方案二:
AjasSelectRequestServiceImpl
public AjasSelectRequestServiceImpl implements AjasSelectRequestService {
private ProvinceManageDAO provincemanagedao;
private CityManageDAO citymanagedao;
private AreaManageDAO areamanagedao;
private SchoolManageDAO schoolmanagedao;
private CollegeManageDAO collegemanagedao;
private MajorManageDAO majormanagedao;
private ClassManageDAO classmanagedao;
public String getXmlByTypeAndIDService(String type, String id){
if(Strutils.isStringNotNull(type) && Strutils.isStringNotNull(id)) {
if("province".equals(type)) {
provincemanagedao ...
}
if("city".equals(type)) {
citymanagedao...
}
if("area".equals(type)) {
areamanagedao...
}
if("school".equals(type)) {
schoolmanagedao...
}
if("college".equals(type)) {
collegemanagedao...
}
if("major".equals(type)) {
majormanagedao...
}
if("class".equals(type)) {
classmanagedao...
}
...
}
}
}

方案一用AjaxSelectRequestDAO为AjaxSelectRequestServiceImpl提供数据访问,并且实现了依据请求类型request和请求id对不同数据库表的访问,并返回对应的List;
方案二用AjaxSelectRequestServiceImpl持有了各类请求的DAO数据库访问各自的一个对象,通过不同的对象为提供不同的数据库访问服务;
个人的一些看法:
方案一仅持有一个数据库访问层的访问对象,对于频繁的Ajax访问请求,处理时创建的对象少一些,至少创建的DAO引用少,但是他破坏了数据访问的底层完整性,也就是说,数据库不同类型的访问应有不同的DAO来处理
方案二保持了数据库底层访问的访问完整性,但对于频繁Ajax处理,会力不从心,虽然Spring会做好持有对象的创建工作,可是对于仅仅一个Ajax请求,就创建了N多个DAO的引用,用完就被废弃,如果不及时垃圾回收,down掉都有可能
彰显智慧火花,说出你的想法,大家共分享,齐进步~~~
...全文
评论 3
收藏 已收藏
分享
返回顶部
Dazzlingwinter
Web 开发 81,108 成员 341,721 内容