zoukankan      html  css  js  c++  java
  • MyBatis加强版_1_接口式编程

    next:MyBatis加强版_2_分页

    **************************************

    mybaits接口式编程:规范参数,与spring结合时非常重要

     sql数据库的xml文件:  

    <mapper namespace="com.imooc.dao.IMessage">

      <resultMap type="com.imooc.bean.Message" id="MessageResult">
        <id column="ID" jdbcType="INTEGER" property="id"/>
        <result column="COMMAND" jdbcType="VARCHAR" property="command"/>
        <result column="DESCRIPTION" jdbcType="VARCHAR" property="description"/>
        <result column="CONTENT" jdbcType="VARCHAR" property="content"/>
      </resultMap>

      <select id="queryMessageList" parameterType="com.imooc.bean.Message" resultMap="MessageResult">
        select <include refid="columns"/> from MESSAGE
        <where>
         <if test="command != null and !&quot;&quot;.equals(command.trim())">
          and COMMAND=#{command}
         </if>
         <if test="description != null and !&quot;&quot;.equals(description.trim())">
          and DESCRIPTION like '%' #{description} '%'
         </if>
        </where>
      </select>
     
      <sql id="columns">ID,COMMAND,DESCRIPTION,CONTENT</sql>
     
    </mapper>

     
    接口:

    package com.imooc.dao;

    import java.util.List;

    import com.imooc.bean.Message;

    /**
     * 与Message配置文件相对应的接口
     */
    public interface IMessage {
     /**
      * 根据查询条件查询消息列表
      */
     public List<Message> queryMessageList(Message message);
    }

    接口的使用:

    /**
     * 和message表相关的数据库操作
     */
    public class MessageDao {
     
     /**
      * 根据查询条件查询消息列表
      */
     public List<Message> queryMessageList(Message message) {
      DBAccess dbAccess = new DBAccess();
      List<Message> messageList = new ArrayList<Message>();
      SqlSession sqlSession = null;
      try {
       sqlSession = dbAccess.getSqlSession();
       // 通过sqlSession执行SQL语句
       IMessage imessage = sqlSession.getMapper(IMessage.class);
       messageList = imessage.queryMessageList(message);
      } catch (Exception e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      } finally {
       if(sqlSession != null) {
        sqlSession.close();
       }
      }
      return messageList;
     }
     

    ==============================================================

    Mybatis是接口式编程实现对.xml中sql语句的执行,其过程如下(取自慕课网视频《通过自动回复机器人学Mybatis---加强版》):http://www.imooc.com/learn/260

    1.加载配置信息
    2.通过加载配置信息加载一个代理工厂Map,是HashMap的实例
    3.这个Map存放的是接口Class与对应的代理工厂,接口作为key,代理工厂作为键值,代理工厂为MapperProxyFactory的实例。
    4.通过接口的Class从代理工厂Map取出对应的代理工厂
    5.在代理工厂的newInstance()方法中实现一个调用处理器(MapperProxy<T> 的实例)。通过代理工厂实例化一个代理类。
    6.用这个代理类生成一个代理实例返回出去

    //配置文件中的重要信息全部放在了调用处理器中,在调用处理器的invoke方法中,用method(xml中sql语句的id)作为参数构造一个MapperMethod实例,在该实例中包含了SqlCommand和MethodSignature的实例。SqlCommand实例中又包含了namespace.id,标签的类型type,标签的id(name)。而MethodSignature的实例中则包含了接口的方法的返回类型。

    7.通过接口与method获取对应的配置文件中的信息:接口名称(全限定名).方法名==namespace.id

    ================================================

    2-2 接口式编程

    动态代理  参见代理的模式

    解决几个问题

    接口式编程的实现依靠的是动态代理

    imessage.queryMessageList();

    动态代理的过程:

    首先需要一个是实现了InvocationHandler的类

    MapperProxy implements InvocationHandler (MapperProxy是Mybatis的源码使用的)

      类里有个方法:MapperProxy.invoke()

     2.MapperProxy.invoke()==sqlSession.selectList()??

    SQL语句在配置文件中,总配置文件加载时,SQL的配置文件也一起加载了

    如果接口的信息与SQL的配置文件能对应上的话

    .

    3.IMessage imessage= Proxy.newProxyInstance()??

    泛型在起作用,传递的参数时IMessage类型的class,就可以用IMessage接受返回值

  • 相关阅读:
    ES6 Symbol类型 附带:Proxy和Set
    why updating the Real DOM is slow, what is Virtaul DOM, and how updating Virtual DOM increase the performance?
    React高级指南
    池(Pool)
    计算机网络Intro
    React基础
    CommonJS 与 ES6 的依赖操作方法(require、import)
    webpack初识(biaoyansu)
    关于时间安排贪心算法正确性的证明
    DP总结
  • 原文地址:https://www.cnblogs.com/charles999/p/6739780.html
Copyright © 2011-2022 走看看