Student.java:

StudentDAO: An interface

The setDataSource method injects javax.sql.DataSource dependencies:

StudentMapper: java.sql.ResultSet contains a single record:

StudentJDBCTemplate: it is the implementation class of the StudentDAO interface. The key is the setDataSource method, which needs to be investigated when it is called. After the DataSource is injected, a jdbcTemplateObject instance is created based on the injected DataSource.

The binding between the data source in Java code and the MySQL server instance is maintained in beans.xml:

Beans.xml:


      
<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>
Copy the code

For more of Jerry’s original articles, please follow the public account “Wang Zixi “: