07-Mybatis之动态SQL:根据不同的条件生成不同的不同的sql语句


动态SQL:根据不同的条件生成不同的不同的sql语句

1.if使用

<select id="selectByIdName" resultType="User">
    select * from user where 1=1
    <if test="id!=null and id!=''">
        and id=#{id}
    </if>
    <if test="username!=null and username!=''">
        and username=#{username}
    </if>
</select>
//接口:
public interface UserMapper {
    User selectByIdName(@Param("id")int id,@Param("username")String username);
}
//调用:
    UserMapper userMapper = session.getMapper(UserMapper.class);
    User user =userMapper.selectByIdName(3,"lisi");
    System.out.println(user);

2.where使用

简介:<where></where> 元素只会在至少有一个子元素的条件返回 SQL 子句的情况下才去插入“WHERE”子句。而且,若语句的开头为“AND”或“OR”,where 元素也会将它们去除。
例如:select * from user where and username=#{username} and password=#{password}将会变成
select * from user where username=#{username} and password=#{password}
<select id="selectByNamePwd1" resultType="User">
    select * from user
    <where>
        <if test="username!=null and username!=''">
            username=#{username}
        </if>
        <if test="password!=null and password!=''">
            and password=#{password}
        </if>
    </where>
</select>
//接口:
public interface UserMapper {
        User selectByNamePwd1(@Param("username") String username,@Param("password")String password);
}
//调用:
    UserMapper userMapper = session.getMapper(UserMapper.class);
    User user=userMapper.selectByNamePwd1("lisi","123");
    System.out.println(user);

3.choose, when, otherwise使用

简介:有时我们不想应用到所有的条件语句,而只想从中择其一项。针对这种情况,MyBatis 提供了 choose 元素,它像 Java 中的 if-else if - else 语句。
注意,如果<choose></choose>下的<when>都满足条件,将会只输出前者(即choose只会选择一个生成语句)
<select id="selectByNamePwd1" resultType="User">
    select * from user
    <where>
        <choose>
            <when test="username!=null and username!=''">
            	username=#{username}
            </when>
            <when test="password!=null and password!=''">
            	password=#{password}
            </when>
        </choose>
    </where>
</select>
//接口:
public interface UserMapper {
        User selectByNamePwd1(@Param("username") String username,@Param("password")String password);
}
//调用:
    UserMapper userMapper = session.getMapper(UserMapper.class);
    User user=userMapper.selectByNamePwd1("1","1");
    System.out.println(user);

4.set使用

简介:去掉最后一个","
<update id="upd" parameterType="User">
    update user
    <set>
        此处加id=#{id},是为了防止下面两个if判断都不成功导致语法错误(update user where id=#{id})
        id=#{id},
        <if test="username!=null">
            username=#{username},
        </if>
        <if test="password!=null">
            password=#{password},
        </if>
    </set>
    where id=#{id}
</update>
//接口:
    public interface UserMapper {
            int upd(User user);
    }
//调用:
    User user=new User(3,"updname","updpwd");
    int index = userMapper.upd(user);
    System.out.println(index);
    session.commit();

5.trim使用

简介:where、set原型
prefix:前缀增加
prefixOverrides:前缀删除
suffix:后缀增加
suffixOverrides:后缀删除
注意:执行顺序,先删除内容再增加内容
where功能:
<select id="selectByNamePwd2" resultType="User">
    select * from user
    <trim prefix="where" prefixOverrides="and">
        <if test="username!=null and username!=''">
            username=#{username}
        </if>
        <if test="password!=null and password!=''">
            and password=#{password}
        </if>
    </trim>
</select>
//接口:
    public interface UserMapper {
        User selectByNamePwd2(@Param("username") String username,@Param("password")String password);
    }
//调用:
    User user=userMapper.selectByNamePwd2("","111");
    System.out.println(user);

6.bind使用

简介:给参数重新赋值,用于模糊查询、在原内容前或者后添加内容
模糊查询
<select id="selectById1" resultType="User">
    <bind name="username" value="'%'+username+'%'"/>
    select * from user where username like #{username}
</select>
//接口:
    public interface UserMapper {
        User selectById1(@Param("id")int id,@Param("username") String username);
    }
//调用:
		User user=userMapper.selectById1(2,"li");
        System.out.println(user);

7.foreach使用

简介:循环遍历参数内容,还具备在内容的前后添加内容,还具备添加分隔符功能
适用于in查询中 select * from user where id in ( , , , )
批量新增(注意:mybatis中foreach效率比较低)      
insert int user values (id1,username1,password1),(id2,username2,password2),(id3,username3,password3)
collection:要遍历的集合
item:迭代变量
open:循环后左侧添加的内容
close:循环后右侧添加的内容
separator:每次循环时,元素之间的分隔符
<select id="selectByIdList" resultType="User">
    select * from user where id in
    <foreach collection="idList" item="idList" open="(" close=")" separator=",">
        #{idList}
    </foreach>
</select>
//接口:
    public interface UserMapper {
        List<User> selectByIdList(@Param("idList")List<Integer> idList);
    }
//调用:
    List<Integer> list=new ArrayList<>();
    list.add(2);
    list.add(3);
    List<User> userList=userMapper.selectByIdList(list);
    for(User user:userList){
        System.out.println(user);
    }

8.sql使用

简介:某些SQL片段如果希望复用,可以使用<sql>定义这个片段
之后在<select>等标签中使用<include>引用
<select id="selectAll2" resultType="User">
    select <include refid="mysql"></include>
    from user
</select>

<sql id="mysql">
    id,username
</sql>
//接口:
    public interface UserMapper {
         List<User> selectAll2();
    }
//调用:
    List<User> userList=userMapper.selectAll2();
    for(User user : userList){
        System.out.println(user);
    }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM