十一、持久层框架设计实现及MyBatis源码分析-MyBatis基础回顾及高级应用-MyBatis的Dao层传统开发方式回顾(五)

MyBatis的Dao实现层有两种方式

一、传统开发⽅式

1、编写IUserDao接⼝

package study.lagou.com.dao;

import study.lagou.com.pojo.User;

import java.io.IOException;
import java.util.List;

public interface IUserDao {

    List findAll() throws IOException;
}

2、编写UserDaoImpl实现

package study.lagou.com.dao.impl;

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 study.lagou.com.dao.IUserDao;
import study.lagou.com.pojo.User;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Description: 功能描述
 * @Author houjh
 * @Email: [email protected]
 * @Date: 2021-3-23 15:19
 */
public class UserDaoImpl implements IUserDao {

    @Override
    public List findAll() throws IOException {
        //1、Resources工具类,完成配置文件的加载,将配置文件加载成字节输入流
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        //2、SqlSessionFactoryBuilder通过构建者模式解析配置文件,并创建了sqlSessionFactory工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //3、sqlSessionFactory通过工厂模式来创建sqlSession,此处需要注意sqlSessionFactory.openSession()会默认开启一个事务,但是
        //该事务不会自动提交,在进行增删改操作时,需要手动提交事务,也要直接通过sqlSessionFactory.openSession(true)方法来设置自动提
        //交事务信息
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //4、sqlSession调用方法,查询所有调用selectList 查询单个调用selectOne 添加调用insert 修改调用update 删除调用delete
        List users = sqlSession.selectList("study.lagou.com.mapper.findAll");
        //方法调用完成注意关闭sqlSession
        sqlSession.close();
        return users;
    }
}

3、编写测试类进行传统方法测试

package study.lagou.com.test;

import org.junit.Test;
import study.lagou.com.dao.IUserDao;
import study.lagou.com.dao.impl.UserDaoImpl;
import study.lagou.com.pojo.User;

import java.io.IOException;
import java.util.List;

/**
 * @Description: MyBatis传统开发方式测试
 * @Author houjh
 * @Email: [email protected]
 * @Date: 2021-3-22 19:48
 */
public class MyBatisTraditionalTest {

    @Test
    public void testFindAll() throws IOException {
        IUserDao userDao = new UserDaoImpl();
        List users = userDao.findAll();
        for (User user : users) {
            System.out.println(user);
        }
    }


}

二、代理开发⽅式

代理开发⽅式介绍

采⽤ Mybatis 的代理开发⽅式实现 DAO 层的开发,这种⽅式是我们后⾯进⼊企业的主流。
Mapper 接⼝开发⽅法只需要程序员编写Mapper 接⼝(相当于Dao 接⼝),由Mybatis 框架根据接⼝定义创建接⼝的动态代理对象,代理对象的⽅法体同上边Dao接⼝实现类⽅法。

Mapper 接⼝开发需要遵循以下规范:
1、 Mapper.xml⽂件中的namespace与mapper接⼝的全限定名相同
2、Mapper接⼝⽅法名和Mapper.xml中定义的每个statement的id相同
3、 Mapper接⼝⽅法的参数类型和mapper.xml中定义的每个sql的parameterType的类型相同
4、Mapper接⼝⽅法的返回值类型和mapper.xml中定义的每个sql的resultType的类型相同

为不影响之前代码的演示使用,我们不对之前的代码进行修改,直接新建一套接口及mapper.xml配置文件

1、编写IUserProxyDao接⼝

package study.lagou.com.dao;

import study.lagou.com.pojo.User;

import java.util.List;

public interface IUserProxyDao {

    List findAll();
}

2、编写UserProxyMapper.xml配置文件






    
    

3、修改sqlMapConfig.xml核心配置文件,在Mappers中将引入UserProxyMapper.xml配置文件


        


    
        
        
            
            
            
            
        
    


    
    


4、编写测试类

package study.lagou.com.test;

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 study.lagou.com.dao.IUserProxyDao;
import study.lagou.com.pojo.User;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Description: MyBatis传统开发方式测试
 * @Author houjh
 * @Email: [email protected]
 * @Date: 2021-3-22 19:48
 */
public class MyBatisProxyTest {

    @Test
    public void testFindAll() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        IUserProxyDao mapper = sqlSession.getMapper(IUserProxyDao.class);
        List all = mapper.findAll();
        for (User user : all) {
            System.out.println(user);
        }
    }


}

上一篇笔记地址:https://www.jianshu.com/p/f138ca176525

下一篇笔记地址:https://www.jianshu.com/p/3773a8319f9f

具体代码对应下载地址:https://gitee.com/happymima/mybatis.git

你可能感兴趣的:(十一、持久层框架设计实现及MyBatis源码分析-MyBatis基础回顾及高级应用-MyBatis的Dao层传统开发方式回顾(五))