java web中分层MVC的意义

在web编程中,由于高内聚、低耦合的特点,需要将多个类实现多层,大致有以下几层:
①entity,实体类,如user,role等,这些类里边包含了私有属性和公共的get、set方法这和数据库中的表相对应,更严格一些,包括字段的顺序和type都要保持一致。
②base,封装了基本的操作数据库的方法(增删改查)
③dao,访问并操作数据库,若想从数据库获取数据,必须调用dao层。dao层没有一点业务逻辑,全是跟数据库操作有关的类
④service,业务逻辑层,里边可以是接口。只要没有访问数据库的操作,都写到service层中。

举个例子说明什么是业务。如做一个分页功能,数据1000条,每页20条,则可以把这个功能写成工具类封装起来,然后在业务层调用这个封装的方法,这才是业务层要做的事。

service层和dao层有什么区别?
再举个例子,如注册用户时,还想往日志表里添加一个日志,那么就要在业务层来实现这个操作,并对该操作添加事务,效果会更好。若不使用service,只用dao的话,那么就要连续使用userDao和logDao,那么万一某一步出错,就可能造成user加进去而log没有添加成功。

⑤action,也就是原来的servlet,位于jsp和service层之间进行交互。主要的操作就是请求(request)和响应(response)。存取某个功能的整体实现方法。

以后不要在servlet中调用dao层,而要写到serviceImpl(对接口的实现层)中,如以前的userServlet中有
private UserDao udao = new UserDaoImpl();//private必须写
以后这一句要写到UserServiceImpl内。

⑥vo,数据传输层,用来转入转出数据,跟前台有关。
⑦util,工具类,用来封装一些公用的方法,如字符串的处理,字符集过滤等。

⑧impl,用来实现接口。一个接口可以有多种实现,用来满足不同需要。

一般的调用情况如下:

贴一张很重要的图,大致说明了各层的继承和implements关系

举一个小项目的例子,实现往数据库增删改查用户。缩略图如下:

其中User.java和UserVO.java现在看起来内容相同,区别在于User类是底层数据,一般不轻易改变。UserVO层业余数据库中内容相对应,将DB中检索出数据,或者要往DB中反映的数据保存在vo实例中。以后若想再有什么扩展,直接在VO层改就行,不用动底层代码。

这里边,UserDao,BaseDao,UserService都是接口类,相应的impl则是对其的实现。

1.BaseDao代码:

[java]  view plain  copy
 
  1. import java.util.List;  
  2. /** 
  3.  * 基本的方法 
  4.  * @author Administrator 
  5.  * 
  6.  */  
  7. public interface BaseDao<Entity> {  
  8.   
  9.     /** 
  10.      * 保存 
  11.      * @param obj 
  12.      * @throws Exception 
  13.      */  
  14.     void save(Entity entity) throws Exception;  
  15.     /** 
  16.      * 删除 
  17.      * @param id 
  18.      * @throws Exception 
  19.      */  
  20.     void delete(int id) throws Exception;  
  21.     /** 
  22.      * 更新 
  23.      * @param obj 
  24.      * @throws Exception 
  25.      */  
  26.     void update(Entity entity) throws Exception ;  
  27.     /** 
  28.      * 根据id查询指定记录 
  29.      * @param id 
  30.      * @return 
  31.      * @throws Exception 
  32.      */  
  33.     Entity findById(int id) throws Exception;  
  34.     /** 
  35.      * 查询所有记录 
  36.      * @return 
  37.      * @throws Exception 
  38.      */  
  39.     List<Entity> findAll() throws Exception;  
  40.       
  41.       
  42. }  

2.BaseDaoImpl代码,利用反射获取数据库数据

