Mybatis学习笔记3 - 增删改查示例

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编写




    
    

    
    
    
        insert into tbl_employee(last_name, gender, email)
        values (#{lastName},#{gender},#{email})
    

    
    
        update tbl_employee set last_name=#{lastName},email=#{email},gender=#{gender} where id=#{id}
    

    
    
        delete from tbl_employee where id=#{id}
    

 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();
        }

    }
}

 

转载于:https://www.cnblogs.com/xidian2014/p/10349323.html

你可能感兴趣的:(Mybatis学习笔记3 - 增删改查示例)