[劉陽Java]_MyBatis_動態SQL標簽用法_第7講


1.MyBatis的動態SQL是基於OGNL表達式的,它可以幫助我們方便的在SQL語句中實現某些邏輯。

2.MyBatis中用於實現動態SQL的元素主要有

  • if
  • choose(when,otherwise)
  • trim
  • where
  • set
  • foreach

可以看出MyBatis的動態SQL的標簽元素和接近JSP中的JSTL語法,下面我就分別詳細的介紹一下

3.動態SQL中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="com.mybatis3.mapping.StudentMapper">

    <resultMap type="Student" id="StudentResultMap">
        <id column="id" property="id"/>
        <result column="sname" property="sname"/>
    </resultMap>
    
    <select id="getStudentMultiple" resultMap="StudentResultMap" parameterType="Student">
        select * from student where 1=1
        <if test="id != 0">
            and id = #{id}
        </if>
        <if test="sname != null">
            and sname = #{sname}
        </if>
    </select>
</mapper>

4.動態SQL中choose用法

<?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.mybatis3.mapping.StudentMapper">

    <resultMap type="Student" id="StudentResultMap">
        <id column="id" property="id"/>
        <result column="sname" property="sname"/>
    </resultMap>

    <select id="getStudentMultipleChoose" resultMap="StudentResultMap" parameterType="Student">
        select * from student where 1=1
        <choose>
            <when test="id != 0">
                and id = #{id}
            </when>
            <when test="sname != null">
                and sname = #{sname}
            </when>
        </choose>
    </select>
</mapper>

5.動態SQL中where語句的作用主要是簡化SQL語句中where中的條件判斷的

<?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.mybatis3.mapping.StudentMapper">

    <resultMap type="Student" id="StudentResultMap">
        <id column="id" property="id"/>
        <result column="sname" property="sname"/>
    </resultMap>
    
    <select id="getStudentMultipleWhere" resultMap="StudentResultMap" parameterType="Student">
        select * from student
        <where>
            <if test="id != 0">
                and id = #{id}
            </if>
            <if test="sname != null">
                and sname = #{sname}
            </if>
        </where>
    </select>
</mapper>

注意: where元素的作用是給SQL語句添加一個條件判斷. 如果輸出后是and 開頭的,MyBatis會把第一個and忽略,當然如果是or開頭的,MyBatis也會把它忽略;此外,在where元素中你不需要考慮空格的問題,MyBatis會智能的幫你加上

6.動態SQL中set元素主要是用在更新操作的時候,它的主要功能和where元素其實是差不多的

<?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.mybatis3.mapping.StudentMapper">

    <resultMap type="Student" id="StudentResultMap">
        <id column="id" property="id"/>
        <result column="sname" property="sname"/>
    </resultMap>
    
    <update id="updateStudentMultipleSet" parameterType="Student">
        update student
        <set>
            <if test="sname != null">
                sname = #{sname},
            </if>
        </set>
        where id = #{id}
    </update>
</mapper>

7.動態SQL中foreach

  • 主要用在構建in條件中,它可以在SQL語句中進行迭代一個集合。
  • foreach元素的屬性主要有item,index,collection,open,separator,close
<?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.mybatis3.mapping.StudentMapper">

    <resultMap type="Student" id="StudentResultMap">
        <id column="id" property="id"/>
        <result column="sname" property="sname"/>
    </resultMap>
    
    <select id="getStudentMultipleForeach" resultMap="StudentResultMap">
        select * from student where id in
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
</mapper>

注意:在Java代碼中如何使用MyBaits的動態SQL語句

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.mybatis3.entity.Student;

public class Test02 {
    private static SqlSessionFactory sqlSessionFactory;
    private static Reader reader;
    
    static {
        try {
            reader = Resources.getResourceAsReader("SqlMapConfig.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 動態SQL中foreach用法
     */
    @Test
    public void m05() {
        SqlSession session = sqlSessionFactory.openSession();
        String statement = "com.mybatis3.mapping.StudentMapper.getStudentMultipleForeach";
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        List<Student> studentList = session.selectList(statement, list);
        session.close();
        System.out.println(studentList);
    }
}

8.動態SQL中trim語句

trim元素的主要功能是可以在自己包含的內容前加上某些前綴,也可以在其后加上某些后綴,與之對應的屬性是prefix和suffix;可以把包含內容的首部某些內容覆蓋,即忽略,也可以把尾部的某些內容覆蓋,對應的屬性是prefixOverrides和suffixOverrides

<select id="dynamicTrimTest" parameterType="Blog" resultType="Blog">
        select * from t_blog
        <trim prefix="where" prefixOverrides="and |or">
            <if test="title != null">
                and title = #{title}
            </if>
            <if test="content != null">
                and content = #{content}
            </if>
            <if test="owner != null">
                or owner = #{owner}
            </if>
        </trim>
</select>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM