问题描述
- 求详解,各位大神麻烦给我解析下下面的的语句,谢谢啦,小弟感激不尽
-
public List getList(String startTime, String endTime, String mc, Pager pager){ String where = " "; where = where + " and " + TypeChange.DateBetweenSql("a.riQi", startTime, endTime); if (!TypeChange.toNotNullDao(mc).equals("")) { where = where + " and a.mingCheng like '%" + TypeChange.toNotNullDao(mc) + "%'"; } return this.findAll(where, pager); } findAll: private List<Test> findAll(String where, Pager pager) { String hSql = " From Test a where 1=1 " + where + " order by id "; pager.setRowCount(getAllRowCount(hSql)); Session sess = this.getSessionFactory().openSession(); try { Query query = sess.createQuery(hSql); query.setFirstResult(pager.getFirstResult()); query.setMaxResults(pager.getPageSize()); return query.list(); } finally { sess.close(); } }
解决方案
public List getList(String startTime, String endTime, String mc, Pager pager)
{
String where = " "; //初始条件为空
where = where + " and " + TypeChange.DateBetweenSql("a.riQi", startTime, endTime); // 把开始结束时间作为条件
if (!TypeChange.toNotNullDao(mc).equals(""))
{
where = where + " and a.mingCheng like '%" + TypeChange.toNotNullDao(mc) + "%'"; // 关键字模糊查询 like %关键字%,就是包含它的文本视作匹配。
}
return this.findAll(where, pager);
}
findAll:
private List findAll(String where, Pager pager) //分页查询
{
String hSql = " From Test a where 1=1 " + where + " order by id "; // 按照条件来匹配,加上1=1是防止where为空
pager.setRowCount(getAllRowCount(hSql)); // 每页的数量
Session sess = this.getSessionFactory().openSession();
try
{
Query query = sess.createQuery(hSql); //构造sql
query.setFirstResult(pager.getFirstResult());
query.setMaxResults(pager.getPageSize());
return query.list();
}
finally
{
sess.close();
}
}
解决方案二:
上面部分就是拼接where条件,然后传入findAll方法进行调用。
findAll方法返回List列表(带分页的)。
解决方案三:
寻找某段时间类型或型号被改编的记录
时间: 2024-11-08 17:19:00