2 baidu 29643173 baidu_29643173 于 2015.07.09 14:37 提问

求详解,各位大神麻烦给我解析下下面的的语句,谢谢啦,小弟感激不尽

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();
    }
}

3个回答

caozhy
caozhy   Ds   Rxr 2015.07.09 15:14
已采纳

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();
}
}

baidu_29643173
baidu_29643173 十分感谢 好人啊~
2 年多之前 回复
guwei4037
guwei4037   Ds   Rxr 2015.07.09 14:58

上面部分就是拼接where条件,然后传入findAll方法进行调用。

findAll方法返回List列表(带分页的)。

baidu_29643173
baidu_29643173 你好,非常感谢你的答复,但是我是个新手,能否把语句拆分来讲,十分感激
2 年多之前 回复
frank_20080215
frank_20080215   2015.07.09 15:17

寻找某段时间类型或型号被改编的记录

Csdn user default icon
上传中...
上传图片
插入图片