Ibatis动态(dynamic)查询

时间:2023-05-16 16:12:38


Ibatis的动态查询使得数据操作变得非常的灵活,下次举出了常用的动态查询的属性信息:
Ibatis配置信息

  1. <!-- Dynamic Sql -->
  2. <typeAlias alias="Student" type="com.ibatis.Student" />
  3. <typeAlias alias="StudentDTO" type="com.ibatis.StudentDTO" />
  4. <select id="dynamicGetStudent" parameterClass="StudentDTO"
  5. resultClass="Student">
  6. select *
  7. from student
  8. <dynamic prepend="WHERE">
  9. <isNotEqual prepend="AND" property="sid" compareValue="0">
  10. sid = #sid#
  11. </isNotEqual>
  12. <isNotNull prepend="AND" property="sname">
  13. sname = #sname#
  14. </isNotNull>
  15. </dynamic>
  16. <dynamic prepend="order by">
  17. <isParameterPresent>
  18. <isEqual prepend="order by" property="sort" compareValue="1">
  19. sname desc,socre
  20. </isEqual>
  21. <isEqual prepend="order by" property="sort" compareValue="2">
  22. sname asc,socre
  23. </isEqual>
  24. </isParameterPresent>
  25. </dynamic>
  26. </select>
<!-- Dynamic Sql -->
  <typeAlias alias="Student" type="com.ibatis.Student" />
	<typeAlias alias="StudentDTO" type="com.ibatis.StudentDTO" />
	<select id="dynamicGetStudent" parameterClass="StudentDTO"
		resultClass="Student">
		select *
		from student
		<dynamic prepend="WHERE">
			<isNotEqual prepend="AND" property="sid" compareValue="0">
				sid = #sid#
			</isNotEqual>

			<isNotNull prepend="AND" property="sname">
				sname = #sname#
			</isNotNull>
		</dynamic>
		<dynamic prepend="order by">
			<isParameterPresent>
				<isEqual prepend="order by" property="sort" compareValue="1">
					sname desc,socre
				</isEqual>

				<isEqual prepend="order by" property="sort" compareValue="2">
					sname asc,socre
				</isEqual>
			</isParameterPresent>
		</dynamic>
	</select>

Student的信息

  1. package com.ibatis;
  2. import java.util.Date;
  3. public class Student {
  4. private int sid;
  5. private String sname;
  6. private String major;
  7. private Date birth;
  8. private int socre;
  9. public int getSid() {
  10. return sid;
  11. }
  12. public void setSid(int sid) {
  13. this.sid = sid;
  14. }
  15. public String getSname() {
  16. return sname;
  17. }
  18. public void setSname(String sname) {
  19. this.sname = sname;
  20. }
  21. public String getMajor() {
  22. return major;
  23. }
  24. public void setMajor(String major) {
  25. this.major = major;
  26. }
  27. public Date getBirth() {
  28. return birth;
  29. }
  30. public void setBirth(Date birth) {
  31. this.birth = birth;
  32. }
  33. public int getSocre() {
  34. return socre;
  35. }
  36. public void setSocre(int socre) {
  37. this.socre = socre;
  38. }
  39. @Override
  40. public String toString() {
  41. String content = "Sid="+this.sid+"\t"+"Sname="+this.sname+"\t"+this.birth.toLocaleString();
  42. return content;
  43. }
  44. }
package com.ibatis;

import java.util.Date;

public class Student {
	private int sid;
	private String sname;
	private String major;
	private Date birth;
	private int socre;
	public int getSid() {
		return sid;
	}
	public void setSid(int sid) {
		this.sid = sid;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public String getMajor() {
		return major;
	}
	public void setMajor(String major) {
		this.major = major;
	}
	public Date getBirth() {
		return birth;
	}
	public void setBirth(Date birth) {
		this.birth = birth;
	}
	public int getSocre() {
		return socre;
	}
	public void setSocre(int socre) {
		this.socre = socre;
	}

	@Override
	public String toString() {
		String content = "Sid="+this.sid+"\t"+"Sname="+this.sname+"\t"+this.birth.toLocaleString();
		return content;
	}

}

StudentDTO的信息

