现在的位置: 首页 > 综合 > 正文

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

2013年08月14日 ⁄ 综合 ⁄ 共 4171字 ⁄ 字号 评论关闭

使用的jar包:

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

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

sys.properties:

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=scott
jdbc.password=tiger

app.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns:aop="http://www.springframework.org/schema/aop"
	   xmlns:tx="http://www.springframework.org/schema/tx"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
	   default-autowire="byName" default-lazy-init="true">

	<!-- 属性文件读入 -->
	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath*:conff/sys.properties</value>
			</list>
		</property>
	</bean>
	<!--
	<bean id="mydataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
		<property name="url"
		  value="jdbc:oracle:thin:@localhost:1521:ORCL">
		</property>
		<property name="username" value="scott"></property>
		<property name="password" value="tiger"></property>
    </bean>
	  -->
	 <!--  -->
	<bean id="mydataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	    <property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
	    <property name="url"><value>${jdbc.url}</value></property>
	    <property name="username"><value>${jdbc.username}</value></property>
	    <property name="password"><value>${jdbc.password}</value></property>
    </bean>
    
    <bean id="myJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource">
         	<ref bean="mydataSource"/>
        </property>
    </bean>

</beans>

Oratest .java


package oracletest;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class Oratest {
	
	private JdbcTemplate myJdbcTemplate;
	private static ApplicationContext applicationContext;
	private String[] xmlClassPath={
			"conff/app.xml"
	};
	
	public Oratest(){
		applicationContext=new ClassPathXmlApplicationContext(xmlClassPath);
	}
    public static Object getBean(String beanName){	
		
		return  applicationContext.getBean(beanName);
	}
    //getter setter
	public JdbcTemplate getMyJdbcTemplate() {
		return myJdbcTemplate;
	}

	public void setMyJdbcTemplate(JdbcTemplate myJdbcTemplate) {
		this.myJdbcTemplate = myJdbcTemplate;
	}
	
	@SuppressWarnings("rawtypes")
	public static void main(String[] args) {
		Oratest dd=new Oratest();
		dd.getBean("myJdbcTemplate");
		String sql="select pass,dd  from t1 where pass=?";
		
		dd.setMyJdbcTemplate((JdbcTemplate)dd.getBean("myJdbcTemplate"));
		List list=dd.getMyJdbcTemplate().queryForList(sql, new Object[]{"vv"});
		
		System.out.println(list.size());
		//遍历list
		for(int i=0;i<list.size();i++){
			Map mm=(Map)list.get(i);
			//遍历map
			Iterator it= mm.entrySet().iterator();
			while(it.hasNext()){
				Map.Entry entry=(Map.Entry)it.next();
				System.out.println("key:"+entry.getKey()+"--value:"+entry.getValue());
			}
		}
	}

}

输出:

- 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


- 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

 

抱歉!评论已关闭.