Rumah >Java >javaTutorial >MyBatis中的多条件查询讲解

MyBatis中的多条件查询讲解

巴扎黑
巴扎黑asal
2017-07-23 13:45:482531semak imbas

一:使用动态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

 

        测试

   

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

----------zhangyu<br>----------zy<br>----------zy<br>----------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());
        }

 

     第二种:传入list集合

     

   public List52fc45c754660ae0425ccd0c640c83e6 getStudentBystuId_foreach_list(Listc0f559cc8d56b43654fcbe4aa9df7b4a 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());
        }

 

    第三种:根据Map集合

     

 public List52fc45c754660ae0425ccd0c640c83e6 getStudentBystuId_foreach_map(Map14bd1badcdee783757181db757c9943f stuMap);

 

  

 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());
       }

    }

 

两种方式都能实现,打印效果

方案一打印效果

==> Preparing: select * from grade,student where grade.gradeId = student.stuGrade and gradeId = ?   ============一条sql<br>==> Parameters: 1(Integer)<br>310972c59990f340cb058bc11fe5bf2f Preparing: select * from grade where gradeId=?                   ==========第一条sql<br>==> Parameters: 1(Integer)<br>816ef50cd7bc472de795aa370f68ea84  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: 3, zy, 20, 1<br><====        Row: 4, zy, 21, 1<br><====      Total: 3<br><==      Total: 1<br>zhangyu<br>zy<br>zy

Process finished with exit code 0

 

Atas ialah kandungan terperinci MyBatis中的多条件查询讲解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn