spring 使用dataSource、JdbcTemplate简单化数据库操作
spring 使用dataSource、JdbcTemplate简化数据库操作
我们在Java代码中使用jdbc中,总是会有很多重复的代码区,可能真正操作数据库的代码只占了20%。作为一名程序员应该简化这些需要重复的部分很着重于核心的数据库操作部分。
Spring提供的jdbcTemplate很好的帮我们解决了问题,让我们真正的只用关心编写操作数据库的代码。
在spring中使用数据源是很好的方案,第一步我们来配置一个c3p0数据源在spring 的配置中:
<!--从类路径下导入jdbc.properties 文件 配置信息放置于其中-->
<util:properties id="jdbc" location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="jdbcUrl" value="#{jdbc.url}" />
<property name="driverClass" value="#{jdbc.driverClassName}" />
<property name="user" value="#{jdbc.username}" />
<property name="password" value="#{jdbc.password}" />
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="100" />
<!--连接池中保留的最小连接数。-->
<property name="minPoolSize" value="1" />
<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="10" />
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="30" />
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="5" />
<!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements
属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0-->
<property name="maxStatements" value="0" />
<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="60" />
<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
<property name="acquireRetryAttempts" value="30" />
<!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效
保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试
获取连接失败后该数据源将申明已断开并永久关闭。Default: false-->
<property name="breakAfterAcquireFailure" value="true" />
<!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的
时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable
等方法来提升连接测试的性能。Default: false -->
<property name="testConnectionOnCheckout" value="false" />
</bean>
我们使用了Spring 提供的<util:properties/>将jdbc配置文件导入为一个bean 并使用#{...}的方式导入
Jdbc.properties文件提供jdbc连接信息:
url=jdbc:MySQL:///testdb
driverClassName=com.mysql.jdbc.Driver
username=root
password=20080808
接下来就是最重要的一步了,配置好jdbc模板(jdbcTemplate)
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource"></constructor-arg>
</bean>
将数据源以构造注入的方式注入
这样我们就可以在代码中通过getBean的方式获取了。
JdbcTemplate template = beanContext.getBean("jdbcTemplate", JdbcTemplate.class);
Persion persion = (Persion) template.queryForObject("select * from t_persion where name=?", new Object[]{"zhao"},
new RowMapper<Persion>() {
@Override
public Persion mapRow(ResultSet rs, int rowNum) throws SQLException {
Persion p = new Persion();
p.setName(rs.getString("name"));
p.setAge(rs.getInt("age"));
p.setId(rs.getInt("id"));
return p;
}
});
System.out.println(persion);
对于jdbcTemplate的操作api 官方文档又详细的解释
jdbcTemplate是线程安全的
我们可以直接将Dao作为一个bean 将jdbcTemplate注入其中
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource"></constructor-arg>
</bean>
<bean id="persionDao" class="cn.zhaoyuening.dao.PersionDao">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
persionDao代码:
public class PersionDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void addPersion(Persion persion){...}
public void removePersionById(int id){...}
public Persion getPersionByName(String name) {
Persion persion = (Persion) jdbcTemplate.queryForObject("select * from t_persion where name=?", new Object[]{"zhao"},
new RowMapper<Persion>() {
@Override
public Persion mapRow(ResultSet rs, int rowNum) throws SQLException {
Persion p = new Persion();
p.setName(rs.getString(name));
p.setAge(rs.getInt("age"));
p.setId(rs.getInt("id"));
return p;
}
});
return persion;
}
}