【问题标题】:How to connect from STS(spring tool suite) to database(oracle)如何从 STS(spring 工具套件)连接到数据库(oracle)
【发布时间】:2016-07-08 02:31:35
【问题描述】:

我是春天的新人。我正在尝试构建我的第一个 Web 服务,我想连接到数据库、获取数据并将其作为 JSON 返回。

我使用 spring 示例 https://spring.io/guides/gs/rest-service 并稍作更改,这样我可以获得一个战争文件。

war 文件将部署的我的服务器是 Wildfly 8.2.0。

我想连接到 oracle 11g 数据库并运行 SQL 查询。你能帮帮我吗?

我的代码贴在下面。

My project format in eclipse

Application.java:

package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {


@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application.class);
}


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


}

问候.java:

package hello;

public class Greeting {

private final long id;
private final String content;

public Greeting(long id, String content) {
    this.id = id;
    this.content = content;
}

public long getId() {
    return id;
}

public String getContent() {
    return content;
}
}

GreetingController.java:

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class GreetingController {


private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();


@RequestMapping(value="/greeting",method=RequestMethod.GET)
public List<Greeting> greeting(@RequestParam(value="name", defaultValue="World") String name,
                               @RequestParam(value="content", defaultValue="World") String content,HttpServletResponse response) {
    List<Greeting> list_greet = new ArrayList<Greeting>();
    list_greet.add(new Greeting(counter.incrementAndGet(),
                                String.format(template, name)));
    list_greet.add(new Greeting(counter.incrementAndGet(),
                                String.format(template, content)));
    list_greet.add(new Greeting(counter.incrementAndGet(),
                                String.format(template, name)));
    list_greet.add(new Greeting(counter.incrementAndGet(),
                                String.format(template, name)));
    //to have webservice work,beacause cors cut it out.
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
    response.setHeader("Access-Control-Max-Age", "3600");
    response.setHeader("Access-Control-Allow-Headers", "x-requested-with");


    return list_greet;
}
}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0</version>
<packaging>war</packaging>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.3.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>

<properties>
    <java.version>1.8</java.version>
</properties>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
</pluginRepositories>
</project>

提前致谢!

【问题讨论】:

    标签: java database spring oracle spring-mvc


    【解决方案1】:

    您可以使用多种不同的方式来归档您的目标,例如:

    【讨论】:

      猜你喜欢
      • 2020-08-22
      • 1970-01-01
      • 2014-11-01
      • 2019-06-15
      • 2020-12-26
      • 2015-05-29
      • 2017-05-18
      • 2013-04-26
      • 2010-09-10
      相关资源
      最近更新 更多