zoukankan      html  css  js  c++  java
  • 动态SQl

    <?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.abc.dao.IStudentDao">
        
        <select id="selectStudentsByIf" resultType="Student">
            select id,name,age,score from student where 1=1
            
            <if test="name != null and name != ''">
                and name like '%' #{name} '%'
            </if>
            <if test="age > 0">
                and age &lt; #{age}
            </if>
            
        </select>
        
        <select id="selectStudentsByWhere" resultType="Student">
            select id,name,age,score from student
            <where>
                <if test="name != null and name != ''">
                    and name like '%' #{name} '%'
                </if>
                <if test="age > 0">
                    and age &lt; #{age}
                </if>
            </where>
        </select>
        
        <!-- 若查询条件中有name,无论有条件中有没有age,都只按照name查询;
        只有当查询条件中只包含age时,才按照age查询;若没有任何的查询条件,则
        不进行查询
         -->
        <select id="selectStudentsByChoose" resultType="Student">
            select id,name,age,score from student
            
            <where>
                <choose>
                    <when test="name != null and name != ''">
                        and name like '%' #{name} '%'
                    </when>
                    <when test="age > 0">
                        and age &lt; #{age}
                    </when>
                    <otherwise>
                        and 1!=1
                    </otherwise>
                </choose>
            </where>
            
        </select>
        
        <select id="selectStudentsByForeachArray" resultType="Student">
            <!-- select * from student where id in (1,5,7) -->
            select id,name,age,score from student
            <if test="array != null and array.length > 0">
                 where id in 
                <foreach collection="array" item="myid" open="(" close=")" separator=",">
                    #{myid}
                </foreach>
            </if>
        </select>
        
        <select id="selectStudentsByForeachList" resultType="Student">
            <!-- select * from student where id in (1,5,7) -->
            select id,name,age,score from student
            <if test="list != null and list.size > 0">
                 where id in 
                <foreach collection="list" item="myid" open="(" close=")" separator=",">
                    #{myid}
                </foreach>
            </if>
        </select>
        
        <select id="selectStudentsByForeachList2" resultType="Student">
            <!-- select * from student where id in (1,5,7) -->
            select id,name,age,score from student
            <if test="list != null and list.size > 0">
                 where id in 
                <foreach collection="list" item="mystu" open="(" close=")" separator=",">
                    #{mystu.id}
                </foreach>
            </if>
        </select>
        
        <select id="selectStudentsByFragment" resultType="Student">
            <!-- select * from student where id in (1,5,7) -->
            select <include refid="fieldNames"/> from student
            <if test="list != null and list.size > 0">
                 where id in 
                <foreach collection="list" item="mystu" open="(" close=")" separator=",">
                    #{mystu.id}
                </foreach>
            </if>
        </select>
        
        <!-- 定义SQL片断 -->
        <sql id="fieldNames">
            id,name,age,score
        </sql>
        
    </mapper>
  • 相关阅读:
    面试数据分析岗,怎么提升一倍成功率?让过来人给你支支招
    SQL执行效率提升几万倍的操作详解!
    记一次因Redis使用不当导致应用卡死过程
    运筹学那些事,专科学生学习运筹学之运输问题,No.5
    如何在C++中嵌入JAVA
    国际站中国区,孟买上Redis 4.0 集群版
    国际站中国区,孟买上Redis 4.0 集群版
    国际站中国区,孟买上Redis 4.0 集群版
    国际站中国区,孟买上Redis 4.0 集群版
    attachEvent与addEventlistener兼容性
  • 原文地址:https://www.cnblogs.com/csslcww/p/9912291.html
Copyright © 2011-2022 走看看