Ich habe versucht, Spring JDBC von SpringBoot zu verwenden, daher werde ich das Verfahren für mich selbst zusammenfassen.
macOS Sierra 10.12.3 postgres (PostgreSQL) 10.4
$ brew install postgresql
$ postgres -D /usr/local/var/postgres #Anlaufen
$ createuser -P leonis_sk #Benutzer erstellt
Enter password for new role:
Enter it again:
$ createdb db-example -O leonis_sk #erstelle db
$ psql -U leonis_sk db-example #DB-Verbindung
psql (10.4)
Type "help" for help.
db-example=> create table users (
db-example(> id varchar(20),
db-example(> name varchar(30)
db-example(> );
CREATE TABLE
db-example=> \d
List of relations
Schema | Name | Type | Owner
--------+-------+-------+-----------
public | users | table | leonis_sk
(1 row)
db-example=> select * from users;
id | name
----+------
(0 rows)
db-example=> insert into users values('A001', 'alex'), ('A002', 'betty'), ('A003', 'carol');
INSERT 0 3
db-example=> select * from users;
id | name
------+-------
A001 | alex
A002 | betty
A003 | carol
(3 rows)
db-example=> \q
https://start.spring.io/ Auf der oben genannten Site können Sie eine Vorlage für das Spring Boot-Projekt mit einem Minimum an Informationen erstellen.
Sie können die Zip-Datei mit Projekt generieren herunterladen. Entpacken Sie und gehen Sie zum Eclipse-Arbeitsbereich. Importiert als Maven-Projekt.
Bearbeiten Sie application.properties
src/main/resources/application.properties
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/db-example
spring.datasource.username=leonis_sk
spring.datasource.password=password
RestController Erstellen Sie TestController.java wie unten
java:com.example.demo.TestController.java
package com.example.demo;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(path="/jdbc/sample")
public class TestController {
private static final Logger LOG = LoggerFactory.getLogger(TestController.class);
@Autowired
JdbcTemplate jdbcTemplate;
@RequestMapping(path="/users", method=RequestMethod.GET)
public String index() {
List<Map<String,Object>> list;
list = jdbcTemplate.queryForList("select * from users");
return list.toString();
}
@RequestMapping(path="/users/{id}", method=RequestMethod.GET)
public String read(@PathVariable String id) {
List<Map<String,Object>> list;
list = jdbcTemplate.queryForList("select * from users where id = ?", id);
return list.toString();
}
}
$ pwd
/path/to/eclipse-workspace/demo
$ mvn spring-boot:run
Ich dachte, es wäre Zeit zu beginnen, aber BUILD SUCCESS kam heraus und hörte auf.
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< com.example:demo >--------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] >>> spring-boot-maven-plugin:1.5.13.RELEASE:run (default-cli) > test-compile @ demo >>>
[WARNING] The POM for org.objenesis:objenesis:jar:2.1 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ demo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/LEON/eclipse-workspace/demo/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ demo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< spring-boot-maven-plugin:1.5.13.RELEASE:run (default-cli) < test-compile @ demo <<<
[INFO]
[INFO]
[INFO] --- spring-boot-maven-plugin:1.5.13.RELEASE:run (default-cli) @ demo ---
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.13.RELEASE)
2018-05-14 01:49:38.701 INFO 8906 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on LEON-Mac.local with PID 8906 (/Users/LEON/eclipse-workspace/demo/target/classes started by LEON in /Users/LEON/eclipse-workspace/demo)
2018-05-14 01:49:38.707 INFO 8906 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2018-05-14 01:49:38.795 INFO 8906 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5901caa1: startup date [Mon May 14 01:49:38 JST 2018]; root of context hierarchy
2018-05-14 01:49:41.108 INFO 8906 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-05-14 01:49:41.127 INFO 8906 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 3.122 seconds (JVM running for 7.238)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.845 s
[INFO] Finished at: 2018-05-14T01:49:41+09:00
[INFO] ------------------------------------------------------------------------
2018-05-14 01:49:41.132 INFO 8906 --- [ Thread-3] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@5901caa1: startup date [Mon May 14 01:49:38 JST 2018]; root of context hierarchy
2018-05-14 01:49:41.134 INFO 8906 --- [ Thread-3] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
Als ich mich fragte "?", Wurden ähnliche Fälle ? utm_medium = organisch & utm_source = google_rich_qa & utm_campaign = google_rich_qa) wurde ebenfalls gefunden, aber ich bin mir nicht sicher. Nachdem ich verschiedene Dinge ausprobiert hatte, änderte ich die übergeordnete Version von pom.xml von 1.5.13 auf 1.5.12 und es funktionierte aus irgendeinem Grund.
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.12.RELEASE</version> <!-- <version>1.5.13.RELEASE</version> -->
<relativePath /> <!-- lookup parent from repository -->
</parent>
Ich werde die Ursache später und als nächstes untersuchen.
$ curl http://localhost:8080/jdbc/sample/users
[{id=A001, name=alex}, {id=A002, name=betty}, {id=A003, name=carol}]%
$ curl http://localhost:8080/jdbc/sample/users/A001
[{id=A001, name=alex}]%
Wenn ich mit dem Befehl curl darauf zugreife, kann ich es anscheinend richtig abrufen. Trotzdem ist Spring Boot einfach.
Recommended Posts