前一篇通過對傳統的JDBC的使用操作,可以體會到使用的繁瑣與復雜,套句話說,是用了20%作了真正的工作,80%作了重復的工作。
那么通過本篇,可以了解如下的內容:
1 如何配置數據源
2 如何在spring中使用模板
3 如何建立數據源的統一的基類
首先看一下如何配置數據源
我們可以使用3種方式配置數據源:
1 JNDI配置數據源
這種做法我是沒用過,感覺每次都要去修改配置Tomcat之類的web容器,很是麻煩。
2 使用DBCP數據源連接池
一般情況下都是采用這種方式,對於連接池的實現,也有很多種,比如DBCP,c3p0等等。
用戶可以針對連接池進行自己的配置,有助於數據庫端的調優。
如果想對數據源連接池多謝了解,可以猛戳該鏈接。
相對來說,最常使用的就是dbcp和c3p0了。
3 基於JDBC的驅動的數據源
這種是最基本的通過驅動程序管理數據源,但是沒有連接池的概念。
有兩種實現方式:
DriverManagerDataSource:一般都是使用這種,這種方式每次請求都會返回一個新的連接。
SingleConnectionDataSource:這種每次都是使用的一個連接。
本篇為了簡單方便,就直接使用的第三種。
Spring中的模板以及提供的基類
在Spring中為我們提供了三種模板:
1 JdbcTemplate
提供最簡單的數據訪問等功能。
2 NamedParameterJdbcTemplate
通過該模板,可以把參數作為查詢的條件傳入方法中。
3 SimpleJdbcTemplate(一般都是使用這種)
結合了一些自動裝箱等功能,3.0以后,整合了NamedParameterJdbcTemplate。
為了避免每次都要把jdbctemplate的bean注入到我們的DAO里面,Spring為我們實現了三種對應的基類,我們的DAO實現類需要繼承這些基類,就可以直接使用模板了。
對應的分別是:JdbcDapSupport、SimpleJdbcDaoSupport、NamedParameterJdbcDaoSupport
最后就是程序的使用介紹了
首先看一下配置數據源的bean.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <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/test"/> <property name="username" value="root"/> <property name="password" value="123qwe"/> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate"> <constructor-arg ref="dataSource"/> </bean> <bean id="newjdbcdao" class="com.spring.chap5.dao.NewJdbcImpl" > <property name="jdbcTemplate" ref="jdbcTemplate" /> </bean> </beans>
這里,我們配置了dataSource,以及jdbcTemplate,最后把jdbcTemplate注入到dao的實現類里面。
接下來的DAO的接口:
public interface NewJdbc { public void insertPerson(String id,String name,int age); public void findPersonById(String id); }
DAO的實現類:
public class NewJdbcImpl implements NewJdbc{ private SimpleJdbcTemplate jdbcTemplate; public void setJdbcTemplate(SimpleJdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public void insertPerson(String id,String name,int age){ jdbcTemplate.update("insert into persons (id,name,age) values (?,?,?)", id,name,age); } public void findPersonById(String id){ Person person = jdbcTemplate.queryForObject("select * from persons where id = ?", new ParameterizedSingleColumnRowMapper<Person>(){ public Person mapRow(ResultSet rs,int rowNum) throws SQLException{ Person p = new Person(); p.setId(rs.getString(1)); p.setName(rs.getString(2)); p.setAge(rs.getInt(3)); return p; } } , id); System.out.println("id:"+person.getId()+" name:"+person.getName()+" age:"+person.getAge()); } }
最后是測試使用的類
public class test { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml"); NewJdbc newjdbc = (NewJdbc)ctx.getBean("newjdbcdao"); newjdbc.insertPerson("003", "xingoo3", 25); newjdbc.findPersonById("003"); } }
以上便是Spring基於JDBC的模板使用了。
可以看到,相對於前面的傳統的JDBC操作數據庫來說,省略了創建連接以及釋放的過程。
僅僅是把操作的真正的實現部分交給開發人員,這就是模板的設計模式的應用——分離模板與開發人員的實現。