住房和城乡建设网站 上海,wordpress标签路径,民勤县建设局网站,免费的行情网站Spring配置数据源数据源的作用环境准备手动创建c3p0数据源封装抽取关键信息#xff0c;手动创建c3p0数据源使用Spring容器配置数据源数据源的作用
数据源(连接池)是提高程序性能如出现的 事先实例化数据源#xff0c;初始化部分连接资源 使用连接资源时从数据源中获取 使用完…
Spring配置数据源数据源的作用环境准备手动创建c3p0数据源封装抽取关键信息手动创建c3p0数据源使用Spring容器配置数据源数据源的作用
数据源(连接池)是提高程序性能如出现的 事先实例化数据源初始化部分连接资源 使用连接资源时从数据源中获取 使用完毕后将连接资源归还给数据源 常见的数据源有DBCP、C3P0、BoneCP、Druid
环境准备
idea中创建一个maven项目导入如下所需的基本坐标(mysql、Junit、spring-contex) 两种数据池可以看自己情况都导或者导入其中一个Junit看个人需要可导可不导。 dependenciesdependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.0.5.RELEASE/version/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.10/versionscopetest/scope/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion5.1.3/version/dependencydependencygroupIdc3p0/groupIdartifactIdc3p0/artifactIdversion0.9.1.1/version/dependencydependencygroupIdcom.alibaba/groupIdartifactIddruid/artifactIdversion1.1.10/version/dependency/dependencies在resources目录下下建一个名为jdbc.properties的文件 properties文件数据配置如下
jdbc.drivercom.mysql.jdbc.Driver
jdbc.urljdbc:mysql://localhost:3306/db1
jdbc.usernameroot
jdbc.password123456手动创建c3p0数据源 //手动测试c3p0数据源Testpublic void test1() throws Exception {//创建数据源对象ComboPooledDataSource dataSource new ComboPooledDataSource();//设置数据源的基本连接数据dataSource.setDriverClass(com.mysql.jdbc.Driver);dataSource.setJdbcUrl(jdbc:mysql://localhost:3306/db1);dataSource.setUser(root);dataSource.setPassword(123456);//使用数据源获取连接资源Connection connection dataSource.getConnection();System.out.println(connection);//关闭连接资源connection.close();}封装抽取关键信息手动创建c3p0数据源
第一种加载配置文件方式:
//用文件流的方式进行读取Properties properties new Properties();properties.load(new FileReader(src/main/resources/jdbc.properties));String driver properties.getProperty(jdbc.driver);String url properties.getProperty(jdbc.url);String username properties.getProperty(jdbc.username);String password properties.getProperty(jdbc.password);第二种加载配置文件方式:
//用resourceBundle进行读取配置ResourceBundle rbResourceBundle.getBundle(jdbc);String driver rb.getString(jdbc.driver);String url rb.getString(jdbc.url);String username rb.getString(jdbc.username);String password rb.getString(jdbc.password);任选上面两种方式读取文件信息 //手动测试c3p0数据源 加载配置文件Testpublic void test4() throws Exception {/*任选上面两种方式读取文件信息读取配置文件....*/ComboPooledDataSource dataSource new ComboPooledDataSource();dataSource.setDriverClass(driver);dataSource.setJdbcUrl(url);dataSource.setUser(username);dataSource.setPassword(password);Connection connection dataSource.getConnection();System.out.println(connection);connection.close();}使用Spring容器配置数据源
把数据源的创建权交给Spring容器去完成在resource目录下创建一个SQLConfiguration.xml的Spring配置文件
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean iddataSource classcom.alibaba.druid.pool.DruidDataSourceproperty namedriverClassName valuecom.mysql.cj.jdbc.Driver/propertyproperty nameurl valuejdbc:mysql://localhost:3306/db1/propertyproperty nameusername valueroot/propertyproperty namepassword value123456/property/bean
/beans
对应的测试代码如下: //spring容器创建Testpublic void test5() throws Exception {ApplicationContext app new ClassPathXmlApplicationContext(SQLConfiguration.xml);
// Object dataSource app.getBean(dataSource);DataSource dataSource app.getBean(DataSource.class);Connection connection dataSource.getConnection();System.out.println(connection);connection.close();}上面spring的xml配置文件里有一个令人头疼的问题就是耦合度太高关键配置信息被写死了以后如果要修改配置文件得一个一个改所以下面采用命名空间的方法对其进行改良。
改良后如下:
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd!-- 加载properties配置文件--context:property-placeholder locationclasspath:jdbc.properties/bean iddataSource classcom.mchange.v2.c3p0.ComboPooledDataSource property namedriverClass value${jdbc.driver} /property namejdbcUrl value${jdbc.url} /property nameuser value${jdbc.username} /property namepassword value${jdbc.password} //bean
/beans将xmlnshttp://www.springframework.org/schema/beans中的beans改成context命名空间和约束路径都要进行修改。
注意细节: spring容器加载properties配置文件方式如下:
!-- 加载properties配置文件--context:property-placeholder locationclasspath:jdbc.properties/property name value${key} /配置完成后即可使用。
参考:https://blog.csdn.net/weixin_59654772/article/details/122549314 视频资料:黑马程序员spring教程第二天 链接地址:视频地址