基礎篇——Spring框架對於JDBC模板的使用


Spring提供的模板類是JdbcTemplate,首先搭建好Spring框架后

 

1、導入jar

  

  

至此lib包中的所有jar包如下:

  

 

 

2、在數據庫中建表:

  

建立實體類:

  

創建Service接口:

  

創建Dao層與Dao的實現類:

  

Service的實現類:

  

  

 

3、配置文件:

  1>引入配置約束

  

 

  2>注冊數據源:(可以直接注冊spring內置的數據源,也可以注冊DBCP數據源或C3P0數據源),注冊非內置數據源需要導入相應的jar包:

  

  

  用屬性文件注冊數據源: 

    1>新建一個.properties文件

    

    2>注冊數據源

    

  3>注冊jdbc模板對象

 

    <bean id="jdbcTemplate" class="org.springframe.jdbc.core.jdbcTemplate">

 

       <property name="dataSource" ref="dataSource"/>

 

    </bean>

  4>注冊Dao

    <bean id="studentDao" class="com.xunpoit.bean.dao.StudentDaoImpl">

      <property name="jdbcTemplate" ref="jdbcTemplate"/>

    </bean>

  5>注冊Service

    <bean id="studentService"class="com.xunpoit.bean.service.StudentServiceImpl">

      <property name="dao" ref="studentDao"/>

    </bean>

4、測試(將配置文件的注入方式單獨抽取出來):

  

                                            ---------------------畢:記2018/4/20  晨

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM