简体   繁体   English

如何在 Spring Boot 中创建一个在运行时返回整数的 SQL 查询?

[英]How to make an SQL Query in Spring Boot that returns an Integer at runtime?

I've been trying to figure out a way to make an SQL Query in my spring boot application and have it return an integer type to be used later.我一直在想办法在我的 spring 启动应用程序中创建一个 SQL 查询,并让它返回一个整数类型以供以后使用。 I'll be upfront and say I have no idea how Spring Boot works and am trying to get a quick app running for work, so please excuse my ignorance.我会坦率地说我不知道​​ Spring Boot 是如何工作的,我正在尝试让一个快速的应用程序运行起来,所以请原谅我的无知。

Currently what I'm trying to do is have a user enter in details into a webpage and have that info be sent as an SQL request to a server, then return the result as an integer that I can display on a results page.目前我正在尝试做的是让用户在网页中输入详细信息,并将该信息作为 SQL 请求发送到服务器,然后将结果作为可以显示在结果页面上的整数返回。 I have the webpage components of the program working but I can't figure out how to get an SQL query sent.我让程序的网页组件正常工作,但我不知道如何发送 SQL 查询。 I've scoured the internet for answers but all the questions/answers I've been finding are for Entity models, mappings, or etc. I'm not trying to re-create the database objects locally in my program I'm trying to do an SQL query in the background get a result and display it.我已经在互联网上搜索了答案,但我找到的所有问题/答案都是针对实体模型、映射等的。我不是在尝试在我的程序中本地重新创建数据库对象在后台做一个SQL查询得到结果并显示出来。 How would I do that?我该怎么做?

Currently this is what I have for the function, but when I try to use it I get a null pointer exception for the jdbcTemplate .目前,这是我对该函数所拥有的,但是当我尝试使用它时,我得到了jdbcTemplate的空指针异常。

@Autowired
private JdbcTemplate jdbcTemplate;


public int getTotalResults(String startDate, String endDate)
{

    String sql = "SELECT COUNT (DISTINCT object) FROM place WHERE property >= '";
    sql += Utility.parseDateToQueryDate(startDate);
    sql += "' AND planning_test_case.modified < '";
    sql += Utility.parseDateToQueryDate(endDate);
    sql += "'";

    if(jdbcTemplate != null) return (int)jdbcTemplate.queryForObject(sql, int.class);
    else return -1;
}

Main Class:主要类:

@SpringBootApplication
public class Application{

    public static void main(String[] args) throws Exception {
         SpringApplication.run(ArtifactCountingToolApplication.class, args);
    }
}

So from the function what I want to do is get the count of objects from the database given the dates and return the result as an integer.因此,从该函数中,我想要做的是从数据库中获取给定日期的对象计数,并将结果作为整数返回。 There is something probably really simple I'm forgetting but I'm at a loss as to what to do.有些事情可能真的很简单,我忘记了,但我不知道该怎么做。 There is more background classes for the web controller but I don't think its relevant to this post. Web 控制器有更多的背景类,但我认为它与这篇文章无关。

You need to configure datasource first.您需要先配置数据源。 For instance for H2 database:例如对于 H2 数据库:

spring.datasource.url=jdbc:h2:file:C:/temp/test 
spring.datasource.username=sa 
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

Read more in official documentation - https://docs.spring.io/spring-boot/docs/2.1.11.RELEASE/reference/html/boot-features-sql.html在官方文档中阅读更多内容 - https://docs.spring.io/spring-boot/docs/2.1.11.RELEASE/reference/html/boot-features-sql.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM