java spring boot中使用Mysql函数

     2023-02-26     255

关键词:

【中文标题】java spring boot中使用Mysql函数【英文标题】:Using Mysql function in java spring boot 【发布时间】:2018-07-10 10:10:34 【问题描述】:

我的存储库中的自定义查询有点问题:

@Query("select v from Visit v where YEARWEEK('date') = YEARWEEK(curdate())")

在 YEARWEEK('date') 左括号之前显示错误:

<expression>, <operator>, AND, GROUP, HAVING, IS, OR or ORDER expected, got '('

【问题讨论】:

另外SELECT v FROM Visit v 看起来模棱两可或错字。也许SELECT v.* FROM Visit v'date' 周围的引号通常代表字符串,你的意思是反引号`? Spring Data 使用 JPQL,而不是 SQL(除非您明确指定)-en.wikipedia.org/wiki/Java_Persistence_Query_Language 【参考方案1】:

你有两个选择:

对于 HQL,您需要以下内容:

@Query("select * from Visit v where YEARWEEK('v.date') = YEARWEEK(curdate())")

你也可以有本地查询,在这种情况下你需要关注(不是 Spring 应用程序的最佳方式,但可以用于某些情况)

@Query(value = "select * from Visit where YEARWEEK('date') = YEARWEEK(curdate())", nativeQuery = true)

【讨论】:

非常感谢 :) 已解决。【参考方案2】:

@Query 注解允许通过将 nativeQuery 标志设置为 true 来执行本机查询。

@Query("select v from Visit v where YEARWEEK('date') = YEARWEEK(curdate())", nativeQuery = true)

请参阅 spring-data 文档 Using @Query section。

【讨论】:

非常感谢 :) 已解决。

如何在 Spring Boot 中使用 MySql 数据库查询?

】如何在SpringBoot中使用MySql数据库查询?【英文标题】:HowtouseMySqlDatabasequeriesinSpringBoot?【发布时间】:2018-10-1007:18:45【问题描述】:我正在使用SpringBoot创建API。在这个项目中,我使用了springweb、JPA、jstl和MySql作为API的依赖。在... 查看详情

如何在 Spring Boot 中使用复合主键从 MySql 中检索数据

】如何在SpringBoot中使用复合主键从MySql中检索数据【英文标题】:HowtoretrivedatafromMySqlinSpringBootusingcompositeprimarykeys【发布时间】:2021-03-2518:03:27【问题描述】:我正在开发一个简单的SpringBoot项目,以从MySQL表中检索数据,该表将... 查看详情

Spring-Boot,无法使用 spring-data JPA 在 MySql 中保存 unicode 字符串

】Spring-Boot,无法使用spring-dataJPA在MySql中保存unicode字符串【英文标题】:Spring-Boot,Can\'tsaveunicodestringinMySqlusingspring-dataJPA【发布时间】:2017-11-1903:51:28【问题描述】:我的application.properties设置如下:spring.datasource.username=rootspring.d... 查看详情

使用 docker-compose 无法在同一网络中连接 mysql 和 spring boot

】使用docker-compose无法在同一网络中连接mysql和springboot【英文标题】:usingdocker-composeunabletoconnectmysqlandspringbootinsamenetwork【发布时间】:2020-05-0516:45:47【问题描述】:任何帮助将不胜感激。我正在尝试在两个容器中运行mysql和spring... 查看详情

spring-boot,无法使用spring-datajpa在mysql中保存unicode字符串

我的application.properties设置如下:spring.datasource.username=rootspring.datasource.password=rootspring.datasource.url=jdbc:mysql://localhost:3306/dbname?useUnicode=yes&characterEncoding=UTF-8spring.dat 查看详情

使用 mysql 驱动程序使用 mysql 数据库设置 spring-boot 项目

】使用mysql驱动程序使用mysql数据库设置spring-boot项目【英文标题】:Setupspring-bootprojectwithmysqldatabaseusingmysqldriver【发布时间】:2019-06-0302:59:12【问题描述】:MySQL连接器是maven依赖项的一部分,所有数据库属性(如url、用户名、密... 查看详情

管理员如何使用 Spring Boot 锁定/解锁在 mysql 数据库中注册的用户?

】管理员如何使用SpringBoot锁定/解锁在mysql数据库中注册的用户?【英文标题】:Howcanaadminlock/unlockauserregisteredinamysqldatabaseusingspringboot?【发布时间】:2019-09-2416:44:24【问题描述】:我正在开发一个springboot项目,该项目必须有一个... 查看详情

在 Spring Boot JPA 应用程序中使用角色

】在SpringBootJPA应用程序中使用角色【英文标题】:UsingRolesInSpringBootJPAApplication【发布时间】:2018-08-1722:59:20【问题描述】:我目前正在使用SpringBoot在Java中开发Web应用程序。我正在使用的应用程序:MySQL作为数据源用于ORM的休眠J... 查看详情

无法使用spring boot连接到mysql数据库

】无法使用springboot连接到mysql数据库【英文标题】:cantconnecttomysqldatabseusingspringboot【发布时间】:2020-09-0303:55:54【问题描述】:当我在Postman中点击url(http://localhost:8080/pjt/samples)获取json数据时,它显示以下错误。"timestamp":"2020-05-1... 查看详情

如何从 html 表单中获取“日期和时间”输入并使用 Spring Boot 保存到 MySQL 数据库中?

】如何从html表单中获取“日期和时间”输入并使用SpringBoot保存到MySQL数据库中?【英文标题】:Howtoget"DateandTime"inputfromahtmlformandsaveintoMySQLdbusingSpringBoot?【发布时间】:2018-06-1508:11:00【问题描述】:我想做什么:用户将通... 查看详情

使用 Spring Boot Java 端口 8080 上的 Tomcat 失败

】使用SpringBootJava端口8080上的Tomcat失败【英文标题】:Tomcatonport8080failedusingSpringbootJava【发布时间】:2021-01-0410:07:13【问题描述】:我是弹簧靴的初学者。当我运行SpringBoot应用程序时,我遇到了配置为侦听端口8080的Tomcat连接器... 查看详情

我如何使用部署在战争 Spring Boot 应用程序中的 mysql jdbc 驱动程序

】我如何使用部署在战争SpringBoot应用程序中的mysqljdbc驱动程序【英文标题】:HowIcanusethemysqljdbcdriverdeployedinsideawarspringbootapplication【发布时间】:2020-03-0212:27:59【问题描述】:我使用JPA和mysql创建了一个SpringBoot应用程序。当我将... 查看详情

如何使用 Hibernate/Spring boot 将 JSON 文件作为 JSON 数据类型存储在 mysql 中?

】如何使用Hibernate/Springboot将JSON文件作为JSON数据类型存储在mysql中?【英文标题】:HowtostoreJSONfileasaJSONdatatypeinmysqlusingHibernate/Springboot?【发布时间】:2020-05-1311:36:21【问题描述】:我尝试了几种将json文件存储在数据库中的方法... 查看详情

我无法使用 Spring Boot 和 JPA 连接到 mysql 数据库

】我无法使用SpringBoot和JPA连接到mysql数据库【英文标题】:Ican\'tconnecttothemysqldatabasewithSpringBoot&JPA【发布时间】:2018-01-0622:30:02【问题描述】:springboot版本4.3.10mysql版本5.7.181.8版mysql-connector-java版本5.1.38错误5976---[main]o.a.tomcat.j... 查看详情

使用嵌入式 tomcat 未在 Spring Boot 中映射请求

】使用嵌入式tomcat未在SpringBoot中映射请求【英文标题】:Requestarenotbeingmappedinspringbootusingembeddedtomcat【发布时间】:2019-12-1603:33:19【问题描述】:我已经使用MySQL启动了一个新的RESTspringboot应用程序,但我无法使用postman注册用户... 查看详情

如何使用表单创建 mysql 数据库。我正在使用 spring boot

】如何使用表单创建mysql数据库。我正在使用springboot【英文标题】:Howtocreatemysqldatabaseusingaform.iamusingspringboot【发布时间】:2015-07-1001:31:10【问题描述】:我现在正在训练中,我必须从一个表单创建mysql数据库,我必须在其中输... 查看详情

为独立的 Spring Boot 应用程序设置 MySQL 数据库以在另一个设备中使用

】为独立的SpringBoot应用程序设置MySQL数据库以在另一个设备中使用【英文标题】:SettingupMySQLdatabaseforstand-aloneSpringBootapplicationtobeusedinanotherdevice【发布时间】:2018-04-2012:26:20【问题描述】:我正在使用以下方式开发应用程序:弹... 查看详情

可怕的 Java Spring Boot 应用程序未使用 Docker-compose java.net.ConnectException 连接到 MySQL:连接被拒绝

】可怕的JavaSpringBoot应用程序未使用Docker-composejava.net.ConnectException连接到MySQL:连接被拒绝【英文标题】:ThedreadedJavaSpringBootappnotconnectingtoMySQLwithDocker-composejava.net.ConnectException:Connectionrefused【发布时间】:2021-02-2309:38:50【问 查看详情