Spring的JDBC模板入门
- 环境搭建
-
导入jar包
image.png
-
创建一个测试类
public class Demo1 {
@Test
public void run1() {
// JDBC模板的基本使用
// 1.使用 Spring内置的连接池
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/SSH_01?useUnicode=true;characterEncoding=utf8");
dataSource.setUsername("root");
dataSource.setPassword("123456");
// 2. 使用JDBC模板 操作数据
JdbcTemplate template = new JdbcTemplate(dataSource);
template.update("insert into values(?, ?, ?)", null, "jake", 1000);
}
}
对于一个项目不同模块都有Dao,我们不需要重复写JDBC模板设置连接池。我们需要将连接池交给Spring管理。然后将连接池注入Spring的JDBC模板。

将连接池交给Spring管理
内置连接池配置
<!-- 配置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/SSH_01?useUnicode=true;characterEncoding=utf8"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
将连接池注入JDBC模板
<!-- 将内置连接池注入JDBC模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
编写测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo1 {
@Resource(name="jdbcTemplate")
private JdbcTemplate jdbcTemplate;
@Test
public void run2() {
jdbcTemplate.update("insert into t_account values(?, ?, ?)", null, "jake", 1000);
}
}
DBCP连接池配置
导入jar包

<!-- dbcp连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/spring_day03?useUnicode=true&characterEncoding=utf8"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
C3P0连接池配置
导入jar包

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring_day03?useUnicode=true&characterEncoding=utf8"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
JDBC模板CRUD
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo1 {
@Resource(name="jdbcTemplate")
private JdbcTemplate jdbcTemplate;
/*
* 查询所有数据
*/
@Test
public void run6() {
// 查询所有数据
List<User> users = jdbcTemplate.query("select *from t_account", new MyRowMapper());
for (User user : users) {
System.out.println(user);
}
}
/*
* 查询一条数据
*/
@Test
public void run5() {
// 查询一条数据
User user = jdbcTemplate.queryForObject("Select *from t_account where id=?", new MyRowMapper() ,5);
System.out.println(user);
}
/*
* 删除数据
*/
@Test
public void run4() {
// 删除数据
jdbcTemplate.update("delete from t_account where id>?", 14);
}
/*
* 修改数据
*/
@Test
public void run3() {
// 修改
jdbcTemplate.update("update t_account set name=? where id=?", "rose", 13);
}
/*
* 插入数据
*/
@Test
public void run2() {
// 添加
jdbcTemplate.update("insert into t_account values(?, ?, ?)", null, "jake", 1000);
}
@Test
public void run1() {
// JDBC模板的基本使用
// 1.使用 Spring内置的连接池
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/spring_day03?useUnicode=true&characterEncoding=utf8");
dataSource.setUsername("root");
dataSource.setPassword("123456");
// 2. 使用JDBC模板 操作数据
JdbcTemplate template = new JdbcTemplate(dataSource);
template.update("insert into t_account values(?, ?, ?)", null, "jake", 1000);
}
}
class MyRowMapper implements RowMapper<User>{
@Override
public User mapRow(ResultSet res, int arg1) throws SQLException {
User u = new User();
u.setId(res.getInt("id"));
u.setName(res.getString("name"));
u.setMoney(res.getDouble("money"));
return u;
}
}
网友评论