10.23
为我测试的连接数据库中新填功能 删除
Controller 层
mapper层
pojo(实体类层 Result
pojo(实体类层) user
Service层(service类
Service层(serviceimp接口
index 起始页面
删除页面

package com.example.cshi.controller; import com.example.cshi.pojo.Result; import com.example.cshi.pojo.User; import com.example.cshi.service.UserService; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Select; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController public class UserController {//接受请求 @Autowired private UserService userService; @PostMapping("/add") public Result add(User user){ userService.add(user); return Result.success(); } @GetMapping("/list") public Result list1(){ userService.list(); return Result.success(); } @RequestMapping("/ceshi")//测试端口是否能用 public String ceshi(User user){ System.out.println(user); return "ok"; } @GetMapping("/delete") public Result delete( Integer id){ userService.delete(id); System.out.println( "成功删除"); return Result.success(); } }

package com.example.cshi.mapper; import com.example.cshi.pojo.User; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import java.util.List; @Mapper public interface UserMapper { @Insert("insert into users (id, password) VALUES (#{id},#{password})") public void add(User user); @Select("SELECT * from users") public void list(); @Delete("delete from users where id=#{id} ") public void delete(Integer id); }

package com.example.cshi.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class Result { private Integer code; // 响应码,1代表成功;0代表失败 private String msg; // 响应信息,描述字符串 private Object data;// 返回的数据 public static Result success() { return new Result(1, "添加成功", null); } // 查询操作的成功响应 public static Result success(Object data) { return new Result(1, "添加成功", data); } // 失败响应 public static Result error(String msg) { return new Result(0, msg, null); } }

package com.example.cshi.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; public class User { private Integer id; private String password; public void setId(int id) { this.id = id; } public void setPassword(String password) { this.password = password; } public User(int id, String password) { this.id = id; this.password = password; } public int getId() { return id; } public String getPassword() { return password; } @Override public String toString() { return "User{" + "id=" + id + ", password='" + password + '\'' + '}'; } }

package com.example.cshi.service; import com.example.cshi.mapper.UserMapper; import com.example.cshi.pojo.User; import com.example.cshi.service.Userserviceimp.UserServiceimp; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService implements UserServiceimp { @Autowired UserMapper userMapper; public void add(User user){ userMapper.add(user); } public void list() { userMapper.list(); } public void delete(Integer id) { userMapper.delete(id); } }

package com.example.cshi.service.Userserviceimp; import com.example.cshi.pojo.User; import java.util.List; public interface UserServiceimp { void add(User user); void delete(Integer id); }

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用户注册</title> </head> <body> <h1>用户注册</h1> <form action="/add" method="post" enctype="application/x-www-form-urlencoded"> <div> <label>输入id:</label> <input type="text" name="id" required> </div> <div> <label>输入密码:</label> <input type="password" name="password" required> </div> <button type="submit">添加</button> </form> <button onclick="window.location.href='kk.html'"> 删除</button> </body> </html>

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>删除</title> </head> <body> <h> </h> <div> <form action="/delete" method="get"> <span>请输入你想要删除的序号 </span> <input type="text" name="id"> <button type="submit"> 删除</button> </form> </div> </body> </html>
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/你自己的数据库名
spring.datasource.username=root
spring.datasource.password=你自己的密码
数据库连接信息
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义