本篇内容主要讲解“mybatis中的动态sql问题怎么解决”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“mybatis中的动态sql问题怎么解决”吧!
Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是通过标签解决拼接SQL语句字符串时的问题
1、if(常用)
if:根据标签中test属性所对应的表达式决定标签中的内容是否需要拼接到SQL中
/**
* 多条件查询
*/
List<Emp> getEmpByCondition(Emp emp);
当empName为null和“ ”时,会拼接and age,此时会报错,可以1=1恒成立解决。
-
用and表示&&(并且)
-
emp_name是字段名
-
if只有test标签且必须使用
<!--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 = ?
2、where
当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>
3、trim
where的增强
若标签中有内容时:
-
|prefix
:将trim标签中内容前面或后面添加指定内容suffix
-
|suffixOverrides
:将trim标签中内容前面或后面去掉指定内容prefixOverrides
若标签中没有内容时,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>
4.choose、when、otherwise
相当于if...else if...else
/**
* 测试choose、when、otherwise
*/
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);
}
5、foreach
-
:设置需要循环的数组或集合collection
-
:表示数组或集合中的每一个数据item
-
:循环体之间的分割符separator
-
:foreach标签所循环的所有内容的开始符open
-
:foreach标签所循环的所有内容的结束符close
5.1批量删除
/**
* 通过数组实现批量删除
*/
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);
}
5.2批量添加
/**
* 通过list集合实现批量添加
*/
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));
}
6、sql标签
设置SQL片段:<sql id="empColumns">eid,emp_name,age,sex,email</sql>
引用SQL片段:<include refid="empColumns"></include>