動態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);
}