Python 官方文档:入门教程 => 点击学习
目录1、if(常用)2、where3、trim4.choose、when、otherwise5、foreach5.1批量删除5.2批量添加6、sql标签总结mybatis框
mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是通过标签解决拼接SQL语句字符串时的问题
if:根据标签中test属性所对应的表达式决定标签中的内容是否需要拼接到SQL中
List<Emp> getEmpByCondition(Emp emp);
当empName为null和“ ”时,会拼接and age,此时会报错,可以1=1恒成立解决。
<!--List<Emp> getEmpListByMoreTJ(Emp emp);-->
<select id="getEmpListByMoreTJ" resultType="Emp">
select * from t_emp where 1=1
<if test="empName!= '' and empName!= null">
and emp_name = #{empName}
</if>
<if test="age != '' and age != null">
and age = #{age}
</if>
<if test="sex != '' and sex != null">
and sex = #{sex}
</if>
</select>
@Test
public void testGetEmpByCondition(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
List<Emp> list = mapper.getEmpByCondition(new Emp(null, "陈智", 33, "女", null));
System.out.println(list);
}
select eid,emp_name,age,sex,email from t_emp where emp_name = ? and age = ? or sex = ?
当where标签中有内容时,会自动生成where关键字,并且将内容前多余的and或or去掉(在程序执行后生成的sql会将内容前多余的and或or去掉)
and写在第二句是为了和上面拼接,写在第一句是和下面拼接(即固定有elect * from t_emp emp_name= ?)
<select id="getEmpListByMoreTJ2" resultType="Emp">
select * from t_emp
<where>
<if test="empName != '' and empName != null">
emp_name = #{empName }
</if>
<if test="age != '' and age != null">
and age = #{age}
</if>
<if test="sex != '' and sex != null">
and sex = #{sex}
</if>
</where>
</select>
当where标签中没有内容时(或者使用getEmpByCondition传入的值全为null或“ ”),此时where标签没有任何效果。则直接SQL语句为select * from t_emp
--------------------分割--------------------
where标签不能将其中内容后面多余的and或or去掉:(会报错)
语句为select * from t_emp emp_name= ? and
<select id="getEmpListByMoreTJ2" resultType="Emp">
select * from t_emp
<where>
<if test="empName != '' and empName != null">
emp_name = #{empName } and
</if>
<if test="age != '' and age != null">
age = #{age} and
</if>
<if test="sex != '' and sex != null">
sex = #{sex}
</if>
</where>
</select>
where的增强
若标签中有内容时:
prefix
|suffix
:将trim标签中内容前面或后面添加指定内容suffixOverrides
|prefixOverrides
:将trim标签中内容前面或后面去掉指定内容若标签中没有内容时,trim标签也没有任何效果(跟上面的where一样)
<!--List<Emp> getEmpByCondition(Emp emp);-->
<select id="getEmpByCondition" resultType="Emp">
select <include refid="empColumns"></include> from t_emp
<trim prefix="where" suffixOverrides="and|or">
<if test="empName != null and empName != ''">
emp_name = #{empName} and
</if>
<if test="age != null and age != ''">
age = #{age} or
</if>
<if test="sex != null and sex != ''">
sex = #{sex} and
</if>
<if test="email != null and email != ''">
email = #{email}
</if>
</trim>
</select>
相当于if...else if...else
List<Emp> getEmpByChoose(Emp emp);
when至少要有一个,otherwise最多只能有一个
与第一点if的区别:choose只会满足一个条件便退出,一个不满足则执行otherwise
<!--List<Emp> getEmpByChoose(Emp emp);-->
<select id="getEmpByChoose" resultType="Emp">
select * from t_emp
<where>
<choose>
<when test="empName != null and empName != ''">
emp_name = #{empName}
</when>
<when test="age != null and age != ''">
age = #{age}
</when>
<when test="sex != null and sex != ''">
sex = #{sex}
</when>
<when test="email != null and email != ''">
email = #{email}
</when>
<otherwise>
did = 1
</otherwise>
</choose>
</where>
</select>
@Test
public void testGetEmpByChoose(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
List<Emp> list = mapper.getEmpByChoose(new Emp(null, "", null, "", ""));
System.out.println(list);
}
collection
:设置需要循环的数组或集合item
:表示数组或集合中的每一个数据separator
:循环体之间的分割符open
:foreach标签所循环的所有内容的开始符close
:foreach标签所循环的所有内容的结束符
int deleteMoreByArray(@Param("eids") Integer[] eids);
<!--int deleteMoreByArray(@Param("eids") Integer[] eids);-->
<delete id="deleteMoreByArray">
delete from t_emp where
<foreach collection="eids" item="eid" separator="or">
eid = #{eid}
</foreach>
<!--
delete from t_emp where eid in
<foreach collection="eids" item="eid" separator="," open="(" close=")">
#{eid}
</foreach>
-->
</delete>
@Test
public void testDeleteMoreByArray(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
int result = mapper.deleteMoreByArray(new Integer[]{12,13,14,15});
System.out.println(result);
}
int insertMoreByList(@Param("emps") List<Emp> emps);
<!--int insertMoreByList(@Param("emps") List<Emp> emps);-->
<insert id="insertMoreByList">
insert into t_emp values
<foreach collection="emps" item="emp" separator=",">
(null,#{emp.empName},#{emp.age},#{emp.sex},#{emp.email},null)
</foreach>
</insert>
Arrays.asList该方法是将数组转化成List集合的方法
如果你的List只是用来遍历,就用Arrays.asList()。
如果你的List还要添加或删除元素,还是乖乖地new一个java.util.ArrayList,然后一个一个的添加元素。
@Test
public void testInsertMoreByList(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
Emp emp1 = new Emp(null,"a1",23,"男","123@qq.com");
Emp emp2 = new Emp(null,"a2",23,"男","123@qq.com");
Emp emp3 = new Emp(null,"a3",23,"男","123@qq.com");
List<Emp> emps = Arrays.asList(emp1, emp2, emp3);
System.out.println(mapper.insertMoreByList(emps));
}
设置SQL片段:<sql id="empColumns">eid,emp_name,age,sex,email</sql>
引用SQL片段:<include refid="empColumns"></include>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: mybatis中的动态sql问题
本文链接: https://lsjlt.com/news/197690.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0