[java]  view plain  copy
 
  1. import java.lang.reflect.Field;  
  2. import java.lang.reflect.Method;  
  3. import java.lang.reflect.ParameterizedType;  
  4. import java.sql.Connection;  
  5. import java.sql.PreparedStatement;  
  6. import java.sql.ResultSet;  
  7. import java.util.ArrayList;  
  8. import java.util.List;  
  9.   
  10. import com.bjsxt.util.DBUtil;  
  11.   
  12. public class BaseDaoImpl<Entity> implements BaseDao<Entity> {  
  13.       
  14.     protected Class clazz;  
  15.       
  16.     public BaseDaoImpl(){  
  17.         ParameterizedType pz = (ParameterizedType) this.getClass().getGenericSuperclass();  
  18.         //利用反射返回当前类的父类的类型,并强制转换  
  19.         clazz = (Class) pz.getActualTypeArguments()[0];  
  20.         //返回此类型实际类型参数的Type对象数组的第一个  
  21.         //这两步的作用就是获取父类泛型参数的实际类型  
  22.         System.out.println(clazz + "这是测试代码,打印出来的");  
  23.     }  
  24.       
  25.       
  26.     @Override  
  27.     public void save(Entity entity) throws Exception {  
  28.         Connection conn = DBUtil.getConn();//连接数据库  
  29.         String sql = "insert into " + clazz.getSimpleName() + " values(null";  
  30.         Field[] fs = clazz.getDeclaredFields();   
  31.         for (int i = 0; i < fs.length; i++) {  
  32.             sql += ",?";  
  33.         }  
  34.         sql = sql + ")";  
  35.         PreparedStatement ps = DBUtil.getPs(conn, sql);  
  36.         //user.setUsername(rs.getString("username"));  
  37.         for (int i = 0; i < fs.length; i++) {  
  38.             String methodName = "get" + Character.toUpperCase(fs[i].getName().charAt(0)) + fs[i].getName().substring(1);  
  39.             //假如有个实体user类,含有uname属性,则获取uname的方法默认会为getUname(),会将uname的首字母大写,其余不变  
  40.             //Character.toUpperCase就是将字符大写,然后将除首字母以外的截取出来,拼到一起,构成类似于getUname()的方法  
  41.             //user.setUsername(uForm.getUsername);    
  42.             //该处用get是因为要把对象保存进数据库里,用get获取属性  
  43.             Method m = clazz.getDeclaredMethod(methodName);  
  44.             ps.setObject(i,m.invoke(entity));  
  45.             //setObject用来给"?"赋值。invoke则是在不知道具体类的情况下利用字符串去调用方法  
  46.             //正常情况下是user.getUsername,m.invoke(entity)中m是一个方法,类似于getUsername(),entity相当于user.  
  47.             //invoke是从实体里边找方法,那句话的意思就是从实体里边调用方法  
  48.         }  
  49.         ps.executeUpdate();  
  50.         DBUtil.close(ps);  
  51.         DBUtil.close(conn);  
  52.     }  
  53.   
  54.     @Override  
  55.     public void view(Entity entity) throws Exception {  
  56.         // TODO Auto-generated method stub  
  57.           
  58.     }  
  59.   
  60.     @Override  
  61.     public void update(Entity entity) throws Exception {  
  62.         Connection conn = DBUtil.getConn();  
  63.         String sql = "update " + clazz.getSimpleName() + "set";  
  64.         Field[] fs = clazz.getDeclaredFields();  
  65.         for (int i = 0; i < fs.length; i++) {  
  66.             sql += fs[i].getName() + " = ?,";  
  67.         }  
  68.         sql = sql.substring(0,sql.length()-1) + " where id = ?"; //减去最后的逗号  
  69.         PreparedStatement ps = DBUtil.getPs(conn, sql);  
  70.         for (int i = 0; i < fs.length; i++) {  
  71.             String methodName = "get" + Character.toUpperCase(fs[i].getName().charAt(0)) + fs[i].getName().substring(1);  
  72.             Method m = clazz.getDeclaredMethod(methodName);  
  73.             ps.setObject(i, m.invoke(entity));  
  74.         }  
  75.         Method getId = clazz.getDeclaredMethod("getId");//更新需要知道id  
  76.         ps.setInt(fs.length, (Integer) getId.invoke(entity));//因为id在sql语句最后一个问号处,所以要用fs.length  
  77.         ps.executeUpdate();  
  78.         DBUtil.close(ps);  
  79.         DBUtil.close(conn);  
  80.           
  81.     }  
  82.   
  83.     @Override  
  84.     public void delete(int id) throws Exception {  
  85.         Connection conn = DBUtil.getConn();  
  86.         String sql = "delete from " + clazz.getSimpleName() + " where id = ?" + id;  
  87.         PreparedStatement ps = DBUtil.getPs(conn, sql);  
  88.         ps.executeUpdate();  
  89.         DBUtil.close(ps);  
  90.         DBUtil.close(conn);  
  91.     }  
  92.   
  93.     @Override  
  94.     public Entity findById(int id) throws Exception {  
  95.         Connection conn = DBUtil.getConn();  
  96.         String sql = "select * from " + clazz.getSimpleName() + " where id= ? ";  
  97.         PreparedStatement ps = DBUtil.getPs(conn, sql);  
  98.         ps.setInt(1,id);  
  99.         ResultSet rs = ps.executeQuery();  
  100.         Entity entity = (Entity) clazz.newInstance();//利用无参构造函数新建实例  
  101.         Field[] fs = clazz.getDeclaredFields();  
  102.         if(rs.next()){  
  103.             for (int i = 0; i < fs.length; i++) {  
  104.                 String methodName = "set" + Character.toUpperCase(fs[i].getName().charAt(0)) + fs[i].getName().substring(1);  
  105.                 Method m = clazz.getDeclaredMethod(methodName, fs[i].getType());  
  106.                 m.invoke(entity, rs.getObject(fs[i].getName()));  
  107.             }  
  108.         }  
  109.         DBUtil.close(rs);  
  110.         DBUtil.close(ps);  
  111.         DBUtil.close(conn);  
  112.           
  113.         return entity;  
  114.     }  
  115.   
  116.     @Override  
  117.     public List<Entity> findAll() throws Exception {  
  118.         Connection conn = DBUtil.getConn();  
  119.         String sql = "select * from " + clazz.getSimpleName();  
  120.         PreparedStatement ps = DBUtil.getPs(conn, sql);  
  121.         ResultSet rs = ps.executeQuery();  
  122.         List<Entity> enList = new ArrayList<Entity>();  
  123.         while(rs.next()){  
  124.             Entity en = (Entity) clazz.newInstance();  
  125.             //user.setUsername(rs.getString("username"));  
  126.             Field[] fs = clazz.getDeclaredFields();  
  127.             for (int i = 0; i < fs.length; i++) {  
  128.                 String methodName = "set" + Character.toUpperCase(fs[i].getName().charAt(0)) + fs[i].getName().substring(1);  
  129.                 Method m = clazz.getDeclaredMethod(methodName, fs[i].getType());  
  130.                 m.invoke(en, rs.getObject(fs[i].getName()));  
  131.             }  
  132.             enList.add(en);  
  133.         }  
  134.         DBUtil.close(rs);  
  135.         DBUtil.close(ps);  
  136.         DBUtil.close(conn);  
  137.         return enList;  
  138.     }  
  139.       
  140. }  

