如何使用没有弹簧数据的弹簧页面

时间:2016-10-27 07:28:19

标签: java spring hibernate

您好我正在使用以下代码查找使用spring with plain hibernate的文章。

public List<Article>  getArticles(int currPosition, int pageSize) {
        Criteria c = getSession().createCriteria(Article.class);
        c.addOrder(Order.desc("createdDate"));
        c.setFirstResult(currPosition);
        c.setMaxResults(pageSize);
        List<Article> result = c.list();
        return result;
    }

我现在不在我的项目中使用spring数据,那么我如何在项目中使用org.springframework.data.domain.Page

Page<Person> persons = personService.findAllPageable(new PageRequest(evalPage, evalPageSize));

2 个答案:

答案 0 :(得分:1)

您可以使用Pageable#getSize设置maxResultssetFirstResult设置public static async Task<DataTable> getTeacherSHS() { DataTable dt = new DataTable(); string query = @"some long query dont mid this"; using (MySqlConnection conn = new MySqlConnection(cs)) { try { await conn.OpenAsync(); using( MySqlCommand cmd = new MySqlCommand(query,conn)) { cmd.Parameters.AddWithValue("@shs", "%SHS%"); cmd.Parameters.AddWithValue("@term", term); await cmd.ExecuteNonQueryAsync(); using (MySqlDataAdapter da = new MySqlDataAdapter(cmd)) { await da.FillAsync(dt); MySqlCommandBuilder cb = new MySqlCommandBuilder(da); } } } catch (MySqlException e) { Console.Write(e.Message); } if (conn != null) await conn.CloseAsync(); } return dt; } Pageable#getOffset以获取排序参数。

答案 1 :(得分:0)

您甚至可以使用pagerequest:

public class PagePersons implements Serializable {
     private List<Persons> persons ;
     private int page ;
     private int numberPersons ;
     private int totalPersons ;
     private int totalPage ;

 //getters setters
}

并调用您的人员存储库以获取示例

   public PagePersons getPagePersons(int codePerson, int page, int size) {
    Page<Person> Persons= personRepository.getPagePersons(codePerson, new PageRequest(page, size));

   PagePersons pagePersons =new PagePersons();
   pagePersons.setPersons(persons.getContent());
   pagePersons.setPage(page);
   pagePersons.setNumbrePersonss(persons.getNumberOfElements());
   pagePersons.setTotalPage(persons.getTotalPages());
   pagePersons.setPage(page);

    return  pagePersons ;
    }