mvc目录结构
模块内分mvc层,mvc层内分模块
eg:下面介绍以功能模块(model)进行mvc分层,类的命名以功能模块名字Dao.java
命名
dao
层
ModelDao.java文件结构如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.xuan.goods.model.dao;
import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import cn.itcast.jdbc.TxQueryRunner; import com.xuan.goods.model.domain.Model;
public class ModelDao{ private QueryRunner qr=new TxQueryRunner();
public Model findByName(String name)throws SQLException{ String sql="select result from model where name=?"; return qr.query(sql, new BeanHandler<Model>(Model.class),name); } }
|
domain
层
Model.java文件结构如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.xuan.goods.model.domain;
public class Model{ private String uid; private String name; public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; }
public String getName() { return name; } public void setName(String name) { this.name = name; } }
|
service
层
ModelService.java文件结构如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.xuan.goods.user.service;
public class ModelService{ private ModelDao modelDao=new ModelDao();
public Model findModel(String name) throw ModelException{ Model model=modelDao.findByName(name); if (model=null){ throw new ModelException("未找到模块!"); } try{ return model; }catch(SQLException e){ throw new RuntimeException(e); } } }
|
web
层
servlet 包下ModelServlet.java文件结构如下
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.xuan.goods.model.web.servlet;
public class ModelServlet extends BaseServlet{ private ModelService modelService=new ModelService(); public string findName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String name = req.getParameter("name"); Model model = modelService.findModel(name); req.setAttribute("model", model); return "f:/jsps/msg.jsp"; }
|
代码见goods
项目