<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>
<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>
<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 prefix="WHERE" prefixOverrides="AND |OR">
...
</trim>
prefixOverrides属性会忽略通过管道符分割的文本序列 ,不同的文本序列之间必须要有空格.
<trim prefix="SET" suffixOverrides=",">
...
</trim>
<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>
<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>
@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);
<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>
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();
}
<T> T selectOne(String statement, Object parameter);
<E> List<E> selectList(String statement, Object parameter);
<T> Cursor<T> selectCursor(String statement, Object parameter);
try (Cursor<Entity> entities = session.selectCursor(statement, param)) {
for (Entity entity : entities) {
// 处理单个实体
}
}
<K,V> Map<K,V> selectMap(String statement, Object parameter, String mapKey);
int insert(String statement, Object parameter);
int update(String statement, Object parameter);
int delete(String statement, Obejct parameter);
<E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds);
<T> List<T> selectCursor(String statement, Object parameter, RowBounds rowBounds);
<K, V> Map<K,V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds);
void select(String statement, Object parameter, ResultHandler<T> handler);
void select(String statement Object parameter, RowBounds rowBounds, ResultHandler<T> handler);
int offset = 100;
int limit = 25;
RowBounds rowBounds = new RowBounds(offset, limit);
package org.apache.ibatis.session;
public interface ResultHandler<T> {
void handlerResult(ResultContext<? extends T> context);
}
List<BatchResult> flushStatements();
void commit();
void commit(boolean force);
void rollback();
void rollback(boolean force);
原文:https://www.cnblogs.com/chova/p/15000217.html