返回

Spring Boot Find By <Column> 包含

发布时间:2022-08-16 19:43:53 251
# php

当我尝试按包含疾病进行搜索时,我得到了结果,但只是在我第一次运行代码时。之后我收到以下错误,你知道为什么这在第一次尝试时有效,然后总是会在相同的请求中返回此错误吗?谢谢!

好结果:

[
    {
        "id": 1,
        "name": "Simvastatina",
        "companyName": "Company1",
        "price": 5.55,
        "disease": "Lorem ipsum dolor sit amet, consectetur adipiscing elit",
        "expirationDate": "2023-12-31"
    },
    {
        "id": 6,
        "name": "Simvastatina",
        "companyName": "Company1",
        "price": 5.55,
        "disease": "Lorem ipsum dolor sit amet, consectetur adipiscing elit",
        "expirationDate": "2023-12-31"
    }
]

错误:

2022-07-05 19:17:55.083 ERROR 4484 --- [nio-8080-exec-6]
o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for
servlet [dispatcherServlet] in context with path [] threw exception
[Request processing failed; nested exception is
org.springframework.dao.InvalidDataAccessApiUsageException: Parameter
value [%Lorem%] did not match expected type [java.lang.Character
(n/a)]; nested exception is java.lang.IllegalArgumentException:
Parameter value [%Lorem%] did not match expected type
[java.lang.Character (n/a)]] with root cause

java.lang.IllegalArgumentException: Parameter value [%Lorem%] did not
match expected type [java.lang.Character (n/a)]
        at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:54)
~[hibernate-core-5.6.7.Final.jar:5.6.7.Final]
        at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:27)
~[hibernate-core-5.6.7.Final.jar:5.6.7.Final]
        at org.hibernate.query.internal.QueryParameterBindingImpl.validate(QueryParameterBindingImpl.java:90)
~[hibernate-core-5.6.7.Final.jar:5.6.7.Final]
        at org.hibernate.query.internal.QueryParameterBindingImpl.setBindValue(QueryParameterBindingImpl.java:55)
~[hibernate-core-5.6.7.Final.jar:5.6.7.Final]
        at org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:501)
~[hibernate-core-5.6.7.Final.jar:5.6.7.Final]
        at org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:122)
~[hibernate-core-5.6.7.Final.jar:5.6.7.Final]
        at org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter.setParameter(CriteriaQueryTypeQueryAdapter.java:393)
~[hibernate-core-5.6.7.Final.jar:5.6.7.Final]
        at org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter.setParameter(CriteriaQueryTypeQueryAdapter.java:61)
~[hibernate-core-5.6.7.Final.jar:5.6.7.Final]
        at org.springframework.data.jpa.repository.query.QueryParameterSetter$BindableQuery.setParameter(QueryParameterSetter.java:319)
~[spring-data-jpa-2.6.3.jar:2.6.3]
        at org.springframework.data.jpa.repository.query.QueryParameterSetter$NamedOrIndexedQueryParameterSetter.lambda$setParameter$3(QueryParameterSetter.java:116)
~[spring-data-jpa-2.6.3.jar:2.6.3]
        at org.springframework.data.jpa.repository.query.QueryParameterSetter$ErrorHandling$1.execute(QueryParameterSetter.java:141)
~[spring-data-jpa-2.6.3.jar:2.6.3]
        at org.springframework.data.jpa.repository.query.QueryParameterSetter$NamedOrIndexedQueryParameterSetter.setParameter(QueryParameterSetter.java:116)
~[spring-data-jpa-2.6.3.jar:2.6.3]
        at org.springframework.data.jpa.repository.query.ParameterBinder.bind(ParameterBinder.java:82)
~[spring-data-jpa-2.6.3.jar:2.6.3]
        at org.springframework.data.jpa.repository.query.ParameterBinder.bind(ParameterBinder.java:74)
~[spring-data-jpa-2.6.3.jar:2.6.3]
        at org.springframework.data.jpa.repository.query.ParameterBinder.bindAndPrepare(ParameterBinder.java:96)
~[spring-data-jpa-2.6.3.jar:2.6.3]
        at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer.invokeBinding(PartTreeJpaQuery.java:324)
~[spring-data-jpa-2.6.3.jar:2.6.3]
        at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer.createQuery(PartTreeJpaQuery.java:243)
