MyBatis初探(总结)

①概念:MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以对配置和原生Map使用简单的 XML 或注解,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。

②MyBatis的功能架构:

API接口层: 提供给外部使用的接口API,开发人员通过这些本地API来操纵数据库。接口层一接收到调用请求就会调用数据处理层来完成具体的数据处理。

数据处理层:负责具体的SQL查找、SQL解析、SQL执行和执行结果映射处理等。它主要的目的是根据调用的请求完成一次数据库操作。

基础支撑层:负责最基础的功能支撑,包括连接管理、事务管理、配置加载和缓存处理,这些都是共用的东西,将他们抽取出来作为最基础的组件。为上层的数据处理层提供最基础的支撑。

③Mapper XML 文件:

resultMap –  用来描述如何从数据库结果集中来加载对象。

sql –  可被其他语句引用的可重用语句块。

insert –  映射插入语句

update –  映射更新语句

delete –  映射删除语句

select –  映射查询语句

select:查询语句是 MyBatis 中最常用的元素之一,例如

<select id="selectPerson" parameterType="int" resultType="hashmap">

  SELECT * FROM PERSON WHERE ID = #{id}

</select>    

接受一个 int(或 Integer)类型的参数,并返回一个 HashMap 类型的对象,其中的键是列名,值便是结果行中的对应值。

insert, update, delete:数据变更语句 insert,update 和 delete,例如

<insert id="insertAuthor">

  insert into Author (id,username,password,email,bio)

  values (#{id},#{username},#{password},#{email},#{bio})

</insert>

<update id="updateAuthor">

  update Author set

    username = #{username},

    password = #{password},

    email = #{email},

    bio = #{bio}

  where id = #{id}

</update>

<delete id="deleteAuthor">

  delete from Author where id = #{id}

</delete>

sql:这个元素可以被用来定义可重用的 SQL 代码段,可以包含在其他语句中。它可以静态地(在加载阶段)参数化。这个 SQL 片段可以被包含在其他语句中,例如

<select id="selectUsers" resultType="map">

  select

    <include refid="userColumns"><property name="alias" value="t1"/></include>,

    <include refid="userColumns"><property name="alias" value="t2"/></include>

  from some_table t1

    cross join some_table t2</select>

属性值也可以用于 include refid 属性或 include 子句中的属性值,例如

<sql id="sometable">

  ${prefix}Table</sql>

<sql id="someinclude">

  from

    <include refid="${include_target}"/></sql>

<select id="select" resultType="map">

  select

    field1, field2, field3

  <include refid="someinclude">

    <property name="prefix" value="Some"/>

    <property name="include_target" value="sometable"/>

  </include></select>

Result Maps: ResultMap 的设计就是简单语句不需要明确的结果映射,而很多复杂语句确实需要描述它们的关系。简单映射语句的示例了,但没有明确的 resultMap。例如:

<select id="selectUsers" resultType="map">

  select id, username, hashedPassword

  from some_table

  where id = #{id}

</select>

一个类有 3 个属性:​id​,​username ​和​ hashedPassword​。这些 在 select 语句中会精确匹配到列名,例如

<select id="selectUsers" resultType="com.someapp.model.User">

  select id, username, hashedPassword

  from some_table

  where id = #{id}

</select>

④MyBatis动态sql:可以根据不同条件拼接 SQL 语句。通常使用动态 SQL 不可能是独立的一部分,MyBatis 当然使用一种强大的动态 SQL 语言来改进这种情形,这种语言可以被用在任意的 SQL 映射语句中。

If:例如:

<select id="findActiveBlogWithTitleLike"

     resultType="Blog">

  SELECT * FROM BLOG

  WHERE state = ‘ACTIVE’

  <if test="title != null">

    AND title like #{title}

  </if>

</select>

这条语句提供了一个可选的文本查找类型的功能。如果没有传入"title",那么所有处于"ACTIVE"状态的BLOG都会返回;反之若传入了"title",那么就会把模糊查找"title"内容的BLOG结果返回

choose, when, otherwise:从所有的条件语句中择其一二,例如:

<select id="findActiveBlogLike"

     resultType="Blog">

  SELECT * FROM BLOG WHERE state = ‘ACTIVE’

  <choose>

    <when test="title != null">

      AND title like #{title}

    </when>

    <when test="author != null and author.name != null">

      AND author_name like #{author.name}

    </when>

    <otherwise>

      AND featured = 1

    </otherwise>

  </choose></select>

提供了"title"就按"title"查找,提供了"author"就按"author"查找,若两者都没有提供,就返回所有符合条件的BLOG

trim, where, set:where 元素知道只有在一个以上的if条件有值的情况下才去插入"WHERE"子句。而且,若最后的内容是"AND"或"OR"开头的,where 元素也知道如何将他们去除。例如

<select id="findActiveBlogLike"

     resultType="Blog">

  SELECT * FROM BLOG

  <where>

    <if test="state != null">

         state = #{state}

    </if>

    <if test="title != null">

        AND title like #{title}

    </if>

    <if test="author != null and author.name != null">

        AND author_name like #{author.name}

    </if>

  </where>

</select>

set 元素可以被用于动态包含需要更新的列,而舍去其他的。例如:

<update id="updateAuthorIfNecessary">

  update Author

    <set>

      <if test="username != null">username=#{username},</if>

      <if test="password != null">password=#{password},</if>

      <if test="email != null">email=#{email},</if>

      <if test="bio != null">bio=#{bio}</if>

    </set>

  where id=#{id}

</update>

Foreach:foreach 元素的功能是非常强大的,它允许你指定一个集合,声明可以用在元素体内的集合项和索引变量。它也允许你指定开闭匹配的字符串以及在迭代中间放置分隔符。这个元素是很智能的,因此它不会偶然地附加多余的分隔符。例如:

<select id="selectPostIn" resultType="domain.blog.Post">

  SELECT *

  FROM POST P

  WHERE ID in

  <foreach item="item" index="index" collection="list"

      open="(" separator="," close=")">

        #{item}

  </foreach>

</select>

⑤MyBatis实现分页功能:

首先可以使用 limit,需要自己处理分页逻辑如下例子所示,其次可以拦截StatementHandler,但是其实质还是在最后生成limit语句,最后还可以使用PageHelper插件。

select * from table limit 5; --返回前5行

select * from table limit 0,5; --同上,返回前5行

select * from table limit 5,10; --返回6-15行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值