MyBatis | 삭제 DELETE
삭제전 DB 테이블
test_table
id | value |
---|---|
1 | hoge |
2 | fuga |
3 | piyo |
소스 코드
TestTable.java
package sample.mybatis;
public class TestTable {
private int id;
private String value;
public TestTable(int id) {
this.id = id;
}
}
sample_mapper.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="sample.mybatis">
<delete id="deleteTest">
delete from test_table where id = #{id}
</delete>
</mapper>
Main.java
package sample.mybatis;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class Main {
public static void main(String[] args) throws Exception {
try (InputStream in = Main.class.getResourceAsStream("/mybatis-config.xml")) {
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
try (SqlSession session = factory.openSession()) {
TestTable table = new TestTable(2);
session.insert("sample.mybatis.deleteTest", table);
session.commit();
}
}
}
}
실행 결과
[DEBUG] s.m.deleteTest - ==> Preparing: delete from test_table where id = ?
[DEBUG] s.m.deleteTest - ==> Parameters: 2(Integer)
[DEBUG] s.m.deleteTest - <== Updates: 1
test_table
id | value |
---|---|
1 | hoge |
3 | piyo |
설명
- <delete> 태그로 DELETE문을 정의할 수 있다.
최종 수정 : 2021-08-26