Ibatis动态(dynamic)查询
生活随笔
收集整理的這篇文章主要介紹了
Ibatis动态(dynamic)查询
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Ibatis的動態查詢使得數據操作變得非常的靈活,下次舉出了常用的動態查詢的屬性信息:
Ibatis配置信息
Xml代碼 ?<!--?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>??
<!-- 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的信息
Java代碼 ?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;?? ????}?? ?????? }??
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;}@Overridepublic String toString() {String content = "Sid="+this.sid+"\t"+"Sname="+this.sname+"\t"+this.birth.toLocaleString();return content;}}
StudentDTO的信息
Java代碼 ?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;?? ????}?? ?????? }??
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;}@Overridepublic String toString() {String content = "Sid="+this.sid+"\t"+"Sname="+this.sname+"\t"+this.birth.toLocaleString();return content;}}
Java中的調用信息
Java代碼 ?@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;?? ????}??
@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());}
@Overridepublic List dynamicGetStudent(StudentDTO studentDTO) {List result = null;try {result = sqlMapClient.queryForList("dynamicGetStudent",studentDTO);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return result;}
動態查詢中的常用屬性:
Ibatis配置信息
Xml代碼 ?
Student的信息
Java代碼 ?
StudentDTO的信息
Java代碼 ?
Java中的調用信息
Java代碼 ?
動態查詢中的常用屬性:
| 屬性關鍵字 | 含義 |
| <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 |
總結
以上是生活随笔為你收集整理的Ibatis动态(dynamic)查询的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 转:ibatis动态sql
- 下一篇: 宁夏一本学校排名及录取分数线(2022宁