mybatis.5.動態SQL

1.動態SQL,解決關聯sql字符串的問題,mybatis的動態sql基於OGNL表達式

   if語句,在DeptMapper.xml增加如下語句;

 

 

<select id="selectByLikeName" resultType="org.mybatis.example.dao.Dept"		parameterType="org.mybatis.example.dao.Dept">		select d.deptno,d.dname,d.loc from dept d where 1=1		<if test="dname!=null and dname!=''">			AND	dname like #{dname}		</if>			</select>

DeptMapper.java接口中增加如下代碼

 

 

public Dept selectByLikeName(Dept dept);


測試類如下:

 

 

public class Test23 {	public static void main(String[] args) {		SqlSession session=SqlSessionFactoryUtil.getSqlSession();		DeptMapper mapper=session.getMapper(DeptMapper.class);		Dept d=new Dept();		d.setDname("開發部");		Dept dept=mapper.selectByLikeName(d);		System.out.println(dept.getDname());	}}

 

如果查詢僱員的姓名,查詢出僱員並且找到一些部門呢?

 

 

EmpMapper.xml增加如下代碼

 

<select id="selectByLike" resultMap="getEmpresultMap"		parameterType="org.mybatis.example.dao.Emp">		select d.deptno,d.dname,d.loc,e.empno,e.ename 		from Dept d  join emp e  on d.deptno=e.deptno where 1=1		<if test="ename!=null and ename!=''">			AND ename like #{ename}		</if>		<if test="dept!=null and dept.dname!=null">			AND dname like #{dept.dname}		</if>		</select>


EmpMapper.java增加如下代碼:

 

public List<Emp> selectByLike(Emp e);

測試類:

 

public class Test24 {	public static void main(String[] args) {		SqlSession session=SqlSessionFactoryUtil.getSqlSession();		EmpMapper mapper=session.getMapper(EmpMapper.class);		Emp e=new Emp();		e.setEname("張%");		List<Emp>empList=mapper.selectByLike(e);		for(Emp emp:empList){			System.out.println("所有名字首字母爲張的員工是:"+emp.getEname());		}	}}

 

3 choose when  otherwise語句

 

有時候針對,有多重選擇的情況,可以使用choose語句

 

<select id="selectByLikeTwo" parameterType="org.mybatis.example.dao.Emp"		resultMap="getEmpresultMap">		select d.deptno,d.dname,d.loc,e.empno,e.ename,e.sal 		from Dept d  join emp e  on d.deptno=e.deptno where 1=1 		<choose> 			<when test="ename!=null">AND ename like #{ename}</when> 			<when test="dept!=null and dept.dname!=null">AND dname=#{dept.dname}</when> 			<otherwise>AND sal >5000</otherwise> 		</choose>	</select>

EmpMapper.java增加接口

 

public List<Emp> selectByLikeTwo(Emp e);

public class Test25 {	public static void main(String[] args) {		SqlSession session=SqlSessionFactoryUtil.getSqlSession();		EmpMapper mapper=session.getMapper(EmpMapper.class);		Emp e=new Emp();		Dept d=new Dept();		d.setDname("開發部");		e.setDept(d);		List<Emp>empList=mapper.selectByLikeTwo(e);		for(Emp emp:empList){			System.out.println("所有開發部的員工:"+emp.getEname());		}	}}

 

 

 

 

 

4.

 

<select id="selectByLikeName" resultType="org.mybatis.example.dao.Dept"		parameterType="org.mybatis.example.dao.Dept">		select d.deptno,d.dname,d.loc from dept d where		<if test="dname!=null">			1=1		</if>		<if test="dname!=null and dname!=''">			AND	dname like #{dname}		</if>			</select>


測試類

 

 

public static void main(String[] args) {		SqlSession session=SqlSessionFactoryUtil.getSqlSession();		DeptMapper mapper=session.getMapper(DeptMapper.class);		Dept d=new Dept(); //或者設置爲null		d.setDname("開發部");//設置爲null的時候註釋		List<Dept>deptList=mapper.selectByLikeName(d);		System.out.println(deptList.get(0).getDname());	}

5. foreach

    動態SQL迭代一個集合,通常放在In條件語句中,foreach允許指定一個集合,聲明集合項和索引變量,他們可以用在元素體內,也允許指定開放和關閉的字符串,在迭代之間放置分隔符。這個元素是智能的,不會偶然地附加多餘的分隔符。

   在EmpMapper.xml中增加代碼段

 

<select id="selectDeptIn" resultType="org.mybatis.example.dao.Dept">		select * from dept d where deptno in		<foreach item="item" index="index" collection="list"			open="(" separator="," close=")">			#{item}		</foreach>	</select>


在接口EmpMapper.java中增加代碼

 

public List<Dept> selectDeptIn(List<Integer>list);

測試類代碼段

 

public class Test26 {	public static void main(String[] args) {		SqlSession session=SqlSessionFactoryUtil.getSqlSession();		DeptMapper mapper=session.getMapper(DeptMapper.class);		List<Integer>idList=new ArrayList<Integer>();		idList.add(5);		idList.add(6);		List<Dept>deptList=mapper.selectDeptIn(idList);		System.out.println(deptList.get(0).getDname());		System.out.println(deptList.get(1).getDname());	}}


對於在Mybatis中出現的TooManyResultsException異常,需要將接口的方法返回類型修改爲List<T>泛型類型即可。

 

 

 

foreach元素的屬性主要有 item,index,collection,open,separator,close。

item表示集合中每一個元素進行迭代時的別名.

index指 定一個名字,用於表示在迭代過程中,每次迭代到的位置.

open表示該語句以什麼開始,separator表示在每次進行迭代之間以什麼符號作爲分隔 符.

close表示以什麼結束.

 

<select id="selectTestForEach" parameterType="News" resultMap="NewsResultMapper">
  select * from t_news n where 
  <foreach collection="listTag" index="index" item="tag" open=""
    separator="or" close="">
   #{tag} in n.tags
  </foreach>
 </select>
所以 去除左右括號 和 把,改成 or 進行 。 就可以轉化爲這種形式。
select * from t_news n where ? in n.tags or ? in n.tags   這樣可以用List<String> 來查。







 

但是查不到數據
需要使用如下方式:
<select id="selectTestForEach" parameterType="News" resultMap="NewsResultMapper">
  select * from t_news n where 
  <foreach collection="listTag" index="index" item="tag" open=""
    separator="or" close="">
    n.tags like  '%'||#{tag}||'%'
  </foreach>
 <lect>







生成的SQL爲

select * from t_news n where n.tags like ? or n.tags like ?    

 

foreach : 用的更多的地方爲: 根據Id批量刪除    /    判斷什麼 in 集合中(此時需要生成(**,***,)的形式)

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章