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

使用的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 [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

Spring Boot中,要实现连接多个数据库,可以使用Spring Boot提供的多数据源配置功能。下面是实现的步骤: 1. 在application.properties文件配置两个数据源,如下所示: ```properties # 配置数据源1 spring.datasource.db1.driver-class-name=com.mysql.jdbc.Driver spring.datasource.db1.url=jdbc:mysql://localhost:3306/db1 spring.datasource.db1.username=root spring.datasource.db1.password=root # 配置数据源2 spring.datasource.db2.driver-class-name=com.mysql.jdbc.Driver spring.datasource.db2.url=jdbc:mysql://localhost:3306/db2 spring.datasource.db2.username=root spring.datasource.db2.password=root ``` 2. 在代码中定义两个数据源,如下所示: ```java @Configuration public class DataSourceConfig { @Bean(name = "db1") @ConfigurationProperties(prefix = "spring.datasource.db1") public DataSource primaryDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "db2") @ConfigurationProperties(prefix = "spring.datasource.db2") public DataSource secondaryDataSource() { return DataSourceBuilder.create().build(); } } ``` 3. 在代码中使用@Primary和@Qualifier注解指定使用哪个数据源,如下所示: ```java @Autowired @Qualifier("db1") private DataSource dataSource1; @Autowired @Qualifier("db2") private DataSource dataSource2; @Autowired @Primary @Bean(name = "jdbcTemplate1") public JdbcTemplate primaryJdbcTemplate() { return new JdbcTemplate(dataSource1); } @Bean(name = "jdbcTemplate2") public JdbcTemplate secondaryJdbcTemplate() { return new JdbcTemplate(dataSource2); } ``` 4. 在代码中使用jdbcTemplate1和jdbcTemplate2访问两个数据源,如下所示: ```java String sql1 = "SELECT * FROM table1"; List<Map<String, Object>> result1 = jdbcTemplate1.queryForList(sql1); String sql2 = "SELECT * FROM table2"; List<Map<String, Object>> result2 = jdbcTemplate2.queryForList(sql2); ``` 以上就是使用Spring Boot配置数据源实现一个Java项目同时连接两个数据库的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值