一.使用choose標簽進行判斷
1.定義映射資源文件.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 此文件主要用於編寫sql語句 namespace是為了解決相同名字的sql操作問題 --> <mapper namespace="cn.et.mybatis.lesson02.dyncsql.DyMapper"> <!-- 問題的引入,假如一條查詢語句,我們不進行傳參,那么執行結果將會拋出異常, 那么理想的結果應該是假如我們不進行傳參,就應該默認查詢出所有的結果才對, 此處我們便引入了一個新的條件判斷標簽<if標簽>,合理的解決上述所描述的異常 --> <select id="selectEmp" resultType="map"> select * from emp where 1=1 <!--<if test="ename!=null"> and ename=#{ename} </if>--> <if test="ename!=null"> and ename like #{ename} </if> </select> <!--此處使用choose條件判斷標簽
<when test="sal==null">標簽表示如果條件為什么,將執行
<>
--> <select id="selectEmp1" resultType="map" parameterType="map"> select * from emp where 1=1 <choose> <when test="sal==null"> and sal <=0 </when> <otherwise> and sal=#{sal} </otherwise> </choose> </select> </mapper>
2.定義接口查詢方法
package cn.et.mybatis.lesson02.dyncsql; import java.util.List; import java.util.Map; public interface DyMapper { // public void queryEmp(Map map); //定義查詢的方法 public List<Map> selectEmp(Map map); public List<Map> selectEmp1(Map map); }
3.定義mybatis中的主xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!-- 說明mybatis的根節點 --> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <!--mybatis的根標簽configuration --> <configuration> <!-- 資源文件的路徑配置 注:cn前面不能用/否則會拋出異常--> <properties resource="cn/et/mybatis/lesson02/jdbc.properties"> </properties> <!-- 配置連接數據庫的環境,(開發環境) --> <environments default="development"> <environment id="development"> <!-- 事務交給jdbc管理,Conection(事務) commit(提交) rollback(回滾) --> <transactionManager type="JDBC"/> <!-- 數據源 用來連接數據庫(包含四要素,driver,url,username,password) --> <dataSource type="POOLED"> <property name="driver" value="${driverClass}"/> <property name="url" value="${url}"/> <property name="username" value="${userAccount}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <!-- 包含隱射文件主要用於編寫sql語句 --> <mappers> <!-- 隱射的資源文件 隱射文件主要編寫sql語句 --> <mapper resource="cn/et/mybatis/lesson02/dyncsql/dy_mapper.xml"/> </mappers> </configuration>
4.定義測試類
package cn.et.mybatis.lesson02.hw; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; public class TestMyBatis { //封裝session的返回 public static SqlSession getSession(){ String resource = "/cn/et/mybatis/lesson02/mybatis.xml"; InputStream inputStream =TestMyBatis.class.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //操作數據庫的類 SqlSession SqlSession session=sqlSessionFactory.openSession(); return session; } @Test //if判斷的測試 public void selectTest(){ SqlSession session=getSession(); HwMapper cm=session.getMapper(HwMapper.class); Map map=new HashMap(); map.put("sal", 3000); List list = cm.selectEmp(map); System.out.println(list.size()); } // @Test
// choost的判斷測試
public void selectEmp1(){ SqlSession session=getSession(); HwMapper cm=session.getMapper(HwMapper.class); Map map=new HashMap(); map.put("ename", "%s%"); List list = cm.selectEmp1(map); System.out.println(list.size()); } }
二.使用 Trim ,where ,set 進行判斷查詢(此處使用注解進行演示)
1.問題的引出:如果以下代碼的if條件都不滿足時,那么實際的數據庫查詢語句將會是:select * from emp where ,在數據庫查詢中,使用該查詢是無法查詢結果的,所以我們理想的方案是當所有的if判斷都不滿足時,我們的查詢語句應該是這樣的
select * from emp 才是合理的方案,那么此時我們就引出了 where標簽來解決上述問題,
問題的引出代碼