<where> 和<if>的使用
为啥要用这个标签。
未使用前
看mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ch1.mapper.EmpMapper">
<select id="list2" resultType="com.ch1.pojo.Emp">
select e.*,d.name deptName from emp e left join dept d on e.dept_id = d.id
where
e.name like concat('%',#{name},'%')
and e.gender = #{gender}
and e.entry_data between #{begin} and #{end}
order by e.update_time desc
</select>
</mapper>
这里很明显,当要查询很多数据的时候,如果这些数据不是必填的情况下,sql很明显就不对了。
使用后
where元素只会在子元素有内容的情况下才插入where子句,而且会自动去除子句的开头的AND或OR
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ch1.mapper.EmpMapper">
<select id="list2" resultType="com.ch1.pojo.Emp">
select e.*,d.name deptName from emp e left join dept d on e.dept_id = d.id
<where>
<if test="name != null and name.trim() != ''">
e.name like concat('%',#{name},'%')
</if>
<if test="gender!=null">
and e.gender = #{gender}
</if>
<if test="begin !=null and end !=null">
and e.entry_data between #{begin} and #{end}
</if>
</where>
order by e.update_time desc
</select>
</mapper>
这样,就sql就灵活了。如果没有值。那么压根就不拼接对应的sql
既然有了<where>这个标签。那么也就应该有<set>标签。
<set>的使用
动态地在行首插入 SET 关键字,并会删掉额外的逗号。(用在update语句中)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ch1.mapper.EmpMapper">
<!--开始进行更新操作-->
<update id="update">
update emp
<!-- 使用set标签,代替update语句中的set关键字 这样就可以了 -->
<set>
<if test="username != null">
username=#{username},
</if>
<if test="name != null">
name=#{name},
</if>
<if test="gender != null">
gender=#{gender},
</if>
</set>
where id=#{id1}
</update>
</mapper>
<foreach>的使用
例如要新增一个list的json串
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ch1.mapper.EmpExprMapper">
<!-- 批量导入数据
collection:集合
item:集合中的元素
index:索引
separator:分隔符
-->
<insert id="insertEmpExpr">
insert into emp_expr(emp_id, begin,end,company,job) values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.empId}, #{item.begin}, #{item.end}, #{item.company},#{item.job})
</foreach>
</insert>
</mapper>
例如要删除
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ch1.mapper.EmpMapper">
<!--删除操作-->
<delete id="deleteByIds">
delete from emp where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
</mapper>
这些都是foreach循环后,数据就摆好了。
至于sql和include
sql就是为了把相同的sql部分提出来
然后用include在需要用的地方进行引用。
按照自己的嗜好来用,可以用也可以不用
例如
<sql id="commonSelectEmp">
select id, username, name, gender, image, job, entrydate, dept_id, create_time, update_time from emp
</sql>
用的时候
<select id="list" resultType="com.ch1.pojo.Emp">
<include refid="commonSelect"/>
<where>
<if test="name != null">
name like concat('%',#{name},'%')
</if>
</where>
order by update_time desc
</select>