注意:String methodName在何处用get,何处用set

3.UserDao代码

[java]  view plain  copy
 
  1. import java.util.List;  
  2.   
  3. import com.bjsxt.base.BaseDao;  
  4. import com.bjsxt.entity.User;  
  5.   
  6. public interface UserDao  extends BaseDao<User>{  
  7.   
  8.     List<User> findByPageList(int currentPage, int pageSize) throws Exception;  
  9.   
  10.     Long getTotal() throws Exception;  
  11.   
  12. }  


4.UserDaoImpl代码

[java]  view plain  copy
 
  1. import java.sql.Connection;  
  2. import java.sql.PreparedStatement;  
  3. import java.sql.ResultSet;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6.   
  7. import sun.nio.cs.US_ASCII;  
  8.   
  9. import com.bjsxt.base.BaseDaoImpl;  
  10. import com.bjsxt.dao.UserDao;  
  11. import com.bjsxt.entity.User;  
  12. import com.bjsxt.util.DBUtils;  
  13.   
  14. public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao{  
  15.   
  16.     @Override  
  17.     public List<User> findByPageList(int currentPage, int pageSize)  
  18.             throws Exception {  
  19.         Connection conn = DBUtils.getConn();  
  20.         String sql = "select * from user limit " + (currentPage-1)*pageSize + "," +pageSize;  
  21.         PreparedStatement ps = DBUtils.getPs(conn, sql);  
  22.         ResultSet rs = ps.executeQuery();  
  23.         List<User> userlist =new ArrayList<User>();  
  24.         while(rs.next()){  
  25.             User user = new User();  
  26.             user.setId(rs.getInt("id"));  
  27.             user.setUsername(rs.getString("username"));  
  28.             user.setPassword(rs.getString("password"));  
  29.             user.setAge(rs.getInt("age"));  
  30.             user.setSex(rs.getString("sex"));  
  31.             user.setBirthday(rs.getString("birthday"));  
  32.             user.setSalary(rs.getString("salary"));  
  33.             user.setDescription(rs.getString("description"));  
  34.             userlist.add(user);  
  35.         }  
  36.         DBUtils.close(rs);  
  37.         DBUtils.close(ps);  
  38.         DBUtils.close(conn);  
  39.         return userlist;  
  40.     }  
  41.   
  42.     @Override  
  43.     public Long getTotal() throws Exception {  
  44.         Connection conn = DBUtils.getConn();  
  45.         String sql = "select count(*) from user";  
  46.         PreparedStatement ps = DBUtils.getPs(conn, sql);      
  47.         ResultSet rs =ps.executeQuery();  
  48.         if(rs.next()){  
  49.             return ((Number)rs.getInt(1)).longValue();  
  50.         }  
  51.         return ((Number)0).longValue();  
  52.     }  
  53.   
  54. }  

