欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

解决springboot的findOne方法没有合适的参数使用问题

程序员文章站 2022-06-25 21:18:12
目录springboot的findone方法没有合适的参数使用springboot踩过的坑之findone()springboot的findone方法没有合适的参数使用开发springboot的dao...

springboot的findone方法没有合适的参数使用

开发springboot的dao层之后,去测试的时候,发现findone()这个方法找不到了,查看了对应的表字段名和实体类的属性都一致

解决springboot的findOne方法没有合适的参数使用问题

找了半天没找到是什么原因

最后发现是依赖的版本出问题了

<parent>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-parent</artifactid>
   <version>2.0.0.release</version>
   <relativepath/> <!-- lookup parent from repository -->
</parent>

这里改为1.5.10之后

就可以正常引入了

<parent>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-parent</artifactid>
   <version>1.5.10.release</version>
   <relativepath/> <!-- lookup parent from repository -->
</parent>

记录一下,花了3个小时找这个问题。。

解决springboot的findOne方法没有合适的参数使用问题

springboot踩过的坑之findone()

因为参考学习的视频还是一点几版本的,所以面对这个问题很无奈

简而言之

findone()方法的返回值压根就是就不是我想要的user类型

只能换成findbyid()的方法,但它的返回值是optional

经过多次百度,我发现这么写可以解决它:

 ...
    @requestmapping("/getuser")    
 public user getuser(integer id) {
  optional<user> user = userdao.findbyid(id);
  system.out.println(user.tostring());
  if(user.ispresent()){
   system.out.println("成功查找到了数据!!!");
   return user.get();
  }else{
   return null;
  }
 }
...

但是!!!

如果你在实体类中,表名与字段名没有对应的话

就会一直报错——

