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 ...
随机推荐
- cdn,wsgi框架
CDN:分布式服务器 wsgi:http请求----wsgi----web框架
- 打造自己的 JavaScript 武器库
原文 https://segmentfault.com/a/1190000011966867 github:https://github.com/proYang/outils 前言 作为战斗在业务一线 ...
- oracle行转列,列转行
多行转字符串这个比较简单,用||或concat函数可以实现 SQL Code select concat(id,username) str from app_userselect id||userna ...
- extjs中的store
1.store中重要的属性和方法 属性:data.proxy.reader.url.root .... 方法:load 2.理解:data--原料,proxy--运输车,reader--加工厂,sto ...
- javaScript高级教程(二)Scope Chain & Closure Example
<!DOCTYPE html> <html> <head> <meta charset=gb2312 /> <title>js</ti ...
- kettle 安装mysql 驱动
错误连接数据库 [mysql] : org.pentaho.di.core.exception.KettleDatabaseException: Error occurred while trying ...
- Miller_Rabbin算法判断大素数,Pollard_rho算法进行质因素分解
Miller-rabin算法是一个用来快速判断一个正整数是否为素数的算法.它利用了费马小定理,即:如果p是质数,且a,p互质,那么a^(p-1) mod p恒等于1.也就是对于所有小于p的正整数a来说 ...
- PAT 1072 Gas Station[图论][难]
1072 Gas Station (30)(30 分) A gas station has to be built at such a location that the minimum distan ...
- windows配置iis网站域名
1.在iis 中添加网站,网站名和主机名设置为test.com 2.在C:\Windows\System32\drivers\etc找到host文件,添加 127.0.0.1 test.com 127 ...
- Lower Power with CPF(二)
CPF文件可以有两种组织方式:Flat CPF file or Hierarchical CPF file. 由于在大型的SoC设计中,一般都采用Hierarchical的形式,所以本文主要按这个方式 ...