2.mybatis is more flexible than hibernate, has better performance than hibernate, and is lightweight.
3. What is mybatis:
MyBatis is an excellent persistence framework that supports common SQL queries, stored procedures and advanced mappings.
MyBatis eliminates manual settings of almost all JDBC code and parameters and retrieval of result sets.
MyBatis uses simple XML or annotations for configuration and original mapping.
Mapping interfaces and Java POJOs (Plan Old Java Objects, common Java objects) into records in the database.
4. The basic idea of ORM tools:
Whether you have used hibernate or mybatis, you can find that they have one thing in common:
1. Get the session factory from the configuration file (usually the XML configuration file).
2. Generating session by session factory
3. Complete data addition, deletion and transaction submission in session.
4. Close session after use.
5. There are mapping configuration files between java objects and databases, usually xml files.
5. For physical paging schemes, different databases have different implementations.
For mysql, it is implemented by limited offset and pagesize.
oracle is implemented through rownum.
6. One of the easiest ways to implement mybatis physical paging is to write directly in your mapper's SQL statement in the following way:
<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>
Paging can certainly be achieved in this way. This is a simple way.
A more general way is to use the mybatis plug-in, which refers to PagePlugin.java in Z_LiamMS_V0.5
7. Overall, mybatis dynamic SQL statements can be classified into the following categories:
If statement (simple conditional judgment)
<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. Select (when, other wize), which is equivalent to switch in java language, is similar to choose in jstl.
<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, etc., prefix, suffix, etc.)
<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 (mainly used to simplify where condition judgment in sql statements, can handle and or intelligently without worrying about redundancy leading to grammatical errors)
<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 (mainly for updates)
<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 (especially useful in implementing mybatis in statement queries)
(6.1) Type of single parameter List
Mapper:<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>
public List<Blog> dynamicForeachTest(List<Integer> ids);
(6.2) Array type parameters
Corresponding to mapper:<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>
public List<Blog> dynamicForeach2Test(int[] ids);
(6.3)Map type parameters
mapper should be such an interface:<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>
public List<Blog> dynamicForeach3Test(Map<String, Object> params);
Detailed original address: http://www.open-open.com/lib/view/open1417486764471.html