在查询表单中填写的项,往往可以将其封状成一个对象。如下代码:
<%
String name=request.getParameter("name");
String age=request.getParameter("age");
Student stu=new Student();
stu.setName(name);
stu.setAge(Integer. parseInt (age));
List list=StudentDAO.findStudent(stu);
%>
上述代码中的findStudent(Student stu)源码为:
public static List findStudents(Student stu) {
List list=null;
try {
session = HibernateUtil.currentSession(); //开启连接
tx = session.beginTransaction(); //开启事务
Example exampleStu =
Example.create(stu).
ignoreCase()
.enableLike(MatchMode.ANYWHERE)
.excludeProperty("age");
list= session.createCriteria(Student.class)
.add( exampleStu )
.list();
tx.commit();
} catch (HibernateException e) { //捕捉例外
e.printStackTrace();
tx.rollback();
}finally
{
HibernateUtil.closeSession(session);
}
return list;
}
模板类中有一些方法用来修饰封装类的属性,比如Student中有一个name属性被填充了“tom”字段,那么Criteria要如何匹配这个“tom”字符串,是MatchMode.ANYWHERE,还是MatchMode.EXACT?这就要通过模板类中的方法来修饰。比较常用的方法如下所示:
 1.ignoreCase():忽略模板类中所有String属性的大小写。
 2.enableLike(MatchMode mode):表示对模板类中所有的String属性进行like模糊匹配,enableLike()中的参数指明以何种方式进行匹配,比如MatchMode.ANYWHERE的方式是like “%变量%”。
 3.excludeZeroes ():不把为0值的字段加入到where条件句中。
 4.excludeProperty(String name):不把属性为name的字段加入到where条件句中。
整个例子的入口程序BM.java的源码为:
public class BM {
public static void main(String[] args) throws HibernateException {
Student stu=new Student();
stu.setName("tom");
stu.setAge(25);
List list=StudentDAO.findStudents(stu);//查找符合条件的Student
for(int i=0;i<list.size();i++)
{
stu=(Student) list.get(i);
System.out.println(stu.getName() +" "+stu.getAge());
}
}
}
<%
String name=request.getParameter("name");
String age=request.getParameter("age");
Student stu=new Student();
stu.setName(name);
stu.setAge(Integer. parseInt (age));
List list=StudentDAO.findStudent(stu);
%>
上述代码中的findStudent(Student stu)源码为:
public static List findStudents(Student stu) {
List list=null;
try {
session = HibernateUtil.currentSession(); //开启连接
tx = session.beginTransaction(); //开启事务
Example exampleStu =
Example.create(stu).
ignoreCase()
.enableLike(MatchMode.ANYWHERE)
.excludeProperty("age");
list= session.createCriteria(Student.class)
.add( exampleStu )
.list();
tx.commit();
} catch (HibernateException e) { //捕捉例外
e.printStackTrace();
tx.rollback();
}finally
{
HibernateUtil.closeSession(session);
}
return list;
}
模板类中有一些方法用来修饰封装类的属性,比如Student中有一个name属性被填充了“tom”字段,那么Criteria要如何匹配这个“tom”字符串,是MatchMode.ANYWHERE,还是MatchMode.EXACT?这就要通过模板类中的方法来修饰。比较常用的方法如下所示:
 1.ignoreCase():忽略模板类中所有String属性的大小写。
 2.enableLike(MatchMode mode):表示对模板类中所有的String属性进行like模糊匹配,enableLike()中的参数指明以何种方式进行匹配,比如MatchMode.ANYWHERE的方式是like “%变量%”。
 3.excludeZeroes ():不把为0值的字段加入到where条件句中。
 4.excludeProperty(String name):不把属性为name的字段加入到where条件句中。
整个例子的入口程序BM.java的源码为:
public class BM {
public static void main(String[] args) throws HibernateException {
Student stu=new Student();
stu.setName("tom");
stu.setAge(25);
List list=StudentDAO.findStudents(stu);//查找符合条件的Student
for(int i=0;i<list.size();i++)
{
stu=(Student) list.get(i);
System.out.println(stu.getName() +" "+stu.getAge());
}
}
}
解决方案 »
- Servlet dwr-invoker is not available 问题
- 版本号一般都怎么规定?
- mysql引擎inodb与isam
- Java程序怎样脱离MyEclipse单独运行
- 班级资源平台管理如何创建一个数据库可以实现动态加载页面
- 用过LazyValidatorForm的朋友帮忙,使用 LazyValidatorForm 验证多文件上载异常。
- ejb中可以多个bean共用同一个远程或本地接口吗?
- web service客户端如何调用多个服务器
- 升了一级,高兴散分!
- 一个很菜的问题!J2EE究竟是什么??
- 急救:hibernate映射表中标注为<map-key>的列为何检索不出来?
- TMOCAT连接池问题?
比如你举的Student类,如果我只填充了age属性,其他属性都是null
那么查询出来的结果就不对,把所有的都查出来了,如果加了excludeNone的话就选不出来郁闷那,难道一定要把不要的属性exclude掉吗?