SpringData--Spring JdbcTemplate的應用-2

Spring Template應用案例源代碼:

MVDem

o.zip

20.72KB

***1-加載MAVEN依賴

 

 

還導一個JDBC的,下面沒展示

***2-編寫Spring配置文件

首先確定resource文件夾是資源根目錄

新建spring的配置文件

編寫配置文檔

 

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--自動掃描組件--> <context:component-scan base-package="com.company"></context:component-scan> <!--配置數據源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql:///test"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <!--配置spring的jdbcTemplate--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>

***3-編寫Dao接口及實現類(運用JdbcTemplate實現,簡化JDBC原型代碼)

 

 

 

 

 

運用JAVA8新特性改寫代碼如下

 

***4-測試

 

 

 

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