Spring JPA存储库通用查找与Dandelion DatatableCriterias [英] Spring JPA Repositories Generic FIND WITH Dandelion DatatableCriterias
问题描述
我找到了关于如何实现ajax服务器端处理的示例。
$ b
@RequestMapping(value =/ persons)
public @ResponseBody
DatatablesResponse< Person> findAllForDataTablesFullSpring(@DatatablesParams DatatablesCriterias标准){
DataSet< Person> dataSet = personService.findPersonsWithDatatablesCriterias(criterias);
return DatatablesResponse.build(dataSet,criterias);
}
在服务层上
public DataSet< Person> findPersonsWithDatatablesCriterias(DatatablesCriterias标准){
List< Person> persons = personRepository.findPersonWithDatatablesCriterias(criterias);
Long count = personRepository.getTotalCount();
Long countFiltered = personRepository.getFilteredCount(criterias);
返回新的DataSet< Person>(persons,count,countFiltered);
在数据访问层上
@Override
public List< Person> findPersonWithDatatablesCriterias(DatatablesCriterias标准){
StringBuilder queryBuilder = new StringBuilder(SELECT p FROM Person p);
$ b / **
*步骤1:全局和个别列过滤
* /
queryBuilder.append(PersonRepositoryUtils.getFilterQuery(criterias));
$ b $ **
*步骤2:对
* /
进行排序if(criterias.hasOneSortedColumn()){
List< String> ; orderParams = new ArrayList< String>();
queryBuilder.append(ORDER BY); (ColumnDef columnDef:criterias.getSortingColumnDefs()){
orderParams.add(p。+ columnDef.getName()++ columnDef.getSortDirection())的
。
}
迭代器< String> itr2 = orderParams.iterator();
while(itr2.hasNext()){
queryBuilder.append(itr2.next());
if(itr2.hasNext()){
queryBuilder.append(,);
}
}
}
TypedQuery< Person> query = entityManager.createQuery(queryBuilder.toString(),Person.class);
$ b $ **
*步骤3:分页
* /
query.setFirstResult(criterias.getDisplayStart());
query.setMaxResults(criterias.getDisplaySize());
返回query.getResultList();
}
/ **
*< p>
*用于返回过滤人数的查询。
*
* @param标准
*用于过滤人员的DataTables标准。
*(maxResult,过滤,分页,...)
* @返回过滤的人数。
* /
@Override
public Long getFilteredCount(DatatablesCriterias标准){
StringBuilder queryBuilder = new StringBuilder(SELECT p FROM Person p);
queryBuilder.append(PersonRepositoryUtils.getFilterQuery(criterias));
查询查询= entityManager.createQuery(queryBuilder.toString());
return Long.parseLong(String.valueOf(query.getResultList()。size()));
}
/ **
* @返回总人数。
* /
@Override
public Long getTotalCount(){
Query query = entityManager.createQuery(SELECT COUNT(p)FROM Person p);
return(Long)query.getSingleResult();
$ / code>
我的问题是我需要重复编码数据访问层,它是对我所有的实体完全一样。现在,即时通讯思想有一个GENERIC实现。有人可以给我这个解决方案。对于任何使用Dandelion Datatables插件的人来说,这都是一个很好的帮助。
因为最近我从事同样的任务,可以给出几个建议。
首先你应该看一下 Spring Data ,因为那部分Spring框架为您提供了数据库访问级别所需的一切。
您将需要创建一个扩展 JpaRepository
的 @Repository
,然后从这里查询数据库。
例如,getTotalCount可能会被 findAll()
方法替代,该方法返回所有实体的列表,甚至更简单,并带有 count()
方法。
然后,带有分页的数据可以通过 findAll(Pageable pageable)
来获取,您只需要传递 new PageRequest(criterias.getStart( ),criterias.getLength())
作为参数。
排序选项也可以通过Sort类在Spring Data中使用,并且它与Dandelion DatatablesCriterias和ColumnDef类非常巧妙地结合在一起。
对于更复杂的查询,您可以使用 @Query
和 @Param
注释。
因此,有关春季数据的更多具体信息,最好提出新问题。
I found samples on how to implement ajax server-side processing. On the controller the code looks like this.
@RequestMapping(value = "/persons")
public @ResponseBody
DatatablesResponse<Person> findAllForDataTablesFullSpring(@DatatablesParams DatatablesCriterias criterias) {
DataSet<Person> dataSet = personService.findPersonsWithDatatablesCriterias(criterias);
return DatatablesResponse.build(dataSet, criterias);
}
On the service layer
public DataSet<Person> findPersonsWithDatatablesCriterias(DatatablesCriterias criterias) {
List<Person> persons = personRepository.findPersonWithDatatablesCriterias(criterias);
Long count = personRepository.getTotalCount();
Long countFiltered = personRepository.getFilteredCount(criterias);
return new DataSet<Person>(persons, count, countFiltered);
}
On the data access layer
@Override
public List<Person> findPersonWithDatatablesCriterias(DatatablesCriterias criterias) {
StringBuilder queryBuilder = new StringBuilder("SELECT p FROM Person p");
/**
* Step 1: global and individual column filtering
*/
queryBuilder.append(PersonRepositoryUtils.getFilterQuery(criterias));
/**
* Step 2: sorting
*/
if (criterias.hasOneSortedColumn()) {
List<String> orderParams = new ArrayList<String>();
queryBuilder.append(" ORDER BY ");
for (ColumnDef columnDef : criterias.getSortingColumnDefs()) {
orderParams.add("p." + columnDef.getName() + " " + columnDef.getSortDirection());
}
Iterator<String> itr2 = orderParams.iterator();
while (itr2.hasNext()) {
queryBuilder.append(itr2.next());
if (itr2.hasNext()) {
queryBuilder.append(" , ");
}
}
}
TypedQuery<Person> query = entityManager.createQuery(queryBuilder.toString(), Person.class);
/**
* Step 3: paging
*/
query.setFirstResult(criterias.getDisplayStart());
query.setMaxResults(criterias.getDisplaySize());
return query.getResultList();
}
/**
* <p>
* Query used to return the number of filtered persons.
*
* @param criterias
* The DataTables criterias used to filter the persons.
* (maxResult, filtering, paging, ...)
* @return the number of filtered persons.
*/
@Override
public Long getFilteredCount(DatatablesCriterias criterias) {
StringBuilder queryBuilder = new StringBuilder("SELECT p FROM Person p");
queryBuilder.append(PersonRepositoryUtils.getFilterQuery(criterias));
Query query = entityManager.createQuery(queryBuilder.toString());
return Long.parseLong(String.valueOf(query.getResultList().size()));
}
/**
* @return the total count of persons.
*/
@Override
public Long getTotalCount() {
Query query = entityManager.createQuery("SELECT COUNT(p) FROM Person p");
return (Long) query.getSingleResult();
}
My problem with this is I need to repeat coding the data access layer which is completely the same for all of my entities. Now, im thinking of having a GENERIC implementation. Can someone give me solution for this. It would be a great help for anyone using Dandelion Datatables plugin.
Since, recently I worked on same kind of task, I can give a couple of suggestions.
First of all you should definitely take a look at Spring Data since that part of Spring framework provides you with everything you would need for database access level.
You will need to create a @Repository
that extends JpaRepository
, and then from here querying your database.
So, for example, getTotalCount could be would be substituted with findAll()
method, that returns a List of all entities, or even simpler, with count()
method.
Then, data with pagination could be retrieved with findAll(Pageable pageable)
where you just need to pass new PageRequest(criterias.getStart(), criterias.getLength())
as a parameter.
Sorting option is also available in Spring Data via Sort class and it works pretty neat with Dandelion DatatablesCriterias and ColumnDef classes.
For more complex queries you could use @Query
and @Param
annotations.
So, for more specific info regarding spring data it's better to ask new questions.
这篇关于Spring JPA存储库通用查找与Dandelion DatatableCriterias的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!