zoukankan      html  css  js  c++  java
  • 8.Mapper动态代理

    在前面例子中自定义 Dao 接口实现类时发现一个问题:Dao 的实现类其实并没有干什么 实质性的工作,

    它仅仅就是通过 SqlSession 的相关 API 定位到映射文件 mapper 中相应 id SQL 语句,

    真正对 DB 进行操作的工作其实是由框架通过 mapper 中的 SQL 完成的。

    所以,MyBatis 框架就抛开了 Dao 的实现类,直接定位到映射文件 mapper 中的相应 SQL语句,对 DB 进行操作。

    这种对 Dao 的实现方式称为 Mapper 的动态代理方式。

    Mapper 动态代理方式无需程序员实现 Dao 接口。接口是由 MyBatis 结合映射文件自动 生成的动态代理实现的。

      1.映射文件的 namespace 属性值

        一般情况下,一个Dao接口的实现类方法使用的是同一个SQL映射文件中的SQL映射id

        所以,MyBatis框架要求,将映射文件中<mapper/>标签的namespace 属性设为 Dao接口的全类名,

        则系统会根据方法所属Dao接口,自动到相应 namespace 的映射文件中查找相关的SQL映射

    1 <!-- 使用 mapper 进行动态代理的前提之一:需要将 mapper 的命名空间 定义为 接口的完整类名 -->
    2 <!-- 系统会根据方法所属DAO接口,自动到响应的 namespace 的映射文件中,查找相关的SQL映射 -->
    3 
    4 <mapper namespace="com.mybatis.dao.StudentDAO" >

      2.修改日志输出控制文件

      3.Dao接口方法名

    MyBatis 框架要求,接口中的方法名,与映射文件中相应的SQL 标签的id值相同。

    系统会自动根据方法名到相应的映射文件中查找同名的SQL映射d

    1 @Test
    2     public void testAdd() {
    3         //使用 动态 mapper 的前提之二:DAO中的方法,需要跟 mapper.xml 文件的 的 statement的 id 相对应
    4         studentDAO.insertStudent(s);
    5         sqlSession.commit();
    6     }

      4.Dao对象的获取

      使用时,只需调用SqlSession的getMapper方法,即可获取指定接口的实现类对象

      该方法的参数为指定Dao接口类的class值

     

      5.删除Dao实现类

      由于通过调用 Dao 接口的方法,不仅可以从 SQL 映射文件中找到所要执行 SQL 语句,还可通过方法参数及返回值,

      将 SQL 语句的动态参数传入,将查询结果返回。所以,Dao 的 实现工作,完全可以由 MyBatis 系统自动根据映射文件完成。

    所以,Dao 的实现类就不再需 要了。Dao 实现对象是由 JDK 的 Proxy 动态代理自动生成的

      6.修改测试类

     1 Student s = null;
     2     private SqlSession sqlSession;
     3     private StudentDAO studentDAO;
     4     @Before
     5     public void before() {
     6         s= new Student();
     7         s.setAge(25);
     8         s.setName("xzk5");
     9         s.setScore(99);
    10         
    11         sqlSession = SqlSessionUtil.getSqlSession();
    12         studentDAO = sqlSession.getMapper(StudentDAO.class);
    13     }
    14     
    15     
    16     @After
    17     public void after() {
    18         if(sqlSession != null) {
    19             sqlSession.close();            
    20         }
    21     }
    22     
    23     //动态代理的过程
    24     //1.首先通过 sqlSession.getMapper方法 ,创建代理对象,对studentDAO 进行代理
    25     //2.通过接口名定位到映射文件 mapper
    26     //3.通过接口的 方法名 定位到 映射文件 mapper中 对应的 SQL语句 
    27     //Mapper 动态代理方式 无需程序员 实现 DAO 接口
    28     //接口是由MyBatis 结合 映射文件自动生成的动态代理实现的  ,(这个代理对象就是dao的实现)
    29     
    30     @Test
    31     public void testAdd() {
    32         //使用 动态 mapper 的前提之二:DAO中的方法,需要跟 mapper.xml 文件的 的 statement的 id 相对应
    33         studentDAO.insertStudent(s);
    34         sqlSession.commit();
    35     }

    多查询条件无法接收的问题

      在实际工作中,表单中所给出的查询条件有时是无法将其封装为一个对象的,也就是说,查询方法只能携带多个参数,

      而不能携带将这多个参数进行封装的一个对象。对于这个问题, 有两种解决方案

    (1)将多个参数封装为一个Map

     1 @Test
     2     public void testSelectStudentsByMap() {
     3         Map<String,Object> map = new HashMap<String,Object>();
     4         map.put("conName", "xx");
     5         map.put("conScore", "59");
     6         map.put("conStu", s);        //s 是一个 student对象
     7         List<Student> students = studentDAO.selectStudentsByMap(map);
     8         for(Student student:students) {
     9             System.out.println(student);
    10         }
    11         
    12     }
    1 <select id="selectStudentsByMap" resultType="com.mybatis.model.Student">
    2         select * from student where name like '%' #{conName} '%' and score > #{conScore} and age > #{conStu.age}
    3         <!-- #{} 可以 1.放入 Map的key  2. 对象的属性-->
    4 </select>

    (2)多个参数逐个接收

     对于mapper中的SQL语句,而已通过参数索引 #{index}的方式逐个接收每个参数

    1 <select id="selectStudentsByMap" resultType="com.mybatis.model.Student">
    2         <!-- select * from student where name like '%' #{conName} '%' and score > #{conScore} and age > #{conStu.age} -->
    3         <!-- #{} 可以放入 1. Map的key  2. Map的value的对象的属性-->
    4         
    5         select * from student where name like '%' #{0} '%'  and age > #{1}
    6         <!-- #{} 可以放入 3.参数的索引-->
    7         <!-- 另外:4.可以放入任意字符,构成占位符   5.放入对象的属性 --></select>
    1 @Test
    2     public void testSelectStudentsByConditions() {
    3         List<Student> students = studentDAO.selectStudentsByConditions("xx",24);
    4         for(Student student:students) {
    5             System.out.println(student);
    6         }
    7         
    8     }
    
    
    
     

      

  • 相关阅读:
    POJ-1465 Multiple
    POJ-2585 Window Pains
    POJ-1094 Sorting It All Out
    Linux基础学习笔记1
    建议3---理解Python与C语言的不同之处
    堆排序的Python实现
    迭代器和生成器详解
    建议2---编写pythonic代码
    python中Switch/Case实现
    建议1---理解Pythonic的概念
  • 原文地址:https://www.cnblogs.com/xuzekun/p/7422305.html
Copyright © 2011-2022 走看看