2018-05-20MVC整体设计

2018-05-20  本文已影响0人  初学者hao
  1. 整体设计架构
graph LR
JSP-->Servlet;
Servlet-->DAO;
DAO-->MySQL;
Servlet-->JSP;
DAO-->Servlet;
MySQL-->DAO;

注意:

1. 不能跨层访问

2. 只能自上向下依赖,而不能自下向上依赖

  1. 多个请求对应一个Servlet
  1. Servlet映射为*.do:可以接受一切.do结尾的请求
    <servlet-mapping>
        <servlet-name>Servlet</servlet-name>
        <url-patten>*.do</url-patten>
    </servlet-mapping>
  1. 在Servletde doGet和doPost方法中:
    • 获取ServletPath:/*.do:String servletPath=request.getServletPath();
    • 去除/和.do,得到*的字符串:
    String methodName=servletPath.subString(1);
    methodName=methodName.subString(0),methodName.length()-3);
    
    • 利用反射获取methodName对应的方法
        
        Method method=getClass().getDeclaredMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
        //利用反射调用对应的方法
        method.invoke(this,request,response);
    
  1. 查询:MVC整个流程
  1. 模糊查询
  1. SQL: String sql="SELECT id,name,address,phone FROM customers WHERE "+"name LIKE ? AND address LIKE ? AND phone LIKE ?"
  2. 填充占位符:重写get方法
    public String getName(){
        if(name==null){
            name = "%";
        }else{
            name="%"+name+"%";
        }
        return name;
    }
  1. 把查询条件封装为一个JavaBean
上一篇 下一篇

猜你喜欢

热点阅读