org.hibernate.exception.sqlgrammarexception: could not extract resultset
    at org.hibernate.exception.internal.sqlexceptiontypedelegate.convert(sqlexceptiontypedelegate.java:63) ~[hibernate-core-5.3.10.final.jar:5.3.10.final]
    at org.hibernate.exception.internal.standardsqlexceptionconverter.convert(standardsqlexceptionconverter.java:42) ~[hibernate-core-5.3.10.final.jar:5.3.10.final]
    at org.hibernate.engine.jdbc.spi.sqlexceptionhelper.convert(sqlexceptionhelper.java:113) ~[hibernate-core-5.3.10.final.jar:5.3.10.final]
    at org.hibernate.engine.jdbc.spi.sqlexceptionhelper.convert(sqlexceptionhelper.java:99) ~[hibernate-core-5.3.10.final.jar:5.3.10.final]
    at org.hibernate.engine.jdbc.internal.resultsetreturnimpl.extract(resultsetreturnimpl.java:69) ~[hibernate-core-5.3.10.final.jar:5.3.10.final]
    at org.hibernate.loader.plan.exec.internal.abstractloadplanbasedloader.getresultset(abstractloadplanbasedloader.java:419) ~[hibernate-core-5.3.10.final.jar:5.3.10.final]
    at org.hibernate.loader.plan.exec.internal.abstractloadplanbasedloader.executequerystatement(abstractloadplanbasedloader.java:191) ~[hibernate-core-5.3.10.final.jar:5.3.10.final]
    at org.hibernate.loader.plan.exec.internal.abstractloadplanbasedloader.executeload(abstractloadplanbasedloader.java:121) ~[hibernate-core-5.3.10.final.jar:5.3.10.final]
    at org.hibernate.loader.plan.exec.internal.abstractloadplanbasedloader.executeload(abstractloadplanbasedloader.java:86) ~[hibernate-core-5.3.10.final.jar:5.3.10.final]
    at org.hibernate.loader.entity.plan.abstractloadplanbasedentityloader.load(abstractloadplanbasedentityloader.java:188) ~[hibernate-core-5.3.10.final.jar:5.3.10.final]
    at org.hibernate.persister.entity.abstractentitypersister.load(abstractentitypersister.java:4273) ~[hibernate-core-5.3.10.final.jar:5.3.10.final]
    at org.hibernate.event.internal.defaultloadeventlistener.loadfromdatasource(defaultloadeventlistener.java:511) [hibernate-core-5.3.10.final.jar:5.3.10.final]
    at org.hibernate.event.internal.defaultloadeventlistener.doload(defaultloadeventlistener.java:481) [hibernate-core-5.3.10.final.jar:5.3.10.final]
    at org.hibernate.event.internal.defaultloadeventlistener.load(defaultloadeventlistener.java:222) [hibernate-core-5.3.10.final.jar:5.3.10.final]
    at org.hibernate.event.internal.defaultloadeventlistener.proxyorload(defaultloadeventlistener.java:281) [hibernate-core-5.3.10.final.jar:5.3.10.final]
    at org.hibernate.event.internal.defaultloadeventlistener.doonload(defaultloadeventlistener.java:124) [hibernate-core-5.3.10.final.jar:5.3.10.final]
    at org.hibernate.event.internal.defaultloadeventlistener.onload(defaultloadeventlistener.java:92) [hibernate-core-5.3.10.final.jar:5.3.10.final]
    at org.hibernate.internal.sessionimpl.fireload(sessionimpl.java:1257) [hibernate-core-5.3.10.final.jar:5.3.10.final]
    at org.hibernate.internal.sessionimpl.access$2000(sessionimpl.java:208) [hibernate-core-5.3.10.final.jar:5.3.10.final]
    at org.hibernate.internal.sessionimpl$identifierloadaccessimpl.doload(sessionimpl.java:2881) [hibernate-core-5.3.10.final.jar:5.3.10.final]
    at org.hibernate.internal.sessionimpl$identifierloadaccessimpl.load(sessionimpl.java:2855) [hibernate-core-5.3.10.final.jar:5.3.10.final]
    at org.hibernate.internal.sessionimpl.find(sessionimpl.java:3490) [hibernate-core-5.3.10.final.jar:5.3.10.final]
    at org.hibernate.internal.sessionimpl.find(sessionimpl.java:3459) [hibernate-core-5.3.10.final.jar:5.3.10.final]
    at sun.reflect.nativemethodaccessorimpl.invoke0(native method) ~[na:1.8.0_181]
    at sun.reflect.nativemethodaccessorimpl.invoke(unknown source) ~[na:1.8.0_181]
    at sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source) ~[na:1.8.0_181]
    at java.lang.reflect.method.invoke(unknown source) ~[na:1.8.0_181]
    at org.springframework.orm.jpa.extendedentitymanagercreator$extendedentitymanagerinvocationhandler.invoke(extendedentitymanagercreator.java:350) [spring-orm-5.1.7.release.jar:5.1.7.release]
    at com.sun.proxy.$proxy81.find(unknown source) [na:na]
    at sun.reflect.nativemethodaccessorimpl.invoke0(native method) ~[na:1.8.0_181]
    at sun.reflect.nativemethodaccessorimpl.invoke(unknown source) ~[na:1.8.0_181]
    at sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source) ~[na:1.8.0_181]
    at java.lang.reflect.method.invoke(unknown source) ~[na:1.8.0_181]
    at org.springframework.orm.jpa.sharedentitymanagercreator$sharedentitymanagerinvocationhandler.invoke(sharedentitymanagercreator.java:308) [spring-orm-5.1.7.release.jar:5.1.7.release]
    at com.sun.proxy.$proxy81.find(unknown source) [na:na]
    at org.springframework.data.jpa.repository.support.simplejparepository.findbyid(simplejparepository.java:241) [spring-data-jpa-2.1.8.release.jar:2.1.8.release]
    at sun.reflect.nativemethodaccessorimpl.invoke0(native method) ~[na:1.8.0_181]
    at sun.reflect.nativemethodaccessorimpl.invoke(unknown source) ~[na:1.8.0_181]
    at sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source) ~[na:1.8.0_181]
    at java.lang.reflect.method.invoke(unknown source) ~[na:1.8.0_181]
    at org.springframework.data.repository.core.support.repositorycomposition$repositoryfragments.invoke(repositorycomposition.java:359) [spring-data-commons-2.1.8.release.jar:2.1.8.release]
    at org.springframework.data.repository.core.support.repositorycomposition.invoke(repositorycomposition.java:200) [spring-data-commons-2.1.8.release.jar:2.1.8.release]
    at org.springframework.data.repository.core.support.repositoryfactorysupport$implementationmethodexecutioninterceptor.invoke(repositoryfactorysupport.java:644) [spring-data-commons-2.1.8.release.jar:2.1.8.release]
    at org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:186) [spring-aop-5.1.7.release.jar:5.1.7.release]
    at org.springframework.data.repository.core.support.repositoryfactorysupport$queryexecutormethodinterceptor.doinvoke(repositoryfactorysupport.java:608) [spring-data-commons-2.1.8.release.jar:2.1.8.release]
    at org.springframework.data.repository.core.support.repositoryfactorysupport$queryexecutormethodinterceptor.lambda$invoke$3(repositoryfactorysupport.java:595) [spring-data-commons-2.1.8.release.jar:2.1.8.release]
    at org.springframework.data.repository.core.support.repositoryfactorysupport$queryexecutormethodinterceptor.invoke(repositoryfactorysupport.java:595) [spring-data-commons-2.1.8.release.jar:2.1.8.release]
    at org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:186) [spring-aop-5.1.7.release.jar:5.1.7.release]
    at org.springframework.data.projection.defaultmethodinvokingmethodinterceptor.invoke(defaultmethodinvokingmethodinterceptor.java:59) ~[spring-data-commons-2.1.8.release.jar:2.1.8.release]
    at org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:186) [spring-aop-5.1.7.release.jar:5.1.7.release]
    at org.springframework.transaction.interceptor.transactionaspectsupport.invokewithintransaction(transactionaspectsupport.java:295) ~[spring-tx-5.1.7.release.jar:5.1.7.release]
    at org.springframework.transaction.interceptor.transactioninterceptor.invoke(transactioninterceptor.java:98) ~[spring-tx-5.1.7.release.jar:5.1.7.release]
    at org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:186) [spring-aop-5.1.7.release.jar:5.1.7.release]
    at org.springframework.dao.support.persistenceexceptiontranslationinterceptor.invoke(persistenceexceptiontranslationinterceptor.java:139) ~[spring-tx-5.1.7.release.jar:5.1.7.release]
    at org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:186) [spring-aop-5.1.7.release.jar:5.1.7.release]
    at org.springframework.data.jpa.repository.support.crudmethodmetadatapostprocessor$crudmethodmetadatapopulatingmethodinterceptor.invoke(crudmethodmetadatapostprocessor.java:138) ~[spring-data-jpa-2.1.8.release.jar:2.1.8.release]
    at org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:186) [spring-aop-5.1.7.release.jar:5.1.7.release]
    at org.springframework.aop.interceptor.exposeinvocationinterceptor.invoke(exposeinvocationinterceptor.java:93) ~[spring-aop-5.1.7.release.jar:5.1.7.release]
    at org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:186) [spring-aop-5.1.7.release.jar:5.1.7.release]
    at org.springframework.data.repository.core.support.surroundingtransactiondetectormethodinterceptor.invoke(surroundingtransactiondetectormethodinterceptor.java:61) ~[spring-data-commons-2.1.8.release.jar:2.1.8.release]
    at org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:186) [spring-aop-5.1.7.release.jar:5.1.7.release]
    at org.springframework.aop.framework.jdkdynamicaopproxy.invoke(jdkdynamicaopproxy.java:212) ~[spring-aop-5.1.7.release.jar:5.1.7.release]
    at com.sun.proxy.$proxy83.findbyid(unknown source) ~[na:na]
    at com.daipanda.controller.usercontroller.getuser(usercontroller.java:30) ~[classes/:na]
    at sun.reflect.nativemethodaccessorimpl.invoke0(native method) ~[na:1.8.0_181]
    at sun.reflect.nativemethodaccessorimpl.invoke(unknown source) ~[na:1.8.0_181]
    at sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source) ~[na:1.8.0_181]
    at java.lang.reflect.method.invoke(unknown source) ~[na:1.8.0_181]
    at org.springframework.web.method.support.invocablehandlermethod.doinvoke(invocablehandlermethod.java:190) ~[spring-web-5.1.7.release.jar:5.1.7.release]
    at org.springframework.web.method.support.invocablehandlermethod.invokeforrequest(invocablehandlermethod.java:138) ~[spring-web-5.1.7.release.jar:5.1.7.release]
    at org.springframework.web.servlet.mvc.method.annotation.servletinvocablehandlermethod.invokeandhandle(servletinvocablehandlermethod.java:104) ~[spring-webmvc-5.1.7.release.jar:5.1.7.release]
    at org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.invokehandlermethod(requestmappinghandleradapter.java:892) ~[spring-webmvc-5.1.7.release.jar:5.1.7.release]
    at org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.handleinternal(requestmappinghandleradapter.java:797) ~[spring-webmvc-5.1.7.release.jar:5.1.7.release]
    at org.springframework.web.servlet.mvc.method.abstracthandlermethodadapter.handle(abstracthandlermethodadapter.java:87) ~[spring-webmvc-5.1.7.release.jar:5.1.7.release]
    at org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:1039) ~[spring-webmvc-5.1.7.release.jar:5.1.7.release]
    at org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:942) ~[spring-webmvc-5.1.7.release.jar:5.1.7.release]
    at org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:1005) ~[spring-webmvc-5.1.7.release.jar:5.1.7.release]
    at org.springframework.web.servlet.frameworkservlet.doget(frameworkservlet.java:897) ~[spring-webmvc-5.1.7.release.jar:5.1.7.release]
    at javax.servlet.http.httpservlet.service(httpservlet.java:634) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:882) ~[spring-webmvc-5.1.7.release.jar:5.1.7.release]
    at javax.servlet.http.httpservlet.service(httpservlet.java:741) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:231) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:53) ~[tomcat-embed-websocket-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.springframework.web.filter.requestcontextfilter.dofilterinternal(requestcontextfilter.java:99) ~[spring-web-5.1.7.release.jar:5.1.7.release]
    at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) ~[spring-web-5.1.7.release.jar:5.1.7.release]
    at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.springframework.web.filter.formcontentfilter.dofilterinternal(formcontentfilter.java:92) ~[spring-web-5.1.7.release.jar:5.1.7.release]
    at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) ~[spring-web-5.1.7.release.jar:5.1.7.release]
    at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.springframework.web.filter.hiddenhttpmethodfilter.dofilterinternal(hiddenhttpmethodfilter.java:93) ~[spring-web-5.1.7.release.jar:5.1.7.release]
    at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) ~[spring-web-5.1.7.release.jar:5.1.7.release]
    at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.springframework.web.filter.characterencodingfilter.dofilterinternal(characterencodingfilter.java:200) ~[spring-web-5.1.7.release.jar:5.1.7.release]
    at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) ~[spring-web-5.1.7.release.jar:5.1.7.release]
    at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:200) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:96) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:490) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:139) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:92) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:74) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:343) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.coyote.http11.http11processor.service(http11processor.java:408) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.coyote.abstractprocessorlight.process(abstractprocessorlight.java:66) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.coyote.abstractprotocol$connectionhandler.process(abstractprotocol.java:836) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.tomcat.util.net.nioendpoint$socketprocessor.dorun(nioendpoint.java:1747) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.tomcat.util.net.socketprocessorbase.run(socketprocessorbase.java:49) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at java.util.concurrent.threadpoolexecutor.runworker(unknown source) ~[na:1.8.0_181]
    at java.util.concurrent.threadpoolexecutor$worker.run(unknown source) ~[na:1.8.0_181]
    at org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at java.lang.thread.run(unknown source) ~[na:1.8.0_181]
