Spring JDBC 框架,我的学习笔记

143 阅读1分钟

Student.java:

StudentDAO: 一个接口

setDataSource方法注入javax.sql.DataSource的依赖:

StudentMapper: java.sql.ResultSet包含的是单条记录:

StudentJDBCTemplate: 是接口StudentDAO的实现类。关键就是setDataSource方法,需要研究其是何时被调用的。DataSource被注入之后,基于这个注入的DataSource创建jdbcTemplateObject实例。

Java代码里的data source和MySQL服务器实例的绑定关系在beans.xml里维护:

Beans.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" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">

   <!-- Initialization for data source -->
   <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="123456"/>
   </bean>

   <!-- Definition for studentJDBCTemplate bean -->
   <bean id="studentJDBCTemplate" 
      class="com.sap.StudentJDBCTemplate">
      <property name="dataSource"  ref="dataSource" />    
   </bean>

</beans>

要获取更多Jerry的原创文章,请关注公众号"汪子熙":