首先介绍动态SQL中的if标签,在介绍这个标签之前,添加一下数据库表。
增加两个表并添加对应数据。这个表后面也会用到,这个地方只用了一个表。
--添加表
CREATE TABLE if not EXISTS t_emp (
`eid` int NOT NULL AUTO_INCREMENT,
`emp_name` VARCHAR(20),
`age` int,
`sex` char,
`email` VARCHAR(20),
`did` int,
PRIMARY KEY(eid)
) ENGINE=INNODB;
--添加数据
INSERT INTO t_emp values(1, '张三', 23, '男', '123445@qq.com', 1);
INSERT INTO t_emp values(2, '李四', 13, '女', '199092@qq.com', 1);
INSERT INTO t_emp values(3, '王五', 32, '男', '144442@qq.com', 2);
INSERT INTO t_emp values(4, '赵六', 25, '女', '166666@qq.com', 2);
--添加表
CREATE TABLE if NOT EXISTS t_dept (
`did` int NOT NULL AUTO_INCREMENT,
`dept_name` VARCHAR(20),
PRIMARY KEY(did)
) ENGINE=INNODB;
--添加数据
insert into t_dept values(1, '研发部');
insert into t_dept values(2, '市场部');
其次,添加两个表对应的实体类。
package com.mlzx.pojo;
/**
* @author: Eric
* @description: TODO
* @date: 2022/4/6 22:47
* @version: 1.0
*/
public class Emp {
private Integer eid;
private String empName;
private Integer age;
private String sex;
private String email;
}
package com.mlzx.pojo;
import jdk.nashorn.internal.runtime.OptimisticReturnFilters;
/**
* @author: Eric
* @description: TODO
* @date: 2022/4/6 22:49
* @version: 1.0
*/
public class Dept {
private Integer did;
private String depaName;
}
下面配置mapper配置信息和接口信息。
<!--List<Emp> getEmpByCondition(Emp emp);-->
<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>
需要注意的是 1=1这个是必须写的,防止出现 select * from t_emp where and sex = ? 的情况,多出来一个and,会报错。
测试代码
@Test
public void testGetEmpByCondition(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
List<Emp> empList = mapper.getEmpByCondition(new Emp(null, "", 23, "男", "123445@qq.com" ));
System.out.println(empList);
}
测试结果可以正常查询
为了方便起见,我直接写mapper配置文件中的代码
<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>
在相同的情况下,比如emp_name为空的时候,本来后面会变成where and age = xxx的,但是用<where></where>会把and自动去掉。
需要注意的是,去掉and只可以去掉内容前的,如果and放到age = #{age}后面,就不能自动去掉了。
select * from t_emp
<!--List<Emp> getEmpByCondition(Emp 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>
上面代码中的作用是表示,prefix表示在前面添加where,and|or表示去掉where内容后面的and和or,生成的sql中会自动带着and。
,
<!--int deleteMoreByArray(Integer[] eids);-->
<delete id="deleteMoreByArray">
delete from t_emp where eid in
<foreach collection="eids" item="eid" separator="," open="(" close=")">
#{eid}
</foreach>
</delete>
<!--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>
<sql>
标签<sql id="empColumns">eid,emp_name,age,sex,email</sql>
引用sql片段:<include>
标签
<!--List<Emp> getEmpByCondition(Emp emp);-->
<select id="getEmpByCondition" resultType="Emp">
select <include refid="empColumns"></include> from t_emp
</select>
祝你每天开心。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。