Spring DBCP用xml和properties2种格式配置DataSource
- <!-- 配置数据源 -->
- <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
- <property name="url" value="jdbc:mysql://localhost:3306/sms"/>
- <property name="username" value="root"/>
- <property name="password" value="root"/>
- </bean>
- @Component("userService")
- public class UserServiceImpl implements UserService{
- private UserDao userDao;
- public void setUserDao(UserDao userDao) {
- this.userDao = userDao;
- }
- @Resource //resource注入
- private DataSource myDataSource;
- public DataSource getMyDataSource() {
- return myDataSource;
- }
- public void setMyDataSource(DataSource myDataSource) {
- this.myDataSource = myDataSource;
- }
- //在下面方法前面加逻辑
- public void save(){
- try{
- //拿到连接执行操作
- Connection conn = myDataSource.getConnection();
- conn.createStatement().execute("insert into dept values('6','bumen2')");
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
- @Test
- public void test01() {
- BeanFactory applicationContext = new ClassPathXmlApplicationContext(
- "beans.xml");
- UserService user = (UserService) applicationContext.getBean("userService");
- user.save();
- }
- jdbc.driverClassName=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/sms
- jdbc.username=root
- jdbc.password=root
- <!-- placeholder 占位符 -->
- <bean
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <value>classpath:jdbc.properties</value>
- </property>
- </bean>
- <bean id="dataSource" destroy-method="close"
- class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName" value="${jdbc.driverClassName}" />
- <property name="url" value="${jdbc.url}" />
- <property name="username" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- </bean>
Spring DBCP用xml和properties2种格式配置DataSource的更多相关文章
- Spring MVC 返回 xml json pdf 数据的配置方法
<!-- Spring MVC 返回 xml 数据的配置方法 --> <bean class="org.springframework.web.servlet.vi ...
- asp.net导出excel-一行代码实现excel、xml、pdf、word、html、csv等7种格式文件导出功能而且美观-SNF快速开发平台
分享: 腾讯微博 新浪微博 搜狐微博 网易微博 腾讯朋友 百度贴吧 豆瓣 QQ好友 人人网 作者:王春天 原文地址:http://www.cnblogs.com/spring_ ...
- 返回Json和XML两种格式
由于项目需要,同一接口支持根据参数不同返回XML和Json两种格式的数据,在网上看了很多大多是加后缀的方式来实现返回不同格式数据的,后来看了一篇http://www.importnew.com/276 ...
- spring 基于XML和注解的两种事务配置方式
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- web.xml中配置spring.xml的三种方式
我们知道spring在web.xml中可以有三种方式来配置其xml路径:org.springframework.web.servlet.DispatcherServletorg.springframe ...
- Spring声明式事务的两种配置方式(注解/xml)
application配置tx:annotation-driven 配置声明式事务tx:TransactionManager 声明式事务需要数据源所以需要配置DataSource 使用:在类或者方法上 ...
- 基于xml文件的格式配置Spring的AOP
用例子直接说明: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http ...
- Spring使用jdbcJdbcTemplate和三种方法配置数据源
三种方法配置数据源 1.需要引入jar包:spring-jdbc-4.3.2.RELEASE.jar <!-- spring内置,springJdbc,配置数据源 --> <bean ...
- SPRING IN ACTION 第4版笔记-第十章Hitting the database with spring and jdbc-003-四种方式获取DataSource
一.概述 1.Spring offers several options for configuring data-source beans in your Spring application, i ...
随机推荐
- 【Git 使用笔记】第二部分:基本命令 和 单分支开发
git 基本命令 git add . git commit -am "请填写你NB的备注" git fetch --all git fetch -p //如果远程分支删除了,本地 ...
- 学习计划 mysql desc表结构分析
在完成数据表建表后,我们需要知道我们的表结构是什么,有没有和构造表时差异的地方. -- 简单查看表结构 desc 表名 这里拿数据库的一张表中做示例 mysql> desc rental; +- ...
- vhost-user 分析1
2018-01-24 占个坑,准备下写vhost-user的东西 vhost-user是vhost-kernel又回到用户空间的实现,其基本思想和vhost-kernel很类似,不过之前在内核的部分现 ...
- if嵌套和elif的区别
if嵌套的使用场景: 2个(多个)条件有前后关系,必须先满足条件1,再判断是否满足条件2. elif的使用场景: 2个(多个)条件是各自独立的平级关系,满足条件几就执行响应的代码. --------- ...
- Prometheus+Grafana+Altermanager搭建监控系统
基本概念 Prometheus 时间序列化数据库,我的理解就是将数据打上标签,以时间维度存储.后面有机会在深入研究. Prometheus架构如下: Grafana Prometheus中存储的数据, ...
- Pycharm模板添加默认信息
我们在Pycharm中使用到python3.x版本的解释器,完全没有问题可以正常使用,但是有的时候多少会出现使用python2的时候 我们明明都把代码和文字注释了,为什么使用的时候还会报错呢?? 报错 ...
- Scala里面如何使用枚举
枚举通常用来定义已知数量的常量,比如月份,星期,季节等等,用过java的人都知道定义枚举的关键字是enum,在scala里面和java有所不同,来看一个完整的例子定义: object EnumTest ...
- [LeetCode] 675. Cut Off Trees for Golf Event_Hard tag: BFS
You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-nega ...
- [lr] 矫正白平衡
中性色区域 • 定义 中性色又称为无彩色系,是指由黑色.白色及由黑白调和成的各种深浅不同的灰色系列.中性色既不属于冷色调,也不属于暖色调.黑白灰是常用到的三大中性色.中性色区域是指不包含色彩的区域,准 ...
- iOS常用第三方类库及Xcode插件
第三方类库(github地址): 1.AFNetworking 网络数据 https://github.com/AFNetworking/AFNetworking 2.SDWebImage 图 ...