元素 作用 描述
if 条件判断 单条件判断
choose(when,otherwise) 条件选择,相当于java中的switch 多条件分支判断
where,set 辅助(条件) 处理sql语句拼接问题
foreach 循环(批量插入,修改) 循环(批量使用)
if标签-单条件判断
<!--如果姓名不为空 则安姓名查找 如果姓名为空则按邮箱查找 否则查询全部-->
<select id="findConditon" resultType="com.wx.entity.User">
select * from tbl_user02
<where>
<if test="name!=null and name!=''">
and name = #{name}
</if>
<if test="email!=null and email!=''">
and email = #{email}
</if>
</where>
</select>
————————————————
choose标签 多条件分支判断 choose 元素,它有点像 Java 中的 switch 语句。
<select id="findByCondition" resultType="com.wx.entity.User">
select * from tbl_user02
<where>
<choose>
<when test="name!=null and name!=''">
and name = #{name}
</when>
<when test="email!=null and email!=''">
and email = #{email}
</when>
<otherwise>
and pwd = #{pwd}
</otherwise>
</choose>
</where>
</select>
————————————————
set标签
这个标签配合if标签一起用 一般用于修改语句 如果传递的参数为null 那么就不会修改该列的值
//这里注意 test="参数" 这里的参数 是前端传过来的值就是你前端页面上是什么 这里就要写什么
//而下面的name=#{name} 第一个name是你数据库中的字段 #{name}中是你的前端传过来的值
<update id="updateUser" parameterType="User">
update tbl_user02
<set>
<if test="name!=null and name!=''">
name=#{name},
</if>
<if test="pwd!=null">
pwd=#{pwd},
</if>
<if test="email!=null">
email=#{email},
</if>
</set>
where id = #{id}
</update>
————————————————
查询id为1 3 5 的用户信息
正常sql语句为 select * from tbl_user02 where id in(1,3,5);
下面的为使用foreach遍历 循环查询
解释:
<foreach collection="集合类型" open="开始的字符" close="结束的字符"
item="集合中的成员" separator="集合成员之间的分割符">
#{item的值}
</foreach>
标签属性:
collection:表示循环的对象是数组还是list集合。如果dao方法的形参是数组,collection="array";
如果dao方法形参是list,collection="list";
open:循环开始的字符。sql.append("(");
close:循环结束的字符。sql.append(")");
item:集合成员,自定义的变量。Integer item = idList.get(i);
separator:集合成员之间的分隔符。sql.append(",");
#{item的值}:获取集合成员的值;
3.4foreach标签
循环标签 适用于批量添加、删除 和查询记录
<insert id="batchInsertBlog" useGeneratedKeys="true" keyProperty="id">
insert into blog (title, author, views, status) values
<foreach collection="blogList" item="blog" separator=",">
(#{blog.title}, #{blog.author}, #{blog.views}, #{blog.status})
</foreach>
</insert>
mapper接口中这样写:
int batchInsertBlog(List<Blog> blogList);
————————————————
<delete id="batchDeleteByIds">
DELETE FROM blog WHERE id in
<foreach collection="ids" item="item" index="index" open="(" separator="," close=")" >
#{item}
</foreach>
</delete>
mapperj接口中这样写:
int batchDeleteByIds(@Param("ids") List<String> ids);
————————————————
<update id="updateTable">
UPDATE ${tableName} SET
<foreach collection="dataMap" index="key" item="value" separator="," >
${key} = #{value}
</foreach>
WHERE
id = #{id}
</update>
mapper中这样写:
void updateTable(@Param("tableName") String tableName, Param("id") String id,
@Param("dataMap") HashMap dataMap);
————————————————
<select id="getAllBlog" resultType="blog">
SELECT title, author, views, status
FROM blog
<if test="blogList != null and blogList.size() > 0">
WHERE id in
<foreach collection="blogList" item="item" index="index" open="("
separator="," close=")">
#{item}
</foreach>
</if>
</select>
批量查询博客信息:
void getAllBlog(@Param("blogList") List<Long> blogList);
————————————————
sql片段
一般用于查询语句的时候 select * … 这种不推荐 所以用sql片段可以很好的解决这个问题
<select id="selectByName" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" /> 代表 *
from zbcg_product
<where>
<if test='name !=null and name !=""'>
name like concat('%',#{name}, '%')
</if>
</where>
</select>
————————————————
4.mybatis映射文件处理特殊字符.
第一种:转义标签 <
第二种: <![CDATA[sql]]>
<select id="findByMaxAndMin" resultType="com.wx.entity.User">
select * from tbl_user02 where id >#{min} and id < #{max}
</select>
<select id="findByMaxAndMin01" resultType="com.wx.entity.User">
<![CDATA[select * from tbl_user02 where id>#{min} and id<#{max}]]>
</select>
————————————————
5.mybatis完成模糊查询
第一种:使用字符串函数(concat)完成拼接
第二种:使用${}
<select id="findByName" resultType="com.wx.entity.User">
select * from tbl_user02 where name like concat('%',#{name},'%');
</select>
<select id="findByName" resultType="com.wx.entity.User">
select * from tbl_user02 where name like '%${name}%';
</select>