Mybatis- dynamic sql queries and fuzzy

 

 sql fragment, resolve duplicate field input sql

where: Adding where, the removal of the first and

set: add a set, remove the first one, No.

<? Xml Version = "1.0" encoding = "UTF-8" ?> 
<! DOCTYPE Mapper the PUBLIC "- // mybatis.org//DTD Mapper 3.0 // EN" 
"http://mybatis.org/dtd/mybatis mapper.dtd--3 " > 
<-! namespace: namespace representation. The aim now is to distinguish between the id. -> 
< Mapper namespace = "com.zhiyou100.xf.dao.UsersDao" > 
    <-! According to id to query the user. id: ID of the tag. parameterType: parameter type. Can write may be omitted resultType: return type of the result. 
        # {id}: similar to the EL expression. Analytical values id -> 
        < SQL id = "Content" > 
        id, name, Age, Sex, address 
        </ SQL > 
        <="com.zhiyou100.xf.bean.Users">
            select 
            <include refid="content"></include>
            from users
        </select>
    <select id="getUser" parameterType="com.zhiyou100.xf.bean.Users" resultType="com.zhiyou100.xf.bean.Users">
        select 
        <include refid="content"></include>
         from users where 
        <if test="id!=null">
            id=#{id}
        </if>
        <if test="age!=null">
            and age=#{age}
        </if>
    </select>
    <select id="getWhere" parameterType="com.zhiyou100.xf.bean.Users" resultType="com.zhiyou100.xf.bean.Users">
        select 
        <include refid="content"></include>
         from users
         <where>
             <if test="id!=null">
            id=#{id}
            </if>
            <if test="age!=null">
            and age=#{age}
            </if>
         </where>
    </select>
    <update id="update" parameterType="com.zhiyou100.xf.bean.Users">
        update Users
        <set>
            <if test="id!=null">
            name=#{name},
            </if>
            <if test="age!=null">
            age=#{age}
            </if>
        </set>
        where id=#{id}
    </update>
    <select id="sel1" parameterType="com.zhiyou100.xf.bean.Users" resultType="com.zhiyou100.xf.bean.Users">
        select
        <include refid="content"></include>
        from users
        <where>
            <choose>
            <when test="id!=null">
                id=#{id}
            </when>
            <when test="age!=null">
                age=#{age}
            </when>
        </choose>
        </where>
        
    </select>
    <select id="sel2" parameterType="com.zhiyou100.xf.bean.Users" resultType="com.zhiyou100.xf.bean.Users">
        select
        <include refid="content"></include>
        from users
        <trim prefix="where" prefixOverrides="and">
            <if test="id!=null">
                id=#{id}
            </if>
            <if test="age!=null">
                and age=#{age}
            </if>
        </trim>
        
    </select>
    <update id="update2" parameterType="com.zhiyou100.xf.bean.Users">
        update Users
        <trim prefix="set" suffixOverrides=",">
            <if test="age!=null">
                age=#{age},
            </if>
            <if test="name!=null">
                name=#{name},
            </if>
        </trim>
        where id=#{id}
    </update>
    <select id="sellByIds" resultType="com.zhiyou100.xf.bean.Users">
        select * from Users
        <where>
            <foreach collection="list" item="id" separator="or">
                id=#{id}
            </foreach>
        </where>
    </select>
    <select id="sellIn" resultType="com.zhiyou100.xf.bean.Users">
        select * from Users
        <where>
            <foreach collection="list" open="id in(" close=")" item="id" separator=",">
                #{id}
            </foreach>
        </where>
    </select>
</mapper>

 

Guess you like

Origin www.cnblogs.com/accc111/p/11449119.html