mybatis sql语句摘要:
1、select语句中:使用标签,则每个条件前加上and关键字,即使第一个条件有效,where标签也会自动处理掉and关键字;
<select id="getcount" parameterType="empVo" resultType="int">
<![CDATA[
select count(*) cnt from emp e inner join dep d on e.depid=d.depid
]]>
<!-- 如果使用where标记,可以省略where 1=1 的特殊处理 -->
<!-- #和$的区别,如果是字符串类型的变量#会自动加上单引号,$符合是原样输出 -->
<where>
<if test="empName !=null and empName!=''">
and empName like concat('%',#{empName},'%')
</if>
<if test="sex !=null and sex!='' ">
and sex =#{sex}
</if>
<if test="depId>0">
and e.depId=#{depId}
</if>
<!-- 条件要结合页面参数进行设置 -->
<if test="status>-1">
and status=#{status}
</if>
</where>
</select>
2、如不使用where标签,则SQL语句中的where后须加上1 = 1,则后面的if条件里的SQL语句每个都加上and(第一个可加可不加),如下:
<select id="searchByCondition" resultType="empVo">
<![CDATA[
select e.*,d.depName,bankName,bankAccount from emp e inner join dep d on e.depid=d.depid left join account a on a.empId=e.empId where 1=1
]]>
<!-- 如果使用where标记,可以省略where 1=1 的特殊处理 -->
<!-- #和$的区别,如果是字符串类型的变量#会自动加上单引号,$符合是原样输出 -->
<if test="emp.empName !=null and emp.empName!=''">
and empName like concat('%',#{emp.empName},'%')
</if>
<if test="emp.sex !=null and emp.sex!='' ">
and sex =#{emp.sex}
</if>
<if test="emp.depId>0">
and e.depId=#{emp.depId}
</if>
<!-- 条件要结合页面参数进行设置 -->
<if test="emp.status>-1">
and status=#{emp.status}
</if>
limit #{pager.startRow},#{pager.pageRow}
</select>
3、带choose条件的查询
<!-- 带choose条件的查询 -->
<select id="searchByCondition1" parameterType="empVo" resultType="empVo">
<![CDATA[
select e.*,d.depName from emp e inner join dep d on e.depid=d.depid where 1=1
]]>
<!-- 多选一,只会执行其中一个when语句 -->
<choose>
<when test="empName !=null and empName!=''">
and empName like concat('%',#{empName},'%')
</when>
<when test="sex !=null and sex!='' ">
and sex =#{sex}
</when>
<when test="depId>0">
and e.depId=#{depId}
</when>
<!-- 条件要结合页面参数进行设置 -->
<when test="status>-1">
and status=#{status}
</when>
</choose>
</select>
4、DAO接口中的方法使用注解:
<!--
查询条件的参数有多个时,可以把参数放到JAVABEAN中,减少接口中参数的数量
如果需要把javabean作为参数的前缀,那么必须把参数设置为注解
当参数设置了注解后,parameterType参数可以省略,该参数不起作用
-->
<select id="findEmpByBean" parameterType="com.ht.vo.EmpVo" resultType="empVo">
<![CDATA[
select * from Emp where sex = #{emp.sex} and job=#{emp.job}
]]>
</select>
5、批量新增
<insert id="batchAdd" parameterType="list">
<![CDATA[
INSERT into emp(empName,password,age,sex,job,status,remark,depId) values
]]>
<!-- 使用foreach实现批量新增 -->
<foreach collection="list" item="item" separator=",">
(#{item.empName},#{item.password},#{item.age},#{item.sex},#{item.job},#{item.status},#{item.remark},#{item.depId})
</foreach>
</insert>
6、<!-- 使用set标记,动态决定要更新的字段(修改密码和修改用户状态调用同一个方法) -->
<update id="updatePartFiled" parameterType="empVo">
<![CDATA[
update emp
]]>
<set>
<!-- 条件要结合页面参数进行设置 -->
<if test="status!=-1">
status=#{status},
</if>
<if test="password!= null and password!=''">
password=#{password},
</if>
</set>
where empId=#{empId}
</update>
7、 <!-- 批量删除 -->
<delete id="batchDelete">
<![CDATA[
delete from emp where empid in
]]>
<foreach collection="array" separator="," item="d" open="(" close=")">
#{d}
</foreach>
</delete>