Mybatis多條件查詢

題目
在EmpMapper接口中增加如下方法:
//根據(jù)動(dòng)態(tài)條件查詢符合條件的員工
List<Emp> getEmpByCondition(條件類);
 
測試如下功能:
1)請查詢 員工所在部門為2,工資在3000-5000的員工。
2)請查詢 員工入職時(shí)間超過5年以上,工資在3000-5000的員工。

代碼示例
EmpMapper.xml:
這里僅列出主要方法代碼。

<!--這里用了兩種大于、小于、大于等于、小于等于的寫法,僅為練習(xí)-->
<select id="getEmpByCondition" parameterType="Map" resultMap="EmpMap">
   select * from emp
   <trim prefix="where" suffixOverrides="and">

   <!--入職時(shí)間區(qū)間-->
   <if test="beginHireDate!=null or endHireDate!=null">
      <choose>
         <!--開始入職時(shí)間和結(jié)束入職時(shí)間相同-->
         <when test="beginHireDate==endHireDate">
             hireDate=#{beginHireDate} and
         </when>
         <when test="beginHireDate!=endHireDate">
            <![CDATA[ hireDate>=#{beginHireDate} and hireDate<=#{endHireDate} and]]>
         </when>
         <when test="beginHireDate!=null and endHireDate==null">
             <![CDATA[ hireDate>=#{beginHireDate} and]]>
         </when>
         <when test="beginHireDate==null and endHireDate!=null">
             <![CDATA[hireDate<=#{endHireDate} and]]>
         </when>
       </choose>
    </if>

    <!--工資區(qū)間-->
    <if test="beginSalary!=null or endSalary!=null">
        <!--開始工資和初始工資相同-->
       <choose>
          <when test="beginSalary==endSalary">
              salary =#{beginSalary} and
          </when>
          <when test="beginSalary!=endSalary">
              salary >=#{beginSalary} and salary <=#{endSalary} and
          </when>
          <when test="beginSalary!=null and endSalary==null">
              salary >=#{beginSalary} and
          </when>
          <when test="beginSalary==null and endSalary!=null">
              salary <=#{endSalary} and
          </when>
        </choose>
      </if>

      <!--入職時(shí)間年長-->
      <if test="hirePerTime!=null and hirePerTimeCon!=null">
         <choose>
            <when test="hirePerTimeCon=='gt'">
                TIMESTAMPDIFF(YEAR,hireDate,curdate())>#{hirePerTime} and
            </when>
            <when test="hirePerTimeCon=='ge'">
                TIMESTAMPDIFF(YEAR,hireDate,curdate())>=#{hirePerTime} and
            </when>
            <when test="hirePerTimeCon=='lt'">
                TIMESTAMPDIFF(YEAR,hireDate,curdate())<#{hirePerTime} and
            </when>
            <when test="hirePerTimeCon=='le'">
                TIMESTAMPDIFF(YEAR,hireDate,curdate())<=#{hirePerTime} and
            </when>
         </choose>
      </if>

      <!--根據(jù)部門編號查詢員工-->
      <if test="deptnoList!=null">
         deptno in
         <foreach collection="list" item="deptno" open="(" separator="," close=")">
                    #{deptno}
         </foreach>
       </if>
  </trim>
</select>

EmpController.java:

    @RequestMapping(value="/getEmpByCondition", method= RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> getEmpByCondition(@RequestParam(value="deptno",required = false) List deptno,
                                                @RequestParam(value="beginHireDate",required = false) String beginHireDate,
                                                @RequestParam(value="endHireDate",required = false) String endHireDate,
                                                @RequestParam(value="hirePerTime",required = false) Integer hirePerTime,
                                                @RequestParam(value="hirePerTimeCon",required = false) String hirePerTimeCon,
                                                @RequestParam(value="beginSalary",required = false) Double beginSalary,
                                                @RequestParam(value="endSalary",required = false) Double endSalary){
        Map<String,Object> conditions = new HashMap<>();
        Map<String,Object> returnMap = new HashMap<>();

        try {
            if(deptno!=null)
                conditions.put("deptno",deptno);
            if(beginHireDate!=null){
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                conditions.put("beginHireDate",format.parse(beginHireDate));
            }
            if(endHireDate!=null){
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                conditions.put("endHireDate",format.parse(endHireDate));
            }
            if(beginSalary!=null)
                conditions.put("beginSalary",beginSalary);
            if(endSalary!=null)
                conditions.put("endSalary",endSalary);
            //入職時(shí)間年長
            if(hirePerTime!=null&&hirePerTimeCon!=null){
                conditions.put("hirePerTime",hirePerTime);
                conditions.put("hirePerTimeCon",hirePerTimeCon);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        List<Employee> elist = employeeService.getEmpByCondition(conditions);
        if(elist.size()!=0){
            returnMap.put("message","findUserByDeptno successfully!");
            returnMap.put("employeeList",elist);
        }else{
            returnMap.put("message","can not find user");
        }
        return returnMap;
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,767評論 18 399
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,969評論 19 139
  • 一. Java基礎(chǔ)部分.................................................
    wy_sure閱讀 3,837評論 0 11
  • 親愛的九(4)班的同學(xué)們: 你們已經(jīng)進(jìn)入初三畢業(yè)沖刺,但是,我從絕大部分同學(xué)身上真的沒有看到畢業(yè)班的影子,我真...
    默默ks閱讀 228評論 3 3
  • 寫下來講出去
    010ed0d5a362閱讀 198評論 0 0