mybatis 动态SQL 语句

本文详细介绍MyBatis框架的使用方法,包括其前身ibase、特点、基本思想及动态SQL语句的实现方式。此外还介绍了物理分页方案及其在不同数据库中的实现。
1.ibatis是mybatis的前身。


2.mybatis比hibernate灵活,性能也比hibernate好,而且也比较轻量级。


3.什么是mybatis:


        MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架。


        MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。


        MyBatis使用简单的XML或注解用于配置和原始映射,


        将接口和Java的POJOs(Plan Old Java Objects,普通的Java对象) 映射成 数据库中的记录.


4.orm工具的基本思想:


        无论是用过的hibernate,mybatis,你都可以发现他们有一个共同点:


        1. 从配置文件(通常是XML配置文件中)得到 sessionfactory.


        2. 由sessionfactory 产生 session


        3. 在session 中完成对数据的增删改查和事务提交等.


        4. 在用完之后关闭session 。


        5. 在java 对象和 数据库之间有做mapping 的配置文件,也通常是xml 文件。


5.对于物理分页方案,不同的数据库,有不同的实现方法。


        对于 mysql 来说 就是利用 limit offset, pagesize 方式来实现的。


        oracle 是通过 rownum 来实现的。


6.实现 mybatis 物理分页,一个最简单的方式是,是在你的mapper的SQL语句中直接写类似如下方式 :

 <select id="getUserArticles" parameterType="Your_params" resultMap="resultUserArticleList">

              select user.id,user.userName,user.userAddress,article.id aid,article.title,article.content from user,article

              where user.id=article.userid and user.id=#{id} limit #{offset},#{pagesize}

       </select>

  用这种方式,肯定可以实现分页。这是简单的一种方式。


        但更通用的一种方式是用 mybatis 插件的方式, 代码参考Z_LiamMS_V0.5中的PagePlugin.java


7.总体说来mybatis 动态SQL 语句主要有以下几类:


        1). if 语句 (简单的条件判断)

 <select id="dynamicIfTest" parameterType="Blog" resultType="Blog">

                     select * from t_blog where 1 = 1

                     <if test="title != null">

                           and title = #{title}

                     </if>

                     <if test="content != null">

                           and content = #{content}

                     </if>

                     <if test="owner != null">

                           and owner = #{owner}

                     </if>

              </select>

2). choose (when,otherwize) ,相当于java 语言中的 switch ,与 jstl 中的choose 很类似.

<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">

                     select * from t_blog where 1 = 1

                     <choose>

                           <when test="title != null">

                                  and title = #{title}

                            </when>

                           <when test="content != null">

                                  and content = #{content}

                           </when>

                           <otherwise>

                                  and owner = "owner1"

                           </otherwise>

                     </choose>

              </select>

3). trim (对包含的内容加上 prefix,或者 suffix 等,前缀,后缀)

<select id="dynamicTrimTest" parameterType="Blog" resultType="Blog">

                     select * from t_blog

                     <trim prefix="where" prefixOverrides="and |or">

                           <if test="title != null">

                                  title = #{title}

                           </if>

                           <if test="content != null">

                                  and content = #{content}

                           </if>

                           <if test="owner != null">

                                  or owner = #{owner}

                           </if>

                     </trim>

              </select>

4). where (主要是用来简化sql语句中where条件判断的,能智能的处理 and or ,不必担心多余导致语法错误)

 <select id="dynamicWhereTest" parameterType="Blog" resultType="Blog">

                     select * from t_blog

                     <where>

                           <if test="title != null">

                                  title = #{title}

                           </if>

                           <if test="content != null">

                                  and content = #{content}

                           </if>

                           <if test="owner != null">

                                  and owner = #{owner}

                           </if>

                     </where>

              </select>

5). set (主要用于更新时)

<update id="dynamicSetTest" parameterType="Blog">

                     update t_blog

                     <set>

                           <if test="title != null">

                                  title = #{title},

                           </if>

                           <if test="content != null">

                                  content = #{content},

                           </if>

                            <if test="owner != null">

                                  owner = #{owner}

                           </if>

                     </set>

                     where id = #{id}

              </update>

6). foreach (在实现 mybatis in 语句查询时特别有用)

(6.1)单参数List的类型

<select id="dynamicForeachTest" resultType="Blog">

                     select * from t_blog where id in

                     <foreach collection="list" index="index" item="item" open="(" separator="," close=")">

                           #{item}

                     </foreach>

              </select>
对应的Mapper:

                public List<Blog> dynamicForeachTest(List<Integer> ids);

                (6.2)数组类型的参数

<select id="dynamicForeach2Test" resultType="Blog">

                     select * from t_blog where id in

                    <foreach collection="array" index="index" item="item" open="(" separator="," close=")">

                           #{item}

                     </foreach>

              </select>
对应mapper:

                public List<Blog> dynamicForeach2Test(int[] ids);

                (6.3)Map 类型的参数

 <select id="dynamicForeach3Test" resultType="Blog">

                     select * from t_blog where title like "%"#{title}"%" and id in

                     <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">

                           #{item}

                     </foreach>

              </select>
mapper 应该是这样的接口:

                public List<Blog> dynamicForeach3Test(Map<String, Object> params);


详细原地址:http://www.open-open.com/lib/view/open1417486764471.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值