您的位置:首页 > 编程语言 > Java开发

第一个 Spring Boot 程序 : 使用 spring jdbc 访问关系型数据库

2016-12-26 14:56 706 查看
文档地址:http://blog.csdn.net/lzlovez/article/details/53761091

pom.xml

<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>com.zlf</groupId>
<artifactId>web</artifactId>
<version>0.0.1-SNAPSHOT</version>

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

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

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
</dependencies>

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

</project>

Customer.java
package com.lzf.jdbc;

public class Customer {

private long id;
private String firstName, lastName;

public Customer(long id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}

@Override
public String toString() {
return String.format("Customer[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);
}

// getters & setters omitted(略) for brevity(简洁)
}


Application.java
package com.lzf.jdbc;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@SpringBootApplication
public class Application implements CommandLineRunner {

private static final Logger log = LoggerFactory.getLogger(Application.class);

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

@Autowired
JdbcTemplate jdbcTemplate;

@Override
public void run(String... strings) throws Exception {

log.info("Creating tables");

jdbcTemplate.execute("drop table customers if exists");

jdbcTemplate.execute("create table customers(" + "id serial, first_name varchar(255), last_name varchar(255))");

List<Object[]> splitUpNames = Arrays.asList("John Woo", "Jeff Dean", "Josh Bloch", "Josh Long").stream().map(name -> name.split(" ")).collect(Collectors.toList());

splitUpNames.forEach(name -> log.info(String.format("Inserting customer record for %s %s", name[0], name[1])));

jdbcTemplate.batchUpdate("insert into customers(first_name, last_name) values (?,?)", splitUpNames);

log.info("Querying for customer records where first_name = 'Josh':");

jdbcTemplate.query("select id, first_name, last_name from customers where first_name = ?", new Object[] { "Josh" }, (rs, rowNum) -> new Customer(rs.getLong("id"), rs.getString("first_name"), rs.getString("last_name"))).forEach(customer -> log.info(customer.toString()));

jdbcTemplate.queryForList("select * from customers").forEach(obj -> log.info(obj.toString()));

}
}


遇到的问题:
pom.xml 文件里面  <parent> 引用的 version 是 1.4.3.RELEASE , 之前自己用的是 1.4.2.RELEASE 导致 Application.java 里面的最后一个查询提示包不对,未找到方法

java8 的新用法看起来很厉害的样子,也是去补了下课

结果:

2016-12-26 14:55:36.993 INFO 2472 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)

2016-12-26 14:55:36.998 INFO 2472 --- [ main] com.lzf.jdbc.Application : Creating tables

2016-12-26 14:55:37.326 INFO 2472 --- [ main] com.lzf.jdbc.Application : Inserting customer record for John Woo

2016-12-26 14:55:37.326 INFO 2472 --- [ main] com.lzf.jdbc.Application : Inserting customer record for Jeff Dean

2016-12-26 14:55:37.326 INFO 2472 --- [ main] com.lzf.jdbc.Application : Inserting customer record for Josh Bloch

2016-12-26 14:55:37.326 INFO 2472 --- [ main] com.lzf.jdbc.Application : Inserting customer record for Josh Long

2016-12-26 14:55:37.373 INFO 2472 --- [ main] com.lzf.jdbc.Application : Querying for customer records where first_name = 'Josh':

2016-12-26 14:55:37.382 INFO 2472 --- [ main] com.lzf.jdbc.Application : Customer[id=3, firstName='Josh', lastName='Bloch']

2016-12-26 14:55:37.382 INFO 2472 --- [ main] com.lzf.jdbc.Application : Customer[id=4, firstName='Josh', lastName='Long']

2016-12-26 14:55:37.387 INFO 2472 --- [ main] com.lzf.jdbc.Application : {ID=1, FIRST_NAME=John, LAST_NAME=Woo}

2016-12-26 14:55:37.387 INFO 2472 --- [ main] com.lzf.jdbc.Application : {ID=2, FIRST_NAME=Jeff, LAST_NAME=Dean}

2016-12-26 14:55:37.387 INFO 2472 --- [ main] com.lzf.jdbc.Application : {ID=3, FIRST_NAME=Josh, LAST_NAME=Bloch}

2016-12-26 14:55:37.387 INFO 2472 --- [ main] com.lzf.jdbc.Application : {ID=4, FIRST_NAME=Josh, LAST_NAME=Long}

2016-12-26 14:55:37.389 INFO 2472 --- [ main] com.lzf.jdbc.Application : Started Application in 24.61 seconds (JVM running for 25.093)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring-boot