java 用properties文件配置spring数据源,用spring的JdbcTemplate的queryForList查数据

时间:2022-10-01 15:56:56

使用的jar包:

ojdbc14.jar spring-2.5.jar commons-dbcp-1.4.jar

目录结构ress(source folder)->conff(package)下有app.xml和sys.properties

sys.properties:

[html] view plaincopy
  1. jdbc.driverClassName=oracle.jdbc.driver.OracleDriver  
  2. jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl  
  3. jdbc.username=scott  
  4. jdbc.password=tiger  



app.xml:

[html] view plaincopy
  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:aop="http://www.springframework.org/schema/aop"  
  5.        xmlns:tx="http://www.springframework.org/schema/tx"  
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  7.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
  8.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"  
  9.        default-autowire="byName" default-lazy-init="true">  
  10.   
  11.     <!-- 属性文件读入 -->  
  12.     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  13.         <property name="locations">  
  14.             <list>  
  15.                 <value>classpath*:conff/sys.properties</value>  
  16.             </list>  
  17.         </property>  
  18.     </bean>  
  19.     <!--  
  20.     <bean id="mydataSource" class="org.apache.commons.dbcp.BasicDataSource">  
  21.         <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>  
  22.         <property name="url"  
  23.           value="jdbc:oracle:thin:@localhost:1521:ORCL">  
  24.         </property>  
  25.         <property name="username" value="scott"></property>  
  26.         <property name="password" value="tiger"></property>  
  27.     </bean>  
  28.       -->  
  29.      <!--  -->  
  30.     <bean id="mydataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  31.         <property name="driverClassName"><value>${jdbc.driverClassName}</value></property>  
  32.         <property name="url"><value>${jdbc.url}</value></property>  
  33.         <property name="username"><value>${jdbc.username}</value></property>  
  34.         <property name="password"><value>${jdbc.password}</value></property>  
  35.     </bean>  
  36.       
  37.     <bean id="myJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
  38.         <property name="dataSource">  
  39.             <ref bean="mydataSource"/>  
  40.         </property>  
  41.     </bean>  
  42.   
  43. </beans>  


Oratest .java

[java] view plaincopy
  1.   
[java] view plaincopy
  1. package oracletest;  
  2.   
  3. import java.util.Iterator;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6. import java.util.Map.Entry;  
  7.   
  8. import org.springframework.context.ApplicationContext;  
  9. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  10. import org.springframework.jdbc.core.JdbcTemplate;  
  11.   
  12. public class Oratest {  
  13.       
  14.     private JdbcTemplate myJdbcTemplate;  
  15.     private static ApplicationContext applicationContext;  
  16.     private String[] xmlClassPath={  
  17.             "conff/app.xml"  
  18.     };  
  19.       
  20.     public Oratest(){  
  21.         applicationContext=new ClassPathXmlApplicationContext(xmlClassPath);  
  22.     }  
  23.     public static Object getBean(String beanName){    
  24.           
  25.         return  applicationContext.getBean(beanName);  
  26.     }  
  27.     //getter setter  
  28.     public JdbcTemplate getMyJdbcTemplate() {  
  29.         return myJdbcTemplate;  
  30.     }  
  31.   
  32.     public void setMyJdbcTemplate(JdbcTemplate myJdbcTemplate) {  
  33.         this.myJdbcTemplate = myJdbcTemplate;  
  34.     }  
  35.       
  36.     @SuppressWarnings("rawtypes")  
  37.     public static void main(String[] args) {  
  38.         Oratest dd=new Oratest();  
  39.         dd.getBean("myJdbcTemplate");  
  40.         String sql="select pass,dd  from t1 where pass=?";  
  41.           
  42.         dd.setMyJdbcTemplate((JdbcTemplate)dd.getBean("myJdbcTemplate"));  
  43.         List list=dd.getMyJdbcTemplate().queryForList(sql, new Object[]{"vv"});  
  44.           
  45.         System.out.println(list.size());  
  46.         //遍历list  
  47.         for(int i=0;i<list.size();i++){  
  48.             Map mm=(Map)list.get(i);  
  49.             //遍历map  
  50.             Iterator it= mm.entrySet().iterator();  
  51.             while(it.hasNext()){  
  52.                 Map.Entry entry=(Map.Entry)it.next();  
  53.                 System.out.println("key:"+entry.getKey()+"--value:"+entry.getValue());  
  54.             }  
  55.         }  
  56.     }  
  57.   
  58. }  


输出:

- Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@19fcc69: display name [org.springframework.context.support.ClassPathXmlApplicationContext@19fcc69]; startup date [Mon Oct 17 16:54:57 CST 2011]; root of context hierarchy
- Loading XML bean definitions from class path resource [conff/app.xml]
- Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@19fcc69]:org.springframework.beans.factory.support.DefaultListableBeanFactory@5e5a50
- Loading properties file from URL [file:/D:/work/des/bin/conff/sys.properties]
- Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5e5a50: defining beans [propertyConfigurer,mydataSource,myJdbcTemplate]; root of factory hierarchy
2
key:PASS--value:vv
key:DD--value:2011-10-10 16:48:39.0
key:PASS--value:vv
key:DD--value:null