MyBatis精简版--实现接口代理方式实现Mapper(Dao) 和动态SQL

Posted GYTTking

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis精简版--实现接口代理方式实现Mapper(Dao) 和动态SQL相关的知识,希望对你有一定的参考价值。

MyBatis接口代理方式实现Dao层

接口代理方式-实现规则

  • 传统方式实现Dao层,我们既要写接口。还要写实现类。而MyBatis框架可以帮助我们省略写Dao层接口实现类的步骤。程序员只需要编写接口,由MyBatis框架根据接口的定义来创

该接口的动态代理对象。

  • 实现规则

  • 1.映射配置文件中的名称空间必须和Dao层接口的全类名相同

  • 2.映射配置文件中的增删改查标签的id属性必须和Dao层接口的方法名相同

  • 3.映射配置文件中的增删改查标签的paramrterType属性必须和Dao层接口方法的参数相同

  • 4.映射配置文件中的增删改查标签的resultType属性必须和Dao层接口方法的返回值相同

接口代理方式-代码实现

  • 1.删除mapper层接口的实现类

  • 2.修改映射配置文件

  • 3.修改service层接口的实现类,采用接口代理方式实现功能

  • 删除mapper层接口的实现类

  • 修改映射配置文件
package com.itheima.service.impl;
 
import com.itheima.bean.Student;
import com.itheima.mapper.StudentMapper;
import com.itheima.service.StudentService;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
/*
    业务层实现类
 */
public class StudentServiceImpl implements StudentService {
 
