利⽤springboot使⽤JdbcTemplate连接数据库
l⽂件配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId&st</groupId>
<artifactId>jdbc</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<!--引⼊jdbc⽀持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--引⼊嵌⼊式数据库⽀持h2,springboot提供⾃动配置的嵌⼊式数据库有h2,hsql,derby,即不需要提供任何连接配置就能使⽤-->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我们在pom⽂件⾥可以看到,com.h2database这个库起作⽤的范围是runtime,也就是说,当应⽤程序启动时,如果Spring Boot在classpath下检测到org.h2.Driver的存在,会⾃动配置H2数据库连接。现在启动应⽤程序来观察,以验证我们的想法。
2.实体类Customer.java⽂件
/**
* Created by guanguan on 17/6/1.
*/
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);    }
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;
jdbctemplate查询一条数据}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
3.Application.java⽂件,为应⽤程序的⼊⼝⽂件,负责程序启动以及⼀些基础性的⼯作。
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.JdbcTemplate;
/**
* Created by guanguan on 17/6/1.
*/
@SpringBootApplication
public class Application  implements CommandLineRunner{
private static final Logger log = Logger(Application.class);
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
@Autowired
JdbcTemplate jdbcTemplate;
@Override
public void strings) throws Exception{
log.info("creating tables;");
List<Object[]> splitUpNames = Arrays.asList("John Woo", "Jeff Dean", "Josh Bloch", "Josh Long").stream()
.map(name -> name.split(" "))
.List());
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 Long("id"), rs.getString("first_name"), rs.getString("last_name"))
).forEach(customer -> log.String()));
}
}
运⾏结果:
2017-06-02 10:40:54.671  INFO 13599 --- [          main] hello.Application                        : creating tables---1;
2017-06-02 10:40:54.845  INFO 13599 --- [          main] hello.Application                        : Inserting customer record for John,Woo
2017-06-02 10:40:54.845  INFO 13599 --- [          main] hello.Application                        : Inserting customer record for Jeff,Dean
2017-06-02 10:40:54.845  INFO 13599 --- [          main] hello.Application                        : Inserting customer record for Josh,Bloch
2017-06-02 10:40:54.845  INFO 13599 --- [          main] hello.Application                        : Inserting customer record for Josh,Long
2017-06-02 10:40:54.890  INFO 13599 --- [          main] hello.Application                        : Querying for customer records where first_name='Josh':
2017-06-02 10:40:54.896  INFO 13599 --- [          main] hello.Application                        : Customer[id=3,firstName='Josh',lastName='Bloch']
2017-06-02 10:40:54.896  INFO 13599 --- [          main] hello.Application                        : Customer[id=4,firstName='Josh',lastName='Long']
2017-06-02 10:40:54.898  INFO 13599 --- [          main] hello.Application                        : Started Application in 2.847 seconds (JVM running for 3.539)
2017-06-02 10:40:54.901  INFO 13599 --- [      Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing t.annotation.Annotation 2017-06-02 10:40:54.907  INFO 13599 --- [      Thread-2] o.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
Process finished with exit code 0
==============================================================================3.如果是⾃定义
的数据库
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
连接⽣产数据库/src/main/l⽂件
spring:
datasource:
url: jdbc:mysql://127.0.0.1/test
username: root
password: 123456
driverClassName: sql.jdbc.Driver

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。