用javeEE实现淘宝收货地址,单表,有上下一页可以翻页,具体代码
时间: 2024-05-03 10:17:05 浏览: 126
基于vue.js仿淘宝收货地址,并设置默认地址
以下是一个简单的JavaEE实现淘宝收货地址的例子,使用单表存储,同时支持上下翻页。
首先,我们创建一个名为`Address`的JavaBean类,表示收货地址,包含以下属性:
```java
public class Address {
private int id; // 地址ID
private String name; // 收货人姓名
private String phone; // 收货人电话
private String province; // 省份
private String city; // 城市
private String district; // 区/县
private String detail; // 详细地址
private boolean isDefault; // 是否为默认地址
// 构造函数、Getter和Setter方法省略
}
```
接下来,我们使用JDBC连接数据库,并创建一个名为`AddressDao`的数据访问对象,提供以下方法:
```java
public class AddressDao {
// 数据库连接信息
private static final String url = "jdbc:mysql://localhost:3306/db_name";
private static final String username = "root";
private static final String password = "root";
// 分页查询收货地址
public List<Address> findByPage(int pageNo, int pageSize) throws Exception {
List<Address> list = new ArrayList<>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 连接数据库
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
// 构造分页SQL语句
String sql = "SELECT * FROM address LIMIT ?, ?";
ps = conn.prepareStatement(sql);
ps.setInt(1, (pageNo - 1) * pageSize);
ps.setInt(2, pageSize);
// 执行查询
rs = ps.executeQuery();
while (rs.next()) {
Address address = new Address();
address.setId(rs.getInt("id"));
address.setName(rs.getString("name"));
address.setPhone(rs.getString("phone"));
address.setProvince(rs.getString("province"));
address.setCity(rs.getString("city"));
address.setDistrict(rs.getString("district"));
address.setDetail(rs.getString("detail"));
address.setDefault(rs.getBoolean("is_default"));
list.add(address);
}
} finally {
// 关闭资源
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
}
return list;
}
}
```
最后,我们在Servlet中使用`AddressDao`对象查询收货地址,并在JSP页面上展示出来,同时提供上下翻页的功能:
```java
@WebServlet("/address/list")
public class AddressListServlet extends HttpServlet {
private static final int PAGE_SIZE = 10; // 每页显示10条记录
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int pageNo = 1; // 默认显示第一页
String pageNoStr = request.getParameter("pageNo");
if (pageNoStr != null && pageNoStr.matches("\\d+")) {
pageNo = Integer.parseInt(pageNoStr);
}
try {
AddressDao dao = new AddressDao();
List<Address> list = dao.findByPage(pageNo, PAGE_SIZE); // 分页查询收货地址
int totalCount = dao.getCount(); // 获取总记录数
int totalPage = (totalCount + PAGE_SIZE - 1) / PAGE_SIZE; // 计算总页数
request.setAttribute("list", list);
request.setAttribute("pageNo", pageNo);
request.setAttribute("totalPage", totalPage);
request.getRequestDispatcher("/WEB-INF/views/address/list.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
在JSP页面中,我们使用JSTL标签库展示收货地址列表,并提供上下翻页的链接:
```jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<table>
<thead>
<tr>
<th>ID</th>
<th>收货人姓名</th>
<th>收货人电话</th>
<th>省份</th>
<th>城市</th>
<th>区/县</th>
<th>详细地址</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="address" items="${list}">
<tr>
<td>${address.id}</td>
<td>${address.name}</td>
<td>${address.phone}</td>
<td>${address.province}</td>
<td>${address.city}</td>
<td>${address.district}</td>
<td>${address.detail}</td>
<td>
<a href="/address/edit?id=${address.id}">编辑</a>
<a href="/address/delete?id=${address.id}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<c:if test="${pageNo > 1}">
<a href="/address/list?pageNo=${pageNo - 1}">上一页</a>
</c:if>
<c:if test="${pageNo < totalPage}">
<a href="/address/list?pageNo=${pageNo + 1}">下一页</a>
</c:if>
```
以上就是一个简单的JavaEE实现淘宝收货地址的例子,使用单表存储,同时支持上下翻页的代码。
阅读全文