发表于: 2017-11-27 17:48:34

2 598


今日完成的事情:


;先说基础的内容

1;小课堂的内容,完成,总结所有;

A;if标签;一条sql语句;代替多条;自动判断条件

 

SELECT * FROM student
<if test="name != null">
   
WHERE name LIKE concat('%',#{name},'%')
</if>

 

B;where标签弥补if标签多字段的不足,更加完善

 

<if test="weight != null">
       
AND weight &lt; #{weight}
   
</if>
</
where>

 

C;set标签,where类似,只是用于update语句,也是自行判断

 

<set>
    <
if test="name != null"> name = #{name},</if>
    <
if test="weight != null"> weight = #{weight}</if>
</
set>

 

D;trim标签,算是扩展吧,可以以自定义的样式替代whereset的标签

Where

<trim prefix="where" prefixOverrides="AND |OR">
    <
if test="name != null">
       
AND name LIKE concat('%',#{name},'%')
   
</if>

 

Update

 

<trim prefix="set" prefixOverrides="," suffix="where id = #{id}">
    <
if test="name != null"> name = #{name},</if>
    <
if test="weight != null"> weight = #{weight},</if>
</
trim>

 

这里说明一下: prefix:前缀; prefixoverride:去掉第一个and或者是or,类似于拼接效果;自动判断,自动匹配

 

E; choose标签;可以使用when…otherwise….来完善逻辑弥补没有else的不足

 

<choose>
<
when test="name != null">
   
AND name LIKE concat('%',#{name},'%')
</when>
<
when test="weight != null">
   
AND weight > #{weight}
</when>
<
otherwise>
       
AND id > #{id}
</otherwise>

 

F; foreach标签,稍微复杂了一点

首先了解这个sql语句

select * from tables where id in(3,6,...);要么3,要么6

然后,知道这几个属性;

item : 表示在迭代过程中每一个元素的别名
index
:表示在迭代过程中每次迭代到的位置(下标)
open
:前缀     close :后缀
separator
:分隔符,表示迭代时每个元素之间以什么分隔

 

最后是,foreach

 

SELECT *FROM student
    WHERE id IN
     
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
                    
#{item}
     
</foreach>

 

贴个测试结果

 

 

G;bind标签;扩展吧….感觉没有施展开,似乎功能很强,这里简单运用,就是可以自定义一个变量,然后绑定到上下文( OGNL表达式)

<select id="list5" resultType="Student">
     <
bind name="haha" value="'%'+name+'%'"/>
      
SELECT * FROM student WHERE name LIKE #{haha}
</select>

 

总结;mybatis的七个常用标签,会在一定程度上减少代码量,同时可以更好的完善逻辑

 

;然后任务…….去接师姐……来回6个小时……


 

 

 

 

明日计划的事情:


意外任务....拖后一点,今天加紧补回来...........


1;学习点基础


2;完成邮箱模块


 

遇到的问题及解决方法:


1;第一次遇见,这样也算url重复……

 

 

 

 

 .....







收获:


1;学习动态sql


2;任务进行一部分

 

 

 



返回列表 返回列表
评论

    分享到