提示:mybatis笔记整合
文章目录
前言
提示:mybatis标签解释:
一、mybayis 的< if >元素
<select id="test1" parameterType="com.service.boot.entity.User" resultType="com.service.boot.entity.User">
select * from User where 1=1
<if test="account !=null and account !=''">
and account like concat('%',#{account},'%')
</if>
<if test="password !=null and password !=''">
and password=#{password}
</if>
</select>
二、mybayis 的< choose > < when > < otherwise >元素
<select id="test2" parameterType="com.service.boot.entity.User" resultType="com.service.boot.entity.User">
select * from User where 1=1
<choose>
<when test="username!=null and username!=''">
and username like concat('%',#{username},'%')
</when>
<when test="job!=null and job!=''">
and job=#{job}
</when>
<otherwise>
and age is not null
</otherwise>
</choose>
</select>
三、mybayis 的< where > < trim >元素
<select id="test3" parameterType="com.service.boot.entity.User" resultType="com.service.boot.entity.User">
select * from User
<where>
<if test="account !=null and account !=''">
and account like concat('%',#{account},'%')
</if>
<if test="password !=null and password !=''">
and password=#{password}
</if>
</where>
</select>
<select id="test4" parameterType="com.service.boot.entity.User" resultType="com.service.boot.entity.User">
select * from User
<trim prefix="where" prefixOverrides="and">
<if test="account !=null and account !=''">
and account like concat('%',#{account},'%')
</if>
<if test="password !=null and password !=''">
and password=#{password}
</if>
</trim>
</select>
四、mybayis 的< set >元素
<update id="test5" parameterType="com.service.boot.entity.User">
update User
<set>
<if test="account !=null and account !=''">
account=#{account}
</if>
<if test="password !=null and password !=''">
password=#{password}
</if>
</set>
where
id =#{id}
</update>
五、mybayis 的< foreach>元素
<select id="test6" parameterType="List" resultType="com.service.boot.entity.User">
select * from User where id in
<foreach collection="list" item="id" index="index" open="(" separator="," close=")">
#{id}
</foreach>
</select>
友情链接:wt_better的: mybatis foreach标签的使用
六、mybayis 的< bind>元素
<select id="test7" parameterType="com.service.boot.entity.User" resultType="com.service.boot.entity.User">
select * from User
<bind name="usernameaaaaa" value="'%'+account+'%'"/>
<where>
<if test="account !=null and account !=''">
and account =#{usernameaaaaa}
</if>
<if test="password !=null and password !=''">
and password=#{password}
</if>
</where>
</select>