前一篇經過對傳統的JDBC的使用操做,能夠體會到使用的繁瑣與複雜,套句話說,是用了20%做了真正的工做,80%做了重複的工做。mysql
那麼經過本篇,能夠了解以下的內容:web
1 如何配置數據源spring
2 如何在spring中使用模板sql
3 如何創建數據源的統一的基類數據庫
咱們可使用3種方式配置數據源:設計模式
1 JNDI配置數據源app
這種作法我是沒用過,感受每次都要去修改配置Tomcat之類的web容器,非常麻煩。測試
2 使用DBCP數據源鏈接池this
通常狀況下都是採用這種方式,對於鏈接池的實現,也有不少種,好比DBCP,c3p0等等。url
用戶能夠針對鏈接池進行本身的配置,有助於數據庫端的調優。
若是想對數據源鏈接池多謝了解,能夠猛戳該連接。
相對來講,最常使用的就是dbcp和c3p0了。
3 基於JDBC的驅動的數據源
這種是最基本的經過驅動程序管理數據源,可是沒有鏈接池的概念。
有兩種實現方式:
DriverManagerDataSource:通常都是使用這種,這種方式每次請求都會返回一個新的鏈接。
SingleConnectionDataSource:這種每次都是使用的一個鏈接。
本篇爲了簡單方便,就直接使用的第三種。
在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操做數據庫來講,省略了建立鏈接以及釋放的過程。
僅僅是把操做的真正的實現部分交給開發人員,這就是模板的設計模式的應用——分離模板與開發人員的實現。