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 ... 
随机推荐
- struct  modbus是大端的
			https://www.cnblogs.com/coser/archive/2011/12/17/2291160.html https://www.cnblogs.com/gala/archive/2 ... 
- CCO2017 Vera and Trail Building 构造+图论
			正解:构造+图论 解题报告: 找了半天才找到的传送门! 先简要表达下题意 一个图上,如果存在(a,b)满足a<b且存在从a到b再回到a的路径,每条道路被经过至多一次,我们称(a,b)为完美点对试 ... 
- kubernetes实战(十五):k8s使用helm持久化部署jenkins集成openLDAP登录
			1.基本概念 Jenkins在DevOps工具链中是核心的流程管理中心,负责串联系统的构建流程.测试流程.镜像制作流程.部署流程等,在持续集成中常用到的工具如下: Maven:源代码编译工具 Robo ... 
- LeetCode 第 338 题 (Counting Bits)
			Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ... 
- JS闭包中的循环绑定处理程序
			前几天工作中写前端js代码时,遇到了遍历元素给它添加单击事件.就是这个问题让我整整调了一个下午.最后还是下班回家,上网查资料才知道怎么解决的. (PS:之前也在<jQuery基础教程>第四 ... 
- 用Delphi写一个UTF8编码格式的文本文件
			... var f: TextFile; begin AssignFile(f, 'test.txt'); Rewrite(f); write(f, #$EF+#$BB+#$BF); // UTF-8 ... 
- 在sublime3中docblockr插件配置apidoc接口文档注释模板
			写在前面: 将进行3个步骤配置 1.在sublime3中安装插件docblockr,可以参考http://www.cnblogs.com/jiangxiaobo/p/8327709.html 2.安装 ... 
- elasticsearch 6.0在Ubuntu下的安装
			1:直接下载 elasticsearch 6.0 zip文件 https://www.elastic.co/downloads/past-releases 2:解压:进入到解压后的bin目录,执行 ... 
- 重读《Java编程思想》
			相关最新代码已上传至我的GitHub了(https://github.com/WenyangSun/ThinkingInJava),后续例子没有在博客上更新. 1.在类的内部,变量定义的先后顺序决定了 ... 
- [Leetcode] 49. Group Anagrams_Medium
			Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ... 
