public void testQueryAllDept(){ String hql="from Deptas model where mode.deptName='人事部' "; //hql="from com.sh.Entity"; Query query=session.createQuery(hql);
List list= query.list();
//... } public void testQueryAllDept(){ String hql="from Deptas model where mode.deptName like '%部%' "; //hql="from com.sh.Entity"; Query query=session.createQuery(hql);
List list= query.list();
//... }
public void testQueryDeptByGt(){
String hql="from Dept model where model.createDate> '2012-03-10'"; Query query=session.createQuery(hql); List list= query.list(); }
public void testModifyDate(){ Transaction tr=session.beginTransaction(); Dept detp=(Dept)session.get(Detp.class,new Integer(6)); detp.seteCreateDate("2000-03-10"); tr.commit();
}
// delete 删除 public void testDelDeptByHql(){ Transaction tr=session.beginTransaction(); String hql="delete Dept as model where model.deptId=6"; Query query=session.createQuery(hql); query.executeUpdate(); tr.commit(); }
public void testQueryDeptName(){ String hql="select model.deptName from Dept as model"; Query query=session.createQuery(hql); List deptNameList=query.list(); //... } public void testQueryDeptName(){ String hql="select model.deptName,model.createDate from Dept as model"; Query query=session.createQuery(hql); List
//排序 public void testQueryOrderBy(){ String hql="from Dept as mode order by model.createDate,model.deptName desc"; Query query=session.createQuery(); List deptList=query.list(); for(){} //
}
//分组 并且 使用 having 筛选 public void testQueryGroupBy(){ String hql="select count(model),model.deptName from Dept as model group by model.deptName having count(model)>1"; Query query=session.createQuery(); List
public void testQueryUn(){ String hql="select model.deptName from Dept as model where model.deptId=2"; Query query=session.createQuery(hql); query.setMaxResults(1); String deptName=(String)query.uniqueResult(); System.out.println(deptNamae); }
public void testQueryByParamsName(){ //使用 ":参数名" 来传递参数 String hql="from Dept as model where model.deptName=:deptName and model.createDate=:createDate";
Query query=session.createQuery(hql); query.setString("deptName","test"); query.setString("ceateDate","2000-02-10"); List deptList=query.list(); for(){}
//...
}
2.使用 "?" 按照位置来绑定参数
public void testQueryByParamsID(){ String hql="from Dept as model where model.deptName=? and model.createDate=?"; Query query=session.createQuery(hql); query.setString(0,"test"); query.setString(1,"2000-02-02"); List list=query.list(); for(){} //.. }
3.采用setEntity() 将参数和一个持久化对象进行绑定
public void testQueryByDept(){ Dept dept=(Dept)Session.get(Dept.clas,new Integer(2)); String hql="select Employee as model where model.dept=?" Query query=session.createQuery(hql); query.setEntity(0,dept); List empList=query.list(); for(){} //..
}
4.setParameter() 用于绑定任意类型的参数
public void testQueryBySetParams(){ String hql="from Dept as model where model.deptName=?"; Query query=session.createQuery(hql); query.setParameter(0,"test"); List deptList=query.list(); for(){} //.. }
5.setProperties() 将一个参数名 于一个对象的属性值绑定
public void testQueryBySetPro(){ Dept deptNew=new Dept(); deptNew.setDeptName("test");
String hql="from Dept as model where model.deptName=:deptName"; Query query=session.createQuery(); query.setProperties(deptNew); List deptList=query.list(); for(){
public void testQueryChild(){ String hql="from Dept as model where (select count(*) from model.employee) >2"; Query query=session.createQuery(hql); List objList=query.list(); for(){} //.. }
JSR-303是一个数据验证的规范,但是spring并没有对其进行实现,Hibernate Validator是实现了这一规范的,通过此这个实现来讲SpringMVC对JSR-303的支持。
JSR-303的校验是基于注解的,首先要把这些注解标记在需要验证的实体类的属性上或是其对应的get方法上。
登录需要验证类
public class Login {
@NotEmpty
引言:有时候需要在项目初始化的时候进行一系列工作,比如初始化一个线程池,初始化配置文件,初始化缓存等等,这时候就需要用到启动监听器,下面分别介绍一下两种常用的项目启动监听器
ServletContextListener
特点: 依赖于sevlet容器,需要配置web.xml
使用方法:
public class StartListener implements
The next group of methods has to do with rounding decimal values into integers. Three methods — Math.ceil(), Math.floor(), and Math.round() — handle rounding in differen