Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是为了解决拼接SQL语句字符串时的痛点问题
1=1
1=1
可以用来拼接and
语句,例如:当empName为null时select * from t_emp where and age = ? and sex = ? and email = ?
,此时where
会与and
连用,SQL语句会报错
select * from t_emp where 1= 1 and age = ? and sex = ? and email = ?
,此时不报错
<select id="getEmpByCondition" 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="sex != null and sex !=''">
and sex = #{sex}
if>
<if test="email != null and email !=''">
and email = #{email}
if>
select>
条件最前方多余的and/or去掉
<select id="getEmpByCondition" 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="sex != null and sex !=''">
and sex = #{sex}
if>
<if test="email != null and email !=''">
and email = #{email}
if>
where>
select>
注意
:where标签不能去掉条件后多余的and/or
select * from t_emp
<select id="getEmpByCondition" resultType="Emp">
select * 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} and
if>
<if test="sex != null and sex !=''">
sex = #{sex} or
if>
<if test="email != null and email !=''">
email = #{email}
if>
trim>
select>
//测试类
@Test
public void getEmpByCondition() {
‘’‘
获取mapper
’‘’
List<Emp> emps= mapper.getEmpByCondition(new Emp(null, "张三", null, null, null, null));
System.out.println(emps);
}
choose、when、otherwise
相当于if...else if..else
<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 getEmpByChoose() {
‘’‘
获取mapper
’‘’
List<Emp> emps = mapper.getEmpByChoose(new Emp(null, "张三", 23, "男", "[email protected]", null));
System.out.println(emps);
}
,
<delete id="deleteMoreByArray">
delete from t_emp where eid in
<foreach collection="eids" item="eid" separator="," open="(" close=")">
#{eid}
foreach>
delete>
@Test
public void deleteMoreByArray() {
''''
获取mapper
''''
int result = mapper.deleteMoreByArray(new Integer[]{6, 7, 8, 9});
System.out.println(result);
}
```xml
<!--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>
@Test
public void insertMoreByList() {
'''
获取mapper
'''
Emp emp1 = new Emp(null,"a",1,"男","[email protected]",null);
Emp emp2 = new Emp(null,"b",1,"男","[email protected]",null);
Emp emp3 = new Emp(null,"c",1,"男","[email protected]",null);
List<Emp> emps = Arrays.asList(emp1, emp2, emp3);
int result = mapper.insertMoreByList(emps);
System.out.println(result);
}
标签<sql id="empColumns">eid,emp_name,age,sex,emailsql>
标签
<select id="getEmpByCondition" resultType="Emp">
select <include refid="empColumns">include> from t_emp
select>