众所周知没学一门语言都会学习if和for的基本用法,mybatis也是如此。无数的if和for去拼接更好更长的数据库语句,以下是我用MYSQL数据库写的语句希望能给你一些启发和引导
1.Mybatis的if语句
<select id="findAllsearch" resultType="java.util.HashMap"> select * from (SELECT answer.*,answerandquestion.questionid as actived FROM gfk_test2.answer left join answerandquestion on FIND_IN_SET(answer.id,answerandquestion.answerid) GROUP BY answer.id order by created DESC) as a where <if test="answer!='+0+'">answer like "%"#{answer}"%" </if> <if test="voicetext!='+0+' and answer!='+0+'">and</if> <if test="voicetext!='+0+' and voicetext!=''"> voicetext like "%"#{voicetext}"%"</if> <if test="answer!='+0+' and playtype!='+0+'">and </if> <if test="voicetext!='+0+' and playtype!='+0+'and answer=='+0+' ">and </if> <if test="playtype!='+0+' and playtype!=''"> playtype =#{playtype}</if> <if test="(answer!='+0+' or voicetext!='+0+' or playtype!='+0+') and type!='+0+'">and </if> <if test="type!='+0+' and type!='' and type==0">actived is null </if> <if test="type!='+0+' and type!='' and type==1">actived !=''</if> order by actived,updated DESC </select>
如上段代码 这个数据库语句在第六行出现了 <if>这个语句 你没猜错 这就是mybaytis的if条件 第六条的意思就是 里面的test所编写的就是条件 整体的意思就是如果answer不等于+0+ 就将 answer like "%"#{answer}"%" 这一语句打印在where之后 这个拼接的时候您得注意 and 的排放方式 不然准会报错 虽然 我现在已经将上面的代码进行改进 但是 你如果是入门 请先看看这个 了解熟悉掌握 Mybatis 的if基本运用
2.Mybatis的for循环语句
大部分小伙伴喜欢在insert语句中用到这个list 这样可以快乐的循环输出 只要控制list的长度就不会出现报错 但是很少有人将这玩意用在select 下面咱为你们证明 万物皆可select
<select id="queryResultToMap2" resultType="java.util.HashMap"> select zong.selectkeyname as keyWords,zong.questionId,zong.nextkeyname from (select * from (SELECT question.id as questionid, GROUP_CONCAT(keyword) as selectkeyname FROM question,keyword where FIND_IN_SET (keyword.id,question.selectkey) GROUP BY question.id) as a ,(SELECT question.id as qid, GROUP_CONCAT(keyword) as nextkeyname FROM question left join keyword on FIND_IN_SET(keyword.id,question.nextkey) or question.nextkey ='' GROUP BY question.id) as c ,question where question.id =a.questionid and c.qid=question.id and actived!=0) as zong LEFT JOIN answerandquestion ON zong.questionid=answerandquestion.questionid where ( <foreach collection="list" index="index" item="item" open="selectkeyname like concat('%'" separator=",'%') and selectkeyname like concat('%'" close=",'%')"> #{item} </foreach> ) or ( <foreach collection="list" index="index" item="item" open="selectkeyname like concat('%'" separator=",'%') or selectkeyname like concat('%'" close=",'%')"> #{item} </foreach> ) </select>
这个语句稍微有点复杂,这是一个几张表联合 在成立虚表 在联合 在成立虚表 最后总和查出数据 这个方法要是悟性较高的一眼就能看出 悟性低的像咱这样的的慢慢来。废话不多说,现在为你介绍这个Mybatis的for到底怎么用。 我们看到这个第十一行出现了foreach这个语句 你没看错 这就是所谓的Mybatis的for语句 <foreach collection="list(传入的字段名)" index="index(插入)" item="item(插入字段名)" open="selectkeyname like concat('%'"(这个语句由什么开启) separator=",'%') and selectkeyname like concat('%'" (这个语句由什么分割) close=",'%')"(结束用什么)> #{item} </foreach>
这个语句还是太复杂 我来写个简单的
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
在引用别人的insert
<insert id="insertData" parameterType="java.util.Map">
insert into ${table}
(<foreach collection="list" item="item" index="index" separator=",">${item.key}</foreach>)
values
(
<foreach collection="list" item="subitem" index="index" separator=",">
#{subitem.value}
</foreach>
)
</insert>
<update id="updateData" parameterType="java.util.Map">
update
${table}
set
<foreach collection="list" item="item" index="index" separator=",">${item.key}=#{item.value}</foreach>
where id=#{id}
</update>
如果还有啥疑问 可以在下方询问 我有时间一定回 今天咱就介绍到这里