第一種方式:.Spring常規的數據庫連接方法:
1 @RunWith(SpringJUnit4ClassRunner.class) 2 @ContextConfiguration(locations="classpath:applicationContext.xml") 3 public class jdbcTemplateTest1 { 4 /* @Test 5 public void test1(){ 6 //1.創建數據庫連接池(Spring) 7 DriverManagerDataSource dataSource = new DriverManagerDataSource(); 8 //2.設置參數 9 //獲取驅動 10 dataSource.setDriverClass("com.mysql.jdbc.Driver"); 11 //連接數據庫 12 dataSource.setJdbcUrl("jdbc:mysql:///springtest"); 13 dataSource.setUser("root"); 14 dataSource.setPassword("123"); 15 //3.創建jdbcTemplate 16 JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); 17 jdbcTemplate.execute("update t_user set name='tom' where id=3"); 18 } */ 19 @Autowired 20 private JdbcTemplate jdbcTemplate; 21 @Test 22 public void test2(){ 23 jdbcTemplate.execute("update t_user set name='tom' where id=1"); 24 } 25 }
第二種方式:注入Spring,由Spring內部管理
1.測試類
1 測試類
@RunWith(SpringJUnit4ClassRunner.class) 2 @ContextConfiguration(locations="classpath:applicationContext.xml") 3 public class jdbcTemplateTest1 { 4 @Autowired 5 private JdbcTemplate jdbcTemplate; 6 @Test 7 public void test2(){ 8 jdbcTemplate.execute("update t_user set name='tom' where id=1");}}
2.1applicationContext.xml配置文件-----Spring內置的連接池
1 <!-- Spring內置的連接池 --> 2 <bean id="driverManagerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 3 <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 4 <property name="url" value="jdbc:mysql:///springtest" /> 5 <property name="username" value="root"/> 6 <property name="password" value="123"></property> 7 </bean> 8 9 <!-- ref聲明數據源,交由Spring管理 --> 10 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 11 <property name="dataSource" ref="driverManagerDataSource"></property> 12 </bean>
2.2 applicationContext.xml配置文件-----c3p0連接池
1 <!-- 創建c3p0連接池 --> 2 <bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 3 <property name="driverClass" value="com.mysql.jdbc.Driver" /> 4 <property name="jdbcUrl" value="jdbc:mysql:///springtest" /> 5 <property name="user" value="root" /> 6 <property name="password" value="123" /> 7 </bean> 8 <!-- ref聲明c3p0的數據源,交由Spring管理 --> 9 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 10 <property name="dataSource" ref="c3p0DataSource"></property>
11 </bean>
2.3在c3p0基礎上引用外部屬性--為了切換oracle等數據庫
1<!-- 引入外部的properties文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 創建c3p0連接池 --> 2 <bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 3 <property name="driverClass" value="${jdbc.driverClass}" /> 4 <property name="jdbcUrl" value="${jdbc.url}" /> 5 <property name="user" value="${jdbc.username}" /> 6 <property name="password" value="${jdbc.password}" /> 7 </bean> 8 <!-- ref聲明c3p0的數據源,交由Spring管理 --> 9 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 10 <property name="dataSource" ref="c3p0DataSource"></property> 11 </bean>
----------------------------------------------------------------------
db.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url= jdbc:mysql:///springtest
jdbc.username=root
jdbc.password=123
