Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Performing All DML Operations with SqlSession

Tech 2

Mapper Mapping File

<?xml version="1.0" encoding="UTF-8" ?>
<mapper namespace="EmployeeMapper">
    <!-- 
    Return type for insert/update/delete is `int`, so `resultType` is not needed.
    `insert`, `update`, `delete` tags don't have `resultType` but support `parameterType`.
    -->
    <!-- Insert method: `public int addEmployee(Employee emp);` -->
    <insert id="addEmployee" parameterType="employee">
        insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) 
        values (#{empno}, #{ename}, #{job}, #{mgr}, #{hiredate}, #{sal}, #{comm}, #{deptno})
    </insert>
    <!-- Update method: Update employee name by empno: `public int updateEmployee(Employee emp);` -->
    <update id="updateEmployee" parameterType="employee">
        update emp set ename = #{ename} where empno = #{empno}
    </update>
    <!-- Delete method: Delete employees with empno ≥ given value: `public int deleteEmployee(int empno)` -->
    <delete id="deleteEmployee" parameterType="int">
        delete from emp where empno >= #{empno}
    </delete>
</mapper>

Test Code

package com.example.test;
import com.example.pojo.Employee;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

public class MyBatisDMLTest {
    private SqlSession session;

    @Before
    public void setup() {
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory factory = builder.build(inputStream);
        // Enable auto-commit for transactions
        session = factory.openSession(true);
    }

    @Test
    public void testInsert() {
        Employee emp = new Employee(null, "HoldLaBaby", "SALESMAN", 7839, new Date(), 3100.0, 200.0, 10);
        int affectedRows = session.insert("addEmployee", emp);
        System.out.println(affectedRows);
    }

    @Test
    public void testUpdate() {
        Employee emp = new Employee();
        emp.setEname("Xiaoming");
        emp.setEmpno(7937);
        int affectedRows = session.update("updateEmployee", emp);
        System.out.println(affectedRows);
    }

    @Test
    public void testDelete() {
        int affectedRows = session.delete("deleteEmployee", 7936);
        System.out.println(affectedRows);
    }

    @After
    public void tearDown() {
        session.close();
    }
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.