MyBatis 删除操作



  • 删除操作

    本章介绍如何使用MyBatis从表中删除记录。
    我们在MySQL中有以下student表-
    
    CREATE TABLE details.student(
       ID int(10) NOT NULL AUTO_INCREMENT,
       NAME varchar(100) NOT NULL,
       BRANCH varchar(255) NOT NULL,
       PERCENTAGE int(3) NOT NULL,
       PHONE int(11) NOT NULL,
       EMAIL varchar(255) NOT NULL,
       PRIMARY KEY (`ID`)
    );
    
    
    假设此表具有两个记录,如下所示-
    
    mysql> select * from STUDENT;
    +----+----------+--------+------------+-----------+----------------------+
    | ID |  NAME    | BRANCH | PERCENTAGE |  PHONE    |       EMAIL          |
    +----+----------+--------+------------+-----------+----------------------+
    | 1  | Mohammad |   It   |     80     | 900000000 | mohamad123@yahoo.com |
    | 2  | shyam    |   It   |     75     | 984800000 | shyam@gmail.com      |
    +----+----------+--------+------------+-----------+----------------------+
    2 rows in set (0.00 sec)
    
    
  • Student POJO类

    要执行删除操作,不需要修改Student.java文件。让我们保持上一章的样子。
    
    public class Student {
    
       private int id;
       private String name;
       private String branch;
       private int percentage;
       private int phone;
       private String email;
    
       public Student(int id, String name, String branch, int percentage, int phone, String email) {
          super();
          this.id = id;
          this.name = name;
          this.setBranch(branch);
          this.setPercentage(percentage);
          this.phone = phone;
          this.email = email;
       }
            
       public Student() {}
    
       public int getId() {
          return id;
       }
            
       public void setId(int id) {
          this.id = id;
       }
            
       public String getName() {
          return name;
       }
            
       public void setName(String name) {
          this.name = name;
       }
            
       public int getPhone() {
          return phone;
       }
            
       public void setPhone(int phone) {
          this.phone = phone;
       }
            
       public String getEmail() {
          return email;
       }
            
       public void setEmail(String email) {
          this.email = email;
       }
    
       public String getBranch() {
          return branch;
       }
    
       public void setBranch(String branch) {
          this.branch = branch;
       }
    
       public int getPercentage() {
          return percentage;
       }
    
       public void setPercentage(int percentage) {
          this.percentage = percentage;
       }
            
       public String toString(){
          StringBuilder sb = new StringBuilder();
                    
          sb.append("Id = ").append(id).append(" - ");
          sb.append("Name = ").append(name).append(" - ");
          sb.append("Branch = ").append(branch).append(" - ");
          sb.append("Percentage = ").append(percentage).append(" - ");
          sb.append("Phone = ").append(phone).append(" - ");
          sb.append("Email = ").append(email);
                    
          return sb.toString();
       }
            
    }
    
    
  • Student.xml文件

    要使用MyBatis定义SQL映射语句,我们将在Student.xml中使用<delete>标记,并且在此标记定义内,我们将定义一个“ id”,它将在mybatisDelete.java文件中用于在数据库上执行SQL DELETE查询。
    
    <?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 = "Student">    
       <resultMap id = "result" type = "Student">
          <result property = "id" column = "ID"/>         
       </resultMap>
            
       <delete id = "deleteById" parameterType = "int">
          DELETE from STUDENT WHERE ID = #{id};
       </delete>
            
    </mapper>
    
    
  • MyBatisDelete.java文件

    该文件具有应用程序级逻辑,可从Student表中删除记录-
    
    import java.io.IOException;
    import java.io.Reader;
    
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    public class mybatisDelete { 
    
       public static void main(String args[]) throws IOException{
          
          Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
          SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);               
          SqlSession session = sqlSessionFactory.openSession(); 
              
          //Delete operation
          session.delete("Student.deleteById", 2);     
          session.commit();
          session.close();      
          System.out.println("Record deleted successfully");
                            
       }
       
    }
    
    
  • 编译并运行

    这是编译和运行mybatisDelete.java的步骤。在继续进行编译和执行之前,请确保已正确设置了PATH和CLASSPATH。
    • 如上所示创建Student.xml。
    • 如本教程的MYBATIS-配置XML章节中所示,创建SqlMapConfig.xml 。
    • 如上所示创建Student.java并进行编译。
    • 如上所示创建mybatisDelete.java并进行编译。
    • 执行mybatisDelete二进制文件以运行程序。
    您将得到以下结果,并且ID = 1的记录将从学生中删除。
    
    Records Read Successfully
    
    
    如果检查student表,它将显示以下结果-
    
    mysql> select * from student;
    +----+----------+--------+------------+----------+----------------------+
    | ID |   NAME   | BRANCH | PERCENTAGE |  PHONE   |        EMAIL         |
    +----+----------+--------+------------+----------+----------------------+
    | 1  | Mohammad |   It   |    80      | 90000000 | mohamad123@yahoo.com |
    +----+----------+--------+------------+----------+----------------------+
    1 row in set (0.00 sec)