文章目录 MybatisplusConfig.java ReceiveAddressController.java ReceiveAddress.java ReceiveAddressMapper.java ReceiveAddressServiceImpl.java IReceiveAddressService.java ServerResult.java ServletInitializer.java SpringbootDemoApplication.java receive_address.sql ReceiveAddressMapper.xml application.yaml detail.jsp list.jsp(忽略) listPage.jsp save.jsp update.jsp web.xml index.jsp pom.xml TestAddressService.java
MybatisplusConfig.java
package com. example. config ; import com. baomidou. mybatisplus. annotation. DbType ;
import com. baomidou. mybatisplus. extension. plugins. MybatisPlusInterceptor ;
import com. baomidou. mybatisplus. extension. plugins. inner. PaginationInnerInterceptor ;
import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ; @Configuration
public class MybatisplusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor ( ) { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor ( ) ; PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor ( ) ; paginationInnerInterceptor. setDbType ( DbType . MYSQL ) ; paginationInnerInterceptor. setOverflow ( true ) ; interceptor. addInnerInterceptor ( paginationInnerInterceptor) ; return interceptor; } }
ReceiveAddressController.java
package com. example. controller ; import com. example. entity. ReceiveAddress ;
import com. example. service. IReceiveAddressService ;
import com. example. util. ServerResult ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. web. bind. annotation. * ; import org. springframework. stereotype. Controller ;
import org. springframework. web. servlet. ModelAndView ; import java. time. LocalDateTime ;
import java. util. List ; @Controller
@RequestMapping ( "/receiveAddress" )
public class ReceiveAddressController { @Autowired private IReceiveAddressService addressService; @GetMapping ( "{addrId}" ) public ModelAndView getById ( @PathVariable ( "addrId" ) Integer arrId) { ServerResult result = addressService. getById ( arrId) ; ModelAndView modelAndView = new ModelAndView ( ) ; modelAndView. addObject ( "result" , result) ; modelAndView. setViewName ( "receiveAddress/detail" ) ; return modelAndView; } @GetMapping ( "" ) public ModelAndView getAll ( ) { int custId = 1 ; ServerResult result = addressService. getByPage ( 1 , 1 ) ; ModelAndView mav = new ModelAndView ( ) ; mav. addObject ( "result" , result) ; mav. setViewName ( "receiveAddress/listPage" ) ; return mav; } @PostMapping public ModelAndView save ( ReceiveAddress receiveAddress) { int customerId = 1 ; receiveAddress. setCustId ( customerId) ; ServerResult result = addressService. save ( receiveAddress) ; ModelAndView mav = new ModelAndView ( ) ; if ( result. getCode ( ) == 200 ) { mav. setViewName ( "redirect:/receiveAddress" ) ; } else { mav. addObject ( "result" , result) ; mav. setViewName ( "/receiveAddress/save" ) ; } return mav; } @DeleteMapping ( "/{addrId}" ) public ModelAndView remove ( @PathVariable ( "addrId" ) Integer addressId) { System . out. println ( "删除id=" + addressId + "的地址" ) ; ServerResult result = addressService. removeById ( addressId) ; ModelAndView mav = new ModelAndView ( ) ; if ( result. getCode ( ) == 200 ) { System . out. println ( "删除成功" ) ; mav. setViewName ( "redirect:/receiveAddress" ) ; } else { mav. setViewName ( "receiveAddress/listPage" ) ; mav. addObject ( "deleteMsg" , "删除失败" ) ; } return mav; } @GetMapping ( "update/{addrId}" ) public ModelAndView getByIdForUpdate ( @PathVariable ( "addrId" ) Integer arrId) { ServerResult result = addressService. getById ( arrId) ; ModelAndView modelAndView = new ModelAndView ( ) ; modelAndView. addObject ( "result" , result) ; modelAndView. setViewName ( "receiveAddress/update" ) ; return modelAndView; } @PutMapping public ModelAndView update ( ReceiveAddress address) { address. setUpdateTime ( LocalDateTime . now ( ) ) ; ServerResult result = addressService. updateById ( address) ; ModelAndView modelAndView = new ModelAndView ( ) ; if ( result. getCode ( ) == 200 ) { modelAndView. setViewName ( "redirect:/receiveAddress/" + address. getAddrId ( ) ) ; } else { modelAndView. addObject ( "updateMsg" , "修改失败" ) ; modelAndView. setViewName ( "update" ) ; } return modelAndView; } @GetMapping ( "page/{pageNum}" ) public ModelAndView getByPage ( @PathVariable ( "pageNum" ) Integer pageNum) { ServerResult result = addressService. getByPage ( pageNum, 1 ) ; ModelAndView mav = new ModelAndView ( ) ; mav. addObject ( "result" , result) ; mav. setViewName ( "receiveAddress/listPage" ) ; return mav; } }
ReceiveAddress.java
package com. example. entity ; import com. baomidou. mybatisplus. annotation. IdType ;
import com. baomidou. mybatisplus. annotation. TableId ;
import com. baomidou. mybatisplus. annotation. TableName ;
import java. io. Serializable ;
import java. time. LocalDateTime ;
@TableName ( "receive_address" )
public class ReceiveAddress implements Serializable { private static final long serialVersionUID = 1L ; @TableId ( value = "addr_id" , type = IdType . AUTO ) private Integer addrId; private Long receiveUserTelno; private String receiveUsername; private Integer custId; private String addrProvince; private String addrCity; private String addrArea; private String addrStreet; private String addrDetail; private Integer status; private Integer version; private LocalDateTime createTime; private LocalDateTime updateTime; public Integer getAddrId ( ) { return addrId; } public void setAddrId ( Integer addrId) { this . addrId = addrId; } public Long getReceiveUserTelno ( ) { return receiveUserTelno; } public void setReceiveUserTelno ( Long receiveUserTelno) { this . receiveUserTelno = receiveUserTelno; } public String getReceiveUsername ( ) { return receiveUsername; } public void setReceiveUsername ( String receiveUsername) { this . receiveUsername = receiveUsername; } public Integer getCustId ( ) { return custId; } public void setCustId ( Integer custId) { this . custId = custId; } public String getAddrProvince ( ) { return addrProvince; } public void setAddrProvince ( String addrProvince) { this . addrProvince = addrProvince; } public String getAddrCity ( ) { return addrCity; } public void setAddrCity ( String addrCity) { this . addrCity = addrCity; } public String getAddrArea ( ) { return addrArea; } public void setAddrArea ( String addrArea) { this . addrArea = addrArea; } public String getAddrStreet ( ) { return addrStreet; } public void setAddrStreet ( String addrStreet) { this . addrStreet = addrStreet; } public String getAddrDetail ( ) { return addrDetail; } public void setAddrDetail ( String addrDetail) { this . addrDetail = addrDetail; } public Integer getStatus ( ) { return status; } public void setStatus ( Integer status) { this . status = status; } public Integer getVersion ( ) { return version; } public void setVersion ( Integer version) { this . version = version; } public LocalDateTime getCreateTime ( ) { return createTime; } public void setCreateTime ( LocalDateTime createTime) { this . createTime = createTime; } public LocalDateTime getUpdateTime ( ) { return updateTime; } public void setUpdateTime ( LocalDateTime updateTime) { this . updateTime = updateTime; } @Override public String toString ( ) { return "ReceiveAddress{" + "addrId=" + addrId + ", receiveUserTelno=" + receiveUserTelno + ", receiveUsername=" + receiveUsername + ", custId=" + custId + ", addrProvince=" + addrProvince + ", addrCity=" + addrCity + ", addrArea=" + addrArea + ", addrStreet=" + addrStreet + ", addrDetail=" + addrDetail + ", status=" + status + ", version=" + version + ", createTime=" + createTime + ", updateTime=" + updateTime + "}" ; }
}
ReceiveAddressMapper.java
package com. example. mapper ; import com. example. entity. ReceiveAddress ;
import com. baomidou. mybatisplus. core. mapper. BaseMapper ;
public interface ReceiveAddressMapper extends BaseMapper < ReceiveAddress > { }
ReceiveAddressServiceImpl.java
package com. example. service. impl ; import com. baomidou. mybatisplus. core. conditions. query. QueryWrapper ;
import com. baomidou. mybatisplus. extension. plugins. pagination. Page ;
import com. example. entity. ReceiveAddress ;
import com. example. mapper. ReceiveAddressMapper ;
import com. example. service. IReceiveAddressService ;
import com. baomidou. mybatisplus. extension. service. impl. ServiceImpl ;
import com. example. util. ServerResult ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. stereotype. Service ; import java. time. LocalDateTime ;
import java. util. List ;
@Service
public class ReceiveAddressServiceImpl implements IReceiveAddressService { @Autowired private ReceiveAddressMapper addressMapper; @Override public ServerResult getById ( Integer addressId) { ReceiveAddress address = addressMapper. selectById ( addressId) ; if ( address != null ) { return ServerResult . getSuccess ( address) ; } return ServerResult . getFail ( null ) ; } @Override public ServerResult getAll ( Integer customerId) { QueryWrapper < ReceiveAddress > wrapper = new QueryWrapper < > ( ) ; wrapper. eq ( "cust_id" , customerId) . eq ( "status" , 1 ) ; List < ReceiveAddress > addressList = addressMapper. selectList ( wrapper) ; if ( addressList == null || addressList. size ( ) == 0 ) return ServerResult . getFail ( "暂无收件地址" ) ; return ServerResult . getSuccess ( addressList) ; } public ServerResult save ( ReceiveAddress receiveAddress) { receiveAddress. setStatus ( 1 ) ; receiveAddress. setVersion ( 1 ) ; receiveAddress. setCreateTime ( LocalDateTime . now ( ) ) ; System . out. println ( "尚未添加,从页面拿到的收件地址是:" + receiveAddress) ; int rows = addressMapper. insert ( receiveAddress) ; if ( rows > 0 ) { System . out. println ( "添加成功后:" + receiveAddress) ; return ServerResult . updateSuccess ( receiveAddress) ; } return ServerResult . updateFail ( receiveAddress) ; } @Override public ServerResult removeById ( Integer addressId) { ReceiveAddress address = addressMapper. selectById ( addressId) ; address. setStatus ( 0 ) ; address. setVersion ( address. getVersion ( ) + 1 ) ; int rows = addressMapper. updateById ( address) ; if ( rows > 0 ) return ServerResult . updateSuccess ( address) ; return ServerResult . updateFail ( address) ; } @Override public ServerResult updateById ( ReceiveAddress address) { int oldVersion = addressMapper. selectById ( address. getAddrId ( ) ) . getVersion ( ) ; address. setUpdateTime ( LocalDateTime . now ( ) ) ; address. setVersion ( oldVersion+ 1 ) ; int rows = addressMapper. updateById ( address) ; if ( rows > 0 ) { return ServerResult . updateSuccess ( address) ; } return ServerResult . updateFail ( address) ; } @Override public ServerResult getByPage ( Integer pageNum, Integer customerId) { QueryWrapper < ReceiveAddress > wrapper = new QueryWrapper < > ( ) ; wrapper. eq ( "cust_id" , customerId) . eq ( "status" , 1 ) ; Page < ReceiveAddress > page = new Page < > ( pageNum, 3 ) ; page = addressMapper. selectPage ( page, wrapper) ; if ( page. getRecords ( ) . size ( ) > 0 ) { return ServerResult . getSuccess ( page) ; } return ServerResult . getFail ( page) ; } }
IReceiveAddressService.java
package com. example. service ; import com. baomidou. mybatisplus. core. conditions. query. QueryWrapper ;
import com. example. entity. ReceiveAddress ;
import com. baomidou. mybatisplus. extension. service. IService ;
import com. example. util. ServerResult ; import java. util. List ;
public interface IReceiveAddressService { public ServerResult getById ( Integer addressId) ; public ServerResult getAll ( Integer customerId) ; public ServerResult save ( ReceiveAddress receiveAddress) ; public ServerResult removeById ( Integer addressId) ; public ServerResult updateById ( ReceiveAddress address) ; public ServerResult getByPage ( Integer pageNum, Integer customerId) ; }
ServerResult.java
package com. example. util ; public class ServerResult { private int code; private String msg; private Object data; public static ServerResult getSuccess ( Object data) { return new ServerResult ( 200 , "查询成功" , data) ; } public static ServerResult getFail ( Object data) { return new ServerResult ( 201 , "查询失败" , data) ; } public static ServerResult updateSuccess ( Object data) { return new ServerResult ( 200 , "处理成功" , data) ; } public static ServerResult updateFail ( Object data) { return new ServerResult ( 201 , "处理失败" , data) ; } public static ServerResult loginSuccess ( Object data) { return new ServerResult ( 200 , "登录成功" , data) ; } public static ServerResult loginFail ( Object data) { return new ServerResult ( 201 , "登失败" , data) ; } public ServerResult ( ) { } public ServerResult ( int code, String msg, Object data) { this . code = code; this . msg = msg; this . data = data; } public int getCode ( ) { return code; } public void setCode ( int code) { this . code = code; } public String getMsg ( ) { return msg; } public void setMsg ( String msg) { this . msg = msg; } public Object getData ( ) { return data; } public void setData ( Object data) { this . data = data; } @Override public String toString ( ) { return "ServerResult{" + "code=" + code + ", msg='" + msg + '\'' + ", data=" + data + '}' ; }
}
ServletInitializer.java
package com. example ; import org. springframework. boot. builder. SpringApplicationBuilder ;
import org. springframework. boot. web. servlet. support. SpringBootServletInitializer ; public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure ( SpringApplicationBuilder application) { return application. sources ( SpringbootDemoApplication . class ) ; } }
SpringbootDemoApplication.java
package com. example ; import org. apache. ibatis. annotations. Mapper ;
import org. mybatis. spring. annotation. MapperScan ;
import org. springframework. boot. SpringApplication ;
import org. springframework. boot. autoconfigure. SpringBootApplication ; @SpringBootApplication
@MapperScan ( "com.example.mapper" )
public class SpringbootDemoApplication { public static void main ( String [ ] args) { SpringApplication . run ( SpringbootDemoApplication . class , args) ; } }
receive_address.sql
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0 ;
DROP TABLE IF EXISTS ` receive_address` ;
CREATE TABLE ` receive_address` ( ` addr_id` int ( 0 ) NOT NULL AUTO_INCREMENT , ` receive_user_telno` bigint ( 0 ) NULL DEFAULT NULL , ` receive_username` varchar ( 255 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL , ` cust_id` int ( 0 ) NULL DEFAULT NULL , ` addr_province` varchar ( 255 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '地址的省份' , ` addr_city` varchar ( 255 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '地址的城市' , ` addr_area` varchar ( 255 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '地址的区域' , ` addr_street` varchar ( 255 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '地址的街道' , ` addr_detail` varchar ( 255 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '详细地址' , ` status` int ( 0 ) NULL DEFAULT NULL COMMENT '状态' , ` version` int ( 0 ) NULL DEFAULT NULL COMMENT '版本号,用于做乐观锁' , ` create_time` datetime ( 0 ) NULL DEFAULT NULL COMMENT '数据添加的时间' , ` update_time` datetime ( 0 ) NULL DEFAULT NULL COMMENT '数据修改时间' , PRIMARY KEY ( ` addr_id` ) USING BTREE , INDEX ` fk_address_customer` ( ` cust_id` ) USING BTREE , CONSTRAINT ` fk_address_customer` FOREIGN KEY ( ` cust_id` ) REFERENCES ` customer` ( ` cust_id` ) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
INSERT INTO ` receive_address` VALUES ( 1 , NULL , NULL , 1 , '江苏省' , '苏州市' , '园区' , '若水路' , '若水路' , 1 , 1 , '2023-08-11 13:47:02' , NULL ) ;
INSERT INTO ` receive_address` VALUES ( 2 , NULL , NULL , 1 , '黑龙江' , '大庆市' , '市区' , '育才路' , '育才路' , 1 , 1 , '2023-07-31 13:47:52' , NULL ) ; SET FOREIGN_KEY_CHECKS = 1 ;
ReceiveAddressMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<! DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace = " com.example.mapper.ReceiveAddressMapper" > </ mapper>
application.yaml
server : servlet : context-path : /appport : 8080 spring : datasource : driver-class-name : com.mysql.cj.jdbc.Driverurl : jdbc: mysql: //localhost: 3306/dicts? useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai username : rootpassword : 123456 mvc : view : prefix : / suffix : .jsp hiddenmethod : filter : enabled : true
detail.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>收件地址详情</title>
</head>
<body>
收件人姓名:${result.data.receiveUsername} <br>
手机号:${result.data.receiveUserTelno}<br>
收件地址:${result.data.addrProvince}${result.data.addrCity}${result.data.addrArea}${result.data.addrStreet}${result.data.addrDetail}</body>
</html>
list.jsp(忽略)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head><title>收件地址列表</title><script src="${pageContext.request.contextPath}/js/jquery-3.7.0.min.js"></script><style>body {font-family: Arial, sans-serif;background-color: #f4f4f4;margin: 0;padding: 0;}.container {max-width: 600px;margin: 20px auto;padding: 20px;background-color: #fff;border-radius: 8px;box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);}h2 {margin-top: 0;}.address-list {list-style-type: none;padding: 0;}.address-list li {background-color: #f9f9f9;padding: 20px;margin-bottom: 20px;border-radius: 8px;box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);position: relative;}.address-list li h3 {margin-top: 0;margin-bottom: 10px;}.address-list li p {margin: 0;margin-bottom: 5px;}.address-list li .btn-container {position: absolute;top: 20px;right: 20px;}.deleteForm{width: 45px;height: 30px;display: block;float: left;}.address-list li .delete-btn, .address-list li .update-btn {background-color: #007bff;color: #fff;border: none;border-radius: 4px;padding: 8px 12px;cursor: pointer;text-decoration: none;font-size: 10px;display:block;float: left;margin-right: 10px;}.address-list li .delete-btn:hover,.address-list li .update-btn:hover {background-color: #0056b3;}.pagination {margin-top: 20px;text-align: center;}.pagination button {background-color: #007bff;color: #fff;border: none;border-radius: 4px;padding: 8px 16px;cursor: pointer;margin: 0 5px;}.pagination button:hover {background-color: #0056b3;}</style>
</head>
<body>${deleteMsg}<c:if test="${result.code !=200}">暂无数据
</c:if>
<c:if test="${result.code ==200}"><div class="container"><h2>我的收件地址列表</h2><ul class="address-list" id="addressList"><c:forEach var="address" items="${result.data}"><li><div class="btn-container"><%-- 修改: 根据主键查询 到修改页面 --%><a href="${pageContext.request.contextPath}/receiveAddress/update/${address.addrId}" class="update-btn">修改</a><%-- 通过表单的隐藏域,将post请求 转换成 delete 请求 --%><form class="deleteForm" method="post" action="${pageContext.request.contextPath}/receiveAddress/${address.addrId}"><input type="hidden" name="_method" value="DELETE"><input type="button" value="删除" class="delete-btn"></form></div><h3>${address.receiveUsername}</h3><p>手机号: ${address.receiveUserTelno}</p><p>收件地址: ${address.addrProvince}${address.addrCity}${address.addrArea}${address.addrStreet}${address.addrDetail}</p></li></c:forEach></ul></div>
</c:if><script>// 事件冒泡document.querySelector(".address-list").onclick = function(event){var ele = event.target; // 目标元素if(ele.nodeName =='INPUT' && ele.className =='delete-btn'){console.log(ele);if(window.confirm("您要删除这条记录么?")){ele.parentElement.submit();}}}</script>
</body>
</html>
listPage.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head><title>收件地址列表</title><script src="${pageContext.request.contextPath}/js/jquery-3.7.0.min.js"></script><style>body {font-family: Arial, sans-serif;background-color: #f4f4f4;margin: 0;padding: 0;}.container {max-width: 600px;margin: 20px auto;padding: 20px;background-color: #fff;border-radius: 8px;box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);}h2 {margin-top: 0;}.address-list {list-style-type: none;padding: 0;}.address-list li {background-color: #f9f9f9;padding: 20px;margin-bottom: 20px;border-radius: 8px;box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);position: relative;}.address-list li h3 {margin-top: 0;margin-bottom: 10px;}.address-list li p {margin: 0;margin-bottom: 5px;}.address-list li .btn-container {position: absolute;top: 20px;right: 20px;}.deleteForm{width: 45px;height: 30px;display: block;float: left;}.address-list li .delete-btn, .address-list li .update-btn {background-color: #007bff;color: #fff;border: none;border-radius: 4px;padding: 8px 12px;cursor: pointer;text-decoration: none;font-size: 10px;display:block;float: left;margin-right: 10px;}.address-list li .delete-btn:hover,.address-list li .update-btn:hover {background-color: #0056b3;}.pagination {margin-top: 20px;text-align: center;}.pagination button {background-color: #007bff;color: #fff;border: none;border-radius: 4px;padding: 8px 16px;cursor: pointer;margin: 0 5px;}.pagination button:hover {background-color: #0056b3;}</style>
</head>
<body>${result}<c:if test="${result.code !=200}">暂无数据
</c:if>
<c:if test="${result.code ==200}"><div class="container"><h2>我的收件地址列表</h2><ul class="address-list" id="addressList"><c:forEach var="address" items="${result.data.records}"><li><div class="btn-container"><%-- 修改: 根据主键查询 到修改页面 --%><a href="${pageContext.request.contextPath}/receiveAddress/update/${address.addrId}" class="update-btn">修改</a><%-- 通过表单的隐藏域,将post请求 转换成 delete 请求 --%><form class="deleteForm" method="post" action="${pageContext.request.contextPath}/receiveAddress/${address.addrId}"><input type="hidden" name="_method" value="DELETE"><input type="button" value="删除" class="delete-btn"></form></div><h3>${address.receiveUsername}</h3><p>手机号: ${address.receiveUserTelno}</p><p>收件地址: ${address.addrProvince}${address.addrCity}${address.addrArea}${address.addrStreet}${address.addrDetail}</p></li></c:forEach></ul><div><c:if test="${result.data.current !=1}"><a href="${pageContext.request.contextPath}/receiveAddress/page/${result.data.current-1}">上一页</a></c:if>当前是${result.data.current} 页,共有${result.data.total} 条记录,共有${result.data.pages}页<c:if test="${result.data.current !=result.data.pages}"><a href="${pageContext.request.contextPath}/receiveAddress/page/${result.data.current+1}">下一页</a></c:if></div></div>
</c:if><script>// 事件冒泡document.querySelector(".address-list").onclick = function(event){var ele = event.target; // 目标元素if(ele.nodeName =='INPUT' && ele.className =='delete-btn'){console.log(ele);if(window.confirm("您要删除这条记录么?")){ele.parentElement.submit();}}}</script>
</body>
</html>
save.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>添加收件地址</title><style>body {font-family: Arial, sans-serif;background-color: #f4f4f4;margin: 0;padding: 0;}.container {max-width: 600px;margin: 20px auto;padding: 20px;background-color: #fff;border-radius: 8px;box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);}h2 {margin-top: 0;}input[type="text"],input[type="tel"],select {width: 100%;padding: 10px;margin-top: 8px;margin-bottom: 20px;border: 1px solid #ccc;border-radius: 4px;box-sizing: border-box;}button {background-color: #007bff;color: #fff;border: none;border-radius: 4px;padding: 12px 20px;cursor: pointer;}button:hover {background-color: #0056b3;}</style>
</head>
<body>
<div class="container"><h2>添加收件地址</h2><%-- ${pageContext.request.contextPath} ===> http://localhost:8080/app --%><form id="addressForm" method="post" action="${pageContext.request.contextPath}/receiveAddress"><label for="recipientName">收件人姓名:</label><input type="text" id="recipientName" name="receiveUsername" required><label for="phoneNumber">收件人手机号:</label><input type="tel" id="phoneNumber" name="receiveUserTelno" required><label for="province">省份:</label><select id="province" name="addrProvince" required><option value="">请选择省份</option><option value="北京">北京</option><option value="上海">上海</option><option value="天津">天津</option><option value="重庆">重庆</option><option value="河北">河北</option><option value="河南">河南</option><option value="湖南">湖南</option><option value="湖北">湖北</option><option value="四川">四川</option><option value="广东">广东</option></select><label for="city">城市:</label><select id="city" name="addrCity" required><option value="">请选择城市</option></select><label for="district">区域:</label><input type="text" id="district" name="addrArea" required><label for="street">街道:</label><input type="text" id="street" name="addrStreet" required><label for="address">详细地址:</label><input type="text" id="address" name="addrDetail" required><input type="submit" value="添加 " /></form>
</div><script>// Cities data for each provincevar citiesData = {"北京": ["北京"],"上海": ["上海"],"天津": ["天津"],"重庆": ["重庆"],"河北": ["石家庄", "唐山", "保定"],"河南": ["郑州", "洛阳", "开封"],"湖南": ["长沙", "株洲", "湘潭"],"湖北": ["武汉", "黄石", "十堰"],"四川": ["成都", "绵阳", "乐山"],"广东": ["广州", "深圳", "东莞"]};// Function to populate cities based on selected provincefunction populateCities() {var provinceSelect = document.getElementById("province");var citySelect = document.getElementById("city");var selectedProvince = provinceSelect.value;// Clear existing city optionscitySelect.innerHTML = "<option value=''>Select City</option>";// Populate city options based on selected provinceif (selectedProvince in citiesData) {citiesData[selectedProvince].forEach(function(city) {var option = document.createElement("option");option.value = city;option.text = city;citySelect.appendChild(option);});}}// Event listener for province select changedocument.getElementById("province").addEventListener("change", populateCities);</script>
</body>
</html>
update.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>修改收件地址</title><style>body {font-family: Arial, sans-serif;background-color: #f4f4f4;margin: 0;padding: 0;}.container {max-width: 600px;margin: 20px auto;padding: 20px;background-color: #fff;border-radius: 8px;box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);}h2 {margin-top: 0;}input[type="text"],input[type="tel"],select {width: 100%;padding: 10px;margin-top: 8px;margin-bottom: 20px;border: 1px solid #ccc;border-radius: 4px;box-sizing: border-box;}button {background-color: #007bff;color: #fff;border: none;border-radius: 4px;padding: 12px 20px;cursor: pointer;}button:hover {background-color: #0056b3;}</style>
</head>
<body>
<div class="container">${updateMsg}<h2>修改收件地址</h2><%--1. post -- put2. 底层根据什么修改地址的???SQL???update address set 列1=值,列2=值2.... where addr_id = 101--%><form id="addressForm" method="post" action="${pageContext.request.contextPath}/receiveAddress"><%--1. post -- put --%><input type="hidden" name="_method" value="PUT"><%--2.底层根据id修改地址的,通过隐藏域传给服务器 --%><input type="hidden" name="addrId" value="${result.data.addrId}"><label for="recipientName">收件人姓名:</label><input type="text" id="recipientName" name="receiveUsername" value="${result.data.receiveUsername}" required><label for="phoneNumber">收件人手机号:</label><input type="tel" id="phoneNumber" name="receiveUserTelno" value="${result.data.receiveUserTelno}" required><label for="province">省份:</label><select id="province" name="addrProvince" required><option value="${result.data.addrProvince}">${result.data.addrProvince}</option><option value="北京">北京</option><option value="上海">上海</option><option value="天津">天津</option><option value="重庆">重庆</option><option value="河北">河北</option><option value="河南">河南</option><option value="湖南">湖南</option><option value="湖北">湖北</option><option value="四川">四川</option><option value="广东">广东</option></select><label for="city">城市:</label><select id="city" name="addrCity" required><option value="${result.data.addrCity}">${result.data.addrCity}</option></select><label for="district">区域:</label><input type="text" id="district" name="addrArea" value="${result.data.addrArea}" required><label for="street">街道:</label><input type="text" id="street" name="addrStreet" value="${result.data.addrStreet}" required><label for="address">详细地址:</label><input type="text" id="address" name="addrDetail" value="${result.data.addrDetail}" required><input type="submit" value="修改 " /></form>
</div><script>// Cities data for each provincevar citiesData = {"北京": ["北京"],"上海": ["上海"],"天津": ["天津"],"重庆": ["重庆"],"河北": ["石家庄", "唐山", "保定"],"河南": ["郑州", "洛阳", "开封"],"湖南": ["长沙", "株洲", "湘潭"],"湖北": ["武汉", "黄石", "十堰"],"四川": ["成都", "绵阳", "乐山"],"广东": ["广州", "深圳", "东莞"]};// Function to populate cities based on selected provincefunction populateCities() {var provinceSelect = document.getElementById("province");var citySelect = document.getElementById("city");var selectedProvince = provinceSelect.value;// Clear existing city optionscitySelect.innerHTML = "<option value=''>Select City</option>";// Populate city options based on selected provinceif (selectedProvince in citiesData) {citiesData[selectedProvince].forEach(function(city) {var option = document.createElement("option");option.value = city;option.text = city;citySelect.appendChild(option);});}}// Event listener for province select changedocument.getElementById("province").addEventListener("change", populateCities);</script>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
< web-app xmlns = " http://xmlns.jcp.org/xml/ns/javaee" xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation= " http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version = " 4.0" >
</ web-app>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<a href="receiveAddress/1" >根据主键查询收件地址</a> <br><a href="receiveAddress" >查询我的所有收件地址(分页)</a><a href="receiveAddress/save.jsp" >添加新收件地址</a><a href="${pageContext.request.contextPath}/receiveAddress/page/1">查询所有收件地址(分页)</a></body>
</html>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
< project xmlns = " http://maven.apache.org/POM/4.0.0" xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation= " http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion> 4.0.0</ modelVersion> < parent> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-starter-parent</ artifactId> < version> 2.7.6</ version> < relativePath/> </ parent> < groupId> com.example</ groupId> < artifactId> springboot_demo</ artifactId> < version> 0.0.1-SNAPSHOT</ version> < packaging> war</ packaging> < name> springboot_demo</ name> < description> springboot_demo</ description> < properties> < java.version> 1.8</ java.version> </ properties> < dependencies> < dependency> < groupId> javax.servlet</ groupId> < artifactId> jstl</ artifactId> < version> 1.2</ version> </ dependency> < dependency> < groupId> taglibs</ groupId> < artifactId> standard</ artifactId> < version> 1.1.0</ version> </ dependency> < dependency> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-starter-web</ artifactId> </ dependency> < dependency> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-starter-tomcat</ artifactId> < scope> provided</ scope> </ dependency> < dependency> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-starter-test</ artifactId> < scope> test</ scope> </ dependency> < dependency> < groupId> mysql</ groupId> < artifactId> mysql-connector-java</ artifactId> < version> 8.0.28</ version> </ dependency> < dependency> < groupId> com.baomidou</ groupId> < artifactId> mybatis-plus-boot-starter</ artifactId> < version> 3.5.2</ version> </ dependency> < dependency> < groupId> com.baomidou</ groupId> < artifactId> mybatis-plus-generator</ artifactId> < version> 3.5.1</ version> </ dependency> < dependency> < groupId> org.freemarker</ groupId> < artifactId> freemarker</ artifactId> < version> 2.3.31</ version> </ dependency> </ dependencies> < build> < plugins> < plugin> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-maven-plugin</ artifactId> </ plugin> </ plugins> </ build> </ project>
TestAddressService.java
package com. example ; import com. baomidou. mybatisplus. extension. plugins. pagination. Page ;
import com. example. entity. ReceiveAddress ;
import com. example. service. IReceiveAddressService ;
import com. example. util. ServerResult ;
import org. junit. jupiter. api. Test ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. boot. test. context. SpringBootTest ; @SpringBootTest
public class TestAddressService { @Autowired private IReceiveAddressService service; @Test public void getByPage ( ) { ServerResult result = service. getByPage ( 2 , 1 ) ; Page < ReceiveAddress > page = ( Page < ReceiveAddress > ) result. getData ( ) ; System . out. println ( "当前页码:" + page. getCurrent ( ) ) ; System . out. println ( "总记录数:" + page. getTotal ( ) ) ; System . out. println ( "总页数:" + page. getPages ( ) ) ; System . out. println ( "地址数据" ) ; page. getRecords ( ) . forEach ( System . out:: println ) ; } }