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 paginationInterceptor = new PaginationInnerInterceptor();
paginationInterceptor.setDbType(DbType.MYSQL);
paginationInterceptor.setOverflow(true);
interceptor.addInnerInterceptor(paginationInterceptor);
return interceptor;
}
}
DeliveryInfoController.java
package com.example.controller;
import com.example.entity.DeliveryInfo;
import com.example.service.IDeliveryInfoService;
import com.example.util.ServerResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.time.LocalDateTime;
import java.util.List;
@Controller
@RequestMapping("/deliveryInfo")
public class DeliveryInfoController {
@Autowired
private IDeliveryInfoService deliveryInfoService;
@GetMapping("{infoId}")
public ModelAndView findById(@PathVariable("infoId") Integer infoId) {
ServerResult result = deliveryInfoService.findById(infoId);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("result", result);
modelAndView.setViewName("deliveryInfo/detail");
return modelAndView;
}
@GetMapping("")
public ModelAndView findAll() {
int customerId = 1;
ServerResult result = deliveryInfoService.findPage(1, customerId);
ModelAndView mav = new ModelAndView();
mav.addObject("result", result);
mav.setViewName("deliveryInfo/listPage");
return mav;
}
@PostMapping
public ModelAndView create(DeliveryInfo deliveryInfo) {
int customerId = 1;
deliveryInfo.setCustomerId(customerId);
ServerResult result = deliveryInfoService.create(deliveryInfo);
ModelAndView mav = new ModelAndView();
if (result.isSuccess()) {
mav.setViewName("redirect:/deliveryInfo");
} else {
mav.addObject("result", result);
mav.setViewName("/deliveryInfo/save");
}
return mav;
}
@DeleteMapping("/{infoId}")
public ModelAndView delete(@PathVariable("infoId") Integer infoId) {
System.out.println("ID=" + infoId + "の配送先情報を削除します");
ServerResult result = deliveryInfoService.deleteById(infoId);
ModelAndView mav = new ModelAndView();
if (result.isSuccess()) {
System.out.println("削除成功");
mav.setViewName("redirect:/deliveryInfo");
} else {
mav.setViewName("deliveryInfo/listPage");
mav.addObject("deleteMsg", "削除失敗");
}
return mav;
}
@GetMapping("update/{infoId}")
public ModelAndView findByIdForUpdate(@PathVariable("infoId") Integer infoId) {
ServerResult result = deliveryInfoService.findById(infoId);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("result", result);
modelAndView.setViewName("deliveryInfo/update");
return modelAndView;
}
@PutMapping
public ModelAndView update(DeliveryInfo deliveryInfo) {
deliveryInfo.setUpdateTime(LocalDateTime.now());
ServerResult result = deliveryInfoService.update(deliveryInfo);
ModelAndView modelAndView = new ModelAndView();
if (result.isSuccess()) {
modelAndView.setViewName("redirect:/deliveryInfo/" + deliveryInfo.getInfoId());
} else {
modelAndView.addObject("updateMsg", "更新失敗");
modelAndView.setViewName("update");
}
return modelAndView;
}
@GetMapping("page/{pageNum}")
public ModelAndView findPage(@PathVariable("pageNum") Integer pageNum) {
int customerId = 1;
ServerResult result = deliveryInfoService.findPage(pageNum, customerId);
ModelAndView mav = new ModelAndView();
mav.addObject("result", result);
mav.setViewName("deliveryInfo/listPage");
return mav;
}
}
DeliveryInfo.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("delivery_info")
public class DeliveryInfo implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "info_id", type = IdType.AUTO)
private Integer infoId;
private Long recipientPhone;
private String recipientName;
private Integer customerId;
private String province;
private String city;
private String district;
private String street;
private String detailAddress;
private Integer status;
private Integer version;
private LocalDateTime createTime;
private LocalDateTime updateTime;
// Getters and Setters
public Integer getInfoId() { return infoId; }
public void setInfoId(Integer infoId) { this.infoId = infoId; }
public Long getRecipientPhone() { return recipientPhone; }
public void setRecipientPhone(Long recipientPhone) { this.recipientPhone = recipientPhone; }
public String getRecipientName() { return recipientName; }
public void setRecipientName(String recipientName) { this.recipientName = recipientName; }
public Integer getCustomerId() { return customerId; }
public void setCustomerId(Integer customerId) { this.customerId = customerId; }
public String getProvince() { return province; }
public void setProvince(String province) { this.province = province; }
public String getCity() { return city; }
public void setCity(String city) { this.city = city; }
public String getDistrict() { return district; }
public void setDistrict(String district) { this.district = district; }
public String getStreet() { return street; }
public void setStreet(String street) { this.street = street; }
public String getDetailAddress() { return detailAddress; }
public void setDetailAddress(String detailAddress) { this.detailAddress = detailAddress; }
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 "DeliveryInfo{" +
"infoId=" + infoId +
", recipientPhone=" + recipientPhone +
", recipientName='" + recipientName + ''' +
", customerId=" + customerId +
", province='" + province + ''' +
", city='" + city + ''' +
", district='" + district + ''' +
", street='" + street + ''' +
", detailAddress='" + detailAddress + ''' +
", status=" + status +
", version=" + version +
", createTime=" + createTime +
", updateTime=" + updateTime +
'}';
}
}
DeliveryInfoMapper.java
package com.example.mapper;
import com.example.entity.DeliveryInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface DeliveryInfoMapper extends BaseMapper<DeliveryInfo> {
}
DeliveryInfoServiceImpl.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.DeliveryInfo;
import com.example.mapper.DeliveryInfoMapper;
import com.example.service.IDeliveryInfoService;
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 DeliveryInfoServiceImpl implements IDeliveryInfoService {
@Autowired
private DeliveryInfoMapper deliveryInfoMapper;
@Override
public ServerResult findById(Integer infoId) {
DeliveryInfo info = deliveryInfoMapper.selectById(infoId);
if (info != null) {
return ServerResult.getSuccess(info);
}
return ServerResult.getFail(null);
}
@Override
public ServerResult findAll(Integer customerId) {
QueryWrapper<DeliveryInfo> wrapper = new QueryWrapper<>();
wrapper.eq("customer_id", customerId).eq("status", 1);
List<DeliveryInfo> infoList = deliveryInfoMapper.selectList(wrapper);
if (infoList == null || infoList.isEmpty()) {
return ServerResult.getFail("配送先情報がありません");
}
return ServerResult.getSuccess(infoList);
}
public ServerResult create(DeliveryInfo deliveryInfo) {
deliveryInfo.setStatus(1);
deliveryInfo.setVersion(1);
deliveryInfo.setCreateTime(LocalDateTime.now());
int rows = deliveryInfoMapper.insert(deliveryInfo);
if (rows > 0) {
return ServerResult.updateSuccess(deliveryInfo);
}
return ServerResult.updateFail(deliveryInfo);
}
@Override
public ServerResult deleteById(Integer infoId) {
DeliveryInfo info = deliveryInfoMapper.selectById(infoId);
info.setStatus(0);
info.setVersion(info.getVersion() + 1);
int rows = deliveryInfoMapper.updateById(info);
if (rows > 0) {
return ServerResult.updateSuccess(info);
}
return ServerResult.updateFail(info);
}
@Override
public ServerResult update(DeliveryInfo deliveryInfo) {
int oldVersion = deliveryInfoMapper.selectById(deliveryInfo.getInfoId()).getVersion();
deliveryInfo.setUpdateTime(LocalDateTime.now());
deliveryInfo.setVersion(oldVersion + 1);
int rows = deliveryInfoMapper.updateById(deliveryInfo);
if (rows > 0) {
return ServerResult.updateSuccess(deliveryInfo);
}
return ServerResult.updateFail(deliveryInfo);
}
@Override
public ServerResult findPage(Integer pageNum, Integer customerId) {
QueryWrapper<DeliveryInfo> wrapper = new QueryWrapper<>();
wrapper.eq("customer_id", customerId).eq("status", 1);
Page<DeliveryInfo> page = new Page<>(pageNum, 3);
page = deliveryInfoMapper.selectPage(page, wrapper);
if (page.getRecords().size() > 0) {
return ServerResult.getSuccess(page);
}
return ServerResult.getFail(page);
}
}
IDeliveryInfoService.java
package com.example.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.DeliveryInfo;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.util.ServerResult;
public interface IDeliveryInfoService extends IService<DeliveryInfo> {
ServerResult findById(Integer infoId);
ServerResult findAll(Integer customerId);
ServerResult create(DeliveryInfo deliveryInfo);
ServerResult deleteById(Integer infoId);
ServerResult update(DeliveryInfo deliveryInfo);
ServerResult findPage(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 ServerResult() {}
public ServerResult(int code, String msg, Object data) {
this.code = code;
this.msg = msg;
this.data = data;
}
// Getters and Setters
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 +
'}';
}
}
SpringbootDemoApplication.java
package com.example;
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);
}
}
application.yaml
server:
servlet:
context-path: /app
port: 8080
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/dicts?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: 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>
配送先氏名:${deliveryInfo.recipientName} <br>
電話番号:${deliveryInfo.recipientPhone}<br>
配送先住所:${deliveryInfo.province}${deliveryInfo.city}${deliveryInfo.district}${deliveryInfo.street}${deliveryInfo.detailAddress}
</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; }
.info-list { list-style-type: none; padding: 0; }
.info-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; }
.info-list li h3 { margin-top: 0; margin-bottom: 10px; }
.info-list li p { margin: 0; margin-bottom: 5px; }
.btn-container { position: absolute; top: 20px; right: 20px; }
.deleteForm { width: 45px; height: 30px; display: block; float: left; }
.info-list li .delete-btn, .info-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; }
.info-list li .delete-btn:hover, .info-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="info-list" id="infoList">
<c:forEach var="info" items="${pageInfo.records}">
<li>
<div class="btn-container">
<a href="${pageContext.request.contextPath}/deliveryInfo/update/${info.infoId}" class="update-btn">更新</a>
<form class="deleteForm" method="post" action="${pageContext.request.contextPath}/deliveryInfo/${info.infoId}">
<input type="hidden" name="_method" value="DELETE">
<input type="button" value="削除" class="delete-btn">
</form>
</div>
<h3>${info.recipientName}</h3>
<p>電話番号: ${info.recipientPhone}</p>
<p>配送先住所: ${info.province}${info.city}${info.district}${info.street}${info.detailAddress}</p>
</li>
</c:forEach>
</ul>
<div class="pagination">
<c:if test="${pageInfo.current != 1}">
<a href="${pageContext.request.contextPath}/deliveryInfo/page/${pageInfo.current-1}">前へ</a>
</c:if>
現在${pageInfo.current}ページ, 合計${pageInfo.total}件, 合計${pageInfo.pages}ページ
<c:if test="${pageInfo.current != pageInfo.pages}">
<a href="${pageContext.request.contextPath}/deliveryInfo/page/${pageInfo.current+1}">次へ</a>
</c:if>
</div>
</div>
</c:if>
<script>
document.querySelector(".info-list").onclick = function(event){
var ele = event.target;
if(ele.nodeName =='INPUT' && ele.className =='delete-btn'){
if(window.confirm("このレコードを削除しますか?")){
ele.parentElement.submit();
}
}
}
</script>
</body>
</html>