caused by: com.mysql.jdbc.exceptions.jdbc4.mysqlsyntaxerrorexception: unknown column 'user0_.dai_id' in 'field list'
    at sun.reflect.nativeconstructoraccessorimpl.newinstance0(native method) ~[na:1.8.0_181]
    at sun.reflect.nativeconstructoraccessorimpl.newinstance(unknown source) ~[na:1.8.0_181]
    at sun.reflect.delegatingconstructoraccessorimpl.newinstance(unknown source) ~[na:1.8.0_181]
    at java.lang.reflect.constructor.newinstance(unknown source) ~[na:1.8.0_181]
    at com.mysql.jdbc.util.handlenewinstance(util.java:411) ~[mysql-connector-java-5.1.21.jar:na]
    at com.mysql.jdbc.util.getinstance(util.java:386) ~[mysql-connector-java-5.1.21.jar:na]
    at com.mysql.jdbc.sqlerror.createsqlexception(sqlerror.java:1053) ~[mysql-connector-java-5.1.21.jar:na]
    at com.mysql.jdbc.mysqlio.checkerrorpacket(mysqlio.java:4074) ~[mysql-connector-java-5.1.21.jar:na]
    at com.mysql.jdbc.mysqlio.checkerrorpacket(mysqlio.java:4006) ~[mysql-connector-java-5.1.21.jar:na]
    at com.mysql.jdbc.mysqlio.sendcommand(mysqlio.java:2468) ~[mysql-connector-java-5.1.21.jar:na]
    at com.mysql.jdbc.mysqlio.sqlquerydirect(mysqlio.java:2629) ~[mysql-connector-java-5.1.21.jar:na]
    at com.mysql.jdbc.connectionimpl.execsql(connectionimpl.java:2719) ~[mysql-connector-java-5.1.21.jar:na]
    at com.mysql.jdbc.preparedstatement.executeinternal(preparedstatement.java:2155) ~[mysql-connector-java-5.1.21.jar:na]
    at com.mysql.jdbc.preparedstatement.executequery(preparedstatement.java:2318) ~[mysql-connector-java-5.1.21.jar:na]
    at com.zaxxer.hikari.pool.proxypreparedstatement.executequery(proxypreparedstatement.java:52) ~[hikaricp-3.2.0.jar:na]
    at com.zaxxer.hikari.pool.hikariproxypreparedstatement.executequery(hikariproxypreparedstatement.java) ~[hikaricp-3.2.0.jar:na]
    at org.hibernate.engine.jdbc.internal.resultsetreturnimpl.extract(resultsetreturnimpl.java:60) ~[hibernate-core-5.3.10.final.jar:5.3.10.final]
    ... 113 common frames omitted
...

特意记录,愿我下次更加仔细……以上为个人经验,希望能给大家一个参考,也希望大家多多支持。