1. 程式人生 > >Mybatis學習筆記3 - 增刪改查示例

Mybatis學習筆記3 - 增刪改查示例

ioe java ger name sts dao testin on() 學習

1、接口定義

package com.mybatis.dao;

import com.mybatis.bean.Employee;

public interface EmployeeMapper {
    public Employee getEmpById(Integer id);

    public void addEmp(Employee employee);

    public void updateEmp(Employee employee);

    public void deleteEmpById(Integer id);
}

2、mapper編寫

<?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="com.mybatis.dao.EmployeeMapper">
    <!--public Employee getEmpById(Integer id);-->
    <select id="getEmpById" resultType="com.mybatis.bean.Employee">
		select * from tbl_employee where id = #{id}
	</select>

    <!--public void addEmp(Employee employee);-->
    <!-- parameterType:參數類型,可以省略-->
    <insert id="addEmp" parameterType="com.mybatis.bean.Employee">
        insert into tbl_employee(last_name, gender, email)
        values (#{lastName},#{gender},#{email})
    </insert>

    <!--public void updateEmp(Employee employee);-->
    <update id="updateEmp">
        update tbl_employee set last_name=#{lastName},email=#{email},gender=#{gender} where id=#{id}
    </update>

    <!--public void deleteEmpById(Integer id);-->
    <delete id="deleteEmpById">
        delete from tbl_employee where id=#{id}
    </delete>
</mapper>

3、測試代碼

package com.mybatis.demo;

import java.io.IOException;
import java.io.InputStream;

import com.mybatis.bean.Employee;
import com.mybatis.dao.EmployeeMapper;
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.Test;

public class MyTest {
    public SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(inputStream);
    }

    /**
     * 測試增刪改
     * 1、mybatis允許增刪改直接定義以下類型返回值
     * Integer、Long、Boolean、void
     * 2、需要手動提交數據
     * sqlSessionFactory.openSession();===》手動提交
     * sqlSessionFactory.openSession(true);===》自動提交
     */

    @Test
    public void testSelect() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
            Employee employee = mapper.getEmpById(1);
            System.out.println(employee);
        } finally {
            openSession.close();
        }
    }

    @Test
    public void testInsert() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        //此處獲取到的SqlSession不會自動提交數據
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
            mapper.addEmp(new Employee("jetty", "[email protected]", 1));
            //手動提交數據
            openSession.commit();
        } finally {
            openSession.close();
        }

    }

    @Test
    public void testUpdate() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
            mapper.updateEmp(new Employee(2, "jerry", "[email protected]", 1));
            openSession.commit();
        } finally {
            openSession.close();
        }

    }

    @Test
    public void testDelete() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        //自動提交
        SqlSession openSession = sqlSessionFactory.openSession(true);
        try {
            EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
            mapper.deleteEmpById(2);
        } finally {
            openSession.close();
        }

    }
}

Mybatis學習筆記3 - 增刪改查示例