一、Spring内置数据源
1. 创建Maven Project,修改pom.xml
<properties> <!-- JDK版本 --> <java.version>1.8</java.version> <!-- spring版本 --> <spring.version>4.1.6.RELEASE</spring.version> <!-- mysql driver 版本 --> <mysql.version>5.1.48</mysql.version> </properties> <dependencies> <!-- 核心容器 之 spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <!-- 核心容器 之 spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <!-- 核心容器 之 spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- 核心容器 之 spring-context-support --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <!-- 核心容器 之 spring-expression --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <!-- spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!-- mysql driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> </dependencies>
2. jdbc.properties
# DriverManagerDataSource + mysql jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://192.168.178.5:12345/ssm?characterEncoding=utf-8 jdbc.username=root jdbc.password=123456
3. applicationContext.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 加载属性文件 --> <!-- 第一种方式 --> <!-- <bean id="config" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:jdbc.properties</value> </property> </bean> --> <!-- 第二种方式 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- spring自带的数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> </beans>
4. 测试
//加载配置文件,spring 容器初始化 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); DriverManagerDataSource dataSource = ctx.getBean("dataSource", DriverManagerDataSource.class); System.out.println(dataSource); System.out.println(dataSource.getUrl());
注意:
DriverManagerDataSource 和 SimpleDriverDataSource 在每次请求时返回一个新建的连接,根本没有连接池的概念。在正式项目中很少使用,常用的是使用第三方数据源。