四 , xmlmapper 文件中sql 语句解析
解析类 XMLLanguageDriver ,可以通过setting 中的配置解析类。
XMLScriptBuilder 动态sql 语句
"bind":BindHandler
<if test="sex !=null">
<bind name="sexConvert" value='sex == "男" ? "M": "F"' />
and sex = #{sexConvert ,jdbcType=VARCHAR}
</if>
通过ognl 计算值,赋值给变量
ForEachHandler : forecah 标签的解析解析
<select id="selectInByList" resultType="blog" >
select * from blog where id in
<foreach item="item" index="index" collection="array"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
<select id="selectInByMap" resultType="blog" parameterType="map">
select * from blog where id in
<foreach item="item" index="index" collection="ids"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
foreach 标签 属性 collection的取值
1.当参数为数组的时候 为 array,
2.为集合的时候 是 collection ,如何是 list类型的时候是list,
3. 当参数为map时,是集合对应的key,为对象时,是对象对应的属性
IfSqlNode if 标签
1.使用ognl 表达式对test 条件进行属性判断
ognl 使用属性名称表示属性 , string " " ,char ' ' ,数字 , null 表示为空 , true and false.
<if test="_parameter.getOrderBy()">
order by title
</if>
test 中进行判断 != , == 调用方法 _parameter.getOrderBy()
trim : TrimHandler TrimSqlNode
<select id="selectTrim" resultType="blog" parameterType="blog">
select * from blog
<trim prefix="where" prefixOverrides="and | or">
<if test="title != null">
and title like #{title}"%"
</if>
<if test="author != null">
and author=#{author}
</if>
</trim>
</select>
prefixesToOverride 以 | 分割 删除头部出项的字符
suffixesToOverride 以 | 分割 删除尾部出项的字符
prefix 如果内容不为空,在头部添加的字符
suffix 如果内容不为空,在尾部添加的字符
"where",WhereHandler 继承自 trimhandler
prefixesToOverride AND, OR
prefix WHERE
set : SetHandler 继承自 trimhandler
suffix SET
suffixesToOverride , 逗号
;
when : IfHandler
choose ChooseHandler ChooseSqlNode
相当于switch
when 相对于 case 比配成功就跳出 ,OtherwiseHandler 相对于 default
<select id="selectChoose" resultType="blog" parameterType="blog">
select * from blog where 1=1
<choose>
<when test="title != null">
and title like #{title}"%"
</when>
<when test="author != null">
and author=#{author}
</when>
<otherwise>
and id < 5
</otherwise>
</choose>
</select>