~[spring-data-jpa-2.6.3.jar:2.6.3]
        at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.doCreateQuery(PartTreeJpaQuery.java:106)
~[spring-data-jpa-2.6.3.jar:2.6.3]
        at org.springframework.data.jpa.repository.query.AbstractJpaQuery.createQuery(AbstractJpaQuery.java:227)
~[spring-data-jpa-2.6.3.jar:2.6.3]
        at org.springframework.data.jpa.repository.query.JpaQueryExecution$CollectionExecution.doExecute(JpaQueryExecution.java:128)
~[spring-data-jpa-2.6.3.jar:2.6.3]
        at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:90)
~[spring-data-jpa-2.6.3.jar:2.6.3]
        at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:155)
~[spring-data-jpa-2.6.3.jar:2.6.3]
        at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:143)
~[spring-data-jpa-2.6.3.jar:2.6.3]
        at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137)
~[spring-data-commons-2.7.0.jar:2.7.0]
        at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121)
~[spring-data-commons-2.7.0.jar:2.7.0]
        at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:159)
~[spring-data-commons-2.7.0.jar:2.7.0]
        at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:138)
~[spring-data-commons-2.7.0.jar:2.7.0]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
~[spring-aop-5.3.18.jar:5.3.18]
        at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80)
~[spring-data-commons-2.7.0.jar:2.7.0]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
~[spring-aop-5.3.18.jar:5.3.18]
        at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123)
~[spring-tx-5.3.18.jar:5.3.18]
        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388)
~[spring-tx-5.3.18.jar:5.3.18]
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119)
~[spring-tx-5.3.18.jar:5.3.18]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
~[spring-aop-5.3.18.jar:5.3.18]
        at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137)
~[spring-tx-5.3.18.jar:5.3.18]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
~[spring-aop-5.3.18.jar:5.3.18]
        at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:145)
~[spring-data-jpa-2.6.3.jar:2.6.3]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
~[spring-aop-5.3.18.jar:5.3.18]
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
~[spring-aop-5.3.18.jar:5.3.18]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
~[spring-aop-5.3.18.jar:5.3.18]
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
~[spring-aop-5.3.18.jar:5.3.18]
        at com.fsdprogram.demo.controllers.medicine.$Proxy239.findByDiseaseContainingIgnoreCase(Unknown
Source) ~[na:na]
        at com.fsdprogram.demo.controllers.medicine.MedicineController.findMedicinesByDisease(MedicineController.java:60)
~[classes/:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown
Source) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown
Source) ~[na:na]
        at java.base/java.lang.reflect.Method.invoke(Unknown Source) ~[na:na]
        at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
~[spring-web-5.3.18.jar:5.3.18]
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)
~[spring-web-5.3.18.jar:5.3.18]
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)
~[spring-webmvc-5.3.18.jar:5.3.18]
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
~[spring-webmvc-5.3.18.jar:5.3.18]
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
~[spring-webmvc-5.3.18.jar:5.3.18]
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
~[spring-webmvc-5.3.18.jar:5.3.18]
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067)
~[spring-webmvc-5.3.18.jar:5.3.18]
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
~[spring-webmvc-5.3.18.jar:5.3.18]
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
~[spring-webmvc-5.3.18.jar:5.3.18]
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
~[spring-webmvc-5.3.18.jar:5.3.18]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:655)
~[tomcat-embed-core-9.0.60.jar:4.0.FR]
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
~[spring-webmvc-5.3.18.jar:5.3.18]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
~[tomcat-embed-core-9.0.60.jar:4.0.FR]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
~[tomcat-embed-core-9.0.60.jar:9.0.60]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
~[tomcat-embed-core-9.0.60.jar:9.0.60]
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
~[tomcat-embed-websocket-9.0.60.jar:9.0.60]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
~[tomcat-embed-core-9.0.60.jar:9.0.60]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
~[tomcat-embed-core-9.0.60.jar:9.0.60]
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:327)
~[spring-security-web-5.6.2.jar:5.6.2]
        at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:115)
~[spring-security-web-5.6.2.jar:5.6.2]
        at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:81)
~[spring-security-web-5.6.2.jar:5.6.2]
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
~[spring-security-web-5.6.2.jar:5.6.2]
        at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:122)
~[spring-security-web-5.6.2.jar:5.6.2]
        at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:116)
~[spring-security-web-5.6.2.jar:5.6.2]
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
~[spring-security-web-5.6.2.jar:5.6.2]
        at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:126)
