在hibernate中是支持多條件動態查詢的
例如有這麼一個界面
那麼要實現這個查詢
下面我寫一個例子
我有一張表叫user表
package com
import java
public class User {
int id;
String name;
int age;
Date brithday;
public String getName() {
returnname;
}
public void setName(String name) {
this
}
public int getAge() {
returnage;
}
public void setAge(int age) {
this
}
public Date getBrithday() {
returnbrithday;
}
public void setBrithday(Date brithday) {
this
}
public int getId() {
returnid;
}
public void setId(int id) {
this
}
}
User
<?xmlversion=
<!DOCTYPEhibernate
<hibernate
<classname=
<idname=
<generatorclass=
</id>
<propertyname=
<propertyname=
<propertyname=
</class>
</hibernate
在XXX
然後我們寫一個測試類
package com
import java
import java
import org
import org
import org
import org
import org
import com
import com
public class Test
public static void main(String[] args) {
// addData();
queryData();
}
// 添加數據
public static void addData() {
Session session = HibernateSessionFactory
Transaction transaction = session
transaction
for (int i =
User user = new User();
user
user
user
session
}
mit();
session
}
// 查詢數據
public static void queryData() {
String outName =
int outAge =
Date outDate = null;
Session session = HibernateSessionFactory
DetachedCriteria dc = DetachedCriteria
if (outName !=null) {
dc
}
if (outAge >
dc
}
if (outDate !=null) {
dc
}
Criteria c = dc
List<User> list = c
for (User i : list) {
System
}
}
}
看看測試類中的查詢方法
intoutAge =
DateoutDate = null;
這三個條件是通過從前台或者是用戶輸入的那麼就實現了多條件的動態查詢
我們看看數據庫的信息
顯然這個裡面有一條記錄是符合的
張
利用hibernate的好處是這樣我只要
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28511.html