10- JdbcTemplate
10.1 JdbcTemplate概述
JdbcTemplate是spring提供的一个模板类,它是对jdbc的封装。用于支持持久层的操作。它的特点是:简单、方便
10.2 入门案例
10.2.1 SQL数据表结构
-- 创建账户表
create table account(
id int primary key auto_increment,
name varchar(40),
money float
)ENGINE=InnoDB character set utf8 collate utf8_general_ci;
-- 初始化新增三个账户
insert into account(name,money) values('kebe',1000);
insert into account(name,money) values('curry',1500);
insert into account(name,money) values('james',2000);
select * from account;
10.2.2 项目目录
10.2.3 pom.xml
<!--依赖版本-->
<properties>
<!-- spring版本 -->
<spring.version>5.0.2.RELEASE</spring.version>
</properties>
<!--spring jdbc依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
<!--lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
</dependency>
10.2.4 案例代码
package cn.guardwhy.jdbc;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
/**
* spring JdbcTemplate入门案例
*/
public class JdbcTemplateDemo {
public static void main(String[] args) {
// 1.创建JdbcTemplate对象
JdbcTemplate jdbcTemplate = new JdbcTemplate();
// 2.设置数据源对象
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/spring");
dataSource.setUsername("root");
dataSource.setPassword("root");
jdbcTemplate.setDataSource(dataSource);
// 3.添加账户信息
jdbcTemplate.update("insert into account(name,money) values('Duncan',2500)");
}
}
10.3 IOC管理JdbcTemplate
10.3.1 bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置JdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入数据源对象-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置数据源对象-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!--注入连接数据库-->
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/spring"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
</beans>
10.3.2 案例代码
package cn.guardwhy.jdbc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* spring JdbcTemplate入门案例
*/
public class JdbcTemplateDemo {
public static void main(String[] args) {
// 1.加载spring配置文件,创建spring IOC容器
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:bean.xml");
// 2.从spring IOC容器中,获取JdbcTemplate
JdbcTemplate jdbcTemplate = (JdbcTemplate) context.getBean("jdbcTemplate");
// 3.添加账户信息
jdbcTemplate.update("insert into account(name,money) values('Gasol',3500)");
}
}
10.4 实现完整CRUD操作
10.4.1 项目目录
10.4.2 持久层(Dao)
- 账户实体类
package cn.guardwhy.domain;
/**
* 账户实体类
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Account {
private Integer id;
private String name;
private Float money;
}
10.4.3 执行操作
package cn.guardwhy.jdbc;
import cn.guardwhy.domain.Account;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
/**
* spring JdbcTemplate实现完整CRUD操作
*/
public class JdbcTemplateCRUD {
public static void main(String[] args) {
// 1.加载spring配置文件,创建spring IOC容器
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:bean.xml");
// 2.从spring IOC容器中,获取JdbcTemplate
JdbcTemplate jdbcTemplate = (JdbcTemplate) context.getBean("jdbcTemplate");
// 3.查询多行
List<Account> list = jdbcTemplate.query("select id,name,money from account",
new RowMapper<Account>() {
@Override
public Account mapRow(ResultSet rs, int index) throws SQLException {
// 创建账户对象
Account account = new Account();
account.setId(rs.getInt("id"));
account.setName(rs.getString("name"));
account.setMoney(rs.getFloat("money"));
return account;
}
});
// 4.打印结果集
for(Account account:list){
System.out.println(account);
}
// 5.查询一行一列
System.out.println("查询一行一列-------");
Integer accountNum = jdbcTemplate.queryForObject("select count(*) from account",
Integer.class);
System.out.println("当前账户数量:" + accountNum);
}
}
10.4.4 执行结果
10.4.5 自定义RowMapper
package cn.guardwhy.resources;
import cn.guardwhy.domain.Account;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
public class AccountRowMapper implements RowMapper<Account> {
/**
* 结果集映射的方法:
* 结果集中的每一行记录,都会调用一次该方法
*/
public Account mapRow(ResultSet rs, int index) throws SQLException{
// 创建账户对象
Account account = new Account();
account.setId(rs.getInt("id"));
account.setName(rs.getString("name"));
account.setMoney(rs.getFloat("money"));
return account;
}
}
10.4.6 执行操作
package cn.guardwhy.jdbc;
import cn.guardwhy.domain.Account;
import cn.guardwhy.resources.AccountRowMapper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
/**
* spring JdbcTemplate实现完整CRUD操作
*/
public class JdbcTemplateCRUD {
public static void main(String[] args) {
// 1.加载spring配置文件,创建spring IOC容器
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:bean.xml");
// 2.从spring IOC容器中,获取JdbcTemplate
JdbcTemplate jdbcTemplate = (JdbcTemplate) context.getBean("jdbcTemplate");
// 3.使用自定义的RowMapper
List<Account> list = jdbcTemplate.query("select * from account",
new AccountRowMapper());
// 4.打印结果集
for(Account account:list){
System.out.println(account);
}
// 5.查询一行一列
System.out.println("查询一行一列-------");
Integer accountNum = jdbcTemplate.queryForObject("select count(*) from account",
Integer.class);
System.out.println("当前账户数量:" + accountNum);
}
}
10.5 JdbcTemplate整合数据源
10.5.1 常见数据源
#在使用JdbcTemplate的时候,需要设置一个数据源对象,才能完成数据库操作
#常见的数据源:
内置数据源:
org.springframework.jdbc.datasource.DriverManagerDataSource
c3p0数据源:
com.mchange.v2.c3p0.ComboPooledDataSource
dbcp数据源:
org.apache.commons.dbcp.BasicDataSource
druid数据源:
com.alibaba.druid.pool.DruidDataSource
10.5.2 整合内置数据源
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置JdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入数据源对象-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置数据源对象-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!--注入连接数据库的四个基本要素-->
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/1_spring"></property>
<property name="username" value="root"></property>
<property name="password" value="admin"></property>
</bean>
</beans>
10.5.3 整合c3p0数据源
10.5.3.1 项目结构
10.5.3.2 配置pom.xml
<!--c3p0版本-->
<c3p0.version>0.9.5</c3p0.version>
<!--c3p0依赖-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0.version}</version>
</dependency>
10.5.3.3 配置bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置JdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入数据源对象-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置数据源(c3p0)对象-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--注入连接数据库-->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/spring"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
</beans>
10.5.3.4 执行结果
10.5.4 整合druid数据源
10.5.4.1 项目结构
10.5.4.2 配置pom.xml
<!--druid版本-->
<druid.version>1.0.29</druid.version>
<!--druid依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
10.5.4.3 配置bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置JdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入数据源对象-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置数据源对象(druid)-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!--注入连接数据库-->
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/spring"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
</beans>