当前位置: 首页 > news >正文

spring学习笔记九

数据源对象管理

1、加入pom坐标

        <dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.16</version></dependency><!-- https://mvnrepository.com/artifact/c3p0/c3p0 --><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version></dependency><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.16</version></dependency>

2、jdbc.properties配置文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/spring_db
jdbc.username=root
jdbc.password=mima12341

3、spring配置文件

3.1、开启context命名空间

<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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context  这里是新增http://www.springframework.org/schema/context/spring-context.xsd  这里是新增
">

3.2、完整配置文件 

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd
"><!--step1:    开启命名空间--><!--step2:    使用context空间加载properties文件--><context:property-placeholder location="jdbc.properties"/><bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driver}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean></beans>

4、使用方法

public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");DataSource dataSource = (DataSource)ctx.getBean("datasource");System.out.println(dataSource);}

5、总结

  • 3.1开启context命名空间
  • 配置context:property-placeholder 设置location为配置文件名
  • 配置datasource的Bean
  • 使用${xxxx}获取配置文件中设置的值
http://www.lryc.cn/news/95555.html

相关文章:

  • java list stream 使用
  • 两个Ubuntu电脑用SSH远程连接
  • 讲解 @ServletComponentScan注解
  • 20款奔驰S350商务型加装原厂前排座椅通风系统,夏天必备的功能
  • Rust vs Go:常用语法对比(十一)
  • Spring MVC拦截器和跨域请求
  • C++初阶--C++入门
  • Matlab实现PID控制仿真(附上30个完整仿真源码+数据)
  • 微信小程序:文件下载
  • QString和QByteArray的区别
  • Vue3 Vite electron 开发桌面程序
  • 【Nodejs】Express模板使用
  • 【iOS】App仿写--管理系统
  • JS实现队列的数据结构
  • title: 用 LangChain 构建基于资料库的问答机器人(四):通过代理使用外部工具
  • 使用 CSS 自定义属性
  • Unity 性能优化一:性能标准、常用工具
  • 【http长连接+池化】
  • opencv-20 深入理解HSV 色彩空间(通过指定,标记颜色等来拓展ROI区域)
  • python调用arcgis功能一例
  • Spring MVC 是什么?
  • Rust操作MySQL
  • JAVA面试总结-Redis篇章(二)——缓存击穿
  • Spring相关知识点
  • Nginx专题--反向代理(未完成)
  • 什么是搜索引擎?2023 年搜索引擎如何运作?
  • Spring系列一:spring的安装与使用
  • Ubuntu--科研工具系列
  • 【压测指南|压力测试核心性能指标及行业标准】
  • spark-submit --files