动态SQL标签
<if>
:判断条件是否成立,如果条件为true,则拼接SQL。<where>
:根据查询条件,来生成where关键字,并会自动去除条件前面多余的and或or。<!--定义Mapper映射文件的约束和基本结构--> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.itheima.mapper.EmpMapper"> <select id="list" resultType="com.itheima.pojo.Emp"> select e.*, d.name deptName from emp as e left join dept as d on e.dept_id = d.id <where> <if test="name != null and name != ''"> 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_date between #{begin} and #{end} </if> </where> </select> </mapper>
<foreach>
:是用来遍历循环,常见的属性说明:
- collection:集合名称
- item:集合遍历出来的元素/项
- separator:每一次遍历使用的分隔符
- open:遍历开始前拼接的片段
- close:遍历结束后拼接的片段
上述的属性,是可选的,并不是所有的都是必须的。 可以自己根据实际需求,来指定对应的属性。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.itheima.mapper.EmpExprMapper"> <!--批量插入员工工作经历信息--> <insert id="insertBatch"> insert into emp_expr (emp_id, begin, end, company, job) values <foreach collection="exprList" item="expr" separator=","> (#{expr.empId}, #{expr.begin}, #{expr.end}, #{expr.company}, #{expr.job}) </foreach> </insert> </mapper>
<set>标签:会自动生成set关键字,会自动删除更新字段后多余的,