🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
最后,我们共同学习CURD的D - Destory删除操作。 # 定制路由 ``` <!-- 删除 --> <action name="delete" class="teacher.Delete"> <result name="success">/jsp/teacher/success.jsp</result> <result name="error">/jsp/teacher/error.jsp</result> </action> ``` # V 使用success.jsp # C ``` package teacher; import entity.Teacher; public class Delete { private int id; private Teacher teacher; public int getId() { return id; } public void setId(int id) { this.id = id; } // 该execute方法将被自动调用, 方法的返回类型必须为String public String execute() { return "success"; } } ``` # C&V测试 [http://localhost:8080/javaee/teacher/delete?id=1](http://localhost:8080/javaee/teacher/delete?id=1) # M - entity ``` public Boolean delete() { // 创建会话(这里的session也是会话的意思,我们以前接触的http中的session,处理的是用户与服务器的对话) Session session = MysqlJavaee.getCurrentSession(); // 开启事务(使用缓冲池进行数据库的连接) Transaction transaction = session.beginTransaction(); // 在这里,必须使用try catch finally语句。来确定会话正常关闭. // 否则,当操作数据库产生错误时,你可能需要重启mysql服务 try { // 删除 session.delete(this); // 提交事务 transaction.commit(); // 捕获异常 } catch (HibernateException e) { // 如果事务执行异常,则回滚事务 if (null != transaction) { transaction.rollback(); } // 打印异常 e.printStackTrace(); } finally { // 如果session处于开启状态,则关闭session if (session.isOpen()) { // 关闭会话 session.close(); } } return true; } ``` ## 单元测试 ``` @Test public void delete() { Teacher teacher = Teacher.getTeacherById(1); System.out.println("更新操作前"); System.out.println(teacher.toString()); // 删除 teacher.setName("hello1"); teacher.delete(); // 查看结果 teacher = Teacher.getTeacherById(1); System.out.println("更新操作后"); System.out.println(teacher.toString()); } ``` # M - server ``` public static Boolean deleteById(int id) { Teacher teacher = Teacher.getTeacherById(id); return teacher.delete(); } ``` ## 单元测试 ``` @Test public void delete() { Teacher teacher = Teacher.getTeacherById(2); System.out.println("删除前"); System.out.println(teacher.toString()); TeacherServer.deleteById(2); teacher = Teacher.getTeacherById(2); System.out.println("删除后"); System.out.println(teacher.toString()); } ``` 控制台: ``` 删除前 Teacher [id=2, name=李四, username=lisi, email=lisi@yunzhiclub.com, sex=false, password=456] Hibernate: select teacher0_.id as id1_0_0_, teacher0_.email as email2_0_0_, teacher0_.name as name3_0_0_, teacher0_.password as password4_0_0_, teacher0_.sex as sex5_0_0_, teacher0_.username as username6_0_0_ from Teacher teacher0_ where teacher0_.id=? Hibernate: delete from Teacher where id=? Hibernate: select teacher0_.id as id1_0_0_, teacher0_.email as email2_0_0_, teacher0_.name as name3_0_0_, teacher0_.password as password4_0_0_, teacher0_.sex as sex5_0_0_, teacher0_.username as username6_0_0_ from Teacher teacher0_ where teacher0_.id=? 删除后 ``` # C&M `// 该execute方法将被自动调用, 方法的返回类型必须为String public String execute() { TeacherServer.deleteById(id); return "success"; }`· ## 集成测试 我们在数据表中,找到一条可以删除的数据。比如ID为4 则输入[http://localhost:8080/javaee/teacher/delete?id=4](http://localhost:8080/javaee/teacher/delete?id=4)进行测试。 > 当要删除的数据存在时,会提示操作成功。当要删除的数据不存在,将得到一个异常。我们在下一章解决这个问题。