Mybatis-动态sql和模糊查询

 sql片段,解决重复sql字段输入

where:添加where,去除第一个and

set:添加set,去除第一个,号

<?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">
<!-- namespace:表示名称空间。现在的目的是区分id的. -->
<mapper namespace="com.zhiyou100.xf.dao.UsersDao">
    <!-- 根据id查询用户。id:标识该标签。 parameterType:参数类型。可以写 也可以省略 resultType:返回结果的类型。 
        #{id}:类似于EL表达式。 解析id的值 -->
        <sql id="content">
        id,name,age,sex,address
        </sql>
        <select id="selAll" resultType="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>

猜你喜欢

转载自www.cnblogs.com/accc111/p/11449119.html