先上一段简单示例程序员
public class MyTemplate { private DataSource dataSource; public DataSource getDataSource() { return dataSource; } public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } public void insert(String sql) throws SQLException{ Connection conn = this.dataSource.getConnection(); Statement stmt = conn.createStatement(); stmt.executeUpdate(sql); stmt.close(); conn.close(); } }
Dao类spring
public class PersonDao extends MyTemplate{ public void savePerson() throws Exception{ this.insert("insert into person(pid,pname) values(3,'aaa')"); } }
spring配置文件sql
<!-- 引入properties配置文件 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:jdbc.properties</value> </property> </bean> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="myTemplate" class="cn.qjc.jdbc.dao.MyTemplate"> <!-- setter注入 --> <property name="dataSource"> <ref bean="dataSource"/> </property> </bean> <bean id="personDao" class="cn.qjc.jdbc.dao.PersonDao"> <property name="dataSource"> <ref bean="dataSource"/> </property> </bean> </beans>
测试类apache
public class PersonDaoTest { @Test public void testPersonDao() throws Exception{ ApplicationContext context = new ClassPathXmlApplicationContext("cn/qjc/jdbc/applicationContext.xml"); PersonDao personDao = (PersonDao)context.getBean("personDao"); personDao.savePerson(); } }
以上代码将DataSource注入给MyTemplate,再把DataSource注入给PersonDao,由于personDao继承MyTemplate,因此拥有Datasource属性。既然PersonDao继承MyTemplate,因此PersonDao类注入能够改成编程
<bean id="personDao" class="cn.qjc.jdbc.dao.PersonDao" parent="myTemplate"></bean>
以上例子中MyTemplate相似于设计模式中的模板模式也叫模板方法模式,模板方法模式是全部模式中最为常见的几个模式之一,是基于继承的代码复用的基本技术。设计模式
模板模式 = 静态代码+动态变量安全
在spring中动态变量能够用注入的形式给予。这样的编程方式适合包装成模板。静态代码构成了模板,而动态变量则是须要传入的参数。app
spring与jdbc结合核心类JdbcTemplate框架
一、基于模板的设置(为何能够设置成基于模板的形式)测试
二、完成了资源的建立和释放的工做
三、简化为咱们对JDBC的操做
四、完成了对JDBC的核心流程的工做,包括SQL语句的建立和执行
五、仅须要传递DataSource就能够把它实例化
六、JdbcTemplate只须要建立一次
七、JdbcTemplate是线程安全类
使用spring+jdbc修改上面例子(myTemplate类去掉)
public class PersonDao extends JdbcDaoSupport {
public void savePerson(String sql){
this.getJdbcTemplate().execute(sql);
}
}
spring配置文件改成
<bean id="personDao" class="cn.qjc.jdbc.dao.PersonDao"> <property name="dataSource"> <ref bean="dataSource"/> </property> </bean>
JdbcTemplate类结构图
执行过程
说明:
一、执行数据的操做的是JdbcTemplate
二、最根本的步骤就是要把dataSource注入到JdbcTemplate
三、经过给JdbcTemplate注入dataSource
a、采用构造器的形式注入
b、采用setter方法进行注入
四、能够给JdbcDaoSupport注入dataSource
五、能够给JdbcDaoSupport注入JdbcTemplate
因此spring与jdbc整合有三种方法,但实际上核心类为JdbcTemplate
一、使用JdbcTemplate
在Dao类中,用JdbcTemplate做为属性,用spring对JdbcTemplate进行注入。再对JdbcTemplate进行DataSource注入。
注:为何只要对JdbcTemplate注入DataSource就能够了?
二、继承jdbcDaoSupport
在Dao类中,继承JdbcDaoSupport。由于JdbcDaoSupport已经有了JdbcTemplate的引用,因此只要继承JdbcDaoSupport就至关于有了JdbcTemplate属性。
三、继承JdbcTemplate
spring还提供了其余ORM框架整合模式都差很少,彻底可直接套用。
spring+hibernate
spring+Jdo
由此可看出spring IOC 和 DI 的强大,IOC和DI 完成了从接口到类的对应。利用spring容器程序员很容易的在客户端实现面向接口编程,并且很容易给接口装配,结构也能够设置的很灵活。由于接口是本身写的,类也是本身写的,配置文件也是本身写的。spring实际完成了建立对象和装配的工做,它会自动的对应起来。