    @Override
    public List<Student> selectAll() {
        List<Student> list = null;
        SqlSession sqlSession = null;
        InputStream is = null;
        try{
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
 
            //2.获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
 
            //3.通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
 
            //4.获取StudentMapper接口的实现类对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); // StudentMapper mapper = new StudentMapperImpl();
 
            //5.通过实现类对象调用方法,接收结果
            list = mapper.selectAll();
 
        } catch (Exception e) {
 
        } finally {
            //6.释放资源
            if(sqlSession != null) {
                sqlSession.close();
            }
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
 
        //7.返回结果
        return list;
    }
 
    @Override
    public Student selectById(Integer id) {
        Student stu = null;
        SqlSession sqlSession = null;
        InputStream is = null;
        try{
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
 
            //2.获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
 
            //3.通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
 
            //4.获取StudentMapper接口的实现类对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); // StudentMapper mapper = new StudentMapperImpl();
 
            //5.通过实现类对象调用方法,接收结果
            stu = mapper.selectById(id);
 
        } catch (Exception e) {
 
        } finally {
            //6.释放资源
            if(sqlSession != null) {
                sqlSession.close();
            }
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
 
        //7.返回结果
        return stu;
    }
 
    @Override
    public Integer insert(Student stu) {
        Integer result = null;
        SqlSession sqlSession = null;
        InputStream is = null;
        try{
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
 
            //2.获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
 
            //3.通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
 
            //4.获取StudentMapper接口的实现类对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); // StudentMapper mapper = new StudentMapperImpl();
 
            //5.通过实现类对象调用方法,接收结果
            result = mapper.insert(stu);
 
        } catch (Exception e) {
 
        } finally {
            //6.释放资源
            if(sqlSession != null) {
                sqlSession.close();
            }
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
 
        //7.返回结果
        return result;
    }
 
    @Override
    public Integer update(Student stu) {
        Integer result = null;
        SqlSession sqlSession = null;
        InputStream is = null;
        try{
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
 
            //2.获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
 
            //3.通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
 
            //4.获取StudentMapper接口的实现类对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); // StudentMapper mapper = new StudentMapperImpl();
 
            //5.通过实现类对象调用方法,接收结果
            result = mapper.update(stu);
 
        } catch (Exception e) {
 
        } finally {
            //6.释放资源
            if(sqlSession != null) {
                sqlSession.close();
            }
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
 
        //7.返回结果
        return result;
    }
 
    @Override
    public Integer delete(Integer id) {
        Integer result = null;
        SqlSession sqlSession = null;
        InputStream is = null;
        try{
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
 
            //2.获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
 
            //3.通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
 
            //4.获取StudentMapper接口的实现类对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); // StudentMapper mapper = new StudentMapperImpl();
 
            //5.通过实现类对象调用方法,接收结果
            result = mapper.delete(id);
 
        } catch (Exception e) {
 
        } finally {
            //6.释放资源
            if(sqlSession != null) {
                sqlSession.close();
            }
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
 
        //7.返回结果
        return result;
    }
}

  • 修改service层接口的实现类,采用接口代理方式实现功能

接口代理方式-源码分析

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

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

  • 分析方法是如何执行的?

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

接口代理方式小结

  • 接口代理方式可以让我们只编写接口即可,而实现类对象由MyBatis生成。

  • 实现规则:

  • 1.映射配置文件中的名称空间必须和Dao层接口的全类名相同

  • 2.映射配置文件中的增删改查标签的id属性必须和Dao层接口的方法名相同

  • 3.映射配置文件中的增删改查标签的paramrterType属性必须和Dao层接口方法的参数相同

  • 4.映射配置文件中的增删改查标签的resultType属性必须和Dao层接口方法的返回值相同

获取动态代理对象

  • SqlSession功能类中的getMapper()方法

  • 删除mapper中的实现类 ,保留接口

  • 在StudentMapper.xml中修改
    更改service包下 StudentServiceImpl文件

package com.itheima.service.impl;
 
import com.itheima.bean.Student;
import com.itheima.mapper.StudentMapper;
import com.itheima.service.StudentService;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
/*
    业务层实现类
 */
public class StudentServiceImpl implements StudentService {
 
    @Override
    public List<Student> selectAll() {
        List<Student> list = null;
        SqlSession sqlSession = null;
        InputStream is = null;
        try{
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
 
            //2.获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
 
            //3.通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
 
            //4.获取StudentMapper接口的实现类对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); // StudentMapper mapper = new StudentMapperImpl();
 
            //5.通过实现类对象调用方法,接收结果
            list = mapper.selectAll();
 
        } catch (Exception e) {
 
        } finally {
            //6.释放资源
            if(sqlSession != null) {
                sqlSession.close();
            }
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
 
        //7.返回结果
        return list;
    }
 
    @Override
    public Student selectById(Integer id) {
        Student stu = null;
        SqlSession sqlSession = null;
        InputStream is = null;
        try{
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
 
            //2.获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
 
            //3.通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
 
            //4.获取StudentMapper接口的实现类对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); // StudentMapper mapper = new StudentMapperImpl();
 
            //5.通过实现类对象调用方法,接收结果
            stu = mapper.selectById(id);
 
        } catch (Exception e) {
 
        } finally {
            //6.释放资源
            if(sqlSession != null) {
                sqlSession.close();
            }
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
 
        //7.返回结果
        return stu;
    }
 
    @Override
    public Integer insert(Student stu) {
        Integer result = null;
        SqlSession sqlSession = null;
        InputStream is = null;
        try{
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
 
            //2.获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
 
            //3.通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
 
            //4.获取StudentMapper接口的实现类对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); // StudentMapper mapper = new StudentMapperImpl();
 
            //5.通过实现类对象调用方法,接收结果
            result = mapper.insert(stu);
 
        } catch (Exception e) {
 
        } finally {
            //6.释放资源
            if(sqlSession != null) {
                sqlSession.close();
            }
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
 
        //7.返回结果
        return result;
    }
 
    @Override
    public Integer update(Student stu) {
        Integer result = null;
        SqlSession sqlSession = null;
        InputStream is = null;
        try{
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
 
            //2.获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
 
            //3.通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
 
            //4.获取StudentMapper接口的实现类对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); // StudentMapper mapper = new StudentMapperImpl();
 
            //5.通过实现类对象调用方法,接收结果
            result = mapper.update(stu);
 
        } catch (Exception e) {
 
        } finally {
            //6.释放资源
            if(sqlSession != null) {
                sqlSession.close();
            }
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
 
        //7.返回结果
        return result;
    }
 
    @Override
    public Integer delete(Integer id) {
        Integer result = null;
        SqlSession sqlSession = null;
        InputStream is = null;
        try{
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
 
            //2.获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
 
            //3.通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
 
            //4.获取StudentMapper接口的实现类对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); // StudentMapper mapper = new StudentMapperImpl();
 
            //5.通过实现类对象调用方法,接收结果
            result = mapper.delete(id);
 
        } catch (Exception e) {
 
        } finally {
            //6.释放资源
            if(sqlSession != null) {
                sqlSession.close();
            }
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
 
        //7.返回结果
        return result;
    }
}
  • 创建dynamic下测试类Test01
package com.itheima.dynamic;
 
import com.itheima.bean.Student;
import com.itheima.mapper.StudentMapper;
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;
import java.util.ArrayList;
import java.util.List;
 
public class Test01 {
    @Test
    public void selectCondition() throws Exception {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
 
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
 
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
 
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
 
        Student stu=new Student();
        stu.setId(2);
        stu.setName("李四");
        //stu.setAge(23);
        List<Student> list = mapper.selectCondition(stu);
 
        for (Student student:list){
            System.out.println以上是关于MyBatis精简版--实现接口代理方式实现Mapper(Dao) 和动态SQL的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis精简版--实现接口代理方式实现Mapper(Dao) 和动态SQL

MyBatis-04-笔记

Mybatis -- MyBatis的Dao层实现(传统方式实现接口代理方式实现)

Mybatis实现Mapper动态代理方式

MyBatis接口代理方式实现Dao层

使用动态代理实现dao接口