${username}是个关键字 不同引入方式优先读取的内容不同
1、bean来定义读取资源文件
优先读取资源文件
2、context命名空间读取资源文件
优先读取系统用户
第一种
1、使用Bean定义 来读取properties文件
<bean id="mnmn" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:/lesson02/jdbc/jdbcoracle.properties"></property>
</bean>
2、获取连接数据看的数据源
<bean id="dataSouce" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${url}"></property>
<property name="username" value="${username1}"></property>
<property name="password" value="${password}"></property>
<property name="driverClassName" value="${driverClass}"></property>
</bean>
3、将数据源注入到jdbcTemplate中
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSouce"></property>
</bean>
第二种
1、先配置context标签的快捷提示
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
2、context命名空间 读取资源文件 读取properties文件
<context:property-placeholder location="classpath:/lesson02/jdbc/jdbcoracle.properties"/>
3、创建数据库的连接类
<bean id="dataSouce" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${url}"></property>
<property name="username" value="${username1}"></property>
<property name="password" value="${password}"></property>
<property name="driverClassName" value="${driverClass}"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSouce"></property>
</bean>
第三种(使用注解)
1、spring.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
">
<!-- 读取properties文件 -->
<context:property-placeholder location="classpath:/lesson02/jdbc/jdbcoracle.properties"/>
<!-- 扫描注解 -->
<context:component-scan base-package="lesson02"></context:component-scan>
</beans>
2、创建一个类
package lesson02.mvc;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
@Configuration
public class MyConf {
@Value("${url}")
private String url;
@Value("${driverClass}")
private String driverClassName;
@Value("${username1}")
private String userName;
@Value("${password}")
private String password;
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource){
return new JdbcTemplate(dataSource);
}
@Bean
public DataSource dataSouce(){
DriverManagerDataSource dmd=new DriverManagerDataSource();
dmd.setUrl(url);
dmd.setDriverClassName(driverClassName);
dmd.setUsername(userName);
dmd.setPassword(password);
return dmd;
}
}
需要引入的jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
测试
package lesson02.jdbc;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class TestCotiner {
public static void main(String[] args) throws SQLException {
ApplicationContext context=new ClassPathXmlApplicationContext("lesson02/jdbc/spring.xml");
/*
* 使用原始的连接
* DataSource d=(DataSource)context.getBean("dataSouce");
System.out.println(d.getConnection());*/
JdbcTemplate jt = (JdbcTemplate)context.getBean("jdbcTemplate");
List<Map<String, Object>> list = jt.queryForList("select * from cc");
System.out.println(list);
}
}