四、MyBatis 框架 Dao 动态代理
生活随笔
收集整理的這篇文章主要介紹了
四、MyBatis 框架 Dao 动态代理
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.1 步驟
(1) 去掉 之前編寫的Dao 接口實現(xiàn)類
(2) getMapper 獲取代理對象
只需調(diào)用 SqlSession 的 getMapper()方法,即可獲取指定接口的實現(xiàn)類對象。該方法的參數(shù)為指定 Dao接口類的 class 值。
不使用工具類:
SqlSession session = factory.openSession(); StudentDao dao = session.getMapper(StudentDao.class);使用工具類:
SqlSession sqlSession = MybatisUtils.getSqlSession(); // 這句代碼可以自動創(chuàng)建dao接口的實現(xiàn)類對象 StudentDao dao = sqlSession.getMapper(StudentDao.class);(3) 使用 Dao 代理對象方法執(zhí)行 sql 語句
@Testpublic void testSelectStudents() {/*** 使用mybatis的動態(tài)代理機制,使用SqlSession.getMapper(dao接口)* getMapper能夠獲取dao接口對應(yīng)的實現(xiàn)類對象。*/SqlSession sqlSession = MybatisUtils.getSqlSession();StudentDao dao = sqlSession.getMapper(StudentDao.class); // 這句代碼可以自動創(chuàng)建dao接口的實現(xiàn)類對象//調(diào)用dao的方法,執(zhí)行數(shù)據(jù)庫的操作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("添加對象的數(shù)量:" + 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("更新對象的數(shù)量:" + nums);}@Testpublic void testDeleteStudents() {SqlSession sqlSession = MybatisUtils.getSqlSession();StudentDao dao = sqlSession.getMapper(StudentDao.class);int nums = dao.deleteStudent(1003);sqlSession.commit();System.out.println("刪除對象的數(shù)量:" + nums);}在上一篇博文三、MyBatis 使用傳統(tǒng) Dao 開發(fā)方式的基礎(chǔ)上,只做了以下更改:
所以缺少的代碼請參考三、MyBatis 使用傳統(tǒng) Dao 開發(fā)方式
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的動態(tài)代理機制,使用SqlSession.getMapper(dao接口)* getMapper能夠獲取dao接口對應(yīng)的實現(xiàn)類對象。*/SqlSession sqlSession = MybatisUtils.getSqlSession();StudentDao dao = sqlSession.getMapper(StudentDao.class); // 這句代碼可以自動創(chuàng)建dao接口的實現(xiàn)類對象//調(diào)用dao的方法,執(zhí)行數(shù)據(jù)庫的操作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("添加對象的數(shù)量:" + 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("更新對象的數(shù)量:" + nums);}@Testpublic void testDeleteStudents() {SqlSession sqlSession = MybatisUtils.getSqlSession();StudentDao dao = sqlSession.getMapper(StudentDao.class);int nums = dao.deleteStudent(1003);sqlSession.commit();System.out.println("刪除對象的數(shù)量:" + nums);}}總結(jié)
以上是生活随笔為你收集整理的四、MyBatis 框架 Dao 动态代理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hdfs复制文件夹_HDFS常用命令
- 下一篇: 论文阅读 - Video Swin Tr