5.User代码,下边的UserVO先和它一样,就不贴了。

[java]  view plain  copy
 
  1. public class User {  
  2.   
  3.     private int id ;   
  4.     private String username ;   
  5.     private String password ;   
  6.     private int age ;   
  7.     private String sex ;   
  8.     private String birthday ;   
  9.     private String salary ;   
  10.     private String description ;  
  11.       
  12.       
  13.     public User() {  
  14.         super();  
  15.         // TODO Auto-generated constructor stub  
  16.     }  
  17.     public User(int id, String username, String password, int age, String sex,  
  18.             String birthday, String salary, String description) {  
  19.         super();  
  20.         this.id = id;  
  21.         this.username = username;  
  22.         this.password = password;  
  23.         this.age = age;  
  24.         this.sex = sex;  
  25.         this.birthday = birthday;  
  26.         this.salary = salary;  
  27.         this.description = description;  
  28.     }  
  29.     public int getId() {  
  30.         return id;  
  31.     }  
  32.     public void setId(int id) {  
  33.         this.id = id;  
  34.     }  
  35.     public String getUsername() {  
  36.         return username;  
  37.     }  
  38.     public void setUsername(String username) {  
  39.         this.username = username;  
  40.     }  
  41.     public String getPassword() {  
  42.         return password;  
  43.     }  
  44.     public void setPassword(String password) {  
  45.         this.password = password;  
  46.     }  
  47.     public int getAge() {  
  48.         return age;  
  49.     }  
  50.     public void setAge(int age) {  
  51.         this.age = age;  
  52.     }  
  53.     public String getSex() {  
  54.         return sex;  
  55.     }  
  56.     public void setSex(String sex) {  
  57.         this.sex = sex;  
  58.     }  
  59.     public String getBirthday() {  
  60.         return birthday;  
  61.     }  
  62.     public void setBirthday(String birthday) {  
  63.         this.birthday = birthday;  
  64.     }  
  65.     public String getSalary() {  
  66.         return salary;  
  67.     }  
  68.     public void setSalary(String salary) {  
  69.         this.salary = salary;  
  70.     }  
  71.     public String getDescription() {  
  72.         return description;  
  73.     }  
  74.     public void setDescription(String description) {  
  75.         this.description = description;  
  76.     }   
  77.       
  78. }  

6.UserService代码,只提供接口,不实现任何方法,只让UserServiceImpl来实现

[javascript]  view plain  copy
 
  1. import java.util.List;  
  2.   
  3. import com.bjsxt.util.DataGrid;  
  4. import com.bjsxt.vo.UserVO;  
  5.   
  6. public interface UserService {  
  7.       
  8.     void saveUser(UserVO userVO) throws Exception ;   
  9.       
  10.     void deleteUser(int id) throws Exception ;  
  11.       
  12.     void updateUser(UserVO userVO) throws Exception ;  
  13.       
  14.     UserVO findById(int id) throws Exception ;   
  15.       
  16.     List<UserVO> findAll() throws Exception ;  
  17.       
  18.     DataGrid findByPage(int currentPage , int PageSize) throws Exception ;  
  19.       
  20.       
  21. }  


