Un : Utiliser du SQL dynamique pour compléter une requête multi-conditions
a : Utiliser if+where pour implémenter une requête multi-conditions
Tout d'abord, les exigences du scénario, il y a une note et la table de classes, la première exigence est une requête conditionnelle basée sur le nom et l'âge de la requête floue, la méthode de la couche d'interface
public List<student> getStudentByIf(student stu);
Vient ensuite la configuration du fichier de mappage
<select id="getStudentByIf" parameterType="stu" resultType="stu">select * from student <where> <if test="stuAge!=0"> and stuAge>#{stuAge} </if> <if test="stuName!=null"> and stuName LIKE '%' #{stuName} '%' </if> </where></select>
Test
studentDao dao = MyBatis.getSessionTwo().getMapper(studentDao.= "z"List<student> list="----------"+
---- ------zhangyu<br/>---------zy<br/>----------zy<br/>----------zhang
<br/>
b : choisir quand la classification
Cette méthode est la même que le principe de structure de boucle de choix en Java Pour juger plusieurs situations, il suffit de modifier le. fichier de mappage
Classe d'interface
public List<student> getAllStudentByLike(Map<String, Object> userMap); //使用map作为参数
Fichier de mappage
<span style="color: #0000ff"><</span><span style="color: #800000">select </span><span style="color: #ff0000">id</span><span style="color: #0000ff">="getAllStudentByLike"</span><span style="color: #ff0000"> parameterType</span><span style="color: #0000ff">="Map"</span><span style="color: #ff0000"> resultType</span><span style="color: #0000ff">="stu"</span><span style="color: #0000ff">></span><span style="color: #000000">select * from student</span><span style="color: #0000ff"><</span><span style="color: #800000">where</span><span style="color: #0000ff">></span><span style="color: #0000ff"><</span><span style="color: #800000">choose</span><span style="color: #0000ff">></span><span style="color: #0000ff"><</span><span style="color: #800000">when </span><span style="color: #ff0000">test</span><span style="color: #0000ff">="stuName!=null"</span><span style="color: #0000ff">></span><span style="color: #000000"> stuName like CONCAT('%',#{stuName},'%')</span><span style="color: #0000ff"></</span><span style="color: #800000">when</span><span style="color: #0000ff">></span><span style="color: #0000ff"><</span><span style="color: #800000">when </span><span style="color: #ff0000">test</span><span style="color: #0000ff">="stuAge!=0"</span><span style="color: #0000ff">></span><span style="color: #000000"> stuAge> #{stuAge}</span><span style="color: #0000ff"></</span><span style="color: #800000">when</span><span style="color: #0000ff">><br/></span>
<otherwise> 1=1 </otherwise>
<span style="color: #0000ff"><br/></span><span style="color: #0000ff"></</span><span style="color: #800000">choose</span><span style="color: #0000ff">></span><span style="color: #0000ff"></</span><span style="color: #800000">where</span><span style="color: #0000ff">></span><span style="color: #0000ff"></</span><span style="color: #800000">select</span><span style="color: #0000ff">></span>
Résultat
zhangyu zy zy zhang
c : Il y a trois façons à utiliser foreach pour compléter des requêtes complexes,
Le premier type : Le paramètre entrant est un type tableau
🎜>
//传一组 xueshengID public List<student> getStudentBystuId_foreach_array(Integer[] ints); 映射文件配置 <!--跟据学生id查询学生Interger--> <select id="getStudentBystuId_foreach_array" resultMap="studentList">select * from student<if test="array.length>0">where stuId IN/*数组形式传入学生Id*/<foreach collection="array" item="stu" open="(" separator="," close=")"> #{stu}</foreach> </if> </select>
Le deuxième type : passer dans la collecte de liste
Integer[] ints = {2,3,4}; List<student> list = dao.getStudentBystuId_foreach_array(ints);for (student item:list) { System.out.println(item.getStuName()); }
public List<student> getStudentBystuId_foreach_list(List<Integer> list);
Test :
<!--跟据学生id查询学生list方式--><select id="getStudentBystuId_foreach_list" resultMap="studentList">select * from student<if test="list.size>0">where stuId IN /*集合形式传入学生Id*/<foreach collection="list" item="stu" open="(" separator="," close=")">#{stu}</foreach></if></select>
Le troisième type : Définir selon la carte
>
studentDao dao = MyBatis.getSessionTwo().getMapper(studentDao.class); Integer ints = 2; List<Integer> list = new ArrayList<Integer>(); list.add(ints); List<student> stulist = dao.getStudentBystuId_foreach_list(list); for (student item:stulist) { System.out.println(item.getStuName()); }
public List<student> getStudentBystuId_foreach_map(Map<String, Object> stuMap);
<!--跟据学生id查询学生map方式--><select id="getStudentBystuId_foreach_map" resultMap="studentList">select * from student where stuId IN /*集合形式传入学生Id*/<foreach collection="stuId" item="stu" open="(" separator="," close=")"> <!--collection是自己定义的,就是map的key值-->#{stu}</foreach></select>
<span style="color: #008000"> Map<String ,Object> stumap = new HashMap<String, Object>(); List<Integer> listStuId = new ArrayList<Integer>(); listStuId.add(2); listStuId.add(3); listStuId.add(4); stumap.put("stuId",listStuId); List<student> list = dao.getStudentBystuId_foreach_map(stumap); for (student item:list ) { System.out.println(item.getStuName()); }</span><span style="color: #008000"><br/></span>
public grade getGradeById(int gradeId);
<span style="color: #008000"><!--</span><span style="color: #008000">实现一 对多的第一中实现</span><span style="color: #008000">--></span><span style="color: #0000ff"><</span><span style="color: #800000">resultMap </span><span style="color: #ff0000">id</span><span style="color: #0000ff">="gradeMapOne"</span><span style="color: #ff0000"> type</span><span style="color: #0000ff">="grade"</span><span style="color: #0000ff">></span><span style="color: #0000ff"><</span><span style="color: #800000">id </span><span style="color: #ff0000">column</span><span style="color: #0000ff">="gradeId"</span><span style="color: #ff0000"> property</span><span style="color: #0000ff">="gradeId"</span><span style="color: #0000ff">></</span><span style="color: #800000">id</span><span style="color: #0000ff">></span><span style="color: #0000ff"><</span><span style="color: #800000">result </span><span style="color: #ff0000">column</span><span style="color: #0000ff">="gradeName"</span><span style="color: #ff0000"> property</span><span style="color: #0000ff">="gradeName"</span><span style="color: #0000ff">></</span><span style="color: #800000">result</span><span style="color: #0000ff">></span><span style="color: #0000ff"><</span><span style="color: #800000">collection </span><span style="color: #ff0000">property</span><span style="color: #0000ff">="gatStudent"</span><span style="color: #ff0000"> ofType</span><span style="color: #0000ff">="stu"</span><span style="color: #0000ff">></span><span style="color: #0000ff"><</span><span style="color: #800000">id </span><span style="color: #ff0000">column</span><span style="color: #0000ff">="stuUd"</span><span style="color: #ff0000"> property</span><span style="color: #0000ff">="stuId"</span><span style="color: #0000ff">></</span><span style="color: #800000">id</span><span style="color: #0000ff">></span><span style="color: #0000ff"><</span><span style="color: #800000">result </span><span style="color: #ff0000">column</span><span style="color: #0000ff">="stuName"</span><span style="color: #ff0000"> property</span><span style="color: #0000ff">="stuName"</span><span style="color: #0000ff">></</span><span style="color: #800000">result</span><span style="color: #0000ff">></span><span style="color: #0000ff"><</span><span style="color: #800000">result </span><span style="color: #ff0000">column</span><span style="color: #0000ff">="stuAge"</span><span style="color: #ff0000"> property</span><span style="color: #0000ff">="stuAge"</span><span style="color: #0000ff">></</span><span style="color: #800000">result</span><span style="color: #0000ff">></span><span style="color: #0000ff"></</span><span style="color: #800000">collection</span><span style="color: #0000ff">></span><span style="color: #0000ff"></</span><span style="color: #800000">resultMap</span><span style="color: #0000ff">></span><span style="color: #008000"><!--</span><span style="color: #008000">实现一 对多的第二中实现</span><span style="color: #008000">--></span><span style="color: #0000ff"><</span><span style="color: #800000">resultMap </span><span style="color: #ff0000">id</span><span style="color: #0000ff">="gradeMap"</span><span style="color: #ff0000"> type</span><span style="color: #0000ff">="entity.grade"</span><span style="color: #0000ff">></span><span style="color: #0000ff"><</span><span style="color: #800000">id </span><span style="color: #ff0000">column</span><span style="color: #0000ff">="gradeId"</span><span style="color: #ff0000"> property</span><span style="color: #0000ff">="gradeId"</span><span style="color: #0000ff">></</span><span style="color: #800000">id</span><span style="color: #0000ff">></span><span style="color: #0000ff"><</span><span style="color: #800000">result </span><span style="color: #ff0000">column</span><span style="color: #0000ff">="gradeName"</span><span style="color: #ff0000"> property</span><span style="color: #0000ff">="gradeName"</span><span style="color: #0000ff">></</span><span style="color: #800000">result</span><span style="color: #0000ff">></span><span style="color: #0000ff"><</span><span style="color: #800000">collection </span><span style="color: #ff0000">property</span><span style="color: #0000ff">="gatStudent"</span><span style="color: #ff0000"> ofType</span><span style="color: #0000ff">="student"</span><span style="color: #ff0000"> select</span><span style="color: #0000ff">="getStudentById"</span><span style="color: #ff0000"> column</span><span style="color: #0000ff">="gradeId"</span><span style="color: #0000ff">></</span><span style="color: #800000">collection</span><span style="color: #0000ff">> <!--column的值主要作为下次查询的条件,既查询学生的条件--></span><span style="color: #0000ff"></</span><span style="color: #800000">resultMap</span><span style="color: #0000ff">><br/></span>
<select id="getGradeById" resultMap="gradeMapOne">select * from grade,student where grade.gradeId = student.stuGrade and gradeId = #{gradeId}</select><!--ddddddddddddddddddd--><select id="getGradeById" resultMap="gradeMap">select * from grade where gradeId=#{gradeId}</select><select id="getStudentById" resultType="entity.student">select * from student where stuGrade = #{stuGrade}</select>
<br/>
==> Préparation : sélectionnez * from grade,student où grade.gradeId = student.stuGrade et gradeId ======== = ===A sql
==> Paramètres : 1(Integer)<br/>
<== Rangée : 1, S1297, 4, zy, 21, 1
<== Total : 3zhangyu
zyzy
@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()); } }
<== Ligne : 1, S1297
====> ====Le deuxième sql= ===> Paramètres : 1(Long)
<==== Colonnes : stuId, stuName, stuAge, stuGrade<==== Row : 2, zhangyu, 19, 1
& lt; ==== rangée : 3, zy, 20, 1
& lt === rangée : 4, zy, 21, 1
& lt; ; ======== == Total : 1
zhangyu
zy
zy
Processus terminé avec le code de sortie 0
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!