Mybatis three | Dynamic SQL

Table of contents

if

where

set

 foreach

 sql&include


ctrl + alt + l format SQL statement

 SQL that changes with user input or external conditions is called dynamic SQL

if

<if> is used to determine whether the condition is true. Use the test attribute to determine the condition. If true, splice SQL 

where

The wehre element will only insert a where clause if the condition is met, and the leading AND or OR will be automatically removed.

If only the name is passed, the previous program will not be able to query successfully. The above problem can be solved through dynamic SQL. 

EmpMapper.xml

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.EmpMapper">
    <select id="list" resultType="com.itheima.pojo.Emp">
        select *
        from emp
        <where>
            <if test="name != null">
              name like concat('%', #{name}, '%')
            </if>
          <if test="gender != null">
              and gender = #{gender}
          </if>
          <if test="begin != null and end != null">
              and entrydate between #{begin} and #{end}
          </if>
        </where>
        order by update_time desc
    </select>
</mapper>

 SpringbootMybatisCrudApplicationTests.java

@SpringBootTest
class SpringbootMybatisCrudApplicationTests {

    @Autowired
    private EmpMapper empMapper;

    @Test
    public void testSelect(){
        List<Emp> list = empMapper.list(null,(short)1,null,null);
        System.out.println(list);
    }

}

 The running results are as follows 

set

<set> dynamically inserts the SET keyword at the beginning of the line and removes extra commas (used in update statements) 

Change the username of employee with ID 18 to Tom111, name to Tom111, gender to 2, and other things remain unchanged.

Updating as before will make all other values ​​null

Can be solved through dynamic SQL

EmpMapper.xml

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.EmpMapper">
    <update id="update">
        update emp
        <set>
            <if test="username != null">username=#{username},</if>
            <if test="password != null">password=#{password},</if>
            <if test="name != null">name=#{name},</if>
            <if test="gender != null">gender=#{gender},</if>
            <if test="image != null">image=#{image},</if>
            <if test="job != null">job=#{job},</if>
            <if test="entrydate != null">entrydate=#{entrydate},</if>
            <if test="deptId != null">dept_id=#{deptId},</if>
            <if test="updateTime != null">update_time=#{updateTime}</if>
        </set>
        where id=#{id}
    </update>
</mapper>

EmpMapper.java

@Mapper
public interface EmpMapper {
    public void update(Emp emp);

}

This time the employee with ID 19 is updated, SpringbootMybatisCrudApplicationTests.java

@SpringBootTest
class SpringbootMybatisCrudApplicationTests {

    @Autowired
    private EmpMapper empMapper;

    @Test
    public void testUpdate(){
        Emp emp = new Emp();
        emp.setId(19);
        emp.setUsername("Tom2222");
        emp.setName("Tom222");
        emp.setGender((short)1);
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
    }

}

 The running results are as follows. It is found that only four fields have been updated, and the remaining fields remain unchanged.

 foreach

Implement batch deletion

collection collection name

The elements traversed from the item collection, items

separator The separator used for each traversal

open traverses the spliced ​​fragments before starting

close The fragments spliced ​​after the traversal is completed

EmpMapper.xml

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.EmpMapper">
    <delete id="deleteByIds">
        delete from emp where id in
        <foreach collection="ids" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </delete>

</mapper>

 SpringbootMybatisCrudApplicationTests.java

@SpringBootTest
class SpringbootMybatisCrudApplicationTests {

    @Autowired
    private EmpMapper empMapper;

    @Test
    public void testDeleteByIds(){
        List<Integer> ids = Arrays.asList(13,14,15);
        empMapper.deleteByIds(ids);
    }

}

EmpMapper.java

@Mapper
public interface EmpMapper {
    public void deleteByIds(List<Integer> ids);

}

The operation found that the deletion was successful.

 sql&include

sql defines reusable fragments

include specifies the included sql fragment through the attribute refid

EmpMapper.xml  

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.EmpMapper">
    <sql id="commonSelect">
        select id, username, password, name, gender, image, job, entrydate, dept_id deptId, create_time createTime, update_time updateTime
        from emp
    </sql>
    <select id="list" resultType="com.itheima.pojo.Emp">
        <include refid="commonSelect"/>
        <where>
            <if test="name != null">
                name like concat('%', #{name}, '%')
            </if>
            <if test="gender != null">
                and gender = #{gender}
            </if>
            <if test="begin != null and end != null">
                and entrydate between #{begin} and #{end}
            </if>
        </where>
        order by update_time desc
    </select>
</mapper>

Guess you like

Origin blog.csdn.net/m0_72832574/article/details/135200990