最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Criteria進(jìn)行分組求和、排序、模糊查詢的實(shí)例

 更新時間:2022年03月26日 11:01:30   作者:追月亮的猴子  
這篇文章主要介紹了使用Criteria進(jìn)行分組求和、排序、模糊查詢的實(shí)例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Criteria進(jìn)行分組求和、排序、模糊查詢

工程框架使用的是spring data,但是spring data 提供的 JpaRepository 以及覆寫JpaSpecificationExecutor接口并不能滿足我的要求,我要進(jìn)行模糊查詢,并根據(jù)bookId對數(shù)量進(jìn)行分組聚合,并獲取數(shù)量最高的top n,由于模糊查詢并不是必填條件,所以直接使用@Query注解感覺也不是很合適,于是采用Criteria來實(shí)現(xiàn)。

1.Entity如下

package com.example.springdatatest.repository.entity;?
import lombok.Data;?
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
?
@Table
@Data
@Entity
public class TestEntity {
?
? ? @Id
? ? String testId;
?
? ? @Column
? ? public String regionCode;
?
? ? @Column
? ? public long count;
?
? ? @Column
? ? public String bookId;?
?
? ? @Column
? ? public String libraryCode;
}

2.repository如下

package com.example.springdatatest.repository;?
import com.example.springdatatest.repository.entity.TestEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;?
public interface TestRepository extends JpaRepository<TestEntity,String>,JpaSpecificationExecutor {
}

3.service如下

package com.example.springdatatest.service;?
import com.example.springdatatest.repository.TestRepository;
import com.example.springdatatest.repository.entity.TestEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
?
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Tuple;
import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;
?
@Service
public class TestService {
?
? ? @Autowired
? ? TestRepository testRepository;
?
? ? @Autowired
? ? @PersistenceContext
? ? private EntityManager entityManager;?
?
? ? public void test(){
? ? ? ? CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
? ? ? ? CriteriaQuery<Tuple> criteriaQuery = criteriaBuilder.createQuery(Tuple.class ?);?
? ? ? ? Root root = criteriaQuery.from(TestEntity.class);
?
? ? ? ? List<Predicate> predicateList = new ArrayList<>();
? ? ? ? predicateList.add(criteriaBuilder.equal( root.get("libraryCode" ), "1" ));
? ? ? ? predicateList.add(criteriaBuilder.like(root.get("regionCode"),"%"+"6101"+"%"));
? ? ? ? Predicate[] p = new Predicate[predicateList.size()];
? ? ? ? predicateList.toArray(p);
?
? ? ? ? Path bookId = root.get("bookId");
? ? ? ? Path bookCount = root.get("count");?
? ? ? ? criteriaQuery.where(p)
? ? ? ? ? ? ? ? .multiselect(bookId,criteriaBuilder.sum(bookCount))
? ? ? ? ? ? ? ? .groupBy(bookId)
? ? ? ? ? ? ? ? .orderBy(criteriaBuilder.desc(criteriaBuilder.sum(bookCount)));
?
? ? ? ? List<Tuple> list = entityManager.createQuery(criteriaQuery).setFirstResult(1)
? ? ? ? ? ? ? ? .setMaxResults(1)
? ? ? ? ? ? ? ? .getResultList();?
? ? }
}

4.順便提及一個不經(jīng)意間的小錯誤

也是由于粗心,沒有寫這一句  predicateList.toArray(p); ,導(dǎo)致一直報空指針異常。

java.lang.NullPointerException
    at org.hibernate.query.criteria.internal.predicate.CompoundPredicate.render(CompoundPredicate.java:166)
    at org.hibernate.query.criteria.internal.predicate.CompoundPredicate.render(CompoundPredicate.java:115)
    at org.hibernate.query.criteria.internal.predicate.CompoundPredicate.render(CompoundPredicate.java:105)
    at org.hibernate.query.criteria.internal.QueryStructure.render(QueryStructure.java:248)
    at org.hibernate.query.criteria.internal.CriteriaQueryImpl.interpret(CriteriaQueryImpl.java:292)
    at org.hibernate.query.criteria.internal.compile.CriteriaCompiler.compile(CriteriaCompiler.java:149)
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:3707)
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:208)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:350)
    at com.sun.proxy.$Proxy81.createQuery(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:309)
    at com.sun.proxy.$Proxy81.createQuery(Unknown Source)
    at com.example.springdatatest.service.TestService.test(TestService.java:50)
    at com.example.springdatatest.SpringdatatestApplicationTests.contextLoads(SpringdatatestApplicationTests.java:19)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

這只是實(shí)現(xiàn)的一個小demo,具體入?yún)⒉]有體現(xiàn),在此做一個記錄。

