Mybatis框架----->(3) Mybatis对象分析、传统Dao开发与动态代理Dao开发的比较

一、Mybatis对象分析

1、几个常用类
(1)Resources类
  • 负责读取主配置文件
InputStream in = Resources.getResourceAsStream("mybatis.xml");
(2)SqlSessionFactoryBuilder类
  • 利用build( ) 方法创建SqlSessionFactory对象,一般会将该 SqlSessionFactoryBuilder 对象创建为一个方法内的局部对象,方法结束,对象销毁。
//3.创建SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder builder  = new SqlSessionFactoryBuilder();
//4.创建SqlSessionFactory对象
SqlSessionFactory factory = builder.build(in);
(3)SqlSessionFactory接口
  • 创建 SqlSession 需要使用 SqlSessionFactory 接口的 openSession() 方法。
SqlSession sqlSession = factory.openSession();
  • openSession(true):创建一个有自动提交功能的 SqlSession
  • openSession(false):创建一个非自动提交功能的 SqlSession,需手动提交
  • openSession():同 openSession(false)
(4)SqlSession接口
  • 定义了操作数据的方法 例如 selectOne() ,selectList() ,insert(),update(), delete(), commit(), rollback()

SqlSession对象不是线程安全的,需要在方法内部使用,在执行SQL语句之前,使用openSession()获取SqlSession对象。在执行SQL语句后需要调用其close()方法,这样才能保证他使用是线程安全的。

2、工具类(MyBatisUtil类)
public class MyBatisUtil {
    
    

    private static SqlSessionFactory factory = null;
    static {
    
    
        try {
    
    
            String config="mybatis.xml";
            //读取配置文件
            InputStream in = Resources.getResourceAsStream(config);
            //创建SqlSessionFactory对象
            factory = new SqlSessionFactoryBuilder().build(in);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }

    /**
     * 获取SqlSession对象
     */
    public static SqlSession getSqlSession(){
    
    
        SqlSession sqlSession = null;
        if (factory != null){
    
    
            sqlSession = factory.openSession();
        }
        return sqlSession;
    }
}

二、MyBatis的传统Dao开发与动态代理Dao开发

1、传统Dao开发
  • 主要使用Dao接口的实现类来操作数据库
(1)创建Dao接口的实现类
public class StudentDaoImp implements StudentDao
(2)实现接口中的方法
**
 * 查询
 * @return
 */
@Override
public List<Student> selectStudens() {
    
    
    //获取SqlSession对象
    SqlSession sqlSession = MyBatisUtil.getSqlSession();

    //6.【重要】指定要执行的SQL语句的标识,sql映射文件中的namespace+"."+标签的id值
    String sqlId = "com.hcz.dao.StudentDao"+"."+"selectStudens";
    //7.【重要】执行SQL语句,通过sqlId找到语句
    List<Student> studentList = sqlSession.selectList(sqlId);
    //9.关闭连接
    sqlSession.close();
    return studentList;
}

/**
 * 插入
 * @param student
 * @return
 */
@Override
public int insertStudent(Student student) {
    
    
    //获取SqlSession对象
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    String sqlId="com.hcz.dao.StudentDao"+"."+"insertStudent";
    //执行sql语句, 使用SqlSession类的方法
    int nums = sqlSession.insert(sqlId,student);
    //提交事务
    sqlSession.commit();
    //关闭
    sqlSession.close();
    return nums;
}
(3)定义测试方法
@Test
public void testSelectStudents(){
    
    
    StudentDao dao = new StudentDaoImp();
    List<Student> studentList = dao.selectStudens();
    for (Student student:studentList){
    
    
        System.out.println(student);
    }
}

@Test
public void testInsertStudent(){
    
    
    StudentDao dao = new StudentDaoImp();
     Student student = new Student();
     student.setId(1006);
     student.setName("李刚");
     student.setEmail("[email protected]");
     student.setAge(26);
    int result = dao.insertStudent(student);
    System.out.println("添加对象的数据为:"+result);
}

分析:

StudentDao dao = new StudentDaoImp();
List<Student> studentList = dao.selectStudens();

1、dao对象,类型是StudentDao,全限定名称是:com.hcz.dao.StudentDao与映射文件的namespace是一样的
2、dao调用的selectStudents() 方法与映射文件中的id值是一样的
3、通过dao中方法的返回值也可以确定Mybatis要调用的SqlSession的方法:
(1)如果返回值是List,调用的是SqlSession.selectList()方法
(2)如果返回值 int ,或是非List的, 看mapper文件中的标签是insert,update就会调用SqlSession的insert, update等方法

总结:

Dao类其实并没有实质性的工作,它仅仅是通过SqlSession的相关接口方法定位到映射文件中相应id的SQL语句,所以对数据库真正操作的是其实是由框架通过映射文件中的Sql语句完成的。

2、动态代理Dao开发
  • 动态代理方式(jdk动态代理机制):

MyBatis框架抛开了Dao的实现类,根据你的dao接口,创建出一个dao接口的实现类,并创建这个类的对象,完成调用dao接口中的方法,然后直接定位到映射文件中的相应的SQL语句进行数据库操作

(1)通过SqlSesssion.getMapper(dao接口)获取dao接口对应的实现类
StudentDao dao = MyBatisUtil.getSqlSession().getMapper(StudentDao.class); 
(2)通过获取到的dao接口实现类来调用接口中对应的方法执行映射文件中的SQL语句
List<Student> studentList = dao.selectStudens();

猜你喜欢

转载自blog.csdn.net/hcz666/article/details/113061978