Spring 开发第一步(三)Spring与JDBC
《spring in action 3rd》中的前面4章讲解的是Spring的核心,也就是DI/IOC和AOP 。从第5章开始是Spring在企业开发中的各个方面的应用。其实作为笔者从事的企业计算来说,J2EE相关的最常见的内容就是如何持久化了,第5、6章讲的就是这方面的内容。
今天主要学习了Spring与JDBC开发。
一、配置数据源
首先我们需要配置数据源,在设置好context.xml后,我们将JDBC数据源配置到Spring:
<jee:jndi-lookup id="dataSource" jndi-name="/jdbc/spitter" resource-ref="true" /> 对,就这么一句话。并且id为dataSource的这个数据源可以像一个bean那样注入到另一个bean里。
比如注入到下面的DSTestBean
package com.spitter.test; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; public class DSTestBean { @Autowired private DataSource dataSource; public void setDataSource(DataSource dataSource){ this.dataSource = dataSource; } public DataSource getDataSource(){ return this.dataSource; } }
对应的spring配置文件如下
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd"> <!-- Beans declarariona go here --> <context:annotation-config/> <jee:jndi-lookup id="dataSource" jndi-name="/jdbc/spitter" resource-ref="true" /> <bean id="dsTest" class="com.spitter.test.DSTestBean" /> </beans>
注意,由于在DSTestBean中我们对dataSource属性使用了注解注入 @autowired
所以在配置文件里要加入<context:annotation-config/> ,因为Spring 默认是关闭注解装配方式的。
二、使用Spring的template简化JDBC代码
假设有如下简单的表结构
使用原生的JDBC开发一个根据city_code查询city_name的方法,一般有如下代码
Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = ds.getConnection(); pstmt = conn.prepareStatement("select city_name from ta_ds_test where city_code='hz'"); rs = pstmt.executeQuery(); while(rs.next()){ //System.out.println("city_name = "+ rs.getString(1)); cityName = rs.getString(1); } } catch (SQLException e) { }finally{ if(rs!=null) try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(pstmt!=null) try { pstmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(conn!=null) try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
而使用SimpleJdbcTemplate简化后
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate"> <constructor-arg ref="dataSource" /> </bean> <bean id="jdbcTemplateDao" class="com.spitter.test.JdbcTemplateDao" />
@SuppressWarnings("deprecation") @Autowired private SimpleJdbcTemplate jdbcTemplate;public String getCityNameByCode(String cityCode){ return this.jdbcTemplate.queryForObject("select city_name from ta_ds_test where city_code=?", String.class, cityCode);
}
高下立判!
上面的代码仍有一个问题:显然我们需要将配置文件中的jdbcTemplate对象注入到每一个需要使用它的dao中,尽管我们在jdbcTemplateDao使用了@Autowired注解来简化代替在配置文件中注入,但是这也需要在每个dao里添加注解啊。
所以,最佳实践是,让每个dao去继承SimpleJdbcDaoSupport
<bean id="jdbcTemplateDao" class="com.spitter.test.JdbcTemplateDao" p:dataSource-ref="dataSource"/>
public String getCityNameByCode(String cityCode){ return getSimpleJdbcTemplate().queryForObject("select city_name from ta_ds_test where city_code=?", String.class, cityCode); }
这也就不需要在配置文件中去配jdbcTemplate了,当然也不需要去dao里添加注解了。数据源原本是 dataSource -> template ->dao 这样一个注入顺序,现在我们将数据源直接注入dao从SimpleJdbcDaoSupport继承而来的dataSource属性中去( 如上所示 p:dataSource-ref="dataSource" ) 。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步