学习笔记--Spring配置数据源
数据库的几种配置方式
1.手动创建c3p0数据源
1 2 3 4 5 6 7 8 9 10 | public void test1() throws Exception { ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setDriverClass("com.mysql.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/ssm_db"); dataSource.setUser("root"); dataSource.setPassword("123456"); Connection connection = dataSource.getConnection(); System.out.println(connection); connection.close(); } |
2.手动创建druid数据源
1 2 3 4 5 6 7 8 9 10 | public void test2() throws Exception { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/ssm_db"); dataSource.setUsername("root"); dataSource.setPassword("123456"); Connection connection = dataSource.getConnection(); System.out.println(connection); connection.close(); } |
3.手动创建c3p0数据源 (加载配置properties配置文件)
直接创建数据源在源码中,耦合度非常高,不利于灵活改变。
有一种方法是可以从配置的properties文件中提取。
3.1创建一个jdbc.properties文件
3.2填写相关的配置信息
1 2 3 4 | jdbc.driver = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/ssm_db jdbc.username = root jdbc.password = 123456 |
3.3使用配置信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public void test3() throws Exception { //读取配置文件,这里的getBundle,填写resources目录下的properties文件的名字 ResourceBundle rb = ResourceBundle.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"); //创建数据源对象 设置连接参数 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(); } |
4.Spring产生数据源对象
原理:由于数据源是一个类,spring的bean标签可以填充类。
另外数据源类有相应的set方法,这样又可以设置数据源的属性,把它交给spring十分的方便。
4.1怎样在xml文件中使用properties文件中的属性
首先添加命名空间
1 | xmlns:context="http://www.springframework.org/schema/context" |
其次在schemaLocation中添加命名实例
1 | http://www.springframework.org/schema/context/spring-context.xsd |
最后在<context>标签中写:
1 | < context:property-placeholder location="classpath:jdbc.properties" /> |
4.2 配置过程
1 2 3 4 5 6 | < bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> < property name="DriverClass" value="${jdbc.driver}"></ property > < property name="JdbcUrl" value="${jdbc.url}"></ property > < property name="User" value="${jdbc.username}"></ property > < property name="password" value="${jdbc.password}"></ property > </ bean > |
这里的注意点是:用el表达式来传参,格式为${XXXX}。
4.3 使用过程
1 2 3 4 5 6 7 | public void test4() throws Exception { ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); ComboPooledDataSource dataSource = (ComboPooledDataSource) app.getBean("dataSource"); Connection connection = dataSource.getConnection(); System.out.println(connection); connection.close(); } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
· 面试官:你是如何进行SQL调优的?