開發准備:
1.導入jar包:
ioc基本jar
jdbcTemplate基本jar
c3p0基本jar
別忘了mysql數據庫驅動jar
原始程序代碼:不使用配置文件方式(IOC)生成訪問數據庫對象
1 package org.dao; 2 import java.beans.PropertyVetoException; 3 import org.springframework.jdbc.core.JdbcTemplate; 4 import org.springframework.jdbc.datasource.DriverManagerDataSource; 5 import com.mchange.v2.c3p0.ComboPooledDataSource; 6 7 public class Dao { 8 public void add(){ 9 /* 10 //使用spring框架中的jdbcTemplate完成數據庫的訪問 11 12 //設置數據庫的信息 13 DriverManagerDataSource dmds=new DriverManagerDataSource(); 14 dmds.setDriverClassName("com.mysql.jdbc.Driver"); 15 dmds.setUrl("jdbc:mysql://localhost:3306/c3p0jdbctemplate"); 16 dmds.setUsername("root"); 17 dmds.setPassword("jay571018"); 18 19 //創建JdbcTemplate對象 設置數據源 20 JdbcTemplate jdbcTemplate=new JdbcTemplate(dmds); 21 22 //調用方法 實現數據庫的訪問 23 String sql="insert into User(name,password) values(?,?)"; 24 jdbcTemplate.update(sql,"joke",520); 25 */ 26 27 //使用c3p0連接池 配置數據庫的信息 然后創建JdbcTemplate對象 設置數據源 完成對數據庫訪問 28 ComboPooledDataSource datesourse=new ComboPooledDataSource(); 29 try { 30 datesourse.setDriverClass("com.mysql.jdbc.Driver"); 31 } catch (PropertyVetoException e) { 32 // TODO Auto-generated catch block 33 e.printStackTrace(); 34 } 35 datesourse.setJdbcUrl("jdbc:mysql://localhost:3306/c3p0jdbctemplate"); 36 datesourse.setUser("root"); 37 datesourse.setPassword("jay571018"); 38 //創建JdbcTemplate對象 設置數據源 39 JdbcTemplate jdbcTemplate=new JdbcTemplate(datesourse); 40 41 //調用方法 實現數據庫的訪問 42 String sql="insert into User(name,password) values(?,?)"; 43 jdbcTemplate.update(sql,"joke",520); 44 System.out.println("增加了一條信息。。。。。。。"); 45 } 46 }
1 package org.service; 2 import org.dao.*; 3 public class Service { 4 //想在service中調用dao對象的add方法 創建dao對象 生成對應的set方法 5 private Dao dao; 6 7 //生成set方法 注入dao對象 8 public void setDao(Dao dao){ 9 this.dao=dao; 10 } 11 public void add(){ 12 dao.add(); 13 } 14 }
1 package org.test; 2 import org.junit.Test; 3 import org.service.Service; 4 import org.springframework.context.ApplicationContext; 5 import org.springframework.context.support.ClassPathXmlApplicationContext; 6 7 public class TestDemo { 8 @Test 9 public void testDemo(){ 10 ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); 11 Service s=(Service) ac.getBean("service"); 12 s.add(); 13 } 14 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/aop 12 http://www.springframework.org/schema/aop/spring-aop.xsd 13 http://www.springframework.org/schema/tx 14 http://www.springframework.org/schema/tx/spring-tx.xsd"> 15 <bean id="dao" class="org.dao.Dao"></bean> 16 <bean id="service" class="org.service.Service"> 17 <property name="dao" ref="dao"></property> 18 </bean> 19 </beans>
改進程序代碼:使用配置文件方式(IOC)生成訪問數據庫對象
需要注意的是:使用配置文件生成bean對象時,在的類中一定需要有屬性對應的set方法以及無參構造方法(不寫的話默認有一個無參構造),否則無法完成注入,
兩者缺一不可,因為使用set注入的時候,spring先調用了構造方法實例化對象,然后才對對象中的屬性進行了注入。
另外IOC注入方式有兩種,一種set方式,另外一種是構造注入,構造注入的時候當然需要構造函數(有參數)。
1 package org.dao; 2 import java.beans.PropertyVetoException; 3 4 import org.springframework.jdbc.core.JdbcTemplate; 5 import org.springframework.jdbc.datasource.DriverManagerDataSource; 6 7 import com.mchange.v2.c3p0.ComboPooledDataSource; 8 9 public class Dao { 10 private JdbcTemplate jdbcTemplate; 11 12 //設置set方法 為了之后的注入 13 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { 14 this.jdbcTemplate = jdbcTemplate; 15 } 16 public void add(){ 17 String sql="insert into User(name,password) values(?,?)"; 18 jdbcTemplate.update(sql,"joke2",520); 19 System.out.println("增加了一條信息。。。。。。。"); 20 } 21 }
1 package org.service; 2 import org.dao.*; 3 public class Service { 4 //想在service中調用dao對象的add方法 創建dao對象 生成對應的set方法 5 private Dao dao; 6 7 //生成set方法 注入dao對象 8 public void setDao(Dao dao){ 9 this.dao=dao; 10 } 11 public void add(){ 12 dao.add(); 13 } 14 }
1 package org.test; 2 import org.junit.Test; 3 import org.service.Service; 4 import org.springframework.context.ApplicationContext; 5 import org.springframework.context.support.ClassPathXmlApplicationContext; 6 7 public class TestDemo { 8 public static void main(String args[]){ 9 ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); 10 Service s=(Service) ac.getBean("service"); 11 s.add(); 12 } 13 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/aop 12 http://www.springframework.org/schema/aop/spring-aop.xsd 13 http://www.springframework.org/schema/tx 14 http://www.springframework.org/schema/tx/spring-tx.xsd"> 15 <!-- 創建連接池 配置連接池的屬性值 --> 16 <bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 17 <property name="driverClass" value="com.mysql.jdbc.Driver"></property> 18 <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/c3p0jdbctemplate"></property> 19 <property name="user" value="root"></property> 20 <property name="password" value="jay571018"></property> 21 </bean> 22 23 <!-- dao中注入jdbctemplate對象 --> 24 <bean id="dao" class="org.dao.Dao"> 25 <property name="jdbcTemplate" ref="jdbctemplate"></property> 26 </bean> 27 28 <!-- service中注入dao對象 --> 29 <bean id="service" class="org.service.Service"> 30 <property name="dao" ref="dao"></property> 31 </bean> 32 33 <!-- 創建jdbctemplate對象 並且向其中注入連接池對象 dateSource是該對象中數據源的屬性 --> 34 <bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 35 <property name="dataSource" ref="ds"></property> 36 </bean> 37 38 </beans>
注入關系:
1.因為service類需要調用dao類中的方法,所以在service中生成了一個dao對象,並且存在set方法
<!-- service中注入dao對象 --> 29 <bean id="service" class="org.service.Service"> 30 <property name="dao" ref="dao"></property> 31 </bean>
2.在dao類中要對數據庫進行操作,所以創建jdbcTemplate對象,並且生成set方法
23 <!-- dao中注入jdbctemplate對象 -->
24 <bean id="dao" class="org.dao.Dao">
25 <property name="jdbcTemplate" ref="jdbctemplate"></property>
26 </bean>
3.但是jdbcTemplate沒有設置數據源,所以我們導入了數據庫連接池,而且在配置文件中直接生成了c3p0連接池對象,就是用於jdbcTemplate對象的實例化
33 <!-- 創建jdbctemplate對象 並且向其中注入連接池對象 dateSource是該對象中數據源的屬性 --> 34 <bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 35 <property name="dataSource" ref="ds"></property> 36 </bean>
4. <!-- 創建連接池 配置連接池的屬性值 -->
<bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource">
17 <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
18 <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/c3p0jdbctemplate"></property>
19 <property name="user" value="root"></property>
20 <property name="password" value="jay571018"></property>
21 </bean>
以上就完成了對數據庫的訪問(向數據庫中插入了一條記錄),個人練習總結,請多指教。
-----------------------------------------
歡迎大家轉載,但請注明原創鏈接:http://www.cnblogs.com/Joke-Jay/p/6502507.html