【Mybatis】(四)执行CRUD操作——基于注解方式

在之前实现的基于XML方式的CRUD操作【Mybatis】(三)执行CRUD操作——基于XML实现的基础之上。

我们介绍执行CRUD操作的第二种方式——基于注解方式

1、定义EmployeeMapperPlus02接口

package com.lhk.mybatis.dao;

import com.lhk.mybatis.bean.Employee;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * @author lhk
 * 使用注解的方式
 * @create 2018-09-16 19:08
 */
public interface EmployeeMapperPlus02 {

    // 使用@Select注解指明getEmpById方法要执行的SQL
    @Select("select id,last_name lastName,email,gender from tb1_employee where id = #{id}")
    public Employee getEmpById(Integer id);

    // 使用@Insert注解指明addEmp方法要执行的SQL
    @Insert("insert into tb1_employee(last_name,email,gender) values (#{lastName},#{email},#{gender})")
    public void addEmp(Employee employee);

    // 使用@Update注解指明updateEmp方法要执行的SQL
    @Update("update tb1_employee set last_name=#{lastName},email=#{email},gender=#{gender} where id=#{id}")
    public void updateEmp(Employee employee);

    // 使用@Delete注解指明deleteEmpById方法要执行的SQL
    @Delete("delete from tb1_employee where id=#{id}")
    public void deleteEmpById(Employee employee);
}

2、在conf.xml文件中注册这个映射接口




<configuration>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
                <property name="username" value="root" />
                <property name="password" value="13579" />
            dataSource>
        environment>
    environments>

    <mappers>
        
        <mapper class="com.lhk.mybatis.dao.EmployeeMapperPlus02"/>
    mappers>

configuration>

3、单元测试类MyBatisPlus02Test.java

package com.lhk.mybatis.test;

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

/**
 * @author lhk
 * @create 2018-09-16 19:17
 */
public class MyBatisPlus02Test {
    public SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "conf/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(inputStream);
    }

    /**
     * 测试添加方法
     * @throws IOException
     */
    @Test
    public void testAdd() throws IOException {
        SqlSessionFactory SqlSessionFactory = getSqlSessionFactory();

        // 获取到的sqlSession不会自动提交数据
        SqlSession openSession = SqlSessionFactory.openSession();

        try {
            EmployeeMapperPlus02 mapper = openSession.getMapper(EmployeeMapperPlus02.class);
            Employee employee = new Employee(8, "hhh", "[email protected]", "1");
            mapper.addEmp(employee); // 1、测试添加
            openSession.commit();  // 手动提交数据
        } finally {
            openSession.close();
        }
    }

    /**
     * 测试更新方法
     * @throws IOException
     */
    @Test
    public void testUpdate() throws IOException {
        SqlSessionFactory SqlSessionFactory = getSqlSessionFactory();

        // 获取到的sqlSession不会自动提交数据
        SqlSession openSession = SqlSessionFactory.openSession();

        try {
            EmployeeMapperPlus02 mapper = openSession.getMapper(EmployeeMapperPlus02.class);
            Employee employee = new Employee(8, "lll", "[email protected]", "1");

            mapper.updateEmp(employee);  // 2、测试修改
            openSession.commit();   // 手动提交数据
        } finally {
            openSession.close();
        }
    }

    /**
     * 测试查询方法
     * @throws IOException
     */
    @Test
    public void testSelect() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();

        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            EmployeeMapperPlus02 mapper = openSession.getMapper(EmployeeMapperPlus02.class);
            Employee employee = mapper.getEmpById(2); // 3、测试查询
            System.out.println(employee);
        } finally {
            openSession.close();
        }
    }

    /**
     * 测试删除方法
     * @throws IOException
     */
    @Test
    public void testDelete() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();

        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            EmployeeMapperPlus02 mapper = openSession.getMapper(EmployeeMapperPlus02.class);
            Employee employee = new Employee(8, "lhk", "[email protected]", "1");
            mapper.deleteEmpById(employee); // 测试删除
            openSession.commit();  // 手动提交数据
        } finally {
            openSession.close();
        }
    }
}

可见当时用注解方式时,不需要再定义XXX.xml配置文件

你可能感兴趣的:(【Mybatis】(四)执行CRUD操作——基于注解方式)