Mybatis(4)---dynamic sql

Four, dynamic sql

1. Dynamic sql

Dynamic SQL refers to that the content of the sql statement is changed, and different sql statements can be obtained according to the conditions. The
main part of the where is changed.

2. The realization of dynamic sql

The implementation of dynamic sql uses the tags provided by mybatis, for example,

(1) if tag

The if tag is used to determine the condition.
Syntax:

part of the sql statement

<?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="customDAO.TestDao">
    <!--
        <if test="java对象的属性">
    -->
    <select id="selectIf" resultType="customDAO.Test">
        <!--这里加入where 1=1的目的是为了保证语法格式正确-->
        SELECT * FROM t_test WHERE 1=1
        <if test="name != null and name != ''">
            name = #{name}
        </if>
    </select>
</mapper>

(2) where tag

The where tag is used to contain multiple if tags. When multiple ifs are established, the where tag will automatically add a where keyword and remove the redundant and, or, etc. in the if

<?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="customDAO.TestDao">
    <!--
        <where标签>
    -->
    <select id="selectIf" resultType="customDAO.Test">
        SELECT * FROM t_test
        <where>
	        <if test="name != null and name != ''">
	            name = #{name}
	        </if>
        </where>
    </select>
</mapper>

(3) foreach tag

The foreach tag is used to loop the array or list collection in java

Mainly used in SQL in statement

For example: Find students whose id is 1001, 1002, 1003
select * from t_test where id in (1001,1002,1003);

Two ways to use

sql mapping file

<?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="customDAO.TestDao">
    <!--
        foreach的第一种用法:List集合
    -->
    <select id="foreach1" resultType="customDAO.Test">
        SELECT * FROM t_test WHERE id IN
        <!--
            collection:表示接口中方法参数的类型,如果是数组使用array,如果是list集合使用list
            item:自定义的表示数组或集合成员中的变量
            open:循环开始的字符
            close:循环结束时的字符
            separator:集合成员之间的分隔
        -->
        <foreach collection="list" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>
    <!--
        foreach的第二种用法
    -->
    <select id="foreach2" resultType="customDAO.Test">
        SELECT * FROM t_test WHERE id IN
        <foreach collection="list" item="test" open="(" close=")" separator=",">
            <!--
                循环对象的某个属性值
            -->
            #{test.id}
        </foreach>
    </select>
</mapper>

Methods in the dao interface

import java.util.List;
//定义操作test表的接口
public interface TestDao
{
    
    
    //foreach用法1
    public List<Test> foreach1(List<Integer> idList);
    //foreach用法2
    public List<Test> foreach2(List<Test> test);
}

(4) sql code snippet

Purpose: reuse some sentences

Step
1. First define the sql statement
2. Use the include tag to import

sql mapping file

<?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="customDAO.TestDao">
    <!--定义sql的片段-->
    <sql id="mysql">
        SELECT * FROM t_test
    </sql>
    <select id="selectIf" resultType="customDAO.Test">
        <!--SELECT * FROM t_test-->
        <!--使用include导入sql语句-->
        <include refid="mysql"></include>
        <where>
            <if test="name != null and name != ''">
                name = #{name}
            </if>
        </where>
    </select>
</mapper>

Guess you like

Origin blog.csdn.net/weixin_46841376/article/details/114667624