8、mybatis之增删改查

文章目录

      • 1、EmployeeMapper.xml
      • 2、测试增删改查

1、EmployeeMapper.xml




<mapper namespace="com.mi.dao.EmployeeMapper">
    
    <select id="getEmployeeById" resultType="com.mi.pojo.Employee">
        select * from employee where id = #{id}
    select>

    <insert id="addEmp" parameterType="com.mi.pojo.Employee">
        insert into employee (last_name,gender) values (#{lastName},#{gender})
    insert>
    
    <delete id="deleteEmp">
        delete from employee where id = #{id}
    delete>

    <update id="updateEmp" >
        update employee set last_name = #{lastName},gender = #{gender} where id = #{id}
    update>
mapper>

2、测试增删改查

package com.mi.test;

import com.mi.dao.EmployeeMapper;
import com.mi.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.Test;

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

public class TestMybatis {

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

    @Test
    public void testGet() throws IOException {
        //1、获取SqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        //2、获取Sqlsesion对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        try {
            //3、获取接口的实现类对象
            //会为接口自动创建一个代理对象,代理对象去执行增删改查方法
            EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
            Employee employee = mapper.getEmployeeById(3);
            System.out.println(employee);
        }finally {
            sqlSession.close();
        }
    }

    @Test
    public void testAdd() throws IOException {
        //1、获取SqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        //2、获取Sqlsesion对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        try {
            //3、获取接口的实现类对象
            //会为接口自动创建一个代理对象,代理对象去执行增删改查方法
            EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
            Integer employee = mapper.addEmp(new Employee(null,"jerry","1"));
            sqlSession.commit();
            System.out.println(employee);
        }finally {
            sqlSession.close();
        }
    }

    @Test
    public void testDel() throws IOException {
        //1、获取SqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        //2、获取Sqlsesion对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        try {
            //3、获取接口的实现类对象
            //会为接口自动创建一个代理对象,代理对象去执行增删改查方法
            EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
            Integer employee = mapper.deleteEmp(1);
            sqlSession.commit();
            System.out.println(employee);
        }finally {
            sqlSession.close();
        }
    }

    @Test
    public void testUpdate() throws IOException {
        //1、获取SqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        //2、获取Sqlsesion对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        try {
            //3、获取接口的实现类对象
            //会为接口自动创建一个代理对象,代理对象去执行增删改查方法
            EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
            Integer employee = mapper.updateEmp(new Employee(3,"jerry","0"));
            sqlSession.commit();
            System.out.println(employee);
        }finally {
            sqlSession.close();
        }
    }

}

你可能感兴趣的:(MyBatis)