徐徐爱coding
  • 首页
  • 爱情买卖
  • 导航
  • 私语
  • 友情链接
  • 关于
    关于本站
    知识库
    弹钢琴
徐徐爱coding

徐徐爱coding

徐徐爱coding是一个个人博客站点,记录编程经历的点点滴滴,分享自己的所见与所得,坚持自己的初心,践行自己的梦想生活不是等着暴风雨过去,而是学会在风雨中跳舞!

Copyright © 2023 徐徐爱coding All Rights Reserved.
陕公网安备61019602000456陕ICP备2023007787号-2

网站已稳定运行

mybatis--动态sql

mybatis--动态sql

徐徐
算法
#SQL
1 热度0 评论0 点赞
发布于2023-09-23 13:44:36
🌺前言
展示?动态sql标签的用法

1.if标签

xml
<select id="getEmpListByParam" resultType="Emp">
   select * from t_emp where 1=1
   <if test="empName!=null and empName!=''">
     and emp_name = #{empName}
   </if>
    <if test="age!=null and age!=''">
        and age = #{age}
    </if>
    <if test="gender!=null and gender!=''">
        and gender = #{gender}
    </if>
</select>

2.where标签往往结合if一起使用

where:

  1. 如果有if成立则声称where关键字
  2. 去掉前面多余的and,后面多余的不会去掉
  3. 如果if都不成立,where不起作用.
xml
   <select id="getEmpListByParam" resultType="Emp">
               select * from t_emp
            <where>
                <if test="empName!=null and empName!=''">
                     emp_name = #{empName}
                </if>
                <if test="age!=null and age!=''">
                    and age = #{age}
                </if>
                <if test="gender!=null and gender!=''">
                    and gender = #{gender}
                </if>
            </where>

    </select>

3.trim标签

  • prefix添加前面的关键字
  • prefixOverrides去掉sql前面多余的关键字
  • suffix添加后面面的关键字
  • suffixOverrides去掉sql后面面多余的关键字
xml
<select id="getEmpListByParam" resultType="Emp">
    select * from t_emp
    <trim prefix="where" prefixOverrides="and">
        <if test="empName!=null and empName!=''">
            emp_name = #{empName}
        </if>
        <if test="age!=null and age!=''">
            and age = #{age}
        </if>
        <if test="gender!=null and gender!=''">
            and gender = #{gender}
        </if>
    </trim>

</select>

4.choose when otherwise

  • 相当于if..else if...else
  • when最少写一次
  • otherwise最多写一次
  • 内部不用加and自己会生成
  • 只会走一个分支
xml
<select id="getEmpListByChoose" resultType="Emp">
    select * from t_emp
        <where>
            <choose>
                 //if
                <when test="empName!=null and empName!=''">
                    emp_name = #{empName}
                </when>
                 //else if
                <when test="age!=null and age!=''">
                    age = #{age}
                </when>
                 //else
                <otherwise>
                    gender = #{gender}
                </otherwise>
            </choose>
        </where>
</select>

5.foreach

  1. collection:循环的list
  2. item:项,
  3. separator:分隔符
  4. open :整个循环以啥开始的
  5. close :一啥结束
  6. index:索引

批量添加

java
void  batchInsertEmp(@Param("emps") List<Emp> emps);
xml
<insert id="batchInsertEmp">
    insert into t_emp values
  <foreach collection="emps" item="emp" separator=",">
      (null ,#{emp.empName},#{emp.age},#{emp.gender},null )
  </foreach>
</insert>
java
public void testBatchInsertEmp(){
    SqlSession sqlSession = SqlSessionUtil.getSqlSession();
    EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
    Emp emp = new Emp(null,"小明1",34,"男");
    Emp emp1 = new Emp(null,"小明2",34,"男");
    Emp emp2 = new Emp(null,"小明3",34,"男");
    List<Emp> list = Arrays.asList(emp,emp1,emp2);
   mapper.batchInsertEmp(list);

}

批量删除

java
void batchDeleteEmpByIds(@Param("ids") Integer [] ids);
java
public void testBatchDeleteEmp(){
    SqlSession sqlSession = SqlSessionUtil.getSqlSession();
    EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
    Integer [] ids = new Integer []{6,7};
    mapper.batchDeleteEmpByIds(ids);

}

方法1

xml
<delete id="batchDeleteEmpByIds">
    delete from t_emp where emp_id in
(
  <foreach collection="ids" item="id" separator=",">
      #{id}
  </foreach>
)
</delete>

方法2

xml
<delete id="batchDeleteEmpByIds">
        delete from t_emp where emp_id in
      <foreach collection="ids" item="id" separator="," open="(" close=")">
          #{id}
      </foreach>
 </delete>

方法3

xml
<delete id="batchDeleteEmpByIds">
    delete from t_emp where
    <foreach collection="ids" item="id" separator="or">
        emp_id = #{id}
    </foreach>
</delete>

6.sql,include

目录

  • 1.if标签
  • 2.where标签往往结合if一起使用
    • where:
  • 3.trim标签
  • 4.choose when otherwise
  • 5.foreach
    • 批量添加
    • 批量删除
      • 方法1
      • 方法2
      • 方法3
  • 6.sql,include
文章最后更新于 2024-08-06 20:57:38
作者:徐徐
版权声明:转载请注明文章出处
留言
暂无数据

~~空空如也