Java之Spring入门到精通IDEA版IoC和DI注解开发(一篇文章精通系列)
Posted 蓝盒子itbluebox
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java之Spring入门到精通IDEA版IoC和DI注解开发(一篇文章精通系列)相关的知识,希望对你有一定的参考价值。
上一篇文章地址:Java之Spring入门到精通【IDEA版】Spring的IoC和DI(一)(一篇文章精通系列)
IoC和DI注解开发
一、Spring配置数据源
1、数据源(连接池)
1)数据源(连接池)的作用
- 数据源(连接池)是提高程序性能如出现的
- 事先实例化数据源,初始化部分连接资源
- 使用连接资源时从数据源中获取
- 使用完毕后将连接资源归还给数据源
常见的数据源(连接池):DBCP、C3P0、BoneCP、Druid等
2)数据源的开发步骤
- 导入数据源的坐标和数据库驱动坐标
- 创建数据源对象
- 设置数据源的基本连接数据
- 使用数据源获取连接资源和归还连接资源
2、数据源的手动创建(Java代码实现)
(1)创建新的项目
(2)导入c3p0和druid的,mysql数据库驱动坐标
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itbluebox</groupId>
<artifactId>Spring-DB</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<spring.version>5.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<!--导入spring的context坐标,context依赖core、beans、expression-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- C3P0连接池 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- Druid连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
(3)创建test数据库
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `book`
-- ----------------------------
DROP TABLE IF EXISTS `book`;
CREATE TABLE `book` (
`id` int(15) NOT NULL AUTO_INCREMENT,
`title` varchar(20) NOT NULL,
`auth` varchar(20) DEFAULT NULL,
`price` float(20,2) DEFAULT NULL,
`cbs` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of book
-- ----------------------------
INSERT INTO `book` VALUES ('1', 'Java从入门到精通', '张三', '15.00', '清华大学');
INSERT INTO `book` VALUES ('5', '11', '11', '11.00', '11');
INSERT INTO `book` VALUES ('6', '11', '11', '11.00', '11');
INSERT INTO `book` VALUES ('8', 'javascript入门', '刘伟', '55.60', '哈哈');
INSERT INTO `book` VALUES ('10', 'MySQL从入门到精通', '刘玉徐', '85.00', '???');
-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`uname` varchar(255) NOT NULL,
`upass` varchar(255) DEFAULT NULL,
`tel` varchar(255) NOT NULL,
`address` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=124 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('123', '123', '123', '123', '123');
(4)创建C3P0连接池
package com.itbluebox.db;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import java.sql.Connection;
public class TestC3P0 {
@Test
public void testC3P0() throws Exception{
//数据源
ComboPooledDataSource dataSource = new ComboPooledDataSource();
//设置数据库连接参数
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
dataSource.setUser("root");
dataSource.setPassword("root");
//获得连接对象
Connection connection = dataSource.getConnection();
System.out.println(connection);
}
}
运行测试
运行成功
(5)创建Druid连接池
package com.itbluebox.db;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.junit.Test;
public class TestDruid {
@Test
public void testDruid() throws Exception{
//创建数据库源
DruidDataSource dataSource = new DruidDataSource();
//设置数据库连接参数
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/test");
dataSource.setUsername("root");
dataSource.setPassword("root");
//获取连接对象
DruidPooledConnection connection = dataSource.getConnection();
System.out.println(connection);
}
}
运行成功
3、数据源的手动创建(读取jdbc.properties)
(1)提取jdbc.properties配置文件
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/test
jdbc.username = root
jdbc.password = root
(2)读取jdbc.properties配置文件创建连接池
package com.itbluebox.db;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import java.sql.Connection;
import java.util.ResourceBundle;
public class TestC3P0ByProperties {
@Test
public void C3P0ByProperties() throws Exception{
//加载类路径下的jdbc.properties
ResourceBundle rb = ResourceBundle.getBundle("jdbc");
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(rb.getString("jdbc.driver"));
dataSource.setJdbcUrl(rb.getString("jdbc.url"));
dataSource.setUser(rb.getString("jdbc.username"));
dataSource.setPassword(rb.getString("jdbc.password"));
Connection connection = dataSource.getConnection();
System.out.println(connection);
}
}
4、测试从容器当中获取数据源(applicationContext.xml)
(1)创建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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
</beans>
(2)创建测试类,测试从容器当中获取数据源
package com.itbluebox.db;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.sql.DataSource;
import java.sql.Connection;
public class TestC3P0Application {
@Test
public void TestC3PO() throws Exception{
ClassPathXmlApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = (DataSource)applicationContext.getBean("dataSource");
Connection connection = dataSource.getConnection();
System.out.println(connection);
}
}
5、抽取jdbc配置文件
(1)修改applicationContext.xml
-
applicationContext.xml加载jdbc.properties配置文件获得连接信息。
-
首先,需要引入context命名空间和约束路径:
-
命名空间:
xmlns:context="http://www.springframework.org/schema/context"
-
约束路径:
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
- Spring容器加载properties文件
<context:property-placeholder location="xx.properties"/>
<property name="" value="${key}"/>
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
>
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>以上是关于Java之Spring入门到精通IDEA版IoC和DI注解开发(一篇文章精通系列)的主要内容,如果未能解决你的问题,请参考以下文章
Java之Spring入门到精通IDEA版Spring的IoC和DI(一篇文章精通系列)
Java之Spring入门到精通IDEA版Spring的IoC和DI(一篇文章精通系列)
Java之Spring Boot入门到精通IDEA版SpringBoot原理分析,SpringBoot监控(一篇文章精通系列)下
Java之Spring AOP入门到精通IDEA版(一篇文章精通系列)