• 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方式来执行增删查改
  • 相关阅读:
    Linux安装redis详细教程
    在线升级 redis 到7.2.2
    excel表格xlsx解密在线网站,excel表格xlsx权限密码多少?
    026-从零搭建微服务-文件服务(二)
    Thinkpad x13 锐龙安装 Archlinux 记录
    仿牛客网项目第二章:开发社区登录模块(详细步骤和思路)
    关于foreach标签传值为list的拼接条件问题,得用size来判断非空
    论文浅尝 | 思维树:使用大语言模型反复思考解决问题
    HTTP 与 HTTPS-HTTP 与 HTTPS 有哪些区别?
    Android帧率监测与优化技巧
  • 原文地址:https://blog.csdn.net/weixin_59624686/article/details/126150592