发布于 2016-08-08 04:24:16 | 250 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的MyBatis教程,程序狗速度看过来!

MyBatis 基于Java的持久层框架

MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。


这篇文章主要介绍了Java的MyBatis框架中对数据库进行动态SQL查询的教程,讲解了MyBatis中一些控制查询流程的常用语句,需要的朋友可以参考下

其实MyBatis具有的一个强大的特性之一通常是它的动态 SQL 能力。 如果你有使用 JDBC 或其他 相似框架的经验,你就明白要动态的串联 SQL 字符串在一起是十分纠结的,确保不能忘了空格或在列表的最后省略逗号。Mybatis中的动态 SQL 可以彻底处理这种痛苦。对于动态SQL,最通俗简单的方法就是我们自己在硬编码的时候赋予各种动态行为的判断,而在Mybatis中,用一种强大的动态 SQL 语 言来改进这种情形,这种语言可以被用在任意映射的 SQL 语句中。动态 SQL 元素和使用 JSTL 或其他相似的基于 XML 的文本处理器相似。MyBatis 采用功能强大的基于 OGNL 的表达式来消除其他元素。
我们常用的几个节点元素有if,choose(when, otherwise),trim(where, if),foreach。真正使用下来我感觉有点像XSLT(文章后面会顺带提一下~)的用法。
(1)if 的用法

在ViisitMapper的分页配置中,如果pageIndex>-1 and pageSize>-1的时候就加入相应的分页SQL,否则就不添加(默认取全部),如下:


<select id="getListByPagenate" parameterType="PagenateArgs"
  resultType="Visitor">
  select * from (
  <include refid="getListSql" /> <include refid="orderBySql"/>
  ) t <!-- #{}表示参数化输出,${}表示直接输出不进行任何转义操作,自己进行转移 -->
  <if test="pageStart>-1 and pageSize>-1">
    limit #{pageStart}, #{pageSize}
  </if>
</select>
<sql id="getListSql">
  select * from Visitor where
  status>0
</sql>
<sql id="orderBySql">
  order by ${orderFieldStr} ${orderDirectionStr}
</sql>

  因为我们的参数pageIndex与pageSize都是int值所以可以这样直接判断,如果是对象实例我们可以利用null判断来进行一些动态逻辑的控制,具体实际开发中就要看业务需求了。这里我认为要注意的是别十分顺手的吧and写成&&,这个在配置中不会被识别~。

(2)choose (when, otherwise)的用法

  choose when 主要在多个条件的情况下只满足其中一个条件的应用场景中使用,例如这里就构建一个query条件,分别传递id,name与createTime。假设我们查询Visitor表时,如果VisitorId有值则,使用Id查询,如果VisitorName有值则采用VisitName查询,如下,还是在david.mybatis.demo.IVisitorOperation接口类中添加List<Visitor> getListChooseWhenDemo(BasicQueryArgs args)方法。在VisitorMapper中添加相应的的select节点配置:


package david.mybatis.demo;

import java.util.List;

import david.mybatis.model.BasicQueryArgs;
import david.mybatis.model.PagenateArgs;
import david.mybatis.model.Visitor;
import david.mybatis.model.VisitorWithRn;

public interface IVisitorOperation {
  /*
   * 添加访问者
   */
  public int add(Visitor visitor);
  
  /*
   * 删除访问者
   */
  public int delete(int id);
  
  /*
   * 更新访问者
   */
  public int update(Visitor visitor);
  
  /*
   * 查询访问者
   */
  public Visitor query(int id);
  
  /*
   * 查询List
   */
  public List<Visitor> getList();
  
  /*
   * 分页查询List
   */
  public List<Visitor> getListByPagenate(PagenateArgs args);
  
  /*
   * 分页查询List(包含Rownum)
   */
  public List<VisitorWithRn> getListByPagenateWithRn(PagenateArgs args);
  
  /*
   * 基础查询
   */
  public Visitor basicQuery(int id);
  
  /*
   * 动态条件查询(choose,when)实例
   */
  public List<Visitor> getListChooseWhenDemo(BasicQueryArgs args);
  
  /*
   * 动态条件查询(where,if)实例
   */
  public List<Visitor> getListWhereDemo(BasicQueryArgs args);
  
  /*
   * 动态查询(foreach)实例
   */
  public List<Visitor> getListForeachDemo(List<Integer> ids);
  
}


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="david.mybatis.demo.IVisitorOperation">  
  <resultMap type="Visitor" id="visitorRs">
    <id column="Id" property="id" />
    <result column="Name" property="name" />
    <result column="Email" property="email" />
    <result column="Status" property="status" />
    <result column="CreateTime" property="createTime" />
  </resultMap>
  <sql id="getListSqlConditions">
    select * from Visitor
  </sql>
  <!-- 满足其中一个条件时候用choose when操作 -->
  <select id="getListChooseWhenDemo" resultMap="visitorRs"
    parameterType="BasicQueryArgs">
    <include refid="getListSqlConditions" />
    <where>
      <if test="queryStatus>0">
        status=#{queryStatus}
      </if>
      <choose>
        <when test="queryId!=0">
          and id=#{queryId}
        </when>
        <when test="queryName!=null">
          and name like #{queryName}
        </when>
        <otherwise>
          and createTime>= #{queryTime}
        </otherwise>
      </choose>
    </where>
  </select>
</mapper>

(3)where if (trim)的用法

where关键词的好处是在于,如果有相应的过滤条件的话,它知道在适当的时候插入where关键词。而且它也知道在何时该去掉相应的AND与OR的连接符,主要应对如下情景


