先上一段簡單示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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類
1
2
3
4
5
6
|
public class PersonDao extends MyTemplate{ public void savePerson() throws Exception{ this .insert( "insert into person(pid,pname) values(3,'aaa')" ); } } |
spring配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<!-- 引入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> |
測試類
1
2
3
4
5
6
7
8
9
|
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中動態變量可以用注入的形式給予。這樣的編程方式適合包裝成模板。靜態代碼構成了模板,而動態變量則是需要傳入的參數。
spring與jdbc結合核心類JdbcTemplate
1、基于模板的設置(為什么可以設置成基于模板的形式)
2、完成了資源的創建和釋放的工作
3、簡化為我們對JDBC的操作
4、完成了對JDBC的核心流程的工作,包括SQL語句的創建和執行
5、僅需要傳遞DataSource就可以把它實例化
6、JdbcTemplate只需要創建一次
7、JdbcTemplate是線程安全類
使用spring+jdbc修改上面例子(myTemplate類去掉)
1
2
3
4
5
|
public class PersonDao extends JdbcDaoSupport { public void savePerson(String sql){ this .getJdbcTemplate().execute(sql); } } |
spring配置文件改為
1
2
3
4
5
|
<bean id= "personDao" class = "cn.qjc.jdbc.dao.PersonDao" > <property name= "dataSource" > <ref bean= "dataSource" /> </property> </bean> |
JdbcTemplate類結構圖
執行過程
說明:
1、執行數據的操作的是JdbcTemplate
2、最根本的步驟就是要把dataSource注入到JdbcTemplate
3、通過給JdbcTemplate注入dataSource
a、采用構造器的形式注入
b、采用setter方法進行注入
4、可以給JdbcDaoSupport注入dataSource
5、可以給JdbcDaoSupport注入JdbcTemplate
所以spring與jdbc整合有三種方法,但實際上核心類為JdbcTemplate
1、使用JdbcTemplate
在Dao類中,用JdbcTemplate作為屬性,用spring對JdbcTemplate進行注入。再對JdbcTemplate進行DataSource注入。
注:為什么只要對JdbcTemplate注入DataSource就可以了?
2、繼承jdbcDaoSupport
在Dao類中,繼承JdbcDaoSupport。因為JdbcDaoSupport已經有了JdbcTemplate的引用,所以只要繼承JdbcDaoSupport就相當于有了JdbcTemplate屬性。
3、繼承JdbcTemplate
spring還提供了其他ORM框架整合模式都差不多,完全可直接套用。
spring+hibernate
spring+Jdo
由此可看出spring IOC 和 DI 的強大,IOC和DI 完成了從接口到類的對應。利用spring容器程序員很容易的在客戶端實現面向接口編程,而且很容易給接口裝配,結構也可以設置的很靈活。因為接口是自己寫的,類也是自己寫的,配置文件也是自己寫的。spring實際完成了創建對象和裝配的工作,它會自動的對應起來。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。