介绍
这篇文章将为大家详细讲解有关弹簧引导使用JDBC获取相关数据的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
<强>什么是JDBC 强>
Java数据库连接是一种用于执行SQL语句的Java API,与数据库建立连接,发送操作数据库的语句并处理结果。
<强>弹簧引导使用JDBC 强>
增加依赖
修改砰的一声。xml:将dependecies修改为如下两个
& lt; dependencies> ,& lt; dependency> & lt;才能groupId> org.springframework.boot</groupId> & lt;才能artifactId> spring-boot-starter-jdbc</artifactId> ,& lt;/dependency> ,& lt; dependency> & lt;才能groupId> com.h3database</groupId> & lt;才能artifactId> h3</artifactId> ,& lt;/dependency> ,& lt;/dependencies>
创建客户。java类
package com.example.kane.Model; public class  Customer { ,private long id; ,private String firstName、lastName; ,public 客户(long id, String firstName,, String lastName), {=,this.id id;=,,this.firstName  firstName;=,this.lastName 姓; ,} ,@Override ,public String  toString (), { ,return String.format ( “才能客户[id=% d, firstName=& # 39; % & # 39;,, lastName=& # 39; % & # 39;]“, id、才能,firstName,, lastName); ,} ,//getters ,, setters omitted for 简洁 }
修改应用类
package com.example.kane; 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.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.scheduling.annotation.EnableScheduling; import java.util.Arrays; import 并不知道; import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.client.RestTemplate; import com.example.kane.Model.Customer; @SpringBootApplication//@EnableScheduling public class  RestfulWebService1Application implements  CommandLineRunner { , ,private static  final Logger  log =, LoggerFactory.getLogger (RestfulWebService1Application.class); ,public static  void 主要(String 参数[]),{ ,SpringApplication.run (RestfulWebService1Application.class, args); ,} ,@ autowired ,JdbcTemplate jdbcTemplate; ,@Override ,public void 运行(字符串…,字符串),throws Exception { ,log.info (“Creating tables"); ,jdbcTemplate.execute (“DROP  TABLE customers IF EXISTS"); ,jdbcTemplate.execute (“CREATE  TABLE 客户(“+ “才能id 串行,first_name VARCHAR (255), last_name VARCHAR (255))“); ,//Split up 从而array of whole names into an array of 第一/last 名字 对象[],List<祝辞,splitUpNames =, arrays . aslist (“John  Woo",,“Jeff Dean",,“Josh Bloch",,“Josh Long") .stream () ,,. map (name →, name.split (“,“)) .collect才能(Collectors.toList ()); ,//Use a  Java 8, stream 用print out each tuple of 从而列表 ,splitUpNames.forEach (name →, log.info (String.format (“Inserting  customer record for % s % s",,名字[0],[1]))); ,//Uses JdbcTemplate& # 39; s batchUpdate operation 用bulk load 数据 ,jdbcTemplate.batchUpdate (“INSERT  INTO 客户(first_name、, last_name), VALUES (? ?)“,, splitUpNames); ,log.info (“Querying  for customer records where first_name =, & # 39; josh # 39;:“); ,jdbcTemplate.query ( “SELECT id、才能,first_name、, last_name 得到customers WHERE first_name ,=, ?“, new 对象[],{,“Josh",}, ,,(rs, rowNum),→, new 客户(rs.getLong (“id"), rs.getString (“first_name"), rs.getString (“last_name")) ,).forEach (customer →, log.info (customer.toString ())); ,} }春天的引导使用JDBC获取相关数据的示例分析