天天看點

Ibatis動态(dynamic)查詢

ibatis的動态查詢使得資料操作變得非常的靈活,下次舉出了常用的動态查詢的屬性資訊:

ibatis配置資訊

Ibatis動态(dynamic)查詢

<!-- 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  

            </isparameterpresent>  

    </select>  

student的資訊

Ibatis動态(dynamic)查詢

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的資訊

Ibatis動态(dynamic)查詢

public class studentdto {  

    private int sort;  

    public int getsort() {  

        return sort;  

    public void setsort(int sort) {  

        this.sort = sort;  

java中的調用資訊

Ibatis動态(dynamic)查詢

@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