文章目录

  • 前言
  • 纯手写配置
  • 导入Spring
  • 编写配置
    • 提取参数
    • 编写XML文件
    • 通过IOC容器获取Conncetion

前言

闲的没事,刚好最近学校讲到这个数据库,就想用Spring来玩玩,优化一下数据库操作。以前是直接用Mybatis的,但是想玩玩别的方式,顺便水一篇博客~

纯手写配置

我们先创建一个Maven项目,然后导入驱动和数据源Druid。


<!--数据库连接池--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.9</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency></dependencies>

为了便于测试,我们还可以导入这个玩意

<!--junit测试单元--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency>

然后我们手写一个获取connection的方法测试一下

    @Testpublic void test() throws SQLException {DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName("com.mysql.jdbc.Driver");dataSource.setUrl("jdbc:mysql://localhost:3306/huterox");dataSource.setUsername("huterox");dataSource.setPassword("666666666");DruidPooledConnection connection = dataSource.getConnection();connection.close();

显然这玩意我们可以封装出一个工具类。但是,我们既然有Spring这玩意,那么我们就直接使用Spring管理不就完了嘛。

导入Spring

        <dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.13.RELEASE</version></dependency>

编写配置

提取参数

现在我们先把我们的配置提取出来

编写XML文件

然后编写XML配置文件,虽然一开始我很烦这个配置文件,毕竟当初从Django过来,那玩意清爽呀,和SpringBoot类似,不过比boot还爽。

这里我导入了context命名空间,然后就可以读取那个配置文件提取变量参数了

<?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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><context:property-placeholder location="classpath:Druid.properties"/><bean id="dataSource" class="${dataSource}"><property name="DriverClassName" value="${DriverClassName}"/><property name="Url" value="${Url}"/><property name="Username" value="${Username}"/><property name="Password" value="${Password}"/></bean>
</beans>

通过IOC容器获取Conncetion

public class Application {public static void main(String[] args) throws SQLException {ApplicationContext app = new ClassPathXmlApplicationContext("Application.xml");DataSource dataSource = app.getBean(DataSource.class);Connection connection = dataSource.getConnection();System.out.println(connection);connection.close();}
}

ok ~水完了

Spring配置Druid数据源相关推荐

  1. springboot配置Druid数据源

    springboot配置druid数据源 Author:SimpleWu springboot整合篇 前言 对于数据访问层,无论是Sql还是NoSql,SpringBoot默认采用整合SpringDa ...

  2. SpringBoot(配置druid数据源、配置MyBatis、事务控制、druid 监控)

    SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少 数据层操作,所有的开发都一定秉持着 MVC 设计模式的原则,MVC 里面业务层不可少 ...

  3. springboot2.5.5配置druid数据源1.2.8与jdbc

    [README] 本文记录了 springboot配置 druid数据源的步骤: [1]新建springboot项目并配置druid 步骤1,新建springbt项目 步骤2,选择spring web ...

  4. Springboot配置Druid数据源详解

    配置数据源 1.添加Druid依赖 <!-- druid数据源--> <!-- https://mvnrepository.com/artifact/com.alibaba/drui ...

  5. springboot 配置DRUID数据源

    druid 是阿里开源的数据库连接池. 开发时整合   druid 数据源过程. 1.修改pom.xml <dependency><groupId>mysql</grou ...

  6. Springboot整合Quartz集群部署以及配置Druid数据源

    参考链接: https://blog.csdn.net/wangmx1993328/article/details/105441308 https://blog.csdn.net/qq_3966905 ...

  7. 配置Druid数据源监控

    Druid 数据源具有监控的功能,并提供了一个 web 界面方便用户查看. 所以第一步需要设置 Druid 的后台管理页面,比如 登录账号.密码 等:配置后台管理: //配置 Druid 监控管理后台 ...

  8. Spring配置多数据源错误总结

    由于系统需要调用多个数据源包含mysql,sqlServe和Oracle,所以要在Spring的xml文件中配置多数据源,一下是配置过程中常见的错误: 1.配置的是mysql的数据源,却报oracle ...

  9. spring配置druid连接池和监控数据库访问性能

    Druid连接池及监控在spring配置如下: [html] view plaincopy <bean id="dataSource" class="com.ali ...

最新文章

  1. 光脚丫学LINQ(002):筛选数据
  2. 周末随笔 | 问好一个问题,有的放矢
  3. “Hello,Github!——如何配置并上传一个已有项目到Git上
  4. PP-YOLO是最先进的吗?
  5. 数据结构与算法python—7.链表题目leetcode总结
  6. 系统分析师-论文准备
  7. C语言数码管是共阴共阳程序,C语言实现共阴极数码管操作
  8. IEEE会议(期刊)检索
  9. r语言调用python_R语言与Python协同工作
  10. 51单片机:独立按键
  11. 限制整个手机的网速 -- 360免费wifi
  12. 关于浮点误差,1为何变成0.999999 ?
  13. 脉宽调制功能(PWM)
  14. CentOS7 下配置svn的安装及基础配置介绍
  15. 我国“十三五”国家大数据战略离不开专业的数据中心基础设施运维服务
  16. 数学不好学java好学吗_数字不好能学java吗?
  17. 猫叔产品读记 | 全时关店潮、那些记得住的2019春节营销案例(2期)
  18. skimage的简介
  19. 华为在芯片领域又一个重磅炸弹,鲲鹏920杀出
  20. android studio 海马玩,react-native 环境的搭建(包括Android和iOS)

热门文章

  1. 上海停车的地方详细清单 0623更新版
  2. 为什么有些人明明看起来友善,却总是独来独往?
  3. java stdin 和stdout_对stdin,stdout 和STDOUT_FILENO,STDIN_FILENO的学习
  4. matplot 坐标点_matplot绘制带箭头的坐标图
  5. 薄荷提取物的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  6. 第三方框架-PDFViewer的使用
  7. 关于Excel的问题,不可编辑的区域--控件
  8. vivos5字体怎么变大_vivo手机字体变大1秒调小,操作就这么简单!
  9. 华为服务器hba卡状态查询,查询HBA卡信息 - OceanStor Dorado V6 主机连通性指南(VMware ESXi) - 华为数据存储...
  10. 金蝶获取登录密码方式