规格模式取代sql查询代码

原文:http://www.abigdreamer.com/index.php/archives/38

 

sql查询代码如下:

 

QueryBuilder qb = new QueryBuilder("select * from XMetaModel where 1=1");
if (StringUtil.isNotEmpty(dga.getParam("SearchName"))) {
	qb.append(" and Name like ?", new Object[] { "%" + dga.getParam("SearchName") + "%" });
}
if (StringUtil.isNotEmpty(dga.getParam("Type"))) {
	qb.append(" and OwnerType=?", new Object[] { dga.getParam("Type") });
}
qb.append(" order by AddTime desc", new Object[0]);
qb.executeDataTable();

 

规格模式查询代码如下:

 

Criteria criteria = new Criteria(XMetaModel.class);
criteria.add(Restrictions.like("Name", dga.getParam("SearchName")));
criteria.add(Restrictions.eq("OwnerType", dga.getParam("Type")));
criteria.addOrder(Order.desc("AddTime"));
criteria.dataTable();

你可能感兴趣的:(sql)