美文网首页
MyBatis(two)

MyBatis(two)

作者: javaMonkey | 来源:发表于2017-01-04 19:56 被阅读0次

    //////////2016-12-22 ~ 2016-12-23///////////

    int study_data(){

    MyBatis select语句扩展

    查询部分信息方法1:

        <sql id="selectFileds(customName)">column1,column2...</sql>

        <select id="userColumns(customName)" resultMap="BaseResultMap">

            select <include refid="sql-customName" /> from tableName

        </select>

     查询部分信息方法2:

        <sql id="customName">${alias}.propertyName,${alias}.propertyName...</sql>

        <select id="aliasGetUser(customName)" resultMap="BaseResultMap">

            select <include refid="userColumns(customName)"> 

            <property name="alias(上方${}中的字符串)" value="className(代替上方的${alias})"/>

            </include> from tableName

        </select>

    动态SQL

    if:

    <select id="customName" parameterType="inputType" resultMap="BaseResultMap">

        select * from tableName

        <if test="(condition)"> 

            where (condition)

        </if>

    </select>

    choose,when,otherwise:

    <select id="customName" parameterType="inputType" resultMap="BaseResultMap">

        select * from tableName

        <choose>

            <when test="(condition)">

            where (condition)

            </when>

            <when test="(condition)">

            where (condition)

            <otherwise>

            (condition)

            </otherwise>

        </choose>

    </select>

    trim(达到if else的效果):

    <select id="customName" parameterType="inputType" resultMap="BaseResultMap">

        select * from tableName

        <trim prefix="where" prefixOverrides="and|or">

            <if test="(condition)">

                (condition)

            </if>

            <if test="(condition)">

                (condition)

            </if>

        </trim>

    </select>

    在最后生成的sql语句中where后无条件符合,则不执行,若where紧跟着and|or则删除紧跟的那个and|or来保证sql语句的正确性

    foreach:

    <select id="customName" parameterType="inputType" resultMap="BaseResultMap">

        select * from tableName where column in

        <foreach collection="inputType" item="item" index="index" open="("

        separator="," close=")">

            #{item}

        </foreach>

    </select>

    可以利用标签实现sql条件的循环,可完成类似批量的sql

    相关文章

      网友评论

          本文标题:MyBatis(two)

          本文链接:https://www.haomeiwen.com/subject/sfswvttx.html