全部課程
發(fā)布時(shí)間: 2018-09-19 23:12:38
if:
就是簡單的條件判斷,利用if語句我們可以實(shí)現(xiàn)某些簡單的條件選擇。
?List<Stu> getAll(Stu stu);
<select id="getAll" resultType="net.togogo.bean.Stu">
select * from t_stu where 1=1
<if test="name!=null">
and name = #{name}
</if>
</select>
@Test
public void selectAll(){
SqlSession session = sessionFactory.openSession();
Stu stu = new Stu();
stu.setName("劉備");
List<Stu> stus =
session.selectList("net.togogo.mapper.StuMapper.getAll",stu);
stus.stream().forEach(System.out::println);
session.close();
}
如果你提供了title參數(shù),那么就要滿足title=#{title},同樣如果你提供了Content和Owner的時(shí)候,它們也需要滿足相應(yīng)的條件,之后就是返回滿足這些條件的所有Blog,這是非常有用的一個(gè)功能,以往我們使用其他類型框架或者直接使用JDBC的時(shí)候, 如果我們要達(dá)到同樣的選擇效果的時(shí)候,我們就需要拼SQL語句,這是極其麻煩的,比起來,上述的動態(tài)SQL就要簡單多了。
choose:
?元素的作用就相當(dāng)于JAVA中的switch語句,基本上跟JSTL中的choose的作用和用法是一樣的,通常都是與when和otherwise搭配的。
?List<Stu> getChoose(Stu stu);
<select id="getChoose" resultType="net.togogo.bean.Stu">
select * from t_stu where 1=1
<choose>
<when test="name!=null">
and name= #{name}
</when>
<when test="id!=0">
and id = #{id}
</when>
<otherwise>
and name = "程咬金"
</otherwise>
</choose>
</select>
@Test
public void getChoose(){
SqlSession session = sessionFactory.openSession();
Stu stu = new Stu();
stu.setName("劉備");
List<Stu> stus =
session.selectList("net.togogo.mapper.StuMapper.getChoose",stu);
stus.stream().forEach(System.out::println);
session.close();
}
content = #{content},當(dāng)所有條件都不滿足的時(shí)候就輸出otherwise中的內(nèi)容。
上一篇: {HCNA-AI 數(shù)學(xué)知識}之線性代數(shù)實(shí)驗(yàn)
下一篇: {HCNA-AI 數(shù)學(xué)知識}之基礎(chǔ)數(shù)學(xué)實(shí)驗(yàn)