前台页面是别人给的。
例子:
package cn.itcast.cus.dao; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.ResultSetHandler; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import cn.itcast.cus.domain.Customer; import cn.itcast.jdbc.TxQueryRunner; public class CustomerDao { private QueryRunner qr=new TxQueryRunner(); public void add(Customer customer){ String sql="INSERT INTO t_customer VALUES(?,?,?,?,?,?,?)"; Object[] params={customer.getCid(),customer.getCname(),customer.getGender(),customer.getBirthday(),customer.getCellphone(),customer.getEmail(),customer.getDescription()}; try { qr.update(sql, params); } catch (SQLException e) { throw new RuntimeException(e); } } public List<Customer> findAll(){ String sql="SELECT * FROM t_customer"; ResultSetHandler<List<Customer>> rsh=new BeanListHandler<Customer>(Customer.class); ArrayList<Customer> customers=null; try { customers = (ArrayList<Customer>) qr.query(sql, rsh); } catch (SQLException e) { throw new RuntimeException(e); } return customers; } public Customer load(String cid) { String sql="SELECT * FROM t_customer WHERE cid=?"; ResultSetHandler<Customer> rsh=new BeanHandler<Customer>(Customer.class); Object[] params={cid}; Customer customer=null; try { customer = qr.query(sql, rsh,params); } catch (SQLException e) { throw new RuntimeException(e); } return customer; } public void update(Customer c){ String sql="UPDATE t_customer SET cname=?,gender=?,birthday=?,cellphone=?,email=?,description=? WHERE cid=?"; Object[] params={c.getCname(),c.getGender(),c.getBirthday(),c.getCellphone(),c.getEmail(),c.getDescription(),c.getCid()}; try { qr.update(sql, params); } catch (SQLException e) { throw new RuntimeException(e); } } public void delete(String cid) { String sql="DELETE FROM t_customer WHERE cid=?"; Object[] params={cid}; try { qr.update(sql, params); } catch (SQLException e) { throw new RuntimeException(e); } } public List<Customer> query(Customer c) { StringBuilder sb=new StringBuilder("SELECT * FROM t_customer WHERE 1=1"); ArrayList<Object> params=new ArrayList<Object>(); String cname=c.getCname(); if(cname!=null&& !cname.trim().isEmpty()){ sb.append(" and cname like ?"); params.add("%"+cname+"%"); } String gender=c.getGender(); if(gender!=null&&!gender.trim().isEmpty()){ sb.append(" and gender=?"); params.add(gender); } String cellphone=c.getCellphone(); if(cellphone!=null &&!cellphone.trim().isEmpty()){ sb.append(" and cellphone like ?"); params.add("%"+cellphone+"%"); } String email=c.getEmail(); if(email!=null && !email.trim().isEmpty()){ sb.append(" and email like ?"); params.add("%"+email+"%"); } String sql=sb.toString(); System.out.println(sql); List<Customer> cus; try { cus=qr.query(sql, new BeanListHandler<Customer>(Customer.class),params.toArray()); } catch (SQLException e) { throw new RuntimeException(e); } return cus; } } package cn.itcast.cus.dao; import org.junit.Test; import cn.itcast.cus.domain.Customer; import cn.itcast.utils.CommonUtils; public class CustomerTest { @Test public void fun1(){ CustomerDao dao=new CustomerDao(); for(int i=1;i<300;i++){ Customer c=new Customer(); c.setCid(CommonUtils.uuid()); c.setCname("cstnm_"+i); c.setBirthday("2016-08-01"); c.setGender(i%2==0?"男":"女"); c.setCellphone("139"+i); c.setEmail("cstm_"+i+"@163.com"); c.setDescription("我是客户"); dao.add(c); } } } package cn.itcast.cus.domain; import java.io.Serializable; public class Customer implements Serializable{ private static final long serialVersionUID = 1L; /** * 对应数据表 */ private String cid;//主键 private String cname;//姓名 private String gender;//性别 private String birthday;//生日 private String cellphone;//电话 private String email;//邮箱 private String description;//描述 public Customer() { super(); // TODO Auto-generated constructor stub } public Customer(String cid, String cname, String gender, String birthday, String cellphone, String email, String description) { super(); this.cid = cid; this.cname = cname; this.gender = gender; this.birthday = birthday; this.cellphone = cellphone; this.email = email; this.description = description; } public String getCid() { return cid; } public void setCid(String cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getCellphone() { return cellphone; } public void setCellphone(String cellphone) { this.cellphone = cellphone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Override public String toString() { return "Customer [c> gender + ", birthday=" + birthday + ", cellphone=" + cellphone + ", email=" + email + ", description=" + description + "]"; } } package cn.itcast.cus.service; import java.util.List; import cn.itcast.cus.dao.CustomerDao; import cn.itcast.cus.domain.Customer; public class CustomerService { private CustomerDao customerDao=new CustomerDao(); public void addCustomer(Customer customer){ customerDao.add(customer); } public List<Customer> findAll(){ return customerDao.findAll(); } public Customer load(String cid){ return customerDao.load(cid); } public void update(Customer c){ customerDao.update(c); } public void delete(String cid) { customerDao.delete(cid); } public List<Customer> query(Customer c) { return customerDao.query(c); } } package cn.itcast.cus.web.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.itcast.cus.domain.Customer; import cn.itcast.cus.service.CustomerService; import cn.itcast.servlet.BaseServlet; import cn.itcast.utils.CommonUtils; public class CustomerServlet extends BaseServlet { private static final long serialVersionUID = 1L; CustomerService customerService=new CustomerService(); public String add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 1、补全表单数据到Customer对象 * 2、补全Cid,使用uuid * 3、使用service方法完成添加工作 * 4.向request域中保存成功信息 * 5、转发到msg.jsp */ Customer c=CommonUtils.toBean(request.getParameterMap(), Customer.class); c.setCid(CommonUtils.uuid()); customerService.addCustomer(c); request.setAttribute("msg", "恭喜!注册成功"); return "f:/msg.jsp"; } public String findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 1、直接调用service方法,得到查询结果 * 2、查询结果保存到request域中 * 3、转发到list.jsp */ List<Customer> customers= customerService.findAll(); request.setAttribute("customers", customers); return "f:/list.jsp"; } public String preEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 1、直接调用service方法,得到查询结果 * 2、查询结果保存到request域中 * 3、转发到EDIT.jsp */ String cid=request.getParameter("cid"); Customer c=customerService.load(cid); request.setAttribute("customer", c); return "f:/edit.jsp"; } public String edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 1、封装表单数据到Customer对象中 * 2、调用service完成编辑 * 3、保存成功信息到request域 * 4、转发到msg.jsp显示成功信息 */ Customer c=CommonUtils.toBean(request.getParameterMap(), Customer.class); customerService.update(c); request.setAttribute("msg", "恭喜,修改成功!"); return "f:/msg.jsp"; } public String delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 1、封装表单数据到Customer对象中 * 2、调用service完成编辑 * 3、保存成功信息到request域 * 4、转发到msg.jsp显示成功信息 */ String cid=request.getParameter("cid"); customerService.delete(cid); request.setAttribute("msg", "恭喜,删除成功!"); return "f:/msg.jsp"; } public String query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 1、封装表单数据到Customer对象中 * 2、调用service完成编辑 * 3、保存查询到request域 * 4、转发到list.jsp */ Customer criteria=CommonUtils.toBean(request.getParameterMap(), Customer.class); List<Customer> customers=customerService.query(criteria); request.setAttribute("customers", customers); return "f:/list.jsp"; } } <?xml version="1.0" encoding="UTF-8" ?> <c3p0-config> <!-- 默认连接配置 --> <default-config> <!-- 连接四大参数配置 --> <property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/customers</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="user">guodaxia</property> <property name="password">961012gz</property> <!-- 池参数配置 --> <property name="acquireIncrement">3</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">2</property> <property name="maxPoolSize">10</property> </default-config> <named-config name="oracle-config"> <!-- 连接四大参数配置 --> <property name="jdbcUrl">jdbc:oracle:thin:@localhost:1521:db</property> <property name="driverClass">oracle.jdbc.driver.OracleDriver</property> <property name="user">scott</property> <property name="password">961012gz</property> <property name="acquireIncrement">3</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">2</property> <property name="maxPoolSize">10</property> </named-config> </c3p0-config>