一:使用動態SQL完成多條件查詢
a:使用if+where實現多條件查詢
首先場景需求,有 個年級和班級表,第一個要求是根據模糊查詢姓名,和年齡大小進行條件查詢,接口層方法
public List<student> getStudentByIf(student stu);
其次是映射文件的配置
<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>
測試
studentDao dao = MyBatis.getSessionTwo().getMapper(studentDao.class); student stu = new student(); stu.setStuName("z"); // stu.setStuAge(19); List<student> list= dao.getStudentByIf(stu); for (student item:list) { System.out.println("----------"+item.getStuName()); }
打印效果
----------zhangyu
----------zy
----------zy
----------zhang
b:choose when 分類
這種方式和java中choose循環結構原理是一樣的,判斷多種情況,只要修改一下映射文件即可
接口 類
public List<student> getAllStudentByLike(Map<String, Object> userMap); //使用map作為參數
映射文件
<select id="getAllStudentByLike" parameterType="Map" resultType="stu"> select * from student <where> <choose> <when test="stuName!=null"> stuName like CONCAT('%',#{stuName},'%') </when> <when test="stuAge!=0"> stuAge> #{stuAge} </when>
<otherwise>
1=1
</otherwise>
</choose> </where> </select>
結果
zhangyu
zy
zy
zhang
c:使用foreach完成復雜 查詢,有三種方式,
第一種:傳入的參數為數組類型
//傳一組 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>
測試類
Integer[] ints = {2,3,4};
List<student> list = dao.getStudentBystuId_foreach_array(ints);
for (student item:list) {
System.out.println(item.getStuName());
}
第二種:傳入list集合
public List<student> getStudentBystuId_foreach_list(List<Integer> list);
<!--跟據學生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>
測試:
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()); }
第三種:根據Map集合
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>
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()); }
打印結果可以執行以下。
d;一對多的兩種實現方式
主要是resultMapper里的配置不同
接口方法
public grade getGradeById(int gradeId);
映射文件配置
<!--實現一 對多的第一中實現--> <resultMap id="gradeMapOne" type="grade"> <id column="gradeId" property="gradeId"></id> <result column="gradeName" property="gradeName"></result> <collection property="gatStudent" ofType="stu"> <id column="stuUd" property="stuId"></id> <result column="stuName" property="stuName"></result> <result column="stuAge" property="stuAge"></result> </collection> </resultMap> <!--實現一 對多的第二中實現--> <resultMap id="gradeMap" type="entity.grade"> <id column="gradeId" property="gradeId"></id> <result column="gradeName" property="gradeName"></result> <collection property="gatStudent" ofType="student" select="getStudentById" column="gradeId"></collection> <!--column的值主要作為下次查詢的條件,既查詢學生的條件--> </resultMap>
<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>
@Test public 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
==> Parameters: 1(Integer)
<== Columns: gradeId, gradeName, stuId, stuName, stuAge, stuGrade
<== Row: 1, S1297, 2, zhangyu, 19, 1
<== Row: 1, S1297, 3, zy, 20, 1
<== Row: 1, S1297, 4, zy, 21, 1
<== Total: 3
zhangyu
zy
zy
Process finished with exit code 0
方案二打印效果
==> Preparing: select * from grade where gradeId=? ==========第一條sql
==> Parameters: 1(Integer)
<== Columns: gradeId, gradeName
<== Row: 1, S1297
====> Preparing: select * from student where stuGrade = ? ==========第二條sql
====> Parameters: 1(Long)
<==== Columns: stuId, stuName, stuAge, stuGrade
<==== Row: 2, zhangyu, 19, 1
<==== Row: 3, zy, 20, 1
<==== Row: 4, zy, 21, 1
<==== Total: 3
<== Total: 1
zhangyu
zy
zy
Process finished with exit code 0
