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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值