~[spring-security-web-5.6.2.jar:5.6.2]
        at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:81)
~[spring-security-web-5.6.2.jar:5.6.2]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
~[tomcat-embed-core-9.0.60.jar:9.0.60]
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
~[tomcat-embed-core-9.0.60.jar:9.0.60]
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
~[tomcat-embed-core-9.0.60.jar:9.0.60]
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
~[tomcat-embed-core-9.0.60.jar:9.0.60]
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
~[tomcat-embed-core-9.0.60.jar:9.0.60]
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
~[tomcat-embed-core-9.0.60.jar:9.0.60]
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
~[tomcat-embed-core-9.0.60.jar:9.0.60]
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360)
~[tomcat-embed-core-9.0.60.jar:9.0.60]
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)
~[tomcat-embed-core-9.0.60.jar:9.0.60]
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
~[tomcat-embed-core-9.0.60.jar:9.0.60]
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:889)
~[tomcat-embed-core-9.0.60.jar:9.0.60]
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1743)
~[tomcat-embed-core-9.0.60.jar:9.0.60]
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
~[tomcat-embed-core-9.0.60.jar:9.0.60]
        at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
~[tomcat-embed-core-9.0.60.jar:9.0.60]
        at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
~[tomcat-embed-core-9.0.60.jar:9.0.60]
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
~[tomcat-embed-core-9.0.60.jar:9.0.60]
        at java.base/java.lang.Thread.run(Unknown Source) ~[na:na]

呼叫:

http://127.0.0.1:8080/api/user/medicines/disease/Lorem

 

package com.fsdprogram.demo.model;

import lombok.Data;

import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;

@Data
@Entity
public class Medicine {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;
    @Column(nullable = false)
    private String name;
    @Column(nullable = false)
    private String companyName;
    @Column(nullable = false)
    private BigDecimal price;
    @Column(name = "disease",nullable = false)
    private String disease;
    @Column(nullable = false)
    @Temporal(TemporalType.DATE)
    private Date expirationDate;
}

存储库:

package com.fsdprogram.demo.controllers.medicine;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.fsdprogram.demo.model.Medicine;

@Repository
interface MedicineRepository extends JpaRepository<Medicine, Long> {
    List findByName(String name);
    List findByDiseaseContainingIgnoreCase(String disease);
    List findByDiseaseStartingWith(String disease);
    List findByDiseaseLike(String disease);
    List findByDisease(String disease);
}

控制器:

package com.fsdprogram.demo.controllers.medicine;

import java.net.URI;
import java.util.List;

import lombok.Data;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import com.fsdprogram.demo.model.Medicine;

@RestController
public @Data class MedicineController {
    private final MedicineRepository repository;

    @Autowired
    JdbcTemplate jdbcTemplate;

@GetMapping("/api/user/medicines/disease/{disease}")
List findMedicinesByDisease(@PathVariable("disease") String disease) {
    return repository.findByDiseaseContainingIgnoreCase(disease);
}

}

数据:

INSERT INTO medicine (
        id, name, company_name, price, disease, expiration_date
    ) 
    VALUES (1, 'Simvastatina', 'Company1', 5.55, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit', '2023-12-31');

INSERT INTO medicine (
        id, name, company_name, price, disease, expiration_date
    ) 
    VALUES (2, 'Aspirina', 'Company2', 1.23, 'Sed non augue et leo interdum congue', '2024-12-31');

INSERT INTO medicine (
        id, name, company_name, price, disease, expiration_date
    ) 
    VALUES (3, 'Omeprazol', 'Company3', 7.55, 'omnis iste natus error sit voluptatem accusantium', '2025-12-31');

INSERT INTO medicine (
        id, name, company_name, price, disease, expiration_date
    ) 
    VALUES (4, 'Lexotiroxina sódica', 'Company4', 6.55, 'At vero eos et accusamus et iusto odio dignissimos', '2026-12-31');

INSERT INTO medicine (
        name, company_name, price, disease, expiration_date
    ) 
    VALUES ('Aspirina', 'Company3', 1.24, 'Seda non augue et leo interdum congue', '2025-12-31');

INSERT INTO medicine (
        name, company_name, price, disease, expiration_date
    ) 
    VALUES ('Simvastatina', 'Company1', 5.55, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit', '2023-12-31');
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(1)
按点赞数排序
用户头像