6.1動(dòng)態(tài)sql
sql的內(nèi)容是變化的,可以根據(jù)條件獲取到不同的sql語(yǔ)句(主要是where部分發(fā)生變化)
動(dòng)態(tài)sql的實(shí)現(xiàn),使用的是mybatis提供的標(biāo)簽,<if>,where>,<foreach>
-
<if>判斷條件
語(yǔ)法:<if test="判斷java對(duì)象的屬性值"> 部分sql語(yǔ)句 </if>mapper文件
test中的name等代表對(duì)象的屬性名,要注意語(yǔ)句中的or,and等關(guān)鍵詞不要少了,缺了就不是一個(gè)完整的sql語(yǔ)句<select id="selectSqlIf" resultType="com.mybatis.domain.Student"> select id,name,email,age from student where <if test="name !=null and name!='' "> name=#{name} </if> <if test="age > 0"> or age > #{age} </if> </select>結(jié)果圖
如果我們沒(méi)有設(shè)置name值,執(zhí)行程序則會(huì)報(bào)錯(cuò)(因?yàn)榈诙€(gè)判斷語(yǔ)句最前面有個(gè)or),但是我們可以在where后面加一個(gè) 1=1(或者其他恒等形式但不對(duì)后面的語(yǔ)句造成影響),就可以解決這個(gè)問(wèn)題;此外還有解決以上問(wèn)題的方法---`<where>`
-
<where>用來(lái)包含 多個(gè)<if>的,當(dāng)多個(gè)if有一個(gè)成立的,<where>會(huì)自動(dòng)增加一個(gè)where關(guān)鍵字,并去掉 if中多余的 and ,or等mapper文件
<select id="selectSqlWhere" resultType="com.mybatis.domain.Student"> select id,name,email,age from student <where> <if test="name !=null and name!='' "> name=#{name} </if> <if test="age > 0"> or age > #{age} </if> </where> </select>實(shí)現(xiàn)類
@Test public void test01ByWhere(){ SqlSession sqlSession= MyBatisUtils.getSqlSession(); StudentDao dao=sqlSession.getMapper(StudentDao.class); Student student=new Student(); student.setName("中文"); student.setAge(4); List<Student> l=dao.selectSqlWhere(student); for (Student s : l) { System.out.println(s); } }接下來(lái)我們分別注釋掉
student.setAge(4)和student.setName("中文"),最后把這兩個(gè)同時(shí)注釋掉結(jié)果圖
注釋掉`student.setAge(4)`
注釋掉`student.setName("中文")`注釋掉兩個(gè)可以看到,
<where>已經(jīng)幫我加上了where關(guān)鍵詞和會(huì)把多余的and,or之類的關(guān)鍵字除去,當(dāng)判斷條件都沒(méi)有時(shí),where也一并消失了 -
<foreach>循環(huán)java中的數(shù)組,list集合的。 主要用在sql的in語(yǔ)句中;如學(xué)生id是 1001,1002,1003的三個(gè)學(xué)生==>select * from student where id in (1001,1002,1003)方法一
接口
public interface StudentDao { List<Student> selectForEach(List<Integer> myid); }mapper文件
<select id="selectForEach" resultType="com.mybatis.domain.Student"> select * from student where id in <foreach collection="list" item="myid" open="(" close=")" separator=","> #{myid} </foreach> </select>- collection:表示接口中的方法參數(shù)的類型, 如果是數(shù)組使用array , 如果是list集合使用list
- item:自定義的,表示數(shù)組和集合成員的變量
- open:循環(huán)開始是的字符
- close:循環(huán)結(jié)束時(shí)的字符
- separator:集合成員之間的分隔符
調(diào)用類
@Test public void test01ByForEach(){ SqlSession sqlSession= MyBatisUtils.getSqlSession(); StudentDao dao=sqlSession.getMapper(StudentDao.class); List<Integer> list=new ArrayList<>(); list.add(1001); list.add(1002); list.add(1003); List<Student> student=dao.selectForEach(list); for (Student s: student ) { System.out.println(s); } }[圖片上傳失敗...(image-d08124-1595058507674)]
方法二(當(dāng)循環(huán)目標(biāo)是對(duì)象時(shí))
接口
public interface StudentDao { List<Student> selectForEachStu(List<Student> mystu); }mapper文件
<select id="selectForEachStu" resultType="com.mybatis.domain.Student"> select * from student where id in <foreach collection="list" item="stu" open="(" close=")" separator=","> #{stu.id} </foreach> </select>@Test public void test01ByForEachStu(){ SqlSession sqlSession= MyBatisUtils.getSqlSession(); StudentDao dao=sqlSession.getMapper(StudentDao.class); List<Student> stulist=new ArrayList<>(); Student s1=new Student(); s1.setId(1003); stulist.add(s1); s1=new Student(); s1.setId(1005); stulist.add(s1); List<Student> student=dao.selectForEachStu(stulist); for (Student s: student ) { System.out.println(s); } }執(zhí)行結(jié)果
對(duì)于一些經(jīng)常用且可以進(jìn)行服用的語(yǔ)句,我們可以定義起來(lái),需要時(shí)直接調(diào)用
步驟
1. 先定義 <sql id="自定義名稱唯一"> sql語(yǔ)句, 表名,字段等 </sql>
2. 再使用, <include refid="id的值" />
例子(mapper文件)
<sql id="sqlone">select * from student</sql>
<select id="selectSqlIf" resultType="com.mybatis.domain.Student">
<include refid="sqlone"></include>
where
<if test="name !=null and name!='' ">
name=#{name}
</if>
<if test="age > 0">
or age > #{age}
</if>
</select>
條條:該學(xué)習(xí)筆記是記錄了我的學(xué)習(xí)過(guò)程,學(xué)習(xí)自動(dòng)力節(jié)點(diǎn)和c語(yǔ)言中文網(wǎng),有不對(duì)的地方歡迎指出




