JDBCTemplate開發(fā)步驟
?、?導(dǎo)入spring-jdbc和spring-tx坐標(biāo)
1 <!-- 導(dǎo)入 spring 的 jdbc 坐標(biāo) 封裝了jdbc--> 2 <dependency> 3 <groupId>org.springframework</groupId> 4 <artifactId>spring-jdbc</artifactId> 5 <version>5.0.5.RELEASE</version> 6 </dependency> 7 <!-- 導(dǎo)入 spring 的 tx 坐標(biāo) transaction事務(wù) --> 8 <dependency> 9 <groupId>org.springframework</groupId> 10 <artifactId>spring-tx</artifactId> 11 <version>5.0.5.RELEASE</version> 12 </dependency>
?、?創(chuàng)建數(shù)據(jù)庫表和實(shí)體
?、?創(chuàng)建JdbcTemplate對(duì)象:
創(chuàng)建JdbcTemplate對(duì)象最重要就是注入數(shù)據(jù)源對(duì)象jdbcTemplate.setDataSource(數(shù)據(jù)源對(duì)象);?數(shù)據(jù)源對(duì)象:c3p0,dbcp,druid
當(dāng)然數(shù)據(jù)源對(duì)象需要先配置驅(qū)動(dòng)、jdbc連接、用戶名和密碼
在實(shí)際開發(fā)中,JDBCTemplate對(duì)象和數(shù)據(jù)源對(duì)象都可以交給Spring創(chuàng)建,在applicationContext.xml配置:
1 <!-- 數(shù)據(jù)源 DataSource--> 2 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 3 <property name="driverClass" value="com.mysql.jdbc.Driver"></property> 4 <property name="jdbcUrl" value="jdbc:mysql:///test"></property> 5 <property name="user" value="root"></property> 6 <property name="password" value="root"></property> 7 </bean> 8 <!--JdbcTemplate--> 9 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 10 <property name="dataSource" ref="dataSource"></property> 11 </bean>
?
本文摘自 :https://www.cnblogs.com/