7.UserServiceImpl代码,方法还没完全实现,只完成一种。

[java]  view plain  copy
 
  1. import java.nio.channels.DatagramChannel;  
  2. import java.util.ArrayList;  
  3. import java.util.Iterator;  
  4. import java.util.List;  
  5.   
  6. import com.bjsxt.dao.UserDao;  
  7. import com.bjsxt.dao.impl.UserDaoImpl;  
  8. import com.bjsxt.entity.User;  
  9. import com.bjsxt.service.UserService;  
  10. import com.bjsxt.util.DataGrid;  
  11. import com.bjsxt.vo.UserVO;  
  12.   
  13. public class UserServiceImpl implements UserService {  
  14.       
  15.       
  16.     private UserDao userDao  = new UserDaoImpl();  
  17.   
  18.     @Override  
  19.     public void saveUser(UserVO userVO) throws Exception {  
  20.           
  21.     }  
  22.   
  23.     @Override  
  24.     public void deleteUser(int id) throws Exception {  
  25.           
  26.     }  
  27.   
  28.     @Override  
  29.     public void updateUser(UserVO userVO) throws Exception {  
  30.           
  31.     }  
  32.   
  33.     @Override  
  34.     public UserVO findById(int id) throws Exception {  
  35.         return null;  
  36.     }  
  37.   
  38.     @Override  
  39.     public List<UserVO> findAll() throws Exception {  
  40.         return null;  
  41.     }  
  42.   
  43.     @Override  
  44.     public DataGrid findByPage(int currentPage, int PageSize)  
  45.             throws Exception {  
  46.         List<UserVO> uservolist = new ArrayList<UserVO>();  
  47.         List<User> userList = this.userDao.findByPageList(currentPage ,PageSize);  
  48.         for (Iterator iterator = userList.iterator(); iterator.hasNext();) {  
  49.             User user = (User) iterator.next();  
  50.             UserVO uvo = new UserVO();  
  51.             uvo.setId(user.getId());  
  52.             uvo.setUsername(user.getUsername());  
  53.             uvo.setPassword(user.getPassword());  
  54.             uvo.setAge(user.getAge());  
  55.             uvo.setBirthday(user.getBirthday());  
  56.             uvo.setSalary(user.getSalary());  
  57.             uvo.setSex(user.getSex());  
  58.             uvo.setDescription(user.getDescription());  
  59.             uservolist.add(uvo);  
  60.         }  
  61.           
  62.         Long total = this.userDao.getTotal();  
  63.         DataGrid datagrid = new DataGrid();  
  64.         datagrid.setRows(uservolist);  
  65.         datagrid.setTotal(total);  
  66.           
  67.         return datagrid;  
  68.     }  
  69.   
  70. }  

8.DataGrid代码

[java]  view plain  copy
 
  1. import java.util.List;  
  2.   
  3. public class DataGrid {  
  4.   
  5.     private Long total ;    //总记录数  
  6.     private List rows  ;    //返回的数据条目  
  7.     public Long getTotal() {  
  8.         return total;  
  9.     }  
  10.     public void setTotal(Long total) {  
  11.         this.total = total;  
  12.     }  
  13.     public List getRows() {  
  14.         return rows;  
  15.     }  
  16.     public void setRows(List rows) {  
  17.         this.rows = rows;  
  18.     }  
  19.       
  20.       
  21. }  


9.DBUtil代码

