spring—配置数据源

数据源(连接池)的作用

数据源(连接池)是提高程序性能如出现的
事先实例化数据源,初始化部分连接资源
使用连接资源时从数据源中获取
使用完毕后将连接资源归还给数据源
常见的数据源(连接池):DBCP、C3P0、BoneCP、Druid等
开发步骤
①导入数据源的坐标和数据库驱动坐标
②创建数据源对象
③设置数据源的基本连接数据
④使用数据源获取连接资源和归还连接资源

数据源的手动创建

①导入c3p0

<!-- C3P0连接池 --><dependency>    <groupId>c3p0</groupId> <artifactId>c3p0</artifactId>    <version>0.9.1.2</version> </dependency>

①导入mysql数据库驱动坐标

        <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.11</version></dependency>

②创建C3P0连接池

    @Testpublic void testC3P0() throws Exception{ComboPooledDataSource comboPooledDataSource=new ComboPooledDataSource();comboPooledDataSource.setDriverClass("com.mysql.cj.jdbc.Driver");comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/book?useSSL=false&serverTimezone=UTC");comboPooledDataSource.setUser("root");comboPooledDataSource.setPassword("123456");Connection connection=comboPooledDataSource.getConnection();System.out.println(connection);}

提取jdbc.properties配置文件

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/book?useSSL=false&serverTimezone=UTC
jdbc.username=xxx
jdbc.password=xxx

Spring配置数据源

可以将DataSource的创建权交由Spring容器去完成 DataSource有无参构造方法,而Spring默认就是通过无参构造方法实例化对象的
DataSource要想使用需要通过set方法设置数据库连接信息,而Spring可以通过set方法进行字符串注入

    @Testpublic void testC3P0() throws Exception{ComboPooledDataSource comboPooledDataSource=new ComboPooledDataSource();ResourceBundle resourceBundle=ResourceBundle.getBundle("jdbc");comboPooledDataSource.setDriverClass(resourceBundle.getString("jdbc.driver"));comboPooledDataSource.setJdbcUrl(resourceBundle.getString("jdbc.url"));comboPooledDataSource.setUser(resourceBundle.getString("jdbc.username"));comboPooledDataSource.setPassword(resourceBundle.getString("jdbc.password"));Connection connection=comboPooledDataSource.getConnection();System.out.println(connection);}
<?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"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
http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd
">
<context:property-placeholder location="jdbc.properties"/><bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driver}"/><property name="jdbcUrl" value="${jdbc.url}"/><property name="user" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean></beans>
    @Testpublic void testC3P02() throws Exception{ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");ComboPooledDataSource comboPooledDataSource=(ComboPooledDataSource) applicationContext.getBean("datasource");System.out.println(comboPooledDataSource.getConnection());}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391962.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

大型网站系统与Java中间件实践pdf

下载地址&#xff1a;网盘下载 基本介绍 编辑内容简介 到底是本什么书&#xff0c;拥有这样一份作序推荐人列表&#xff1a;阿里集团章文嵩博士|新浪TimYang|去哪网吴永强|丁香园冯大辉|蘑菇街岳旭强|途牛汤峥嵘|豆瓣洪强宁|某电商陈皓/林昊…… 这本书出自某电商技术部总监之手…

office漏洞利用--获取shell

环境&#xff1a; kali系统&#xff0c; windows系统 流程&#xff1a; 在kali系统生成利用文件&#xff0c; kali系统下监听本地端口&#xff0c; windows系统打开doc文件&#xff0c;即可中招 第一种利用方式&#xff0c; 适合测试用&#xff1a; 从git下载代码&#xff1a; …

pandas之DataFrame合并merge

一、merge merge操作实现两个DataFrame之间的合并&#xff0c;类似于sql两个表之间的关联查询。merge的使用方法及参数解释如下&#xff1a; pd.merge(left, right, onNone, howinner, left_onNone, right_onNone, left_indexFalse, right_indexFalse,    sortFalse, suffi…

typescript_如何掌握高级TypeScript模式

typescriptby Pierre-Antoine Mills皮埃尔安托万米尔斯(Pierre-Antoine Mills) 如何掌握高级TypeScript模式 (How to master advanced TypeScript patterns) 了解如何为咖喱和Ramda创建类型 (Learn how to create types for curry and Ramda) Despite the popularity of curry…

html函数splice,js数组的常用函数(slice()和splice())和js引用的三种方法总结—2019年1月16日...

总结&#xff1a;slice()和splice()slice(参数1,参数2)可以查找数组下对应的数据&#xff0c;参数1为起始位置&#xff0c;参数2为结束位置&#xff0c;参数2可以为负数&#xff0c;-1对应的是从后向前数的第一个数值。splice()可以进行增删改查数据操作&#xff0c;splice(参数…

leetcode 643. 子数组最大平均数 I(滑动窗口)

给定 n 个整数&#xff0c;找出平均数最大且长度为 k 的连续子数组&#xff0c;并输出该最大平均数。 示例&#xff1a; 输入&#xff1a;[1,12,-5,-6,50,3], k 4 输出&#xff1a;12.75 解释&#xff1a;最大平均数 (12-5-650)/4 51/4 12.75 代码 class Solution {publ…

python ==字符串

字符串类型(str)&#xff1a; 包含在引号&#xff08;单&#xff0c;双&#xff0c;三&#xff09;里面&#xff0c;由一串字符组成。 用途&#xff1a;姓名&#xff0c;性别&#xff0c;地址&#xff0c;学历&#xff0c;密码 Name ‘zbk’ 取值: 首先要明确&#xff0c;字符…

认证鉴权与API权限控制在微服务架构中的设计与实现(一)

作者&#xff1a; [Aoho’s Blog] 引言&#xff1a; 本文系《认证鉴权与API权限控制在微服务架构中的设计与实现》系列的第一篇&#xff0c;本系列预计四篇文章讲解微服务下的认证鉴权与API权限控制的实现。 1. 背景 最近在做权限相关服务的开发&#xff0c;在系统微服务化后&a…

mac下完全卸载程序的方法

在国外网上看到的&#xff0c;觉得很好&#xff0c;不仅可以长卸载的知识&#xff0c;还对mac系统有更深的认识。比如偏好设置文件&#xff0c;我以前设置一个程序坏了&#xff0c;打不开了&#xff0c;怎么重装都打不开&#xff0c;后来才知道系统还保留着原来的偏好设置文件。…

机器学习集群_机器学习中的多合一集群技术在无监督学习中应该了解

机器学习集群Clustering algorithms are a powerful technique for machine learning on unsupervised data. The most common algorithms in machine learning are hierarchical clustering and K-Means clustering. These two algorithms are incredibly powerful when appli…

自考本科计算机要学什么,计算机自考本科需要考哪些科目

高科技发展时代&#xff0c;怎离得开计算机技术&#xff1f;小学生都要学编程了&#xff0c;未来趋势一目了然&#xff0c;所以如今在考虑提升学历的社会成人&#xff0c;多半也青睐于计算机专业&#xff0c;那么计算机自考本科需要考哪些科目&#xff1f;难不难&#xff1f;自…

审查指南 最新版本_代码审查-最终指南

审查指南 最新版本by Assaf Elovic通过阿萨夫埃洛维奇 代码审查-最终指南 (Code Review — The Ultimate Guide) 构建团队代码审查流程的终极指南 (The ultimate guide for building your team’s code review process) After conducting hundreds of code reviews, leading R…

非对称加密

2019独角兽企业重金招聘Python工程师标准>>> 概念 非对称加密算法需要两个密钥&#xff1a;公钥&#xff08;publickey&#xff09;和私钥&#xff08;privatekey&#xff09;。公钥与私钥是一对&#xff0c;如果用公钥对数据进行加密&#xff0c;只有用对应的私…

管理Sass项目文件结构

http://www.w3cplus.com/preprocessor/architecture-sass-project.html 编辑推荐&#xff1a; 掘金是一个高质量的技术社区&#xff0c;从 CSS 到 Vue.js&#xff0c;性能优化到开源类库&#xff0c;让你不错过前端开发的每一个技术干货。 点击链接查看最新前端内容&#xff0c…

Spring—注解开发

Spring原始注解 Spring是轻代码而重配置的框架&#xff0c;配置比较繁重&#xff0c;影响开发效率&#xff0c;所以注解开发是一种趋势&#xff0c;注解代替xml配置文 件可以简化配置&#xff0c;提高开发效率。 Component 使用在类上用于实例化BeanController 使用在web层类…

政府公开数据可视化_公开演讲如何帮助您设计更好的数据可视化

政府公开数据可视化What do good speeches and good data visualisation have in common? More than you may think.好的演讲和好的数据可视化有什么共同点&#xff1f; 超出您的想象。 Aristotle — the founding father of all things public speaking — believed that th…

C++字符串完全指引之一 —— Win32 字符编码 (转载)

C字符串完全指引之一 —— Win32 字符编码原著&#xff1a;Michael Dunn翻译&#xff1a;Chengjie Sun 原文出处&#xff1a;CodeProject&#xff1a;The Complete Guide to C Strings, Part I 引言  毫无疑问&#xff0c;我们都看到过像 TCHAR, std::string, BSTR 等各种各样…

网络计算机无法访问 请检查,局域网电脑无法访问,请检查来宾访问帐号是否开通...

局域网电脑无法访问&#xff0c;有时候并不是由于网络故障引起的&#xff0c;而是因为自身电脑的一些设置问题&#xff0c;例如之前谈过的网络参数设置不对造成局域网电脑无法访问。今天分析另一个电脑设置的因素&#xff0c;它也会导致局域网电脑无法访问&#xff0c;那就是宾…

unity中创建游戏场景_在Unity中创建Beat Em Up游戏

unity中创建游戏场景Learn how to use Unity to create a 3D Beat Em Up game in this full tutorial from Awesome Tuts. 在Awesome Tuts的完整教程中&#xff0c;了解如何使用Unity来创建3D Beat Em Up游戏。 This tutorial covers everything you need to know to make a …

雷军的金山云D轮获3亿美元!投后估值达19亿美金

12月12日&#xff0c;雷军旗下金山云宣布D轮完成3亿美元融资&#xff0c;金额为云行业单轮融资最高。至此金山云投后估值达到19亿美元&#xff0c;成为国内估值最高的独立云服务商。金山集团相关公告显示&#xff0c;金山云在本轮融资中总计发行3.535亿股D系列优先股。骊悦投资…