MyBatis框架使用解析!数据库相关API的使用介绍

2021-07-11  本文已影响0人  攻城狮Chova

动态SQL

if

<select id="findActiveBlogLike" resultType="Blog">
    SELECT * FROM BLOG WHERE state = 'ACTIVE'
    <where>
        <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>

choose-when-otherwise

<select id="findActiveBlogLike" resultType="Blog">
    SELECT * FROM BLOG WHERE state = 'ACTIVE'
    <choose>
        <when tset="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>

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>

trim

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

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

script

@update({"<script>",
            "update Author",
                "<set>",
                    "<if test='username != null'>username=#{username},</if>",
                    "<if test='password != null'>password=#{password},</if>",
                    "<if test='email != null'>email=#{email},</if>",    
                "</set>",
                "where id=#{id}",
                "</script>"})   
})
void updateAuthorValues(Author author);

bind

<select id="selectBlogsLike" resultType="Blog">
    <bind name="pattern" value="'%' + _parameter.getTitle() + '%'" />
    SELECT * FROM BLOG
    WHERE title LIKE #{pattern}
</select>

多数据库支持

<insert id="insert">
    <selectKey keyProperty="id" resultType="int" order="BEFORE">
        <if test="_databaseId == 'oracle'">
            select seq_users.nextval from dual
        </if>
        <if test="_databaseId == 'db2'">
            select nextval for seq_users from sysibm.sysdummy1
        </if>
    </selectKey>
    insert into users values (#{id}, #{name})
</insert>

动态SQL中插入脚本语言

public interface LanguageDriver {
    ParameterHandler createParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql);
    
    SqlSource createSqlSource(Configuration configuration, XNode script, Class<?> parameterType);
    SqlSource crateSqlSource(Configuration configuration, String script, Class<?> parameterType);
}
<typeAliases>
    <typeAliase type="com.oxford.MyLanguageDriver" alias="myLanguage" />
</typeAliases>
<settings>
    <setting name="defaultScriptingLanguage" value="myLanguage" />
</settings>
<select id="selectBlog" lang="myLanguage">
    SELECT * FROM BLOG
</select>
public interface Mapper {
    @Lang(MyLanguageDriver.class)
    @Select("SELECT * FROM BLOG")
    List<Blog> selectBlog();
}

Java API

语句执行方法

selectOne
<T> T selectOne(String statement, Object parameter);
selectList
<E> List<E> selectList(String statement, Object parameter);
selectCursor
<T> Cursor<T> selectCursor(String statement, Object parameter);
try (Cursor<Entity> entities = session.selectCursor(statement, param)) {
    for (Entity entity : entities) {
        // 处理单个实体
    }
}
     
selectMap
<K,V> Map<K,V> selectMap(String statement, Object parameter, String mapKey); 
insert
int insert(String statement, Object parameter);
update
int update(String statement, Object parameter);
delete
int delete(String statement, Obejct parameter);

select高级版本

selectList
<E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds);
selectCursor
<T> List<T> selectCursor(String statement, Object parameter, RowBounds rowBounds);
selectMap
<K, V> Map<K,V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds);
select
void select(String statement, Object parameter, ResultHandler<T> handler);
select
void select(String statement Object parameter, RowBounds rowBounds, ResultHandler<T> handler);
package org.apache.ibatis.session;
public interface ResultHandler<T> {
    void handlerResult(ResultContext<? extends T> context);
}

清除批量更新方法

flushStatements
List<BatchResult> flushStatements();

事务控制方法

void commit();

void commit(boolean force);

void rollback();

void rollback(boolean force);
上一篇下一篇

猜你喜欢

热点阅读