1=1
- <!--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>
where元素主要是用于简化查询语句中where部分的条件判断,where元素可以再
- <!--List<Emp> getEmpByCondition(Emp emp);-->
- <select id="getEmpByCondition" resultType="Emp">
- select * from t_emp
- <where>
- <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>
- </where>
- </select>
- <!--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>
- //测试类
- @Test
- public void getEmpByCondition() {
- SqlSession sqlSession = SqlSessionUtils.getSqlSession();
- DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
- 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() {
- SqlSession sqlSession = SqlSessionUtils.getSqlSession();
- DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
- List<Emp> emps = mapper.getEmpByChoose(new Emp(null, "张三", 23, "男", "123@qq.com", null));
- System.out.println(emps);
- }
结果如下:
if a else if b else if c else d
,只会执行其中一个属性:
,
- <!--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>
- @Test
- public void deleteMoreByArray() {
- SqlSession sqlSession = SqlSessionUtils.getSqlSession();
- DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
- int result = mapper.deleteMoreByArray(new Integer[]{6, 7, 8, 9});
- System.out.println(result);
- }
结果如下:
其中的xml文件里的SQL语句也可以为:
- <!--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() {
- SqlSession sqlSession = SqlSessionUtils.getSqlSession();
- DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
- Emp emp1 = new Emp(null,"a",1,"男","123@321.com",null);
- Emp emp2 = new Emp(null,"b",1,"男","123@321.com",null);
- Emp emp3 = new Emp(null,"c",1,"男","123@321.com",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,email</sql>
标签- <!--List<Emp> getEmpByCondition(Emp emp);-->
- <select id="getEmpByCondition" resultType="Emp">
- select <include refid="empColumns"></include> from t_emp
- </select>