JdbcTemplate快速入门
JdbcTemplate是Spring的一款用于简化Dao代码的工具包,它底层封装了JDBC技术,特点是简单、方便
核心对象
public class JdbcTemplateTest {
public static void main(String[] args) {
//创建数据源,即内置的数据库连接池
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setPassword("root");
dataSource.setUsername("root");
dataSource.setUrl("jdbc:mysql://localhost:3306/spring2");
//创建jdbcTemplate对象,将数据源交给jdbcTemplate
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
//通过jdbcTemplate操作sql语句
jdbcTemplate.update("insert into account values(null,'张三',2000)");
}
}
Spring的IOC管理JdbcTemplate
public class JdbcTemplateTest {
public static void main(String[] args) {
创建数据源,即内置的数据库连接池
//DriverManagerDataSource dataSource = new DriverManagerDataSource();
//dataSource.setDriverClassName("com.mysql.jdbc.Driver");
//dataSource.setPassword("root");
//dataSource.setUsername("root");
//dataSource.setUrl("jdbc:mysql://localhost:3306/spring2");
创建jdbcTemplate对象,将数据源交给jdbcTemplate
//JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
//通过spring的IOC容器来获取JdbcTemplate对象
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
JdbcTemplate jdbcTemplate = ac.getBean(JdbcTemplate.class);
//通过jdbcTemplate操作sql语句
jdbcTemplate.update("insert into account values(null,'李四',2000)");
}
}
JdbcTemplate实现完整的CRUD
核心方法
int update(); 执行增、删、改语句
List<T> query(); 查询多个
T queryForObject(); 查询一个
RowMapper<>(); ORM映射接口
new BeanPropertyRowMapper<>(); 实现ORM映射封装子类
代码实现
public class JdbcTemplateTest {
public static void main(String[] args) {
创建数据源,即内置的数据库连接池
//DriverManagerDataSource dataSource = new DriverManagerDataSource();
//dataSource.setDriverClassName("com.mysql.jdbc.Driver");
//dataSource.setPassword("root");
//dataSource.setUsername("root");
//dataSource.setUrl("jdbc:mysql://localhost:3306/spring2");
创建jdbcTemplate对象,将数据源交给jdbcTemplate
//JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
//通过spring的IOC容器来获取JdbcTemplate对象
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
JdbcTemplate jdbcTemplate = ac.getBean(JdbcTemplate.class);
//通过jdbcTemplate操作sql语句
//jdbcTemplate.update("insert into account values(null,'李四',2000)");
//JdbcTemplate实现完整的CRUD
//需求:新增一个用户
//jdbcTemplate.update("insert into account value(null,?,?)", "亚索", "2500");
//根据id修改用户
jdbcTemplate.update("update account set money=money-? where id=?", 500, 6);
//根据用户id删除一个用户
//jdbcTemplate.update("delete from account where id =?", 6);
//查询表中所有的用户
//编写sql语句
String sql = "select * from account";
List<Account> accountList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Account.class));
for (Account account : accountList) {
System.out.println(account);
//Account{id=1, name='小明', money=1000.0}
//Account{id=2, name='小花', money=1000.0}
//Account{id=3, name='小王', money=1000.0}
//Account{id=4, name='张三', money=2000.0}
//Account{id=5, name='李四', money=2000.0}
}
//查询一条数据
//编写sql语句
String sql1 = "select * from account where id = ?";
//执行sql语句
Account account = jdbcTemplate.queryForObject(sql1, new BeanPropertyRowMapper<>(Account.class), 4);
System.out.println(account);
//Account{id=4, name='张三', money=2000.0}
}
}
JdbcTemplate整合数据源
pom.xml:
<!--c3p0-->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- 德鲁伊连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<!--dbcp连接池-->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
applicationContext.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">
<!--spring的内置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 依赖注入数据源的连接属性-->
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/spring2"></property>
<property name="password" value="root"></property>
<property name="username" value="root"></property>
</bean>
<!--c3p0连接池-->
<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="password" value="root"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring2"></property>
<property name="user" value="root"></property>
</bean>
<!--德鲁伊-->
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="password" value="root"></property>
<property name="username" value="root"></property>
<property name="url" value="jdbc:mysql://localhost:3306/spring2"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
</bean>
<!--dbcp连接池-->
<bean id="dbcpDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/spring2"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 注入spring内置的数据源-->
<!--<property name="dataSource" ref="dataSource"></property>-->
<!-- 注入c3p0的数据源-->
<!-- <property name="dataSource" ref="c3p0DataSource"></property>-->
<!-- 注入德鲁伊连接池-->
<!--<property name="dataSource" ref="druidDataSource"></property>-->
<!-- 注入dbcp连接池-->
<property name="dataSource" ref="dbcpDataSource"></property>
</bean>
</beans>