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