spring-guide之spring-jdbc

本文通过构建一个使用Spring JDBC的关系数据应用示例,演示如何利用Java 8的新特性进行数据库操作。示例中创建了一个Spring Boot项目,并使用H2数据库进行数据访问,展示了如何查询、插入数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring-guide之spring-jdbc

创建一个关系数据的spring-jdbc
其中将使用jdk 8新特性来撸代码。mark一下java 8的IBM的新特性描述

https://www.ibm.com/developerworks/cn/java/j-lo-jdk8newfeature/
http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html

gradle的配置

创建gradle的配置文件build.gradle、

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar {
    baseName = 'spring-guide'
    version =  '0.1.0'
}

springBoot {
    mainClass = "gs.relational.data.access.Application"
}


repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile(
        "org.springframework.boot:spring-boot-starter-web",
        "org.springframework.boot:spring-boot-starter",
        "org.springframework:spring-web",
        "com.fasterxml.jackson.core:jackson-databind",
        //gs.relational.data.access鍔犲叆
        "org.springframework:spring-jdbc",
        "com.h2database:h2",
        "junit:junit"

    )
    testCompile('org.springframework.boot:spring-boot-starter-test')   
}

代码实现

目标根据first_name查出该数据的last_name。其中数据库是h2database内存test版本。

创建gs.relational.data.access目录。

创建跟数据库的对应实体类,Customer.java

public class Customer {

    private Long id;

    private String firstName, lastName;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

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

}

定义一个spring-boot启动类

@SpringBootApplication
public class Application implements CommandLineRunner {

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

    @Autowired
    JdbcTemplate jdbcTemplate;

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

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

        log.info("create tables");
        jdbcTemplate.execute("drop table customers if exists");
        jdbcTemplate.execute("create table customers(id serial, first_name varchar(265), last_name varchar(265))");

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

        log.info(String.format("splitUpNames: %s", splitUpNames.get(0)));

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

//      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("query for customer records where first_name is 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()));

    }

}

其中用到的java8的新特性,参考IBM的描述

https://www.ibm.com/developerworks/cn/java/j-lo-jdk8newfeature/
http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html

编译并运行

gradlew build;

java -jar build/libs/spring-guide-0.1.0.jar

2016-09-24 00:18:58.189  INFO 10300 --- [           main] gs.relational.data.access.Application    : create tables
2016-09-24 00:18:58.258  INFO 10300 --- [           main] gs.relational.data.access.Application    : splitUpNames: John
2016-09-24 00:18:58.273  INFO 10300 --- [           main] gs.relational.data.access.Application    : insert customer record fo John Woo
2016-09-24 00:18:58.300  INFO 10300 --- [           main] gs.relational.data.access.Application    : insert customer record fo Jeff Dean
2016-09-24 00:18:58.305  INFO 10300 --- [           main] gs.relational.data.access.Application    : insert customer record fo Josh Long
2016-09-24 00:18:58.379  INFO 10300 --- [           main] gs.relational.data.access.Application    : query for customer records where first_name is Josh
2016-09-24 00:18:58.388  INFO 10300 --- [           main] gs.relational.data.access.Application    : gs.relational.data.access.Customer@3a079870
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值