1、动态SQL是一种可以根据不同条件生成不同SQL语句的技术,随着用户输入或外部条件变化而变化的SQL语句
2、SQL语句中的like模糊查询 xm like '%小米%',但开发中经常用到 xm like concat("%",#{xm},"%"),可以防止sql注入
3、concat()函数用于将多个字符串连接成一个字符串
4、动态标签<if>:用于判断条件是否成立,使用test属性进行条件判断,如果条件为true,则拼接SQL
IPage<JcCVo> selectByCyJddm(Page<JcCVo> page, @Param("xm")String xm,@Param("hjqx")String hjqx, @Param("zszldm") String zszldm, @Param("sfzhm")String sfzhm, @Param("jddm") String jddm, @Param("zszldmx") String zszldmx);
<select id="selectByCJddm" resultType="org.jeecg.modules.wlyg.vo.JcVo">
SELECT *,CONCAT(LEFT(sfzhm,6),'********',RIGHT(sfzhm,4)) AS sfzhmb
from jc_c where sfsc='N'
<if test="hjqx == 'ws' ">
AND hjqx NOT LIKE '33%' AND zszldm like 'S%'
</if>
<if test="zszldm =='S1'">
AND LEFT(zszldm, 2) LIKE 'S1'
</if>
<if test="zszldm =='S0'">
AND LEFT(zszldm, 2) LIKE 'S0'
</if>
<if test="zszldmx != null and zszldmx != '' ">
and zszldm = #{zszldmx}
</if>
<if test="sfzhm != null and sfzhm != '' ">
and sfzhm like concat("%",#{sfzhm},"%")
</if>
<if test="jddm != null and jddm != '' ">
and jddm = #{jddm}
</if>
<if test="xm != null and xm != '' ">
and xm like concat("%",#{xm},"%")
</if>
AND zszt='1'
AND jddm is not null
and lbdm not like 'O%' and lbdm not like 'I%'
</select>
5、动态标签<foreach>:一般用于批量操作,比如批量查询或删除
Integer getCynumberByZszl(@Param("ids") List<String> ids,@Param("flag") String flag);
foreach有几个属性:collection:遍历的集合 item:遍历出来的元素 separator:分割符 open:遍历开始前拼接的SQL片段 close:遍历结束后拼接的SQL片段
<select id="getCynumberByZszl" resultType="java.lang.Integer">
SELECT count(*)
FROM jc_cyzs s
where zszt='1'
<if test="flag == 'zwcy' ">
and s.zszldm LIKE 'S%' and s.zszldm != 'S000'
</if>
<if test="flag == 'ptcy' ">
and s.zszldm = 'S000'
</if>
<if test="ids != null and ids != ''">
<foreach collection="ids" item="id" open="and s.id in (" close=")" separator=",">
#{id}
</foreach>
</if>
</select>
<?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="org.jeecg.modules..mapper.Mapper">
<select id="selectByCzId" resultType="org.jeecg.modules.wlyg.entity.Jc">
select *
from jc_c
where id = #{czid}
and sfsc = 'N'
</select>
<select id="selectByCyJddm" resultType="org.jeecg.modules.wlyg.vo.Jc">
SELECT *,CONCAT(LEFT(sfzhm,6),'********',RIGHT(sfzhm,4)) AS sfzhmb
from jc_c where sfsc='N'
<if test="hjqx == 'bq' ">
AND hjqx like'330322' AND zszldm like 'S%'
</if>
<if test="hjqx == 'wq' ">
AND hjqx NOT LIKE '330322' AND hjqx LIKE '33%' AND zszldm like 'S%'
</if>
<if test="hjqx == 'ws' ">
AND hjqx NOT LIKE '33%' AND zszldm like 'S%'
</if>
<if test="zszldm =='S1'">
AND LEFT(zszldm, 2) LIKE 'S1'
</if>
<if test="zszldm =='S2'">
AND LEFT(zszldm, 2) LIKE 'S2'
</if>
<if test="zszldm =='S5'">
AND LEFT(zszldm, 2) LIKE 'S5'
</if>
<if test="zszldm =='S0'">
AND LEFT(zszldm, 2) LIKE 'S0'
</if>
<if test="zszldmx != null and zszldmx != '' ">
and zszldm = #{zszldmx}
</if>
<if test="sfzhm != null and sfzhm != '' ">
and sfzhm like concat("%",#{sfzhm},"%")
</if>
<if test="jddm != null and jddm != '' ">
and jddm = #{jddm}
</if>
<if test="xm != null and xm != '' ">
and xm like concat("%",#{xm},"%")
</if>
AND zszt='1'
AND jddm is not null
and lbdm not like 'O%' and lbdm not like 'I%'
</select>
<select id="getCynumberByZszl" resultType="java.lang.Integer">
SELECT count(*)
FROM jc_cyzs s
where zszt='1'
<if test="flag == 'zwcy' ">
and s.zszldm LIKE 'S%' and s.zszldm != 'S000'
</if>
<if test="flag == 'ptcy' ">
and s.zszldm = 'S000'
</if>
<if test="ids != null and ids != ''">
<foreach collection="ids" item="id" open="and s.id in (" close=")" separator=",">
#{id}
</foreach>
</if>
</select>
</mapper>