1. mapper介绍
    1. mapper使用规则:按业务划分,一个业务模块相关的sql均定义在一个mapper文件
    2. mapper的xml格式:
      1. doctype:

        • <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
          "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
      2. 使用mapper标签包含:
        • <mapper namespace="com.pro.dao.UserDao"></mapper>
          
  2. mapper概览
    • cache:指定名称的缓存
    • cache-ref:引用其他文件的命名配置
    • resultMap:描述如何加载数据库结果的对象
    • sql:可重用的sql块,也可以被其他语句使用
    • insert:插入
    • update:更新
    • delete:删除
    • select:查询  
  3. 使用介绍
    1. select标签

      <select
      id="selectPerson" //唯一标识sql语句,与接口中的标识一致。
      parameterType="int"//入参类型
      resultType="hashmap"//期望返回类型的类名或别名,集合时,应该是集合可以包含的类型,而不能是集合本身
      resultMap="personResultMap"//引用resultMap标签定义
      flushCache="false"//设置为true,清空缓存
      useCache="true"//使用cache:本条结果将被缓存
      timeout="10000"
      fetchSize="256"
      statementType="PREPARED"//Statement,PreparedStatement 或 CallableStatement。 默认值:PREPARED
      resultSetType="FORWARD_ONLY">//FORWARD_ONLY|SCROLL_SENSITIVE|SCROLL_INSENSITIVE 中的一种。默认是不设置(驱动自行处理)。 多表查询:
      <!-- Very Complex Statement -->
      <select id="selectBlogDetails" resultMap="detailedBlogResultMap">
      select
      B.id as blog_id,
      B.title as blog_title,
      B.author_id as blog_author_id,
      A.id as author_id,
      A.username as author_username,
      A.password as author_password,
      A.email as author_email,
      A.bio as author_bio,
      A.favourite_section as author_favourite_section,
      P.id as post_id,
      P.blog_id as post_blog_id,
      P.author_id as post_author_id,
      P.created_on as post_created_on,
      P.section as post_section,
      P.subject as post_subject,
      P.draft as draft,
      P.body as post_body,
      C.id as comment_id,
      C.post_id as comment_post_id,
      C.name as comment_name,
      C.comment as comment_text,
      T.id as tag_id,
      T.name as tag_name
      from Blog B
      left outer join Author A on B.author_id = A.id
      left outer join Post P on B.id = P.blog_id
      left outer join Comment C on P.id = C.post_id
      left outer join Post_Tag PT on PT.post_id = P.id
      left outer join Tag T on PT.tag_id = T.id
      where B.id = #{id}
      </select>
    2. insert、update、delete标签
      • <insert id="insertAuthor"//唯一标签,与接口中的方法名称一致 parameterType="domain.blog.Author"//参数类型,限定名 flushCache="true" statementType="PREPARED" keyProperty="" keyColumn="" useGeneratedKeys="" timeout="20">
      • selectKey:selectKey 元素将会首先运行, id 会被设置,然后插入语句 会被调用
        <insert id="insertAuthor">
        <selectKey keyProperty="id" resultType="int" order="BEFORE">
        select CAST(RANDOM()*1000000 as INTEGER) a from SYSIBM.SYSDUMMY1
        </selectKey>
        insert into Author
        (id, username, password, email,bio, favourite_section)
        values
        (#{id}, #{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR})
        </insert>
    3. sql标签
      • 这个元素可以被用来定义可重用的 SQL 代码段,可以包含在其他语句中
      • <sql id="userColumns"> id,username,password </sql>
        <select id="selectUsers" resultType="map">
        select <include refid="userColumns"/>
        from some_table
        where id = #{id}
        </select>
        <insert id="insertUser" parameterType="User">
          insert into users (id, username, password) values (#{id}, #{username}, #{password})
        </insert>
    4. resultMap标签
      • 结果映射定义:
      • constructor - 类在实例化时,用来注入结果到构造方法中
        • idArg - ID 参数;标记结果作为 ID 可以帮助提高整体效能
        • arg - 注入到构造方法的一个普通结果
      • id – 一个 ID 结果;标记结果作为 ID 可以帮助提高整体效能
      • result – 注入到字段或 JavaBean 属性的普通结果
      • association – 一个复杂的类型关联;许多结果将包成这种类型
        • 嵌入结果映射 – 结果映射自身的关联,或者参考一个
        • has a关系:
        • 关联元素处理“有一个”类型的关系。
          
        • <resultMap id="blogResult" type="Blog">
          <id property="id" column="blog_id" />
          <result property="title" column="blog_title"/>
          <association property="author" column="blog_author_id" javaType="Author" resultMap="authorResult"/>
          </resultMap> <resultMap id="authorResult" type="Author">
          <id property="id" column="author_id"/>
          <result property="username" column="author_username"/>
          <result property="password" column="author_password"/>
          <result property="email" column="author_email"/>
          <result property="bio" column="author_bio"/>
          </resultMap> <select id="selectBlog" resultMap="blogResult">
          select
          B.id as blog_id,
          B.title as blog_title,
          B.author_id as blog_author_id,
          A.id as author_id,
          A.username as author_username,
          A.password as author_password,
          A.email as author_email,
          A.bio as author_bio
          from Blog B left outer join Author A on B.author_id = A.id
          where B.id = #{id}
          </select>
      • collection – 复杂类型的集
        • 嵌入结果映射 – 结果映射自身的集,或者参考一个
      • discriminator – 使用结果值来决定使用哪个结果映射case – 基于某些值的结果映射
        • 嵌入结果映射 – 这种情形结果也映射它本身,因此可以包含很多相 同的元素,或者它可以参照一个外部的结果映射。
    5. Cache标签
      • <cache/>

        字面上看就是这样。这个简单语句的效果如下:

        • 映射语句文件中的所有 select 语句将会被缓存。
        • 映射语句文件中的所有 insert,update 和 delete 语句会刷新缓存。
        • 缓存会使用 Least Recently Used(LRU,最近最少使用的)算法来收回。
        • 根据时间表(比如 no Flush Interval,没有刷新间隔), 缓存不会以任何时间顺序 来刷新。
        • 缓存会存储列表集合或对象(无论查询方法返回什么)的 1024 个引用。
        • 缓存会被视为是 read/write(可读/可写)的缓存,意味着对象检索不是共享的,而 且可以安全地被调用者修改,而不干扰其他调用者或线程所做的潜在修改。

        所有的这些属性都可以通过缓存元素的属性来修改。比如:

        <cache
        eviction="FIFO"
        flushInterval="60000"
        size="512"
        readOnly="true"/>

mybatis--mapper配置总结的更多相关文章

  1. mybatis mapper配置

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC ...

  2. Mybatis中配置Mapper的方法

    在这篇文章中我主要想讲一下Mybatis配置文件中mappers元素的配置.关于基础部分的内容可以参考http://haohaoxuexi.iteye.com/blog/1333271. 我们知道在M ...

  3. MyBatis—mapper.xml映射配置

    SQL文件映射(mapper文件),几个顶级元素的配置: mapper元素:根节点只有一个属性namespace(命名空间)作用: 1:用于区分不同的mapper,全局唯一. 2:绑定DAO接口,即面 ...

  4. mybatis mapper xml文件配置resultmap时,id行和result行有什么区别?

    mybatis mapper xml文件配置resultmap时,id行和result行有什么区别? <resultMap id = "CashInvoiceMap" typ ...

  5. MyBatis工程搭建&MyBatis实现Mapper配置查询

    一.MyMyBatis工程搭建 新建Maven项目:mybatis-demo 准备数据源 1 # 删除mybatis_demo数据库 2 drop database if exists mybatis ...

  6. Mybatis XML配置

    Mybatis常用带有禁用缓存的XML配置 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ...

  7. MyBatis Cache配置

    @(MyBatis)[Cache] MyBatis Cache配置 MyBatis提供了一级缓存和二级缓存 配置 全局配置 配置 说明 默认值 可选值 cacheEnabled 全局缓存的开关 tru ...

  8. mybatis mapper.xml 配置文件问题(有的错误xml是不报的) 导致服务无法启动 。

    转载自 开源编程 一舟mybatsi xml编译报错,tomcat启动一直循环,导致内存溢出,启动失败 mapper.xml怎么知道有没有编译错误,哪个位置有错误 这应该是mybatis的一个bug, ...

  9. spring和mybatis整合配置

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  10. SSM ( Spring 、 SpringMVC 和 Mybatis )配置详解

    使用 SSM ( Spring . SpringMVC 和 Mybatis )已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没 ...

随机推荐

  1. Python 小知识点(7)--类的创建方式

    1. 创建类的第1方式(常用) class Foo(object): def func(self): print("Hello Foo") 2.创建类的第2方式 def func( ...

  2. Python实践练习:生成随机的测验试卷文件

    题目 假如你是一位地理老师,班上有 35 名学生,你希望进行美国各州首府的一个小测验.不妙的是,班里有几个坏蛋,你无法确信学生不会作弊.你希望随机调整问题的次序,这样每份试卷都是独一无二的,这让任何人 ...

  3. 了解zookeeper

    ZooKeeper操作和维护多个小型的数据节点,这些节点被称为znode,采用类似于文件系统的层级树状结构进行管理.图2-1描述了一个znode树的结构,根节点包含4个个节点,其中三个子节点拥有下一级 ...

  4. From Ruby array to JS array in Rails- 'quote'?

    From Ruby array to JS array in Rails- 'quote'? <%= raw @location_list.as_json %>

  5. Unsupported compiler 'com.apple.compilers.llvmgcc42' selected for architecture 'armv7' Xcode5

    刚刚将Xcode更新到Xcode5,一运行报如下错误: Unsupported compiler 'com.apple.compilers.llvmgcc42' selected for archit ...

  6. /etc/rc5.d/s991local: line25: eject:command not found错误

      使用虚拟机安装centos出现错误,原因是我使用的镜像是最小级别的,没有图形化界面,只有终端窗口 有人用vmware安装minimal centos报错/etc/rc5.d/s99local : ...

  7. 解决ecplise+phthon2.7中使用pytesser和tesseract进行ocr,出现报错的问题

    网上很多使用ecplise+phthon2.7中使用pytesser或者tesseract进行OCR网站验证码的案例,但配置起来实在让人崩溃. 通用步骤:1.下载了pytesser_v0.0.1: 2 ...

  8. WCF配置多个终节点

    配置多个终节点的意义(自己理解):一个服务可以有多个终节点,网上也经常有人说终节点才是服务的真正的接口,的确如此,当我们为一个服务配置多个终节点时,就表明这个服务可以被以不同的方式访问(不同的绑定等等 ...

  9. 在Ajax请求中什么时候用GET方式什么时候用POST方式?

    当我们有大量数据要传送时最好的办法是一次发出多个只传递少量信息的Ajax调用时.如果你正用一个Ajax调用发送大量数据,那么最好是结束这种做法,因为这样做并不能节约时间. 因此,需要传送大量数据能成为 ...

  10. Jquery循环select标签,并给指定option添加select属性后在页面上不显示的问题

    <select id="testId"> <option value="">--请选择--</option> <opt ...