四、MyBatis 框架 Dao 动态代理
生活随笔
收集整理的這篇文章主要介紹了
四、MyBatis 框架 Dao 动态代理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.1 步驟
(1) 去掉 之前編寫的Dao 接口實現類
(2) getMapper 獲取代理對象
只需調用 SqlSession 的 getMapper()方法,即可獲取指定接口的實現類對象。該方法的參數為指定 Dao接口類的 class 值。
不使用工具類:
SqlSession session = factory.openSession(); StudentDao dao = session.getMapper(StudentDao.class);使用工具類:
SqlSession sqlSession = MybatisUtils.getSqlSession(); // 這句代碼可以自動創建dao接口的實現類對象 StudentDao dao = sqlSession.getMapper(StudentDao.class);(3) 使用 Dao 代理對象方法執行 sql 語句
@Testpublic void testSelectStudents() {/*** 使用mybatis的動態代理機制,使用SqlSession.getMapper(dao接口)* getMapper能夠獲取dao接口對應的實現類對象。*/SqlSession sqlSession = MybatisUtils.getSqlSession();StudentDao dao = sqlSession.getMapper(StudentDao.class); // 這句代碼可以自動創建dao接口的實現類對象//調用dao的方法,執行數據庫的操作List<Student> students = dao.selectStudents();for (Student student : students) {System.out.println("學生=" + student);}}@Testpublic void testInsertStudents() {SqlSession sqlSession = MybatisUtils.getSqlSession();StudentDao dao = sqlSession.getMapper(StudentDao.class);Student student = new Student();student.setId(1004);student.setName("亞瑟");student.setEmail("yase@qq.com");student.setAge(35);int nums = dao.insertStudent(student);sqlSession.commit();System.out.println("添加對象的數量:" + nums);}@Testpublic void testUpdateStudents() {SqlSession sqlSession = MybatisUtils.getSqlSession();StudentDao dao = sqlSession.getMapper(StudentDao.class);Student student = new Student();student.setId(1002);student.setAge(222);int nums = dao.updateStudent(student);sqlSession.commit();System.out.println("更新對象的數量:" + nums);}@Testpublic void testDeleteStudents() {SqlSession sqlSession = MybatisUtils.getSqlSession();StudentDao dao = sqlSession.getMapper(StudentDao.class);int nums = dao.deleteStudent(1003);sqlSession.commit();System.out.println("刪除對象的數量:" + nums);}在上一篇博文三、MyBatis 使用傳統 Dao 開發方式的基礎上,只做了以下更改:
所以缺少的代碼請參考三、MyBatis 使用傳統 Dao 開發方式
package com.zep;import com.zep.dao.StudentDao; import com.zep.domain.Student; import com.zep.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test;import java.util.List;public class TestMybatis {@Testpublic void testSelectStudents() {/*** 使用mybatis的動態代理機制,使用SqlSession.getMapper(dao接口)* getMapper能夠獲取dao接口對應的實現類對象。*/SqlSession sqlSession = MybatisUtils.getSqlSession();StudentDao dao = sqlSession.getMapper(StudentDao.class); // 這句代碼可以自動創建dao接口的實現類對象//調用dao的方法,執行數據庫的操作List<Student> students = dao.selectStudents();for (Student student : students) {System.out.println("學生=" + student);}}@Testpublic void testInsertStudents() {SqlSession sqlSession = MybatisUtils.getSqlSession();StudentDao dao = sqlSession.getMapper(StudentDao.class);Student student = new Student();student.setId(1004);student.setName("亞瑟");student.setEmail("yase@qq.com");student.setAge(35);int nums = dao.insertStudent(student);sqlSession.commit();System.out.println("添加對象的數量:" + nums);}@Testpublic void testUpdateStudents() {SqlSession sqlSession = MybatisUtils.getSqlSession();StudentDao dao = sqlSession.getMapper(StudentDao.class);Student student = new Student();student.setId(1002);student.setAge(222);int nums = dao.updateStudent(student);sqlSession.commit();System.out.println("更新對象的數量:" + nums);}@Testpublic void testDeleteStudents() {SqlSession sqlSession = MybatisUtils.getSqlSession();StudentDao dao = sqlSession.getMapper(StudentDao.class);int nums = dao.deleteStudent(1003);sqlSession.commit();System.out.println("刪除對象的數量:" + nums);}}總結
以上是生活随笔為你收集整理的四、MyBatis 框架 Dao 动态代理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hdfs复制文件夹_HDFS常用命令
- 下一篇: 指令系统——数据寻址(2)(详解)