MyBatis接口代理

MyBatis接口代理:
  • 采用 Mybatis 的代理开发方式实现 DAO 层的开发,这种方式是目前的主流方式。
  • Mapper 接口开发方法只需要程序员编写Mapper 接口(相当于Dao 接口),由Mybatis 框架根据接口定义创建接口的动态代理对象,代理对象的方法体同上边Dao接口实现类方法。

Mapper接口开发需要遵循以下规范:

  1. 映射配置文件中的namespace与mapper接口的类名相同
  2. 映射配置文件中的增删改查标签的id属性要和Mappe接口中的方法名相同
  3. 映射配置文件中的增删改查标签的parameterType属性要和Mappe接口中的方法参数相同
  4. 映射配置文件中的增删改查标签的resultType属性要和Mappe接口中方法的返回值相同
接口代理原理:

分析动态代理对象如何生成的?

通过动态代理开发模式,我们只编写一个接口,不写实现类,我们通过 getMapper() 方法最终获取到 org.apache.ibatis.binding.MapperProxy 代理对象,然后执行功能,而这个代理对象正是 MyBatis 使用了 JDK 的动态代理技术,帮助我们生成了代理实现类对象。从而可以进行相关持久化操作。

分析方法是如何执行的?

动态代理实现类对象在执行方法的时候最终调用了 mapperMethod.execute() 方法,这个方法中通过 switch 语句根据操作类型来判断是新增、修改、删除、查询操作,最后一步回到了 MyBatis 最原生的 SqlSession 方式来执行增删改查。

接口代理实现演示:
  1. 编写StudentMapper接口
public interface StudentMapper {
    //查询全部
    public abstract List<Student> selectAll();

    //根据id查询
    public abstract Student selectById(Integer id);

    //新增数据
    public abstract Integer insert(Student stu);

    //修改数据
    public abstract Integer update(Student stu);

    //删除数据
    public abstract Integer delete(Integer id);
}
  1. Service接口
public interface StudentService {
    //查询全部
    public abstract List<Student> selectAll();

    //根据id查询
    public abstract Student selectById(Integer id);

    //新增数据
    public abstract Integer insert(Student stu);

    //修改数据
    public abstract Integer update(Student stu);

    //删除数据
    public abstract Integer delete(Integer id);
}
  1. 映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!--MyBatis的DTD约束-->
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!--
    mapper:核心根标签
    namespace属性:名称空间
-->
<mapper namespace="com.mybatis.mapper.StudentMapper">
    <!--
        select:查询功能的标签
        id属性:唯一标识
        resultType属性:指定结果映射对象类型
        parameterType属性:指定参数映射对象类型
    -->
    <select id="selectAll" resultType="student">
        SELECT * FROM student
    </select>

    <select id="selectById" resultType="student" parameterType="int">
        SELECT * FROM student WHERE id = #{id}
    </select>

    <!--对于增删改返回的都是影响行数,所以resultType属性可以不写-->
    <insert id="insert" parameterType="student">
        INSERT INTO student VALUES (#{id},#{name},#{age})
    </insert>

    <update id="update" parameterType="student">
        UPDATE student SET name = #{name},age = #{age} WHERE id = #{id}
    </update>

    <delete id="delete" parameterType="int">
        DELETE FROM student WHERE id = #{id}
    </delete>
</mapper>
  1. Service实现类
public class StudentMapperImpl implements StudentService {

    @Override
    public List<Student> selectAll() {
        List<Student> list = null;
        SqlSession sqlSession = null;
        InputStream rs = null;
        try {
            // 加载核心配置文件
            rs = Resources.getResourceAsStream("MybatisConfig.xml");
            // 获取工厂对象
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(rs);
            // 获取SqlSession对象
            sqlSession = build.openSession(true);
            // 获取接口实现类对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            // 通过实现类对象调用方法,接收结果
            list = mapper.selectAll();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 释放资源SS
            if (sqlSession != null) {
                sqlSession.close();
            }
            if (rs != null) {
                try {
                    rs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        // 返回结果
        return list;
    }

    @Override
    public Student selectById(Integer id) {
        Student stu = null;
        SqlSession sqlSession = null;
        InputStream rs = null;
        try {
            // 加载核心配置文件
            rs = Resources.getResourceAsStream("MybatisConfig.xml");
            // 获取工厂对象
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(rs);
            // 获取SqlSession对象
            sqlSession = build.openSession(true);
            // 获取接口实现类对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            // 通过实现类对象调用方法,接收结果
            stu = mapper.selectById(id);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 释放资源SS
            if (sqlSession != null) {
                sqlSession.close();
            }
            if (rs != null) {
                try {
                    rs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return stu;
    }

    @Override
    public Integer insert(Student stu) {
        Integer result = null;
        SqlSession sqlSession = null;
        InputStream rs = null;
        try {
            // 加载核心配置文件
            rs = Resources.getResourceAsStream("MybatisConfig.xml");
            // 获取工厂对象
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(rs);
            // 获取SqlSession对象
            sqlSession = build.openSession(true);
            // 获取接口实现类对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            // 通过实现类对象调用方法,接收结果
            result = mapper.insert(stu);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 释放资源SS
            if (sqlSession != null) {
                sqlSession.close();
            }
            if (rs != null) {
                try {
                    rs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    @Override
    public Integer update(Student stu) {
        Integer result = null;
        SqlSession sqlSession = null;
        InputStream rs = null;
        try {
            // 加载核心配置文件
            rs = Resources.getResourceAsStream("MybatisConfig.xml");
            // 获取工厂对象
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(rs);
            // 获取SqlSession对象
            sqlSession = build.openSession(true);
            // 获取接口实现类对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

            result = mapper.update(stu);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 释放资源SS
            if (sqlSession != null) {
                sqlSession.close();
            }
            if (rs != null) {
                try {
                    rs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    @Override
    public Integer delete(Integer id) {
        Integer result = null;
        SqlSession sqlSession = null;
        InputStream rs = null;
        try {
            // 加载核心配置文件
            rs = Resources.getResourceAsStream("MybatisConfig.xml");
            // 获取工厂对象
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(rs);
            // 获取SqlSession对象
            sqlSession = build.openSession(true);
            // 获取接口实现类对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

            result = mapper.delete(id);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 释放资源SS
            if (sqlSession != null) {
                sqlSession.close();
            }
            if (rs != null) {
                try {
                    rs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}
  1. 测试类
public class StuTest {
    // 创建业务层对象
   private StudentService  service = new StudentMapperImpl();

   //查询全部功能测试
   @Test
   public void selectAll() {
      List<Student> students = service.selectAll();
      for (Student stu : students) {
         System.out.println(stu);
      }
   }

   //根据id查询功能测试
   @Test
   public void selectById() {
      Student stu = service.selectById(3);
      System.out.println(stu);
   }

   //新增功能测试
   @Test
   public void insert() {
      Student stu = new Student(4,"赵六",26);
      Integer result = service.insert(stu);
      System.out.println(result);
   }

   //修改功能测试
   @Test
   public void update() {
      Student stu = new Student(4,"赵六",16);
      Integer result = service.update(stu);
      System.out.println(result);
   }

   //删除功能测试
   @Test
   public void delete() {
      Integer result = service.delete(4);
      System.out.println(result);
   }
}
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

itzhuzhu.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值