【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文件中注册这个映射接口
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <!-- 数据库连接的配置信息 驱动,URL,用户名,密码-->
            <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>
        <!-- 注册EmployeeMapperPlus02映射接口-->
        <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", "hhh@qq.com", "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", "lll@qq.com", "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", "lhk@qq.com", "1");
            mapper.deleteEmpById(employee); // 测试删除
            openSession.commit();  // 手动提交数据
        } finally {
            openSession.close();
        }
    }
}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值