<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>
</select>

不会因为所有条件不满足变为


<select id="findActiveBlogLike"
   resultType="Blog">
 SELECT * FROM BLOG
 WHERE
</select>

或者因为没有满足第一个条件,单单满足后面的条件变成


<select id="findActiveBlogLike"
   resultType="Blog">
  SELECT * FROM BLOG
  WHERE
  AND title like ‘someTitle'
</select>

所以针对这种我们可以在建立choose when条件示例,同样在IVisitorOperation接口类中加入相应的方法public List<Visitor> getListWhereDemo(BasicQueryArgs args),把VisitorMapper配置文件中的相对应配置加上去如下:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="david.mybatis.demo.IVisitorOperation">  
  <sql id="getListSqlConditions">
    select * from Visitor
  </sql>
  <!-- 满足条件的都加上去操作 -->
  <select id="getListWhereDemo" resultMap="visitorRs"
    parameterType="BasicQueryArgs">
    <include refid="getListSqlConditions" />
    
    <where> 
      <if test="queryStatus>0"> 
        status>0 
      </if> 
      <if test="queryId>0"> 
        and id=#{queryId} 
      </if> 
      <if test="queryName!=null"> 
        and name like=#{queryName} 
      </if> 
      <if test="queryTime!=null"> 
        and createTime>=#{queryTime} 
      </if>
    </where>
    <!--
    <trim prefix="WHERE" prefixOverrides="AND |OR ">
      <if test="queryStatus>0">
        status>0
      </if>
      <if test="queryId>0">
        and id=#{queryId}
      </if>
      <if test="queryName!=null">
        and name like=#{queryName}
      </if>
      <if test="queryTime!=null">
        and createTime>=#{queryTime}
      </if>
    </trim>
    -->
  </select>
</mapper>

(4)foreach的用法

在常用的动态SQL中我们有个业务场景是要where id in 一大串的ID,像这种情况我们就可以用到foreach啦,不必自己辛辛苦苦去拼接Id字符串啦。同样的步骤还是在IVisitorOperation接口类中加入相应的方法public List<Visitor> getListForeachDemo(List<Integer> ids),然后再对应的Mapper文件中配置上相应的节点元素信息,如下:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="david.mybatis.demo.IVisitorOperation">  
  <sql id="getListSqlConditions">
    select * from Visitor
  </sql>
  <!-- Foreach循环条件 -->
  <select id="getListForeachDemo" resultMap="visitorRs">
    <include refid="getListSqlConditions"/>
    where status>0 and id in 
    <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
      ${item}
    </foreach>
  </select>
</mapper>

最后你只需要在DemoRun中建立相应的测试方法,Mybatis里面的动态SQL也就完成啦,下面测试用的DemoRun方法


/*
   * 动态查询foreach实例
   */
  public static void getListForeachDemo(List<Integer> ids) {
    SqlSession session = MybatisUtils.getSqlSession();
    IVisitorOperation vOperation = session.getMapper(IVisitorOperation.class);
    List<Visitor> ls = vOperation.getListForeachDemo(ids);
    for (Visitor visitor : ls) {
      System.out.println(visitor);
    }
  }
  
  /*
   * 动态查询where if实例
   */
  public static void getListWhereCondition(int id, String name, Date createTime) {
    name = name == "" ? null : name;
    SqlSession session = MybatisUtils.getSqlSession();
    BasicQueryArgs args = new BasicQueryArgs(id, name, createTime);
    IVisitorOperation vOperation = session.getMapper(IVisitorOperation.class);
    List<Visitor> ls = vOperation.getListWhereDemo(args);
    if (ls.size() == 0)
      System.out.println("查无匹配!");
    else {
      for (Visitor visitor : ls) {
        System.out.println(visitor);
      }
    }
  }

  /*
   * 动态查询choose when实例
   */
  public static void getListChooseWhenDemo(int id, String name, Date createTime) {
    name = name == "" ? null : name;
    SqlSession session = MybatisUtils.getSqlSession();
    BasicQueryArgs args = new BasicQueryArgs(id, name, createTime);
    IVisitorOperation vOperation = session.getMapper(IVisitorOperation.class);
    List<Visitor> ls = vOperation.getListChooseWhenDemo(args);
    if (ls.size() == 0)
      System.out.println("查无匹配!");
    else {
      for (Visitor visitor : ls) {
        System.out.println(visitor);
      }
    }
  }

   
PS:关于OGNL
OGNL 是 Object-Graph Navigation Language 的缩写,从语言角度来说:它是一个功能强大的表达式语言,用来获取和设置 java 对象的属性 , 它旨在提供一个更高抽象度语法来对 java 对象图进行导航,OGNL 在许多的地方都有应用,例如:
作为 GUI 元素(textfield,combobox, 等)到模型对象的绑定语言。
数据库表到 Swing 的 TableModel 的数据源语言。
web 组件和后台 Model 对象的绑定语言 (WebOGNL,Tapestry,WebWork,WebObjects) 。
作为 Jakarata Commons BeanUtils 或者 JSTL 的表达式语言的一个更具表达力的替代语言。
另外,java 中很多可以做的事情,也可以使用 OGNL 来完成,例如:列表映射和选择。 对于开发者来说,使用 OGNL,可以用简洁的语法来完成对 java 对象的导航。通常来说: 通过一个“路径”来完成对象信息的导航,这个“路径”可以是到 java bean 的某个属性,或者集合中的某个索引的对象,等等,而不是直接使用 get 或者 set 方法来完成。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务