MyBatis | 등록 INSERT | 객체를 매개 변수로 전달
소소 코드
TestTable.java
package sample.mybatis;
public class TestTable {
    private int id;
    private String value;
    public TestTable(String value) {
        this.value = value;
    }
    @Override
    public String toString() {
        return "TestTable [id=" + id + ", value=" + value + "]";
    }
}
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("fuga");
                session.insert("sample.mybatis.insertTest", table);
                session.commit();
            }
        }
    }
}
실행 결과
[DEBUG] s.m.insertTest  - ==>  Preparing: insert into test_table ( value ) values ( ? ) 
[DEBUG] s.m.insertTest  - ==> Parameters: fuga(String)
[DEBUG] s.m.insertTest  - <==    Updates: 1
test_table
| id | value | 
|---|---|
| 1 | hoge | 
| 2 | fuga | 
설명
- 이쪽도 <select>때와 같은 방식이다.
- parameterType은 생략 가능하다.