定义CustomerDAO接口
public interface CustomerDAO {
public List<Customer> getForListWithCriteriaCustomer(CriteriaCustomer cc);
public List<Customer> getAll();
public void save(Customer customer);
public Customer get(Integer id);
public void delete(Integer id);
public void update(Customer customer);
/**
* 返回和name相等的记录数
* @param name
* @return
*/
public long getCountWithName(String name);
}
定义两个实现类CustomerDAOJdbcImpl,CustomerDAOXMLImpl
public class CustomerDAOJdbcImpl extends DAO<Customer> implements CustomerDAO{
@Override
public List<Customer> getAll() {
String sql = "select * from customer";
return getForList(sql);
}
@Override
public void save(Customer customer) {
String sql = "insert into customer values (null,?,?,?)";
update(sql, customer.getName(), customer.getAddress(),customer.getPhone());
}
@Override
public Customer get(Integer id) {
String sql = "select id, name,address,phone from customer where id=?";
return get(sql, id);
}
@Override
public void delete(Integer id) {
String sql = "delete from customer where id = ?";
update(sql, id);
}
@Override
public long getCountWithName(String name) {
String sql = "select count(id) from customer where name = ?";
return getForValue(sql, name);
}
@Override
public List<Customer> getForListWithCriteriaCustomer(CriteriaCustomer cc) {
String sql = "select * from customer where name LIKE ? AND address LIKE ? AND phone LIKE ?";
return getForList(sql,cc.getName(),cc.getAddress(), cc.getPhone());
}
@Override
public void update(Customer customer) {
String sql = "update customer set name = ?, address = ?, phone = ? where id = ?";
update(sql, customer.getName(), customer.getAddress(), customer.getPhone(), customer.getId());
}
}
CustomerDAOXMLImpl类
public class CustomerDAOXMLImpl implements CustomerDAO{
@Override
public List<Customer> getForListWithCriteriaCustomer(CriteriaCustomer cc) {
System.out.println("getForListWithcriteriaCustomer");
return null;
}
@Override
public List<Customer> getAll() {
System.out.println("getAll");
return null;
}
@Override
public void save(Customer customer) {
System.out.println("save");
}
@Override
public Customer get(Integer id) {
System.out.println("get");
return null;
}
@Override
public void delete(Integer id) {
System.out.println("delete");
}
@Override
public void update(Customer customer) {
System.out.println("update");
}
@Override
public long getCountWithName(String name) {
System.out.println("getCountWithName");
return 0;
}
}
面向接口编程。定义一个接口,多个实现类可以有不同的具体实现
用的时间这样:
// private CustomerDAO customerDAO = new CustomerDAOJdbcImpl();
private CustomerDAO customerDAO = new CustomerDAOXMLImpl();
只改变这个地方就ok。
调用的类CustomerServlet
@WebServlet(urlPatterns="*.do",name="CustomerServlet")
public class CustomerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
// private CustomerDAO customerDAO = new CustomerDAOJdbcImpl();
private CustomerDAO customerDAO = new CustomerDAOXMLImpl();
public CustomerServlet() {
super();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
// @Override
// protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// String method = req.getParameter("method");
// System.out.println("para:" + method);
// switch(method) {
// case "add":
// addCustomer(req, resp);
// break;
// case "query":
// query(req, resp);
// break;
// case "delete":
// delete(req, resp);
// break;
// }
// }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String servletPath = request.getServletPath();
System.out.println("servletPath:" + servletPath);
String methodName = servletPath.substring(1);
methodName = methodName.substring(0, methodName.length()-3);
System.out.println("methodName:" + methodName);
Method method;
try {
//啊,啊,嗯。。。嗯。。。银家是反射。。。。呃,我擦,好恶心
method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
method.invoke(this, request, response);
} catch (Exception e) {
e.printStackTrace();
response.sendRedirect("error.jsp");
}
}
@SuppressWarnings("unused")
private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
System.out.println("delete");
String idStr = req.getParameter("id");
int id = 0;
try {
id = Integer.valueOf(idStr);
customerDAO.delete(id);
} catch (NumberFormatException e) {
e.printStackTrace();
}
resp.sendRedirect("query.do");
}
@SuppressWarnings("unused")
private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
String id = req.getParameter("id");
String name = req.getParameter("name");
String oldName = req.getParameter("oldName");
String phone = req.getParameter("phone");
String address = req.getParameter("address");
if(!oldName.equalsIgnoreCase(name)) {
long count = customerDAO.getCountWithName(name);
if(count > 0) {
req.setAttribute("message", "用戶名" + name + "已经被占用,请重新选择");
req.getRequestDispatcher("/updatecustomer.jsp").forward(req, resp);
return;
}
}
Customer customer = new Customer(name, address, phone);
customer.setId(Integer.valueOf(id));
customerDAO.update(customer);
resp.sendRedirect("query.do");
}
@SuppressWarnings("unused")
private void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
System.out.println("edit");
String forwordPath = "/error.jsp";
String idStr = req.getParameter("id");
int id = -1;
try {
Customer customer = customerDAO.get(Integer.valueOf(idStr));
if(customer != null) {
forwordPath = "/updatecustomer.jsp";
req.setAttribute("customer", customer);
}
} catch (Exception e) {}
req.getRequestDispatcher(forwordPath).forward(req, resp);
}
@SuppressWarnings("unused")
private void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("query");
String name = req.getParameter("name");
String address = req.getParameter("address");
String phone = req.getParameter("phone");
CriteriaCustomer cc = new CriteriaCustomer(name, address, phone);
List<Customer> customers = customerDAO.getForListWithCriteriaCustomer(cc);
req.setAttribute("customers", customers);
req.getRequestDispatcher("/index.jsp").forward(req, resp);
}
@SuppressWarnings("unused")
private void addCustomer(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("addCustomer");
String name = req.getParameter("name");
String address = req.getParameter("address");
String phone = req.getParameter("phone");
System.out.println("name:" + name + "...address:" + address + "...phone:" + phone);
long count = customerDAO.getCountWithName(name);
if(count > 0) {
req.setAttribute("message", "用户名" + name + "已被占用,请重新选择");
req.getRequestDispatcher("/newcustomer.jsp").forward(req, resp);
return;
}
Customer customer = new Customer(name, address, phone);
customerDAO.save(customer);
resp.sendRedirect("success.jsp");
}
}