當前位置:首頁 > IT技術(shù) > 數(shù)據(jù)庫 > 正文

JDBCTemplate
2021-09-24 14:50:55

JDBCTemplate開發(fā)步驟

 ?、?導入spring-jdbc和spring-tx坐標

 1 <!-- 導入 spring 的 jdbc 坐標  封裝了jdbc-->
 2 <dependency>
 3     <groupId>org.springframework</groupId>
 4     <artifactId>spring-jdbc</artifactId>
 5     <version>5.0.5.RELEASE</version>
 6 </dependency>
 7 <!-- 導入 spring 的 tx 坐標  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ù)庫表和實體

 ?、?創(chuàng)建JdbcTemplate對象:

    創(chuàng)建JdbcTemplate對象最重要就是注入數(shù)據(jù)源對象jdbcTemplate.setDataSource(數(shù)據(jù)源對象);?數(shù)據(jù)源對象:c3p0,dbcp,druid

    當然數(shù)據(jù)源對象需要先配置驅(qū)動、jdbc連接、用戶名和密碼

  在實際開發(fā)中,JDBCTemplate對象和數(shù)據(jù)源對象都可以交給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/

開通會員,享受整站包年服務(wù)立即開通 >