首頁 >Java >java教程 >MyBatis中的多條件查詢解說

MyBatis中的多條件查詢解說

巴扎黑
巴扎黑原創
2017-07-23 13:45:482526瀏覽

一:使用動態SQL完成多條件查詢

     a:使用if+where實現多條件查詢

       首先場景需求,有年級和班級表,第一個要求是根據模糊查詢姓名,和年齡大小進行條件查詢,介面層方法

       

 public  List<student>  getStudentByIf(student stu);

  其次是對應檔案的設定

 

  其次是對應檔案的設定

 

 205a3d64de2de7feacbf63eab3b424e3select * from student       196185dae55b7edbe154a5051db664a7   e5ca04733cd4c163365b5b71c59d203b   and stuAge>#{stuAge}       0e19ae19f9bb3871b7693b46538542ae a1372c9923d43a5e03b73f8c613dcb74 and stuName LIKE '%' #{stuName} '%'             0e19ae19f9bb3871b7693b46538542ae   bdcefec14b04ffcf937d99934241e90418bb6ffaf0152bbe49cd8a3620346341

 

        測驗

<br>   <br><br>

 studentDao dao = MyBatis.getSessionTwo().getMapper(studentDao.= "z"List52fc45c754660ae0425ccd0c640c83e6 list="----------"+
----------zhangyu
---- ------zy

----------zy

----------zhang

 <br>

 

    b:choose  when 分類

     這種方式和java中choose循環結構原理是一樣的,判斷多種情況,只要修改一下映射檔即可

     介面類別

#    

  public List52fc45c754660ae0425ccd0c640c83e6 getAllStudentByLike(Map14bd1badcdee783757181db757c9943f userMap);  //使用map作为参数
 

#     映射檔案

 <span style="color: #0000ff">0e63805a2de8455a048b4668ad10d675</span><span style="color: #000000">select * from student</span><span style="color: #0000ff">a8b5ac47a686a99ccb908da547004cd5</span><span style="color: #0000ff">0f9385b590763a82b374dc1243e0395e</span><span style="color: #0000ff">b4c1920efd4ca16791ac67f56ee364a7</span><span style="color: #000000"> stuName like CONCAT('%',#{stuName},'%')</span><span style="color: #0000ff">8e451dd14ca9d75461e627358c8c51fc</span><span style="color: #0000ff">fec7d1461bf1f38a4138b3abbc4979f1</span><span style="color: #000000"> stuAge> #{stuAge}</span><span style="color: #0000ff">8e451dd14ca9d75461e627358c8c51fc<br></span>
08840e2c213e7be6cb36177580c1b0f8
    1=1
96a92f425558413b8076d61f18cda98a
<span style="color: #0000ff"><br></span><span style="color: #0000ff">2d54759db178a877bef5e9a218bb4371</span><span style="color: #0000ff">977d983c9eb9b00f3f009121e5682940</span><span style="color: #0000ff">6072f1bfa61ceab3da8ab53a4cd73651</span>
 

    結果

#
zhangyu
zy
zy
zhang

 

 

  c:使用foreach完成複雜查詢,有三種方式,

     第一種:傳入的參數為陣列型別
        

//传一组 xueshengID public List52fc45c754660ae0425ccd0c640c83e6 getStudentBystuId_foreach_array(Integer[] ints);




映射文件配置 fce7260c3087690639459e0047a0a932
    e9f3e24cc593136c4a1cc5669e7135feselect * from studenta8fe01ee7c2d53fc9df09183bae757e40">where stuId IN/*数组形式传入学生Id*/f84a91398f1d55c211691993d7ee7b1d  #{stu}70b0f4e7c41f2707b12056d6eb778fbc
        0e19ae19f9bb3871b7693b46538542ae
    18bb6ffaf0152bbe49cd8a3620346341

 

測試類別
 

  Integer[] ints = {2,3,4};
        List52fc45c754660ae0425ccd0c640c83e6 list = dao.getStudentBystuId_foreach_array(ints);for (student item:list) {
            System.out.println(item.getStuName());
        }

 

##  

#
   public List52fc45c754660ae0425ccd0c640c83e6 getStudentBystuId_foreach_list(Listc0f559cc8d56b43654fcbe4aa9df7b4a list);
 

##  二種:傳入list集合

     

  b1759409fbe6e773cf15efd292acda5f6ca04fcf8695599a6433593917d5eaa8select * from student294570585459929d13002b82f6df56280">where stuId IN
        /*集合形式传入学生Id*/1b0dccf704dbe04bd327bc29ff46c0bf#{stu}70b0f4e7c41f2707b12056d6eb778fbc0e19ae19f9bb3871b7693b46538542ae18bb6ffaf0152bbe49cd8a3620346341

 

  

 studentDao dao = MyBatis.getSessionTwo().getMapper(studentDao.class);
        Integer ints = 2;
        Listc0f559cc8d56b43654fcbe4aa9df7b4a list = new ArrayListc0f559cc8d56b43654fcbe4aa9df7b4a();
        list.add(ints);
        List52fc45c754660ae0425ccd0c640c83e6 stulist = dao.getStudentBystuId_foreach_list(list);
        for (student item:stulist) {
            System.out.println(item.getStuName());
        }
 

     測驗:

   

 public List52fc45c754660ae0425ccd0c640c83e6 getStudentBystuId_foreach_map(Map14bd1badcdee783757181db757c9943f stuMap);
 

    三種:依據Map集合

     

  

 500814846e93bf7563684fd406722e061575b890bc44889798d9b61bad170577select * from student where stuId IN
        /*集合形式传入学生Id*/865a90ad19b10e4ed70f9c32b18acdc4    9f236976ece23b5ebc842d21e9c9ec5e#{stu}70b0f4e7c41f2707b12056d6eb778fbc18bb6ffaf0152bbe49cd8a3620346341

  

<span style="color: #008000">  Map8164ffbb17e475e5db8fce2014315fbf stumap = new HashMap994a833a6ffa28d85b72cb15422c29d6();
        Listc0f559cc8d56b43654fcbe4aa9df7b4a listStuId = new ArrayListc0f559cc8d56b43654fcbe4aa9df7b4a();
        listStuId.add(2);
        listStuId.add(3);
        listStuId.add(4);
        stumap.put("stuId",listStuId);
         List52fc45c754660ae0425ccd0c640c83e6 list = dao.getStudentBystuId_foreach_map(stumap);
        for (student item:list
             ) {
            System.out.println(item.getStuName());
        }</span><span style="color: #008000"><br></span>

# 列印結果可以執行以下。

  d;一對多的兩種實作方式

    主要是resultMapper裡的設定不同
   介面方法 

#   

 public grade getGradeById(int gradeId);

 
   對映檔案設定

   

 <span style="color: #008000">3e24fdd8bdee9bcc7f76ddfc64b5060e</span><span style="color: #0000ff">07f76d11ea805dcf0f1e5be18a55a2ff</span><span style="color: #0000ff">89eb85887fd78fe40232e4c60d6588c07ade8bcab33b053a395251de7c37598b</span><span style="color: #0000ff">06b89fa75138dc59b789389e1761357072907de4312d36ee29181cebb340226f</span><span style="color: #0000ff">79d489e1c8bd166d4058a36b00bbdaa3</span><span style="color: #0000ff">3f4933324310633519160ed6e5b624357ade8bcab33b053a395251de7c37598b</span><span style="color: #0000ff">ecadb8ef0d10d23a98f349f35549d4dd72907de4312d36ee29181cebb340226f</span><span style="color: #0000ff">356e3e4d5ffa8c3409e69a4ff0cde16272907de4312d36ee29181cebb340226f</span><span style="color: #0000ff">5121d7f61d2c80c282dc21d6efdab83b</span><span style="color: #0000ff">68a1eb647461cc0f565daafa15ff5b88</span><span style="color: #008000">1dc14dbc4242b6eac33046d95285ac0b</span><span style="color: #0000ff">cddb0d9f82afd6be509fdb66f8b4580b</span><span style="color: #0000ff">89eb85887fd78fe40232e4c60d6588c07ade8bcab33b053a395251de7c37598b</span><span style="color: #0000ff">06b89fa75138dc59b789389e1761357072907de4312d36ee29181cebb340226f</span><span style="color: #0000ff">d5d47b954b5dbee8ca1449ddadd851425121d7f61d2c80c282dc21d6efdab83b    4b82ad52836e169d546e763e6dadff6c</span><span style="color: #0000ff">68a1eb647461cc0f565daafa15ff5b88<br></span>

    e810673d89ab4acfa23f154308ea6405select * from grade,student where grade.gradeId = student.stuGrade and gradeId = #{gradeId}18bb6ffaf0152bbe49cd8a3620346341148491c2b3b1e59077655ed8efdd1a62e3bbf5b6c6ca0311058d13239f23a427select * from grade where gradeId=#{gradeId}18bb6ffaf0152bbe49cd8a36203463417690f6b97a724db97458a5b8ecaffabbselect * from student where stuGrade = #{stuGrade}18bb6ffaf0152bbe49cd8a3620346341
<br>

 

 <br>

 
       

 

  @Testpublic void  TestConn(){
       gradeDao dao = MyBatis.getSessionTwo().getMapper(gradeDao.class);

       grade grade = dao.getGradeById(1);       for (student item:grade.getGatStudent()            ) {
           System.out.println(item.getStuName());
       }

    }

 

<br>兩種方式都能實現,列印效果<br><br>#方案一列印效果<br><br>==> Preparing: select * from grade,student where grade.gradeId = student.stuGrade and gradeId = ?   ==============一條sql<br>==> Parameters: 1(Integer)<br>817fd801618715f0db2923f2fadf6790 Preparing: select * from grade where gradeId=? ==========第一sql<br>==> Parameters: 1(Integer)<br>65c7272fee761790cc2715040765b48e  Preparing: select * from student where stuGrade = ?       ==========第二條sql<br>====> Parameters: 1(Long)<br><====    Columns: stuId, stuName, stuAge, stuGrade<br><====        Row: 2, zhangyu, 19, 1<br><====  Row  20, 1<br><====        Row: 4, zy, 21, 1<br><====      Total: 3<br><==      Total: 1. zy

zy

Process finished with exit code 0

 ###

以上是MyBatis中的多條件查詢解說的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn