web层:

    public String query(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* 1. 封装表单数据到Customer对象中,它只有四个属性(cname、gender、cellphone、email)
* 它就是一个条件
* 2. 使用Customer调用service方法,得到List<Customer>
* 3. 保存到request域中
* 4. 转发到list.jsp
*/
Customer criteria = CommonUtils.toBean(request.getParameterMap(), Customer.class);
List<Customer> cstmList = customerService.query(criteria);
request.setAttribute("cstmList", cstmList);
return "/list.jsp";
}

service层:

    /**
* 多条件组合查询
* @param criteria
* @return
*/
public List<Customer> query(Customer criteria) {
return customerDao.query(criteria);
}

domain层:

/**
* 领域对象 与表单和数据库表对应
*
* @author cxf
*
*/
public class Customer {
/*
* 对应数据库表
*/
private String cid;// 主键
private String cname;// 客户名称
private String gender;// 客户性别
private String birthday;// 客户生日
private String cellphone;// 客户手机
private String email;// 客户邮箱
private String 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 [cid=" + cid + ", cname=" + cname + ", gender="
+ gender + ", birthday=" + birthday + ", cellphone="
+ cellphone + ", email=" + email + ", description="
+ description + "]";
}
}

dao层:

 /**

     * 多条件组合查询
* @param criteria
* @return
*/
public List<Customer> query(Customer criteria) {
try {
/*
* 1. 给出sql模板
* 2. 给出参数
* 3. 调用query方法,使用结果集处理器:BeanListHandler
*/
/*
* 一、 给出sql模板
* 二、 给出参数!
*/
/*
* 1. 给出一个sql语句前半部
*/
StringBuilder sql = new StringBuilder("select * from t_customer where 1=1");
/*
* 2. 判断条件,完成向sql中追加where子句
*/
/*
* 3. 创建一个ArrayList,用来装载参数值
*/
List<Object> params = new ArrayList<Object>();
String cname = criteria.getCname();
if(cname != null && !cname.trim().isEmpty()) {
sql.append(" and cname like ?");
params.add("%" + cname + "%");
} String gender = criteria.getGender();
if(gender != null && !gender.trim().isEmpty()) {
sql.append(" and gender=?");
params.add(gender);
} String cellphone = criteria.getCellphone();
if(cellphone != null && !cellphone.trim().isEmpty()) {
sql.append(" and cellphone like ?");
params.add("%" + cellphone + "%");
} String email = criteria.getEmail();
if(email != null && !email.trim().isEmpty()) {
sql.append(" and email like ?");
params.add("%" + email + "%");
} /*
* 三、执行query
*/
return qr.query(sql.toString(),
new BeanListHandler<Customer>(Customer.class),
params.toArray());
} catch(SQLException e) {
throw new RuntimeException(e);
}
}

最新文章

  1. UVa2326
  2. Ubuntu 安装系统资源托盘监视应用
  3. Andriod ADB开启Activity、Service以及BroadCast(包括参数的传递)
  4. COJ983 WZJ的数据结构(负十七)
  5. JS 学习笔记--2--变量的声明
  6. 异步非阻塞IO的Python Web框架--Tornado
  7. 【转】C++中多重继承的二义性及解决办法
  8. 10个精妙的Java编码最佳实践
  9. excel 下载
  10. 2016大连网络赛 Weak Pair
  11. Memcached在windows下的安装和使用
  12. Xshell5下利用sftp上传下载传输文件
  13. PHP Libxml 函数
  14. 持续集成之应用k8s自动部署
  15. SpringBoot动态配置加载
  16. loadrunner中JavaVuser脚本的编写
  17. unity2D动画和图片切割
  18. EasyUI的功能树之扁平化
  19. linux交叉编译gcc4.8.3
  20. 在Linux上利用core dump和GDB调试

热门文章

  1. F2BPM 开发Api与RESTfull应用服务Api 层次关系及示例
  2. 2015 测试赛 大神和小伙伴 hihoCoder
  3. ubuntu 关于sublime text3的一些应用
  4. Think in ISP(image sensor pipe )之How to implement an effecitive AE
  5. GDUT Krito的讨伐(bfs&amp;amp;&amp;amp;优先队列)
  6. 使用Linq 查询数据 构建对象 select new{}
  7. Hilbert曲线简单介绍及生成算法
  8. HDU2037 事件排序问题
  9. Linux(redhat 5.8)下 安装jetty 部署 使用
  10. POJ2559 Largest Rectangle in a Histogram 单调栈