动态SQL

bind

<!-- 实现模糊査询 like'%xx%
1可以使用mysql的字符串拼接  1.1.空格拼接   1.2.CONCAT函数
2.可以拼接好再传进来
3使用bind在Mapper映射文件上下文声明一个变量<bind>
<bind>在Mapper映射文件上下文声明一个变量
    name 变量名称
    value 值(支持OGNL表达式)
<select id="QueryEmp" resultType="Emp”>
    <bind name="_username" value="'%'+username+'%'"/>
     SELECT * FROM emp where user_name like  #{_username}
</select>

sql片段

sql片段 解决SQL中重复的代码冗余,可以提取出来放在sql片段中
1.<sql 定义sql片段
    id 唯一标识
2.<include 在SQL中引用SQL片段片段
    refid 需要引用的SQL片段的id
<select id="QueryEmp" resultType="Emp”>
    <bind name="_username" value="'%'+username+'%'"/>
     <include refid="SQL片段id"></include>where user_name like  #{_username}
</select>

<include refid="SQL片段id">
     <property 声明变量,
     就可以在SQL片段中动态调用,让不同的SQL调用同一个SQL片段达到不同的功能
          name 变量名
          value 变量值
     一般情况使用${}在sql片段中引用,一旦引用了,一定保证每个include都声明了该变量
</include>
此时refid对应的sql片段的select的查询声明为${name对应变量名} 然后include自定义条件

循环

<foreach 循环
      实现in 范围查询 使用$可以实现但是有sql注入风险
     collection 需要循环的集合的参数名字
     item 每次循环使用的接收变量
     separator 分割符设置(每次循环在结尾添加什么分隔符,会自动去除最后一个结尾的分隔符)
     open 循环开始添加的字符串
     close 循环开添加的字符串
     index 循环下标的变量
<where>
    <foreach collection="usernames" item="username" separator="," open="user_name in (" close=")" index="i">
        ${username}
    </foreach)
</where>
<!-- 使用foreach批量插入用户-->
        <insert id="insertMultiUsers">
            insert into user(user_name,gender,email,address,dept_id)
            values
            <foreach collection="users" item="user" separator=",">
                (#{user.userName},#{user.gender},#{user.email},#{user.address},#{user.deptId})
            </foreach>
        </insert>

set

<set 用在update语句上面的
    会自动加上set关键字
    会自动去除最后一个更新字段的,
<set>
    if的条件
</set>
可使用<trim prefix="set" suffixOverrides=","></trim>替代

if-where-trim

<if test="字段!=null and 字段!='' ">and 字段 LIKE CONCAT(CONCAT('%', #{字段}), '%')</if>
问题:and 需要动态拼接的问题(只有一个条件的情况就不需要and,如果多个条件就必须用and/or 来拼接)
解决:1.加一个永远都成立的条件(比如:1=1),后面条件都加上and就行
2.使用where标签
3.trim

<where 一般会加载动态条件中配合使用, 它会自动在所有条件的前面加上WHERE关键字, 还会去掉所有条件
前面的AND/OR,当无内容时不起作用
<trim 它的功能比较灵活、广泛。 它可以用来实现<where>节点的功能
      prefix 在所有包含的SQL前面加上指定的字符串
      prefixOverrides 在所有包含的SQL前面加上去除指定的字符串
      suffix在所有包含的SQL后面加上指定的字符串
      prefixOverrides 在所有包含的SQL后面加上去除指定的字符串

choose--when--otherwise

choose--when--otherwise:相当于if...else if...else
(与if标签的区别:用if时,条件都要执行,choose,when,otherwise只要一个满足条件后面就不再执行因此不
需要and)
   <select id="searchUsers" resultType="com.example.entity.User">
        SELECT * FROM users
        <choose>
            <when test="id != null">
                WHERE id = #{id}
            </when>
            <when test="username != null and username != ''">
                WHERE username = #{username}
            </when>
            <otherwise>
                -- 若前面条件都不满足,查询所有用户
                WHERE 1 = 1
            </otherwise>
        </choose>
    </select>

where-choose-when

SELECT * FROM emp
<where>
      <choose>
            <when test="deptName=='经理'">
                  dept id=1
            </when>
            <when test="deptName=='普通员工' ">
                  dept id=2
            </when>
            <otherwise>
                  dept_id=# {id}
            </otherwise>
      </choose>
</where>
相当于switch case

一对多多对一关系处理

<!--关联表多对一的查询-->
构造函数加一个private Dept dept;
//mapper层传一个Integer类型的id
<select id="getEmpAndDeptByEmpId" resultType="Emp">
    select
    t_emp.*,t_dept.* from t_emp left join t_dept
    on t_emp.dept_id = t_dept.dept_id where t_emp. emp_id = #{empId}
</select>
//级联处理多对一映射问题
<resultMap id="empAndDeptResultMap" type="Emp">
    <id column="emp_id" property="empId"></id>
    <result column="emp_name" property="empName"></result>
    <result column="age" property="age"></result>
    <result column="gender" property="gender"></result>
    <result column="dept_id" property="dept.deptId"></result>
    <result column="dept_name" property="dept.deptName"></result>
</resultMap>
association处理多对一映射问题:
<resultMap id="empAndDeptResultMap" type="Emp">
    <id column="emp_id" property="empId"></id>
    <result column="emp_name" property="empName"></result>
    <result column="age" property="age"></result>
    <result column="gender" property="gender"></result>
    <association property="dept" javaType="Dept">
        <id column="dept_id" property="deptId"></id>
        <result column="depk_name" property="deptName"></result>
    </association>
</resultMap>
<!--关联表一对多的查询-->
构造函数加一个private List<Emp> emp;
//mapper层传一个Integer类型的id
<select id="getEmpAndDeptByEmpId" resultType="Emp">
    select
    t_emp.*,t_dept.* from t_emp right join t_dept
    on t_emp.dept_id = t_dept.dept_id where t_emp. emp_id = #{empId}
</select>
<resultMap id="deptAndEmpResultMap" type="Dept">
    <id column="dept_id" property="deptId"></id>
    <result column="dept_name" property="deptName"></result>
    <collection property "emp" ofype="Emp">
        <id column="emp_id" property="empId"></id>
        <result column="emp_name" property="empName"></result>
        <result column="age" property="age"></result>
        <result column="gender" property="gender"></result></collection>
 </resultMap>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容