Spring中JDBC的模板的使用
Spring中JDBC的模板的使用
Spring的JDBC的模板
Spring是EE开发的一站式的框架,有EE开发的每层的解决方案。Spring对持久层也提供了解决方案:ORM模块和JDBC的模板。
创建项目,引入jar包:
引入基本开发包:4 + 2。
数据库驱动包。
需要引入spring_aop的jar包,不然报错。
Spring的JDBC模板的jar包。
1、创建表
CREATE TABLE `t_act` (
`no` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`balance` double(8,2) DEFAULT '0.00',
PRIMARY KEY (`no`)
) ENGINE=InnoDB AUTO_INCREMENT=1001 DEFAULT CHARSET=utf8
2、使用JDBC的模板:保存数据
@Test
public void test01() {
// 创建连接池对象
DriverManagerDataSource dataSource = new DriverManagerDataSource();
// 设置连接数据库的基本信息
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/study_test");
dataSource.setUsername("root");
dataSource.setPassword("666666");
// 创建jdbc模板
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.update("insert into t_act values(null, ?, ?)", "王五", 2000d);
}
3、将连接池和模板交给Spring管理
(1)编写Spring的配置文件
<!-- 配置Spring内置的连接池 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 属性注入 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/study_test" />
<property name="username" value="user" />
<property name="password" value="666666" />
</bean>
<!-- 配置Spring的jdbc模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
(2)再使用Jdbc的模板
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:applicationContext.xml")
public class JdbcTest02 {
@Resource(name = "jdbcTemplate")
private JdbcTemplate jdbcTemplate;
@Test
public void test01() {
jdbcTemplate.update("insert into t_act values(null, ?, ?)", "赵六", 2000d);
}
}
4、使用开源的数据库连接池
(1)DBCP的使用
在Spring配置文件中配置DBCP连接池
<!-- 配置DBCP的数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/study_test" />
<property name="username" value="root" />
<property name="password" value="666666" />
</bean>
<!-- 配置Spring的jdbc模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
(2)C3P0的使用
在Spring配置文件中配置c3p0连接池
<!-- 配置C3P0的数据库连接池,注意该类中的属性名称 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/study_test" />
<property name="user" value="root" />
<property name="password" value="666666" />
</bean>
<!-- 配置Spring的jdbc模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
5、抽取数据库连接信息到属性文件
在实际开发中,一般不会把数据库的连接信息直接写在配置文件中。
而是另外写在属性文件中,在配置文件中去读取配置文件中的信息。
(1)定义一个属性文件jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/study_test
jdbc.username=root
jdbc.password=666666
(2)在Spring的配置文件中引入属性文件
第一种:通过一个bean标签引入属性文件(很少使用)
<!-- 第一种:通过bean标签的引入(这种方式很少使用) -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
第二种:通过context标签引入属性文件(常用)
<!-- 第二种:通过context标签的引入 -->
<context:property-placeholder location="classpath:jdbc.properties" />
(3)在连接池中获取属性文件的值
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
(4)最终Spring配置文件内容为
<!-- 第二种:通过context标签的引入 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置C3P0的数据库连接池,注意该类中的属性名称,值读取属性文件 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置Spring的jdbc模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
6、使用JDBC的模板完成CRUD的操作
(1)保存操作
// insert操作:增删改用update(),查询用query()
@Test
public void test01() {
jdbcTemplate.update("insert into t_act values(null, ?, ?)", "赵六", 2000d);
}
(2)删除操作
// delete操作
@Test
public void test02() {
jdbcTemplate.update("delete from t_act where id=?", 1009);
}
(3)修改操作
// update操作
@Test
public void test03() {
jdbcTemplate.update("update t_act set balance = ? where id = ?", 1000d, 1004);
}
(4)查询操作
(01)查询某个属性:
// select操作:查询某一个属性
@Test
public void test04() {
// 查询一个字段
Double balance = jdbcTemplate.queryForObject("select balance from t_act where id=?", Double.class, 1001);
System.out.println(balance); // 2000.0
// 查询统计个数
Long count = jdbcTemplate.queryForObject("select count(*) from t_act", Long.class);
System.out.println(count); // 4
}
(02)查询返回一条记录,封装到对象中:
// 查询一条记录,封装到一个对象中
@Test
public void test05() {
Account account = jdbcTemplate.queryForObject("select * from t_act where id=?", new MyRowMapper(), 1001);
System.out.println(account); // Account{id=1001, name='张三', balance=2000.0}
}
(03)查询多条记录,封装到List集合中:
// 查询多条记录,封装到List集合中
@Test
public void test06() {
List<Account> list = jdbcTemplate.query("select * from t_act", new MyRowMapper());
for (Account a : list) {
System.out.println(a);
}
}
(04)数据封装
public class Account {
private Integer id;
private String name;
private Double balance;
// ......
}
class MyRowMapper implements RowMapper<Account> {
@Override
public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
Account act = new Account();
act.setId(rs.getInt("id"));
act.setName(rs.getString("name"));
act.setBalance(rs.getDouble("balance"));
return act;
}
}
还没有评论,来说两句吧...