泛型的使用
目前这个项目使用的jsp+servlet+javabean,没有使用其它的框架。在处理数据库的时候,多张表就对应多个dao,而每个dao都有增删改查的方法,显得很冗余。于是写了一个CommonDao,其中包含了其他dao里面共有的一些方法。然后让其他的dao类继承CommonDao,servlet中直接使用CommonDao中的相应方法。那么这个时候遇到了一个问题,怎样判断需要对哪张表执行相应的操作。这时可以从servlet向dao类传入相应的sql,同时也需要判断传入的JavaBean对象,因为需要向sql中赋值。那么这个时候就需要使用泛型达到这一目的。
使用泛型时:
1 public class CommonDao<T> { 2 private ExecuteStatement executeStatement = new ExecuteStatement(); 3 private ReturnList returnList = new ReturnList(); 4 private T t; 5 6 //添加数据到数据库 7 public void add(T t, String sql) { 8 Connection conn = null; 9 PreparedStatement stmt = null; 10 try { 11 conn = DataBaseUtil.getConnection(); 12 stmt = conn.prepareStatement(sql); 13 if(t instanceof Customer) { //判断泛型t的类型 14 Customer customer = (Customer) t; 15 stmt = executeStatement.setStatement(customer,stmt); 16 } 17 else if(t instanceof Person) { 18 Person person = (Person) t; 19 stmt = executeStatement.setStatement(person,stmt); 20 } 21 else if(t instanceof Department) { 22 Department department = (Department) t; 23 stmt = executeStatement.setStatement(department,stmt); 24 } 25 stmt.executeUpdate(); 26 } catch (SQLException e) { 27 e.printStackTrace(); 28 } finally { 29 DataBaseUtil.close(stmt, conn); 30 } 31 }
对应的其他dao:
1 public class CustomerDao<T> extends CommonDao<T>{ 2 3 public CustomerDao() { 4 5 } 6 7 public void add(Customer customer) { 8 Connection conn = null; 9 PreparedStatement stmt = null; 10 String sql = "insert into SYSCUSTOMER(cuscode,cusname,cusdepcode,cuspercode,devdate,busdate,exitdate," + 11 "cellphone,email,address,tenid,ufts) values(?,?,?,?,?,?,?,?,?,?,?,?)"; 12 try { 13 conn = DataBaseUtil.getConnection(); 14 stmt = conn.prepareStatement(sql); 15 stmt.setString(1, customer.getCusCode()); 16 stmt.setString(2, customer.getCusName()); 17 stmt.setString(3, customer.getCusDepCode()); 18 stmt.setString(4, customer.getCusPerCode()); 19 stmt.setString(5, customer.getDevDate()); 20 stmt.setString(6, customer.getBusDate()); 21 stmt.setString(7, customer.getExitDate()); 22 stmt.setString(8, customer.getCellphone()); 23 stmt.setString(9, customer.getEmail()); 24 stmt.setString(10, customer.getAddress()); 25 stmt.setString(11, customer.getTenId()); 26 long currentTime = System.currentTimeMillis(); //获取当前时间,毫秒级别,距离1970年 27 Timestamp deptIsEnd = new Timestamp(currentTime); //获取时间戳 28 stmt.setTimestamp(12, deptIsEnd); 29 stmt.executeUpdate(); 30 } catch (SQLException e) { 31 e.printStackTrace(); 32 } finally { 33 if(stmt != null) { 34 DataBaseUtil.closeStatement(stmt); 35 } 36 if(conn != null) { 37 DataBaseUtil.closeConnection(conn); 38 } 39 } 40 }
executeStatement.setStatement(customer,stmt)代码:
1 //为customer表执行PreparedStatement方法 2 public PreparedStatement setStatement(Customer customer, PreparedStatement stmt) { 3 try { 4 stmt.setString(1, customer.getCusCode()); 5 stmt.setString(2, customer.getCusName()); 6 stmt.setString(3, customer.getCusDepCode()); 7 stmt.setString(4, customer.getCusPerCode()); 8 stmt.setString(5, customer.getDevDate()); 9 stmt.setString(6, customer.getBusDate()); 10 stmt.setString(7, customer.getExitDate()); 11 stmt.setString(8, customer.getCellphone()); 12 stmt.setString(9, customer.getEmail()); 13 stmt.setString(10, customer.getAddress()); 14 stmt.setString(11, customer.getTenId()); 15 long currentTime = System.currentTimeMillis(); //获取当前时间,毫秒级别,距离1970年 16 Timestamp deptIsEnd = new Timestamp(currentTime); //获取时间戳 17 stmt.setTimestamp(12, deptIsEnd); 18 } catch (SQLException e) { 19 e.printStackTrace(); 20 } 21 return stmt; 22 }
CustomerServlet的add方法:
1 /* 2 * 功能:添加数据并返回到customer.jsp页面 3 */ 4 @SuppressWarnings("unchecked") 5 public void add(HttpServletRequest request, HttpServletResponse response) 6 throws ServletException, IOException { 7 String cusCode = request.getParameter("cuscode"); 8 String cusName = null; 9 try { 10 cusName = new String(request.getParameter("cusname").getBytes("ISO-8859-1"),"utf-8"); 11 } catch (UnsupportedEncodingException e1) { 12 e1.printStackTrace(); 13 } 14 String cusDepCode = request.getParameter("cusdepcode"); 15 String cusPerCode = request.getParameter("cuspercode"); 16 String devDate = request.getParameter("devdate"); 17 String busDate = request.getParameter("busdate"); 18 String exitDate = request.getParameter("exitdate"); 19 Customer customer = new Customer(); 20 customer.setCusCode(cusCode); 21 customer.setCusName(cusName); 22 customer.setCusDepCode(cusDepCode); 23 customer.setCusPerCode(cusPerCode); 24 customer.setDevDate(devDate); 25 customer.setBusDate(busDate); 26 customer.setExitDate(exitDate); 27 String sql = "insert into SYSCUSTOMER(cuscode,cusname,cusdepcode,cuspercode,devdate,busdate,exitdate," + 28 "cellphone,email,address,tenid,ufts) values(?,?,?,?,?,?,?,?,?,?,?,?)"; 29 //customerDao.add(customer); 30 customerDao.add(customer, sql); 31 select(request, response); 32 try { 33 response.sendRedirect("common/customer.jsp"); 34 } catch (IOException e) { 35 e.printStackTrace(); 36 } 37 }
这样就很好的解决了代码冗余的问题,当然后面还可以再优化。