简介
Spring JdbcTemplate类是Spring提供的简化数据库操作的一个类,这个类使用了模板方法模式,可以减少一些重复代码。这里主要演示一下 JdbcTemplate
的使用。
完整的代码看这里:spring_jdbc_01
创建数据库
第一步是创建数据库。下面的SQL语句创建两个表。
create table A(
`id` INT (255) not null,
primary key (`id`)
);
create table B(
`id` INT (255) not null,
primary key (`id`)
);
添加依赖
一共需要加入四个依赖:spring-context
、spring-jdbc
、commons-dbcp
、mysql-connector
等,完整的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.lcl</groupId>
<artifactId>springlearning</artifactId>
<