spring框架jdbc連接數據庫

(前提,導入spring依賴的包、mysql數據庫包)

1、配置數據源

2、配置jdbc模版

3、測試類



1、配置數據源

spring支持三種方式配置數據源:1、jdbc  2、jdni   3、連接池。此處用的是jdbc中的DriverManagerDataSource數據源,另外一種是SingleConnectionDataSource

<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.xsd">
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://127.0.0.1:3306/***"></property>
		<property name="username" value="root"></property>
		<property name="password" value="111111"></property>
	</bean>

</beans>

2、配置jdbc模版

spring提供三種jdbc模版,1、JdbcTemplate     2、SimpleJdbcTemplate      3、NamedParameterJdbcTemplate。此處用的是simpleJdbcTemplate。

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
  		<constructor-arg ref="dataSource"></constructor-arg>
  	</bean>
3、編寫測試類   略過

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章