基础篇——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