Criteria進(jìn)行模糊查詢實(shí)現(xiàn)站內(nèi)搜索功能

今天給網(wǎng)站新加入了一個站內(nèi)搜索的功能,思想是:使用Criteria進(jìn)行模糊查詢。

Dao層的方法如下

//搜索方法
public List<Question> findSearch(String scon) {
? ? ? ? Session s = ?getHibernateTemplate().getSessionFactory().openSession();
? ? ? ? Criteria criteria =s.createCriteria(Question.class);
? ? ? ? criteria.add(Expression.or(Expression.like("qdesc","%"+scon+"%"),Expression.like("qname","%"+scon+"%")));
? ? ? ? return criteria.list();
? ? }

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 如何用Java模擬XN*2圖靈機(jī)

    如何用Java模擬XN*2圖靈機(jī)

    這篇文章主要介紹了如何用Java模擬XN*2圖靈機(jī)方法,感興趣的朋友可以參考下
    2021-04-04
  • Javacv使用ffmpeg實(shí)現(xiàn)音視頻同步播放

    Javacv使用ffmpeg實(shí)現(xiàn)音視頻同步播放

    這篇文章主要介紹了Javacv使用ffmpeg實(shí)現(xiàn)音視頻同步播放,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • SpringBoot實(shí)現(xiàn)基于URL和IP的訪問頻率限制

    SpringBoot實(shí)現(xiàn)基于URL和IP的訪問頻率限制

    在現(xiàn)代?Web?應(yīng)用中,接口被惡意刷新或暴力請求是一種常見的攻擊手段,為了保護(hù)系統(tǒng)資源,需要對接口的訪問頻率進(jìn)行限制,下面我們就來看看如何使用?Spring?Boot?實(shí)現(xiàn)基于?URL?和?IP?的訪問頻率限制吧
    2025-01-01
  • Java實(shí)現(xiàn)將每日新聞添加到自己博客中

    Java實(shí)現(xiàn)將每日新聞添加到自己博客中

    這篇文章主要為大家詳細(xì)介紹了Java如何實(shí)現(xiàn)將每日新聞添加到自己博客中并發(fā)送到微信群中,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-12-12
  • 關(guān)于RowBounds分頁原理、RowBounds的坑記錄

    關(guān)于RowBounds分頁原理、RowBounds的坑記錄

    這篇文章主要介紹了關(guān)于RowBounds分頁原理、RowBounds的坑記錄,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • 詳解Reactor中Context的用法

    詳解Reactor中Context的用法

    在Reactor中提供了Context來替代ThreadLocal,可以實(shí)現(xiàn)一個跨線程的共享變量的透明方式。本文主要為大家介紹了Context的用法的用法,感興趣的可以了解一下
    2023-02-02
  • Spring Boot通過Junit實(shí)現(xiàn)單元測試過程解析

    Spring Boot通過Junit實(shí)現(xiàn)單元測試過程解析

    這篇文章主要介紹了Spring Boot通過Junit實(shí)現(xiàn)單元測試過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-01-01
  • Java基本類型與byte數(shù)組之間相互轉(zhuǎn)換方法

    Java基本類型與byte數(shù)組之間相互轉(zhuǎn)換方法

    下面小編就為大家?guī)硪黄狫ava基本類型與byte數(shù)組之間相互轉(zhuǎn)換方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-08-08
  • Spring中@Async注解實(shí)現(xiàn)異步調(diào)詳解

    Spring中@Async注解實(shí)現(xiàn)異步調(diào)詳解

    在本篇文章里小編給大家分享的是關(guān)于Spring中@Async注解實(shí)現(xiàn)異步調(diào)詳解內(nèi)容,需要的朋友們可以學(xué)習(xí)下。
    2020-04-04
  • 淺談idea中導(dǎo)入maven項(xiàng)目的兩種方式

    淺談idea中導(dǎo)入maven項(xiàng)目的兩種方式

    本文主要介紹了淺談idea中導(dǎo)入maven項(xiàng)目的兩種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-08-08

最新評論

阜平县| 莲花县| 和静县| 文成县| 桐柏县| 高雄市| 砚山县| 阿图什市| 缙云县| 南郑县| 新竹县| 巴东县| 新密市| 庆城县| 湘乡市| 兰溪市| 拜城县| 平乐县| 台湾省| 金坛市| 米林县| 福泉市| 清原| 三门县| 阿克陶县| 察哈| 措勤县| 南宫市| 含山县| 项城市| 汝州市| 霸州市| 疏勒县| 江城| 石楼县| 陕西省| 临澧县| 宁陕县| 称多县| 丹江口市| 敖汉旗|