• Mybatis接口代理方式实现Dao层


    • 目录

      接口代理方式-实现介绍

      接口代理方式-实现规则

      接口代理方式-代码实现

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

      分析方法是如何执行的


    • 接口代理方式-实现介绍

    • 传统方式实现Dao层,既要写接口,还要写实现类
    • 而Mybatis框架可以帮助我们省略编写Dao层接口实现类的步骤
    • 程序员只需要编写接口,由Mybatis框架根据接口的定义来创建该接口的动态代理对象
    • 接口代理方式-实现规则

    • 1.映射配置文件中的名称空间必须和Dao层接口的全类名相同
    • 2.映射配置文件中的增删改查标签的id属性必须和Dao层接口的方法名相同
    • 3.映射配置文件中的增删改查标签的parameterType属性必须和Dao层接口方法的参数相同
    • 4.映射配置文件中的增删改查标签的result Type属性必须和Dao层接口方法的返回值相同
    • 接口代理方式-代码实现

    • 1.删除之前的dao层接口的实现类
    • 2.修改映射配置文件
    • 3.修改service层接口的实现类,采用接口代理方式实现功能
    • 删除不要演示
    • 修改映射配置文件
    • <mapper namespace="demo3.dao.StudentMapper">
    • 修改service层接口的实现类
      1. @Override
      2. public List selectAll() {
      3. List list = null;
      4. SqlSession sqlSession = null;
      5. InputStream is = null;
      6. try{
      7. //1.加载核心配置文件
      8. is = Resources.getResourceAsStream("MybatisConfig.xml");
      9. //2.获取SqlSession工厂对象
      10. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
      11. //3.通过工厂对象获取SqlSession
      12. sqlSession= sqlSessionFactory.openSession(true);
      13. //4.获取StudentMapper接口的实现类对象
      14. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);//StudentMapper mapper = new StudentMapperImpl();
      15. //5.通过实现类对象调用方法,接收结果
      16. list = mapper.selectAll();
      17. }catch(Exception e){
      18. e.printStackTrace();
      19. }finally {
      20. //6.释放资源
      21. if(sqlSession!=null){
      22. sqlSession.close();
      23. }
      24. if(is!=null){
      25. try {
      26. is.close();
      27. } catch (IOException e) {
      28. e.printStackTrace();
      29. }
      30. }
      31. }
      32. //7.返回结果
      33. return list;
      34. }
      1. @Override
      2. public Student selectById(Integer sid) {
      3. Student stu = null;
      4. SqlSession sqlSession = null;
      5. InputStream is = null;
      6. try{
      7. //1.加载核心配置文件
      8. is = Resources.getResourceAsStream("MybatisConfig.xml");
      9. //2.获取SqlSession工厂对象
      10. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
      11. //3.通过工厂对象获取SqlSession
      12. sqlSession= sqlSessionFactory.openSession(true);
      13. //4.获取StudentMapper接口的实现类对象
      14. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);//StudentMapper mapper = new StudentMapperImpl();
      15. //5.通过实现类对象调用方法,接收结果
      16. stu = mapper.selectById(sid);
      17. }catch(Exception e){
      18. e.printStackTrace();
      19. }finally {
      20. //6.释放资源
      21. if(sqlSession!=null){
      22. sqlSession.close();
      23. }
      24. if(is!=null){
      25. try {
      26. is.close();
      27. } catch (IOException e) {
      28. e.printStackTrace();
      29. }
      30. }
      31. }
      32. //7.返回结果
      33. return stu;
      34. }
      1. @Override
      2. public Integer insert(Student stu) {
      3. Integer result = null;
      4. SqlSession sqlSession = null;
      5. InputStream is = null;
      6. try{
      7. //1.加载核心配置文件
      8. is = Resources.getResourceAsStream("MybatisConfig.xml");
      9. //2.获取SqlSession工厂对象
      10. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
      11. //3.通过工厂对象获取SqlSession
      12. sqlSession= sqlSessionFactory.openSession(true);
      13. //4.获取StudentMapper接口的实现类对象
      14. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);//StudentMapper mapper = new StudentMapperImpl();
      15. //5.通过实现类对象调用方法,接收结果
      16. result = mapper.insert(stu);
      17. }catch(Exception e){
      18. e.printStackTrace();
      19. }finally {
      20. //6.释放资源
      21. if(sqlSession!=null){
      22. sqlSession.close();
      23. }
      24. if(is!=null){
      25. try {
      26. is.close();
      27. } catch (IOException e) {
      28. e.printStackTrace();
      29. }
      30. }
      31. }
      32. //7.返回结果
      33. return result;
      34. }
      1. @Override
      2. public Integer update(Student stu) {
      3. Integer result = null;
      4. SqlSession sqlSession = null;
      5. InputStream is = null;
      6. try{
      7. //1.加载核心配置文件
      8. is = Resources.getResourceAsStream("MybatisConfig.xml");
      9. //2.获取SqlSession工厂对象
      10. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
      11. //3.通过工厂对象获取SqlSession
      12. sqlSession= sqlSessionFactory.openSession(true);
      13. //4.获取StudentMapper接口的实现类对象
      14. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);//StudentMapper mapper = new StudentMapperImpl();
      15. //5.通过实现类对象调用方法,接收结果
      16. result = mapper.update(stu);
      17. }catch(Exception e){
      18. e.printStackTrace();
      19. }finally {
      20. //6.释放资源
      21. if(sqlSession!=null){
      22. sqlSession.close();
      23. }
      24. if(is!=null){
      25. try {
      26. is.close();
      27. } catch (IOException e) {
      28. e.printStackTrace();
      29. }
      30. }
      31. }
      32. //7.返回结果
      33. return result;
      34. }
      1. @Override
      2. public Integer delete(Integer sid) {
      3. Integer result = null;
      4. SqlSession sqlSession = null;
      5. InputStream is = null;
      6. try{
      7. //1.加载核心配置文件
      8. is = Resources.getResourceAsStream("MybatisConfig.xml");
      9. //2.获取SqlSession工厂对象
      10. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
      11. //3.通过工厂对象获取SqlSession
      12. sqlSession= sqlSessionFactory.openSession(true);
      13. //4.获取StudentMapper接口的实现类对象
      14. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);//StudentMapper mapper = new StudentMapperImpl();
      15. //5.通过实现类对象调用方法,接收结果
      16. result = mapper.delete(sid);
      17. }catch(Exception e){
      18. e.printStackTrace();
      19. }finally {
      20. //6.释放资源
      21. if(sqlSession!=null){
      22. sqlSession.close();
      23. }
      24. if(is!=null){
      25. try {
      26. is.close();
      27. } catch (IOException e) {
      28. e.printStackTrace();
      29. }
      30. }
      31. }
      32. //7.返回结果
      33. return result;
      34. }
    • 分析动态代理对象是如何生成的

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

    • 动态代理想执行方法需要借助invoke方法
    • 在其中,帮助执行功能的对象调用execute方法(传进sqlSession和需要的实际参数)
    • 在execute方法中,弄了个switch语句
    • 在这里面它获取到了执行的类型,然后通过case进行匹配
    • 它底层还是借助sqlSession里面的方法(如insert方法)来执行功能的
    • 动态代理实现类对象在执行方法的时候最终调用了mapperMethod.execute()方法,这个方法中通过switch语句根据操作类型来判断是新增,修改,删除,查询操作
    • 最后一步回到了Mybatis最原生的sqlSession方式来执行增删查改
  • 相关阅读:
    Go Web——Gin使用session
    数组和对象的解构
    23种模式之一— — — —适配器模式的详细介绍与讲解
    Android Material Design之BottomAppBar(八)
    全志A40i PRREMPT-RT Linux平台搭建IgH环境
    电脑在开机时出现了bootmenu
    windows系统中用windbg收集程序崩溃信息
    基于Java的个性化旅游攻略系统设计与实现(源码+lw+ppt+部署文档+视频讲解等)
    关于变电站综合自动化系统的案例应用分析-安科瑞 蒋静
    基于springboot+vue的餐饮管理系统实现与设计(源码+数据库+文档)
  • 原文地址:https://blog.csdn.net/weixin_59624686/article/details/126150592