package cn.edu.guet;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
/**
* @Author liwei
* @Date 2023/1/8 17:26
* @Version 1.0
*/
public class Test {
public static void main(String[] args) {
String employee_id = null;
String first_name = null;
String last_name = "JACK";
String email = null;
String phone_number = "18076";
String hire_date = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date hireDate = null;
if (hire_date != null) {
hireDate = Date.valueOf(hire_date);
}
try {
Map
map = new HashMap();
int count = 1;
String sql = "SELECT * FROM employees";
String whereClause = "";
StringBuffer sb = new StringBuffer();
sb.append(sql);
StringBuffer where = new StringBuffer();
if (employee_id != null && !"".equals(employee_id)) {
if (whereClause.equals("")) {
where.append(" WHERE employee_id=?");
whereClause = where.toString();
} else {
where.append(" AND employee_id=?");
whereClause = where.toString();
}
map.put(count++, employee_id);
}
if (first_name != null && !"".equals(first_name)) {
if (whereClause.equals("")) {
where.append(" WHERE first_name LIKE ?");
whereClause = where.toString();
} else {
where.append(" AND first_name LIKE ?");
whereClause = where.toString();
}
map.put(count++, first_name);
}
if (last_name != null && !"".equals(last_name)) {
if (whereClause.equals("")) {
where.append(" WHERE last_name LIKE ?");
whereClause = where.toString();
} else {
where.append(" AND last_name LIKE ?");
whereClause = where.toString();
}
map.put(count++, last_name);
}
if (email != null && !"".equals(email)) {
if (whereClause.equals("")) {
where.append(" WHERE email LIKE ?");
whereClause = where.toString();
} else {
where.append(" AND email LIKE ?");
whereClause = where.toString();
}
map.put(count++, email);
}
if (phone_number != null && !"".equals(phone_number)) {
if (whereClause.equals("")) {
where.append(" WHERE phone_number LIKE ?");
whereClause = where.toString();
} else {
where.append(" AND phone_number LIKE ?");
whereClause = where.toString();
}
map.put(count++, phone_number);
}
if (hire_date != null && !"".equals(hire_date)) {
if (whereClause.equals("")) {
where.append(" WHERE hire_date=?");
whereClause = where.toString();
} else {
where.append(" AND hire_date=?");
whereClause = where.toString();
}
map.put(count++, hireDate);
}
sb.append(where.toString());
sql = sb.toString();
System.out.println("SQL语句是:" + sql);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public List searchBill(PlanDesignDTO planDesignDTO) throws SQLException {
String sql = "SELECT * FROM PlanDesignInfo";
String whereClause = "";
StringBuffer sb = new StringBuffer();
sb.append(sql);
StringBuffer where = new StringBuffer();
//把所有的值存储起来
Map map = new TreeMap();
int count = 1;
if (!StringUtils.isBlank(planDesignDTO.getPlanDesignName())) {
if (whereClause.equals("")) {
where.append(" WHERE plan_design_name LIKE ?");
whereClause = where.toString();
} else {
where.append(" AND plan_design_name LIKE ?");
whereClause = where.toString();
}
map.put(count++, planDesignDTO.getPlanDesignName());
}
if (planDesignDTO.getSpecId() != null) {
if (whereClause.equals("")) {
where.append(" WHERE spec_id=?");
whereClause = where.toString();
} else {
where.append(" AND spec_id=?");
whereClause = where.toString();
}
map.put(count++, planDesignDTO.getSpecId());
}
if (!StringUtils.isBlank(planDesignDTO.getDesigner())) {
if (whereClause.equals("")) {
where.append(" WHERE designer LIKE ?");
whereClause = where.toString();
} else {
where.append(" AND designer LIKE ?");
whereClause = where.toString();
}
map.put(count++, planDesignDTO.getDesigner());
}
if (CollectionUtil.isNotEmpty(planDesignDTO.getDesignTime())) {
if (whereClause.equals("")) {
where.append(" WHERE create_time between ? AND ?");
whereClause = where.toString();
} else {
where.append(" AND create_time between ? AND ?");
whereClause = where.toString();
}
map.put(count++, planDesignDTO.getDesignTime());
}
sb.append(whereClause);
sql = sb.toString();
Connection conn = DBConnection.getConn();
PreparedStatement pstmt = conn.prepareStatement(sql);
/*
给占位符赋值
*/
Set keySet = map.keySet();
Iterator iterator = keySet.stream().iterator();
while (iterator.hasNext()) {
Integer key = iterator.next();
if (ClassUtils.isAssignable(map.get(key).getClass(), String.class)) {
pstmt.setObject(key, "%" + map.get(key) + "%");
} else {
pstmt.setObject(key, map.get(key));
}
}
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
/*
把查询到的数据封装成PlanDesignInfo类,并且返回
*/
System.out.println(rs.getString("plan_design_name"));
}
return null;
}
}