訂單管理
前期準備
- 創(chuàng)建供應商 、bill 的servlet
- 注冊 bill 的servlet
- 創(chuàng)建相關的Dao,Service,javaBean等
- 導入相關的jsp
新增訂單
billDao
- billDao
boolean addBill(Connection connection, Bill bill) throws SQLException;
- billDaoImpl
@Override
public boolean addBill(Connection connection, Bill bill) throws SQLException {
boolean addFlag = false;
int addRows;
PreparedStatement preparedStatement;
String sql = "insert into smbms_bill values (null,?,?,?,?,?,?,?,?,?,?,?,?)";
preparedStatement = connection.prepareStatement(sql);
Object[] params = {
bill.getBillCode(),
bill.getProductName(),
bill.getProductDesc(),
bill.getProductUnit(),
bill.getProductCount(),
bill.getTotalPrice(),
bill.getIsPayment(),
bill.getCreatedBy(),
bill.getCreationDate(),
bill.getModifyBy(),
bill.getModifyDate(),
bill.getProviderId()
};
addRows = BaseDao.executeUpdate(connection, sql, preparedStatement, params);
if (addRows > 0) {
addFlag = true;
}
BaseDao.closeResource(connection, preparedStatement, null);
return addFlag;
}
s
billService
- billService
boolean addBill(Bill bill) throws SQLException;
- billServiceImpl
@Override
public boolean addBill(Bill bill) throws SQLException {
Connection conn = null;
boolean addFlag = false;
try {
conn = BaseDao.connection();
conn.setAutoCommit(false);
addFlag = billDao.addBill(conn, bill);
conn.commit();
} catch (Exception e) {
e.printStackTrace();
conn.rollback();
} finally {
BaseDao.closeResource(conn, null, null);
}
return addFlag;
}
billServlet
- billServlet
private void getBill(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String productName = "";
int isPayment = 0;
int providerId = 0;
String queryProductName = req.getParameter("queryProductName");
String queryProviderId = req.getParameter("queryProviderId");
String queryIsPayment = req.getParameter("queryIsPayment");
String pageIndex = req.getParameter("pageIndex");
int currentPageNo = 1;
int pageSize = 5;
queryProductName = queryProductName == null ? "" : queryProductName;
queryProviderId = queryProviderId == null ? "" : queryProviderId;
queryIsPayment = queryIsPayment == null ? "" : queryIsPayment;
currentPageNo = pageIndex == null ? currentPageNo : Integer.parseInt(pageIndex);
BillService billService = new BillServiceImpl();
int totalCount = billService.getBillCount(queryProductName, queryProviderId,
queryIsPayment);
PageSupport pageSupport = new PageSupport();
pageSupport.setTotalCount(totalCount);
pageSupport.setPageIndex(currentPageNo);
pageSupport.setPageSize(pageSize);
int totalPageCount = pageSupport.getTotalPageCount();
if (currentPageNo < 1) {
currentPageNo = 1;
} else if (currentPageNo > totalPageCount) {
currentPageNo = totalPageCount;
}
List<Bill> billList = billService.getBillList(queryProductName, queryProviderId,
queryIsPayment);
req.setAttribute("billList", billList);
req.setAttribute("queryProductName", queryProductName);
req.setAttribute("queryProviderId", queryProviderId);
req.setAttribute("queryIsPayment", queryIsPayment);
req.setAttribute("totalPageCount", totalPageCount);
req.setAttribute("totalCount", totalCount);
req.setAttribute("currentPageNo", currentPageNo);
req.getRequestDispatcher("/billlist.jsp").forward(req, resp);
}
- web.xml
<servlet>
<servlet-name>BillServlet</servlet-name>
<servlet-class>com.dashangms.servlet.bill.BillServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BillServlet</servlet-name>
<url-pattern>/jsp/bill.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>BillServlet</servlet-name>
<url-pattern>/bill</url-pattern>
</servlet-mapping>
jsp
<a href="${pageContext.request.contextPath}/jsp/billadd.jsp">添加訂單</a>
取消訂單
billDao
- billDao
int deleteBill(Connection connection, int delId) throws SQLException;
- billDaoImpl
@Override
public int deleteBill(Connection connection, int delId) throws SQLException {
int deletedRows = 0;
PreparedStatement preparedStatement = null;
if (connection != null) {
String sql = "delete from smbms_bill where id=?";
preparedStatement = connection.prepareStatement(sql);
Object[] param = {delId};
deletedRows = BaseDao.executeUpdate(connection, sql, preparedStatement, param);
}
BaseDao.closeResource(connection, preparedStatement, null);
return deletedRows;
}
billService
- billService
boolean deleteBill(String delId) throws SQLException;
- billServiceImpl
@Override
public boolean deleteBill(String delId) throws SQLException {
boolean deleteFlag = false;
int delIdTmp = Integer.parseInt(delId != null ? delId : "0");
Connection conn = null;
try {
conn = BaseDao.connection();
conn.setAutoCommit(false);
int deleteRows = billDao.deleteBill(conn, delIdTmp);
if (deleteRows > 0) {
deleteFlag = true;
}
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
conn.rollback();
} finally {
BaseDao.closeResource(conn, null, null);
}
return deleteFlag;
}
billServlet
- billServlet
private void delBill(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
BillService billService = new BillServiceImpl();
if (billService.deleteBill(req.getParameter("delId"))) {
req.getSession().setAttribute("message", "刪除成功!");
} else {
req.getSession().setAttribute("message", "刪除失敗!");
}
resp.sendRedirect(req.getContextPath() + "/provider?method=query");
}
- web.xml
jsp
<span><a class="deleteBill" href="javascript:;" billid=${bill.id} billcc=${bill.billCode}><img src="${pageContext.request.contextPath}/images/schu.png" alt="刪除" title="刪除"/></a></span>
更改訂單信息
billDao
- billDao
int updateBill(Connection connection, Bill bill) throws SQLException;
- billDaoImpl
@Override
public int updateBill(Connection connection, Bill bill) throws SQLException {
int updateRows = 0;
PreparedStatement preparedStatement = null;
String sql = "update smbms_bill set " +
"billCode=?," +
"productName=?, " +
"productDesc=?," +
"productUnit=?," +
"productCount=?," +
"totalPrice=?," +
"isPayment=?," +
"modifyBy=?," +
"modifyDate=?," +
"providerId=?" +
"where id=?";
Object[] params = {
bill.getBillCode(),
bill.getProductName(),
bill.getProductDesc(),
bill.getProductUnit(),
bill.getProductCount(),
bill.getTotalPrice(),
bill.getIsPayment(),
bill.getModifyBy(),
bill.getModifyDate(),
bill.getProviderId(),
bill.getId()
};
updateRows = BaseDao.executeUpdate(connection, sql, preparedStatement, params);
BaseDao.closeConnection(connection);
return updateRows;
}
billService
- billService
boolean updateBill(Bill bill) throws SQLException;
- billServiceImpl
@Override
public boolean updateBill(Bill bill) throws SQLException {
boolean updateFlag = false;
int updateRows = 0;
Connection conn = null;
try {
conn = BaseDao.connection();
conn.setAutoCommit(false);
updateRows = billDao.updateBill(conn, bill);
conn.commit();
if (updateRows > 0) {
updateFlag = true;
}
} catch (Exception e) {
e.printStackTrace();
conn.rollback();
} finally {
BaseDao.closeResource(conn, null, null);
}
return updateFlag;
}
billServlet
- billServlet
private void updateBill(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
BillService billService = new BillServiceImpl();
Bill bill = new Bill();
bill.setBillCode(req.getParameter("billCode"));
bill.setProductName(req.getParameter("productName"));
bill.setProductDesc(req.getParameter("productDesc"));
bill.setProductUnit(new BigDecimal(req.getParameter("productUnit")));
bill.setProductCount(new BigDecimal(req.getParameter("productCount")));
bill.setTotalPrice(req.getParameter("totalPrice"));
bill.setIsPayment(Integer.parseInt(req.getParameter("isPayment")));
bill.setProviderId(Integer.parseInt(req.getParameter("providerId")));
bill.setId(Integer.parseInt(req.getParameter("id")));
bill.setModifyBy(Integer.parseInt(req.getParameter("userId")));
bill.setModifyDate(new Date());
if (billService.updateBill(bill)) {
req.getSession().setAttribute("message", "修改成功!");
resp.sendRedirect(req.getContextPath() + "/provider?method=query");
} else {
req.getSession().setAttribute("message", "修改失敗!");
resp.sendRedirect(req.getContextPath() + "/provider?method=modify");
}
}
- web.xml
jsp
查詢訂單信息
billDao
- billDao
List<Bill> queryBillBy(Connection connection, String queryProductName, String queryProviderId, String queryIsPayment ) throws SQLException;
- billDaoImpl
@Override
public List<Bill> queryBillBy(Connection connection, String queryProductName,
String queryProviderId, String queryIsPayment) throws SQLException {
ResultSet resultSet = null;
List<Bill> billList = new ArrayList<>();
List<Object> params = new ArrayList<>();
PreparedStatement preparedStatement;
String sql = "select * from smbms_bill ";
if (queryProductName != null || queryProviderId != null || queryIsPayment != null) {
sql += "where 1=1 ";
if (queryProductName != null && !queryProductName.isEmpty()) {
sql += "and productName like ? ";
params.add("%" + queryProductName + "%");
}
if (queryProviderId != null && !queryProviderId.isEmpty()) {
sql += "and providerId = ? ";
params.add(queryProviderId);
}
if (queryIsPayment != null && !queryIsPayment.isEmpty()) {
sql += "and isPayment = ? ";
params.add(queryIsPayment);
}
}
preparedStatement = connection.prepareStatement(sql);
resultSet = BaseDao.executeQuery(connection, sql, preparedStatement, params.toArray(),
resultSet);
while (resultSet.next()) {
billList.add(getBillList(resultSet));
}
BaseDao.closeResource(connection, preparedStatement, resultSet);
return billList;
}
billService
- billService
List<Bill> getBillList(String queryProductName,
String queryProviderId, String queryIsPayment);
- billServiceImpl
@Override
public List<Bill> getBillList(String queryProductName, String queryProviderId,
String queryIsPayment) {
List<Bill> billList;
Connection con = BaseDao.connection();
try {
billList = billDao.queryBillBy(con, queryProductName, queryProviderId, queryIsPayment);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
BaseDao.closeResource(con, null, null);
}
return billList;
}
billServlet
- billServlet
private void getBill(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String productName = "";
int isPayment = 0;
int providerId = 0;
String queryProductName = req.getParameter("queryProductName");
String queryProviderId = req.getParameter("queryProviderId");
String queryIsPayment = req.getParameter("queryIsPayment");
String pageIndex = req.getParameter("pageIndex");
int currentPageNo = 1;
int pageSize = 5;
queryProductName = queryProductName == null ? "" : queryProductName;
queryProviderId = queryProviderId == null ? "" : queryProviderId;
queryIsPayment = queryIsPayment == null ? "" : queryIsPayment;
currentPageNo = pageIndex == null ? currentPageNo : Integer.parseInt(pageIndex);
BillService billService = new BillServiceImpl();
int totalCount = billService.getBillCount(queryProductName, queryProviderId,
queryIsPayment);
PageSupport pageSupport = new PageSupport();
pageSupport.setTotalCount(totalCount);
pageSupport.setPageIndex(currentPageNo);
pageSupport.setPageSize(pageSize);
int totalPageCount = pageSupport.getTotalPageCount();
if (currentPageNo < 1) {
currentPageNo = 1;
} else if (currentPageNo > totalPageCount) {
currentPageNo = totalPageCount;
}
List<Bill> billList = billService.getBillList(queryProductName, queryProviderId,
queryIsPayment);
req.setAttribute("billList", billList);
req.setAttribute("queryProductName", queryProductName);
req.setAttribute("queryProviderId", queryProviderId);
req.setAttribute("queryIsPayment", queryIsPayment);
req.setAttribute("totalPageCount", totalPageCount);
req.setAttribute("totalCount", totalCount);
req.setAttribute("currentPageNo", currentPageNo);
req.getRequestDispatcher("/billlist.jsp").forward(req, resp);
}
- web.xml
jsp