Home >Java >javaTutorial >Explanation of multi-condition query in MyBatis
One: Use dynamic SQL to complete multi-condition query
a:Use if+where to implement multi-condition query
First of all, the scenario requirements, there is a grade and class table, the first requirement is Conditional query based on fuzzy query name and age, interface layer method
public List<student> getStudentByIf(student stu);
The second is the configuration of the mapping file
205a3d64de2de7feacbf63eab3b424e3select * from student 196185dae55b7edbe154a5051db664a7 e5ca04733cd4c163365b5b71c59d203b and stuAge>#{stuAge} 0e19ae19f9bb3871b7693b46538542ae a1372c9923d43a5e03b73f8c613dcb74 and stuName LIKE '%' #{stuName} '%' 0e19ae19f9bb3871b7693b46538542ae bdcefec14b04ffcf937d99934241e90418bb6ffaf0152bbe49cd8a3620346341
Test
studentDao dao = MyBatis.getSessionTwo().getMapper(studentDao.= "z"List52fc45c754660ae0425ccd0c640c83e6 list="----------"+
------------zhangyu<br>---- ------zy<br>----------zy<br>----------zhang
<br>
b: choose when classification
This method is the same as the choose loop structure principle in java. To judge multiple situations, just modify the mapping file
Interface class
public List52fc45c754660ae0425ccd0c640c83e6 getAllStudentByLike(Map14bd1badcdee783757181db757c9943f userMap); //使用map作为参数
Mapping file
<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>
Result
zhangyu zy zy zhang
c: There are three ways to use foreach to complete complex queries,
The first one: the parameters passed in are array types
//传一组 xueshengID public List52fc45c754660ae0425ccd0c640c83e6 getStudentBystuId_foreach_array(Integer[] ints); 映射文件配置 fce7260c3087690639459e0047a0a932 e9f3e24cc593136c4a1cc5669e7135feselect * from studenta8fe01ee7c2d53fc9df09183bae757e40">where stuId IN/*数组形式传入学生Id*/f84a91398f1d55c211691993d7ee7b1d #{stu}70b0f4e7c41f2707b12056d6eb778fbc 0e19ae19f9bb3871b7693b46538542ae 18bb6ffaf0152bbe49cd8a3620346341
Test Class
Integer[] ints = {2,3,4}; List52fc45c754660ae0425ccd0c640c83e6 list = dao.getStudentBystuId_foreach_array(ints);for (student item:list) { System.out.println(item.getStuName()); }
Two: Pass in the list collection
public List52fc45c754660ae0425ccd0c640c83e6 getStudentBystuId_foreach_list(Listc0f559cc8d56b43654fcbe4aa9df7b4a list);
b1759409fbe6e773cf15efd292acda5f6ca04fcf8695599a6433593917d5eaa8select * from student294570585459929d13002b82f6df56280">where stuId IN /*集合形式传入学生Id*/1b0dccf704dbe04bd327bc29ff46c0bf#{stu}70b0f4e7c41f2707b12056d6eb778fbc0e19ae19f9bb3871b7693b46538542ae18bb6ffaf0152bbe49cd8a3620346341
Test:
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()); }
Third type: Based on Map collection
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>
Print results You can do the following.
d;Two implementation methods of one-to-many
Mainly due to different configurations in resultMapper
Interface method
public grade getGradeById(int gradeId);
Mapping file configuration
<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()); } }
Both methods can be achieved, the printing effect
Option 1 printing effect
==> Preparing: select * from grade,student where grade.gradeId = student.stuGrade and gradeId = ? ============A sql<br>==> Parameters: 1(Integer)<br>663434725ee819c43c84818ef8c22174 Preparing: select * from grade where gradeId=? ==========First sql<br>==> Parameters: 1(Integer)<br>6e3e4fe9ff26e6b034420199303479e4 Preparing: select * from student where stuGrade = ? ==========The second 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
The above is the detailed content of Explanation of multi-condition query in MyBatis. For more information, please follow other related articles on the PHP Chinese website!