[java]  view plain  copy
 
  1. import java.sql.Connection;  
  2. import java.sql.DriverManager;  
  3. import java.sql.PreparedStatement;  
  4. import java.sql.ResultSet;  
  5. import java.sql.SQLException;  
  6.   
  7. /** 
  8.  * 链接数据库的 
  9.  * @author Administrator 
  10.  * 
  11.  */  
  12. public class DBUtils {  
  13.   
  14.     /** 
  15.      * 获得connection对象 
  16.      * @return 
  17.      */  
  18.     public static  Connection getConn(){  
  19.         Connection conn = null;  
  20.         try {  
  21.             Class.forName("com.mysql.jdbc.Driver");  
  22.             conn = DriverManager.getConnection("jdbc:mysql://localhost/ext_001", "root", "root");  
  23.         } catch (ClassNotFoundException e) {  
  24.             e.printStackTrace();  
  25.         } catch (SQLException e) {  
  26.             // TODO Auto-generated catch block  
  27.             e.printStackTrace();  
  28.         }  
  29.         return conn;  
  30.     }  
  31.       
  32.     /** 
  33.      * 预编译sql 
  34.      * @param conn 
  35.      * @param sql 
  36.      * @return 
  37.      */  
  38.     public static PreparedStatement getPs(Connection conn , String sql){  
  39.         PreparedStatement ps = null;  
  40.         try {  
  41.             ps = conn.prepareStatement(sql);  
  42.         } catch (SQLException e) {  
  43.             e.printStackTrace();  
  44.         }  
  45.         return ps;  
  46.     }  
  47.       
  48.     /** 
  49.      * 一系列关闭对象的close 
  50.      * @param conn 
  51.      */  
  52.       
  53.       
  54.     public static void close(Connection conn){  
  55.         if(conn != null){  
  56.             try {  
  57.                 conn.close();  
  58.             } catch (SQLException e) {  
  59.                 e.printStackTrace();  
  60.             }  
  61.         }  
  62.     }  
  63.       
  64.     public static  void close(PreparedStatement ps){  
  65.         if(ps != null){  
  66.             try {  
  67.                 ps.close();  
  68.             } catch (SQLException e) {  
  69.                 e.printStackTrace();  
  70.             }  
  71.         }         
  72.     }  
  73.       
  74.     public static  void close(ResultSet rs){  
  75.         if(rs != null){  
  76.             try {  
  77.                 rs.close();  
  78.             } catch (SQLException e) {  
  79.                 e.printStackTrace();  
  80.             }  
  81.         }         
  82.     }  
  83. }  

最后粘一篇百度知道上的内容,关于service层和dao层的,供参考。

首先,service层会很大,方法很多。 
第二,试想,所有DAO都有增删改查四个基本方法。假设在DAO层(如StudentDAO.java)里student.add(),student.delete().student.update(),student.query()。
而如果应用增加了service层(如ApplictionServiceImpl.java),需要在service层中加上 
applicationService.addStudent(),deleteStudent(),updateStudent(),queryStudent()四个方法。
这时你需要更新service层的接口类ApplicationService.Java,加上这四个方法。然后再更新service层的实现类ApplicationServiceImpl,加上这四个方法,最后你发现这四个方法每个方法里都只有一句话(以addStudent()为例)
public int addStudent(Student student){ 
    return student.add(); 

这样是不是太傻了点,还不如在action中直接调用StudentDAO中的student.add()。 
以上是我的个人看法,请各位指点





这么来讲好了 按你所说 在action中直接调用StudentDAO中的student.add()。 现在有客户A 客户B 

客户A的需求是 addStudent 这个和你所说的情况一致

客户B的需求是 addStudent 前去加一个动作 将学生父母的信息也插入数据库

这时如果按照只调用DAO的方法的话,你需要从新建立一个action 
CustomerBAction 
再重新写一个DAO 因为这个DAO里要有添加父母的方法 student.addStudentAndParentInfo()。 

CustomerBAction 去调用 student.addStudentAndParentInfo()。 

这样加大了很多工作量

如果中间有service层的话 action始终用一个action
而调用的也是service接口 只要对接口注入不同的实现就可以满足 不同客户的需求了

知识要活学活用,要按照自己项目以后的发展趋势来搭设环境,别人家说什么就用什么。其实有时候javabean+jsp 也很好用啊,因为jsp不用重启服务 开发速度很快。我做小项目就是这种模式,以后小项目的更改也会很少,所以不用搭建的过于复杂。

补充:

entity和vo的不同还可以通过下边这个例子说明。如有user.java和userVO.java
user的属性为

[java]  view plain  copy
 
  1. private int id ;   
  2. private String username ;   
  3. private String password ;  

而userVO的属性则可为:

[java]  view plain  copy
 
  1. private int id ;   
  2. private String username ;   
  3. private String password1 ;  //第一次输入密码  
  4. private String password2 ;  //确认密码  

在表单验证时,password1和password2相同时才会被提交。这样不用改entity类,只需修改vo层即可。当然,这里的password1和password2也得写上get和set方法。

猜你喜欢

转载自www.cnblogs.com/szdszz/p/9181793.html