  1. package com.ibatis;
  2. import java.util.Date;
  3. public class StudentDTO {
  4. private int sid;
  5. private String sname;
  6. private String major;
  7. private Date birth;
  8. private int socre;
  9. private int sort;
  10. public int getSort() {
  11. return sort;
  12. }
  13. public void setSort(int sort) {
  14. this.sort = sort;
  15. }
  16. public int getSid() {
  17. return sid;
  18. }
  19. public void setSid(int sid) {
  20. this.sid = sid;
  21. }
  22. public String getSname() {
  23. return sname;
  24. }
  25. public void setSname(String sname) {
  26. this.sname = sname;
  27. }
  28. public String getMajor() {
  29. return major;
  30. }
  31. public void setMajor(String major) {
  32. this.major = major;
  33. }
  34. public Date getBirth() {
  35. return birth;
  36. }
  37. public void setBirth(Date birth) {
  38. this.birth = birth;
  39. }
  40. public int getSocre() {
  41. return socre;
  42. }
  43. public void setSocre(int socre) {
  44. this.socre = socre;
  45. }
  46. @Override
  47. public String toString() {
  48. String content = "Sid="+this.sid+"\t"+"Sname="+this.sname+"\t"+this.birth.toLocaleString();
  49. return content;
  50. }
  51. }
package com.ibatis;

import java.util.Date;

public class StudentDTO {
	private int sid;
	private String sname;
	private String major;
	private Date birth;
	private int socre;
	private int sort;
	public int getSort() {
		return sort;
	}
	public void setSort(int sort) {
		this.sort = sort;
	}
	public int getSid() {
		return sid;
	}
	public void setSid(int sid) {
		this.sid = sid;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public String getMajor() {
		return major;
	}
	public void setMajor(String major) {
		this.major = major;
	}
	public Date getBirth() {
		return birth;
	}
	public void setBirth(Date birth) {
		this.birth = birth;
	}
	public int getSocre() {
		return socre;
	}
	public void setSocre(int socre) {
		this.socre = socre;
	}

	@Override
	public String toString() {
		String content = "Sid="+this.sid+"\t"+"Sname="+this.sname+"\t"+this.birth.toLocaleString();
		return content;
	}

}

Java中的调用信息

  1. @SuppressWarnings("unchecked")
  2. public static void main(String[] args) {
  3. List<Student> list = dao.dynamicGetStudent(studentDTO);
  4. for(Student student : list){
  5. System.out.println("name:"+student.getSname());
  6. }
  7. System.out.println("size:"+list.size());
  8. }
  9. @Override
  10. public List dynamicGetStudent(StudentDTO studentDTO) {
  11. List result = null;
  12. try {
  13. result = sqlMapClient.queryForList("dynamicGetStudent",studentDTO);
  14. } catch (SQLException e) {
  15. // TODO Auto-generated catch block
  16. e.printStackTrace();
  17. }
  18. return result;
  19. }
@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		List<Student> list = dao.dynamicGetStudent(studentDTO);
		for(Student student : list){
			System.out.println("name:"+student.getSname());
		}
		System.out.println("size:"+list.size());
	}
@Override
	public List dynamicGetStudent(StudentDTO studentDTO) {
		List result = null;
		try {
			result = sqlMapClient.queryForList("dynamicGetStudent",studentDTO);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

动态查询中的常用属性:

属性关键字 含义
<isEqual> 如果参数相等于值则查询条件有效
<isNotEqual> 如果参数不等于值则查询条件有效
<isGreaterThan> 如果参数大于值则查询条件有效
<isGreaterEqual> 如果参数等于值则查询条件有效
<isLessEqual> 如果参数小于值则查询条件有效。如下所示:<isLessEqual prepend = ”AND” property = ”age” compareValue = ”18” >ADOLESCENT = ‘TRUE’</isLessEqual>
<isPropertyAvailable> 如果参数有使用则查询条件有效。
<isNotPropertyAvailable> 如果参数没有使用则查询条件有效
<isNull> 如果参数为NULL则查询条件有效
<isNotNull> 如果参数不为NULL则查询条件有效
<isEmpty> 如果参数为空则查询条件有效
<isNotEmpty> 如果参数不为空则查询条件有效
<isParameterPresent> 如果参数类不为NULL则查询条件有效
<isNotParameterPresent> Checks to see if the parameter object is not present (null). Example Usage